f1f82d243ee05fd8be0a4ed114611f3009b7beed
[samba.git] / source3 / modules / vfs_expand_msdfs.c
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
43 static 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
110 static 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));
130                 return NULL;
131         }
132
133         filename_len = PTR_DIFF(filename_end, filename_start+1);
134         mapfilename = talloc_strdup(ctx, filename_start+1);
135         if (!mapfilename) {
136                 return NULL;
137         }
138         mapfilename[filename_len] = '\0';
139
140         DEBUG(10, ("Expanding from table [%s]\n", mapfilename));
141
142         targethost = read_target_host(
143                 ctx, conn->sconn->client_id.addr, mapfilename);
144         if (targethost == NULL) {
145                 DEBUG(1, ("Could not expand target host from file %s\n",
146                           mapfilename));
147                 return NULL;
148         }
149
150         targethost = talloc_sub_advanced(ctx,
151                                 lp_servicename(SNUM(conn)),
152                                 conn->session_info->unix_name,
153                                 conn->connectpath,
154                                 conn->session_info->utok.gid,
155                                 conn->session_info->sanitized_username,
156                                 conn->session_info->info3->base.domain.string,
157                                 targethost);
158
159         DEBUG(10, ("Expanded targethost to %s\n", targethost));
160
161         /* Replace the part between '@...@' */
162         *filename_start = '\0';
163         new_target = talloc_asprintf(ctx,
164                                 "%s%s%s",
165                                 target,
166                                 targethost,
167                                 filename_end+1);
168         if (!new_target) {
169                 return NULL;
170         }
171
172         DEBUG(10, ("New DFS target: %s\n", new_target));
173         return new_target;
174 }
175
176 static int expand_msdfs_readlink(struct vfs_handle_struct *handle,
177                                  const char *path, char *buf, size_t bufsiz)
178 {
179         TALLOC_CTX *ctx = talloc_tos();
180         int result;
181         char *target = TALLOC_ARRAY(ctx, char, PATH_MAX+1);
182         size_t len;
183
184         if (!target) {
185                 errno = ENOMEM;
186                 return -1;
187         }
188         if (bufsiz == 0) {
189                 errno = EINVAL;
190                 return -1;
191         }
192
193         result = SMB_VFS_NEXT_READLINK(handle, path, target,
194                                        PATH_MAX);
195
196         if (result <= 0)
197                 return result;
198
199         target[result] = '\0';
200
201         if ((strncmp(target, "msdfs:", 6) == 0) &&
202             (strchr_m(target, '@') != NULL)) {
203                 target = expand_msdfs_target(ctx, handle->conn, target);
204                 if (!target) {
205                         errno = ENOENT;
206                         return -1;
207                 }
208         }
209
210         len = MIN(bufsiz, strlen(target));
211
212         memcpy(buf, target, len);
213
214         TALLOC_FREE(target);
215         return len;
216 }
217
218 static struct vfs_fn_pointers vfs_expand_msdfs_fns = {
219         .vfs_readlink = expand_msdfs_readlink
220 };
221
222 NTSTATUS vfs_expand_msdfs_init(void);
223 NTSTATUS vfs_expand_msdfs_init(void)
224 {
225         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "expand_msdfs",
226                                 &vfs_expand_msdfs_fns);
227 }