source: trunk/server/source3/modules/vfs_expand_msdfs.c

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 5.4 KB
Line 
1/*
2 * Expand msdfs targets based on client IP
3 *
4 * Copyright (C) Volker Lendecke, 2004
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "includes.h"
21#include "system/filesys.h"
22#include "smbd/smbd.h"
23#include "../librpc/gen_ndr/ndr_netlogon.h"
24#include "smbd/globals.h"
25#include "auth.h"
26
27#undef DBGC_CLASS
28#define DBGC_CLASS DBGC_VFS
29
30/**********************************************************
31 Under mapfile we expect a table of the following format:
32
33 IP-Prefix whitespace expansion
34
35 For example:
36 192.168.234 local.samba.org
37 192.168 remote.samba.org
38 default.samba.org
39
40 This is to redirect a DFS client to a host close to it.
41***********************************************************/
42
43static char *read_target_host(TALLOC_CTX *ctx, const char *mapfile,
44 const char *clientaddr)
45{
46 XFILE *f;
47 char buf[1024];
48 char *space = buf;
49 bool found = false;
50
51 f = x_fopen(mapfile, O_RDONLY, 0);
52
53 if (f == NULL) {
54 DEBUG(0,("can't open IP map %s. Error %s\n",
55 mapfile, strerror(errno) ));
56 return NULL;
57 }
58
59 DEBUG(10, ("Scanning mapfile [%s]\n", mapfile));
60
61 while (x_fgets(buf, sizeof(buf), f) != NULL) {
62
63 if ((strlen(buf) > 0) && (buf[strlen(buf)-1] == '\n'))
64 buf[strlen(buf)-1] = '\0';
65
66 DEBUG(10, ("Scanning line [%s]\n", buf));
67
68 space = strchr_m(buf, ' ');
69
70 if (space == NULL) {
71 DEBUG(0, ("Ignoring invalid line %s\n", buf));
72 continue;
73 }
74
75 *space = '\0';
76
77 if (strncmp(clientaddr, buf, strlen(buf)) == 0) {
78 found = true;
79 break;
80 }
81 }
82
83 x_fclose(f);
84
85 if (!found) {
86 return NULL;
87 }
88
89 space += 1;
90
91 while (isspace(*space))
92 space += 1;
93
94 return talloc_strdup(ctx, space);
95}
96
97/**********************************************************
98
99 Expand the msdfs target host using read_target_host
100 explained above. The syntax used in the msdfs link is
101
102 msdfs:@table-filename@/share
103
104 Everything between and including the two @-signs is
105 replaced by the substitution string found in the table
106 described above.
107
108***********************************************************/
109
110static char *expand_msdfs_target(TALLOC_CTX *ctx,
111 connection_struct *conn,
112 char *target)
113{
114 char *mapfilename = NULL;
115 char *filename_start = strchr_m(target, '@');
116 char *filename_end = NULL;
117 int filename_len = 0;
118 char *targethost = NULL;
119 char *new_target = NULL;
120
121 if (filename_start == NULL) {
122 DEBUG(10, ("No filename start in %s\n", target));
123 return NULL;
124 }
125
126 filename_end = strchr_m(filename_start+1, '@');
127
128 if (filename_end == NULL) {
129 DEBUG(10, ("No filename end in %s\n", target));