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 "../librpc/gen_ndr/ndr_netlogon.h"
23 #include "smbd/globals.h"
26 #define DBGC_CLASS DBGC_VFS
28 /**********************************************************
29 Under mapfile we expect a table of the following format:
31 IP-Prefix whitespace expansion
34 192.168.234 local.samba.org
35 192.168 remote.samba.org
38 This is to redirect a DFS client to a host close to it.
39 ***********************************************************/
41 static char *read_target_host(TALLOC_CTX *ctx, const char *mapfile,
42 const char *clientaddr)
49 f = x_fopen(mapfile, O_RDONLY, 0);
52 DEBUG(0,("can't open IP map %s. Error %s\n",
53 mapfile, strerror(errno) ));
57 DEBUG(10, ("Scanning mapfile [%s]\n", mapfile));
59 while (x_fgets(buf, sizeof(buf), f) != NULL) {
61 if ((strlen(buf) > 0) && (buf[strlen(buf)-1] == '\n'))
62 buf[strlen(buf)-1] = '\0';
64 DEBUG(10, ("Scanning line [%s]\n", buf));
66 space = strchr_m(buf, ' ');
69 DEBUG(0, ("Ignoring invalid line %s\n", buf));
75 if (strncmp(clientaddr, buf, strlen(buf)) == 0) {
89 while (isspace(*space))
92 return talloc_strdup(ctx, space);
95 /**********************************************************
97 Expand the msdfs target host using read_target_host
98 explained above. The syntax used in the msdfs link is
100 msdfs:@table-filename@/share
102 Everything between and including the two @-signs is
103 replaced by the substitution string found in the table
106 ***********************************************************/
108 static char *expand_msdfs_target(TALLOC_CTX *ctx,
109 connection_struct *conn,
112 char *mapfilename = NULL;
113 char *filename_start = strchr_m(target, '@');
114 char *filename_end = NULL;
115 int filename_len = 0;
116 char *targethost = NULL;
117 char *new_target = NULL;
119 if (filename_start == NULL) {
120 DEBUG(10, ("No filename start in %s\n", target));
124 filename_end = strchr_m(filename_start+1, '@');
126 if (filename_end == NULL) {
127 DEBUG(10, ("No filename end in %s\n", target));
131 filename_len = PTR_DIFF(filename_end, filename_start+1);
132 mapfilename = talloc_strdup(ctx, filename_start+1);
136 mapfilename[filename_len] = '\0';
138 DEBUG(10, ("Expanding from table [%s]\n", mapfilename));
140 targethost = read_target_host(
141 ctx, conn->sconn->client_id.addr, mapfilename);
142 if (targethost == NULL) {
143 DEBUG(1, ("Could not expand target host from file %s\n",
148 targethost = talloc_sub_advanced(ctx,
149 lp_servicename(SNUM(conn)),
150 conn->session_info->unix_name,
152 conn->session_info->utok.gid,
153 conn->session_info->sanitized_username,
154 conn->session_info->info3->base.domain.string,
157 DEBUG(10, ("Expanded targethost to %s\n", targethost));
159 /* Replace the part between '@...@' */
160 *filename_start = '\0';
161 new_target = talloc_asprintf(ctx,
170 DEBUG(10, ("New DFS target: %s\n", new_target));
174 static int expand_msdfs_readlink(struct vfs_handle_struct *handle,
175 const char *path, char *buf, size_t bufsiz)
177 TALLOC_CTX *ctx = talloc_tos();
179 char *target = TALLOC_ARRAY(ctx, char, PATH_MAX+1);
191 result = SMB_VFS_NEXT_READLINK(handle, path, target,
197 target[result] = '\0';
199 if ((strncmp(target, "msdfs:", 6) == 0) &&
200 (strchr_m(target, '@') != NULL)) {
201 target = expand_msdfs_target(ctx, handle->conn, target);
208 len = MIN(bufsiz, strlen(target));
210 memcpy(buf, target, len);
216 static struct vfs_fn_pointers vfs_expand_msdfs_fns = {
217 .vfs_readlink = expand_msdfs_readlink
220 NTSTATUS vfs_expand_msdfs_init(void);
221 NTSTATUS vfs_expand_msdfs_init(void)
223 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "expand_msdfs",
224 &vfs_expand_msdfs_fns);