2 * Expand msdfs targets based on client IP
4 * Copyright (C) Volker Lendecke, 2004
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.
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.
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/>.
23 #define DBGC_CLASS DBGC_VFS
25 /**********************************************************
26 Under mapfile we expect a table of the following format:
28 IP-Prefix whitespace expansion
31 192.168.234 local.samba.org
32 192.168 remote.samba.org
35 This is to redirect a DFS client to a host close to it.
36 ***********************************************************/
38 static char *read_target_host(TALLOC_CTX
*ctx
, const char *mapfile
)
45 f
= x_fopen(mapfile
, O_RDONLY
, 0);
48 DEBUG(0,("can't open IP map %s. Error %s\n",
49 mapfile
, strerror(errno
) ));
53 DEBUG(10, ("Scanning mapfile [%s]\n", mapfile
));
55 while (x_fgets(buf
, sizeof(buf
), f
) != NULL
) {
56 char addr
[INET6_ADDRSTRLEN
];
58 if ((strlen(buf
) > 0) && (buf
[strlen(buf
)-1] == '\n'))
59 buf
[strlen(buf
)-1] = '\0';
61 DEBUG(10, ("Scanning line [%s]\n", buf
));
63 space
= strchr_m(buf
, ' ');
66 DEBUG(0, ("Ignoring invalid line %s\n", buf
));
72 if (strncmp(client_addr(get_client_fd(),addr
,sizeof(addr
)),
73 buf
, strlen(buf
)) == 0) {
87 while (isspace(*space
))
90 return talloc_strdup(ctx
, space
);
93 /**********************************************************
95 Expand the msdfs target host using read_target_host
96 explained above. The syntax used in the msdfs link is
98 msdfs:@table-filename@/share
100 Everything between and including the two @-signs is
101 replaced by the substitution string found in the table
104 ***********************************************************/
106 static char *expand_msdfs_target(TALLOC_CTX
*ctx
,
107 connection_struct
*conn
,
110 char *mapfilename
= NULL
;
111 char *filename_start
= strchr_m(target
, '@');
112 char *filename_end
= NULL
;
113 int filename_len
= 0;
114 char *targethost
= NULL
;
115 char *new_target
= NULL
;
117 if (filename_start
== NULL
) {
118 DEBUG(10, ("No filename start in %s\n", target
));
122 filename_end
= strchr_m(filename_start
+1, '@');
124 if (filename_end
== NULL
) {
125 DEBUG(10, ("No filename end in %s\n", target
));
129 filename_len
= PTR_DIFF(filename_end
, filename_start
+1);
130 mapfilename
= talloc_strdup(ctx
, filename_start
+1);
134 mapfilename
[filename_len
] = '\0';
136 DEBUG(10, ("Expanding from table [%s]\n", mapfilename
));
138 if ((targethost
= read_target_host(ctx
, mapfilename
)) == NULL
) {
139 DEBUG(1, ("Could not expand target host from file %s\n",
144 targethost
= talloc_sub_advanced(ctx
,
145 lp_servicename(SNUM(conn
)),
146 conn
->server_info
->unix_name
,
148 conn
->server_info
->utok
.gid
,
149 conn
->server_info
->sanitized_username
,
150 conn
->server_info
->info3
->base
.domain
.string
,
153 DEBUG(10, ("Expanded targethost to %s\n", targethost
));
155 /* Replace the part between '@...@' */
156 *filename_start
= '\0';
157 new_target
= talloc_asprintf(ctx
,
166 DEBUG(10, ("New DFS target: %s\n", new_target
));
170 static int expand_msdfs_readlink(struct vfs_handle_struct
*handle
,
171 const char *path
, char *buf
, size_t bufsiz
)
173 TALLOC_CTX
*ctx
= talloc_tos();
175 char *target
= TALLOC_ARRAY(ctx
, char, PATH_MAX
+1);
187 result
= SMB_VFS_NEXT_READLINK(handle
, path
, target
,
193 target
[result
] = '\0';
195 if ((strncmp(target
, "msdfs:", 6) == 0) &&
196 (strchr_m(target
, '@') != NULL
)) {
197 target
= expand_msdfs_target(ctx
, handle
->conn
, target
);
204 len
= MIN(bufsiz
, strlen(target
));
206 memcpy(buf
, target
, len
);
212 static struct vfs_fn_pointers vfs_expand_msdfs_fns
= {
213 .vfs_readlink
= expand_msdfs_readlink
216 NTSTATUS
vfs_expand_msdfs_init(void);
217 NTSTATUS
vfs_expand_msdfs_init(void)
219 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "expand_msdfs",
220 &vfs_expand_msdfs_fns
);