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/>.
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23 #include "../librpc/gen_ndr/ndr_netlogon.h"
24 #include "smbd/globals.h"
26 #include "../lib/tsocket/tsocket.h"
30 #define DBGC_CLASS DBGC_VFS
32 /**********************************************************
33 Under mapfile we expect a table of the following format:
35 IP-Prefix whitespace expansion
38 192.168.234 local.samba.org
39 192.168 remote.samba.org
42 This is to redirect a DFS client to a host close to it.
43 ***********************************************************/
45 static char *read_target_host(TALLOC_CTX *ctx, const char *mapfile,
46 const char *clientaddr)
53 f = fopen(mapfile, "r");
56 DEBUG(0,("can't open IP map %s. Error %s\n",
57 mapfile, strerror(errno) ));
61 DEBUG(10, ("Scanning mapfile [%s]\n", mapfile));
63 while (fgets(buf, sizeof(buf), f) != NULL) {
65 if ((strlen(buf) > 0) && (buf[strlen(buf)-1] == '\n'))
66 buf[strlen(buf)-1] = '\0';
68 DEBUG(10, ("Scanning line [%s]\n", buf));
70 space = strchr_m(buf, ' ');
73 DEBUG(0, ("Ignoring invalid line %s\n", buf));
79 if (strncmp(clientaddr, buf, strlen(buf)) == 0) {
93 while (isspace(*space))
96 return talloc_strdup(ctx, space);
99 /**********************************************************
101 Expand the msdfs target host using read_target_host
102 explained above. The syntax used in the msdfs link is
104 msdfs:@table-filename@/share
106 Everything between and including the two @-signs is
107 replaced by the substitution string found in the table
110 ***********************************************************/
112 static char *expand_msdfs_target(TALLOC_CTX *ctx,
113 connection_struct *conn,
116 const struct loadparm_substitution *lp_sub =
117 loadparm_s3_global_substitution();
118 char *mapfilename = NULL;
119 char *filename_start = strchr_m(target, '@');
120 char *filename_end = NULL;
121 int filename_len = 0;
122 char *targethost = NULL;
123 char *new_target = NULL;
126 if (filename_start == NULL) {
127 DEBUG(10, ("No filename start in %s\n", target));
131 filename_end = strchr_m(filename_start+1, '@');
133 if (filename_end == NULL) {
134 DEBUG(10, ("No filename end in %s\n", target));
138 filename_len = PTR_DIFF(filename_end, filename_start+1);
139 mapfilename = talloc_strdup(ctx, filename_start+1);
143 mapfilename[filename_len] = '\0';
146 * dfs links returned have had '/' characters replaced with '\'.
147 * Return them to '/' so we can have absoute path mapfilenames.
149 string_replace(mapfilename, '\\', '/');
151 DEBUG(10, ("Expanding from table [%s]\n", mapfilename));
153 raddr = tsocket_address_inet_addr_string(conn->sconn->remote_address,
159 targethost = read_target_host(ctx, mapfilename, raddr);
160 if (targethost == NULL) {
161 DEBUG(1, ("Could not expand target host from file %s\n",
166 targethost = talloc_sub_full(ctx,
167 lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
168 conn->session_info->unix_info->unix_name,
170 conn->session_info->unix_token->gid,
171 conn->session_info->unix_info->sanitized_username,
172 conn->session_info->info->domain_name,
175 DEBUG(10, ("Expanded targethost to %s\n", targethost));
177 /* Replace the part between '@...@' */
178 *filename_start = '\0';
179 new_target = talloc_asprintf(ctx,
188 DEBUG(10, ("New DFS target: %s\n", new_target));
192 static NTSTATUS expand_read_dfs_pathat(struct vfs_handle_struct *handle,
194 struct files_struct *dirfsp,
195 struct smb_filename *smb_fname,
196 struct referral **ppreflist,
197 size_t *preferral_count)
201 struct referral *reflist = NULL;
203 TALLOC_CTX *frame = talloc_stackframe();
206 * Always call the NEXT function first, then
207 * modify the return if needed.
209 status = SMB_VFS_NEXT_READ_DFS_PATHAT(handle,
216 if (!NT_STATUS_IS_OK(status)) {
222 * This function can be called to check if a pathname
223 * is an MSDFS link, but not return the values of it.
224 * In this case ppreflist and preferral_count are NULL,
225 * so don't bother trying to look at any returns.
227 if (ppreflist == NULL || preferral_count == NULL) {
233 * We are always returning the values returned
234 * returned by the NEXT call, but we might mess
235 * with the reflist[i].alternate_path values,
236 * so use local pointers to minimise indirections.
238 count = *preferral_count;
239 reflist = *ppreflist;
241 for (i = 0; i < count; i++) {
242 if (strchr_m(reflist[i].alternate_path, '@') != NULL) {
243 char *new_altpath = expand_msdfs_target(frame,
245 reflist[i].alternate_path);
246 if (new_altpath == NULL) {
247 TALLOC_FREE(*ppreflist);
248 *preferral_count = 0;
250 return NT_STATUS_NO_MEMORY;
252 reflist[i].alternate_path = talloc_move(reflist,
260 static struct vfs_fn_pointers vfs_expand_msdfs_fns = {
261 .read_dfs_pathat_fn = expand_read_dfs_pathat,
265 NTSTATUS vfs_expand_msdfs_init(TALLOC_CTX *ctx)
267 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "expand_msdfs",
268 &vfs_expand_msdfs_fns);