s3:param: make "servicename" a substituted option
[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 #include "../lib/tsocket/tsocket.h"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_VFS
30
31 /**********************************************************
32   Under mapfile we expect a table of the following format:
33
34   IP-Prefix whitespace expansion
35
36   For example:
37   192.168.234 local.samba.org
38   192.168     remote.samba.org
39               default.samba.org
40
41   This is to redirect a DFS client to a host close to it.
42 ***********************************************************/
43
44 static char *read_target_host(TALLOC_CTX *ctx, const char *mapfile,
45                               const char *clientaddr)
46 {
47         FILE *f;
48         char buf[1024];
49         char *space = buf;
50         bool found = false;
51
52         f = fopen(mapfile, "r");
53
54         if (f == NULL) {
55                 DEBUG(0,("can't open IP map %s. Error %s\n",
56                          mapfile, strerror(errno) ));
57                 return NULL;
58         }
59
60         DEBUG(10, ("Scanning mapfile [%s]\n", mapfile));
61
62         while (fgets(buf, sizeof(buf), f) != NULL) {
63
64                 if ((strlen(buf) > 0) && (buf[strlen(buf)-1] == '\n'))
65                         buf[strlen(buf)-1] = '\0';
66
67                 DEBUG(10, ("Scanning line [%s]\n", buf));
68
69                 space = strchr_m(buf, ' ');
70
71                 if (space == NULL) {
72                         DEBUG(0, ("Ignoring invalid line %s\n", buf));
73                         continue;
74                 }
75
76                 *space = '\0';
77
78                 if (strncmp(clientaddr, buf, strlen(buf)) == 0) {
79                         found = true;
80                         break;
81                 }
82         }
83
84         fclose(f);
85
86         if (!found) {
87                 return NULL;
88         }
89
90         space += 1;
91
92         while (isspace(*space))
93                 space += 1;
94
95         return talloc_strdup(ctx, space);
96 }
97
98 /**********************************************************
99
100   Expand the msdfs target host using read_target_host
101   explained above. The syntax used in the msdfs link is
102
103   msdfs:@table-filename@/share
104
105   Everything between and including the two @-signs is
106   replaced by the substitution string found in the table
107   described above.
108
109 ***********************************************************/
110
111 static char *expand_msdfs_target(TALLOC_CTX *ctx,
112                                 connection_struct *conn,
113                                 char *target)
114 {
115         const struct loadparm_substitution *lp_sub =
116                 loadparm_s3_global_substitution();
117         char *mapfilename = NULL;
118         char *filename_start = strchr_m(target, '@');
119         char *filename_end = NULL;
120         int filename_len = 0;
121         char *targethost = NULL;
122         char *new_target = NULL;
123         char *raddr;
124
125         if (filename_start == NULL) {
126                 DEBUG(10, ("No filename start in %s\n", target));
127                 return NULL;
128         }
129
130         filename_end = strchr_m(filename_start+1, '@');
131
132         if (filename_end == NULL) {
133                 DEBUG(10, ("No filename end in %s\n", target));
134                 return NULL;
135         }
136
137         filename_len = PTR_DIFF(filename_end, filename_start+1);
138         mapfilename = talloc_strdup(ctx, filename_start+1);
139         if (!mapfilename) {
140                 return NULL;
141         }
142         mapfilename[filename_len] = '\0';
143
144         DEBUG(10, ("Expanding from table [%s]\n", mapfilename));
145
146         raddr = tsocket_address_inet_addr_string(conn->sconn->remote_address,
147                                                  ctx);
148         if (raddr == NULL) {
149                 return NULL;
150         }
151
152         targethost = read_target_host(ctx, mapfilename, raddr);
153         if (targethost == NULL) {
154                 DEBUG(1, ("Could not expand target host from file %s\n",
155                           mapfilename));
156                 return NULL;
157         }
158
159         targethost = talloc_sub_full(ctx,
160                                 lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
161                                 conn->session_info->unix_info->unix_name,
162                                 conn->connectpath,
163                                 conn->session_info->unix_token->gid,
164                                 conn->session_info->unix_info->sanitized_username,
165                                 conn->session_info->info->domain_name,
166                                 targethost);
167
168         DEBUG(10, ("Expanded targethost to %s\n", targethost));
169
170         /* Replace the part between '@...@' */
171         *filename_start = '\0';
172         new_target = talloc_asprintf(ctx,
173                                 "%s%s%s",
174                                 target,
175                                 targethost,
176                                 filename_end+1);
177         if (!new_target) {
178                 return NULL;
179         }
180
181         DEBUG(10, ("New DFS target: %s\n", new_target));
182         return new_target;
183 }
184
185 static int expand_msdfs_readlinkat(struct vfs_handle_struct *handle,
186                                 files_struct *dirfsp,
187                                 const struct smb_filename *smb_fname,
188                                 char *buf,
189                                 size_t bufsiz)
190 {
191         TALLOC_CTX *ctx = talloc_tos();
192         int result;
193         char *target = talloc_array(ctx, char, PATH_MAX+1);
194         size_t len;
195
196         if (!target) {
197                 errno = ENOMEM;
198                 return -1;
199         }
200         if (bufsiz == 0) {
201                 errno = EINVAL;
202                 return -1;
203         }
204
205         result = SMB_VFS_NEXT_READLINKAT(handle,
206                                 dirfsp,
207                                 smb_fname,
208                                 target,
209                                 PATH_MAX);
210
211         if (result <= 0)
212                 return result;
213
214         target[result] = '\0';
215
216         if ((strncmp(target, "msdfs:", 6) == 0) &&
217             (strchr_m(target, '@') != NULL)) {
218                 target = expand_msdfs_target(ctx, handle->conn, target);
219                 if (!target) {
220                         errno = ENOENT;
221                         return -1;
222                 }
223         }
224
225         len = MIN(bufsiz, strlen(target));
226
227         memcpy(buf, target, len);
228
229         TALLOC_FREE(target);
230         return len;
231 }
232
233 static struct vfs_fn_pointers vfs_expand_msdfs_fns = {
234         .readlinkat_fn = expand_msdfs_readlinkat
235 };
236
237 static_decl_vfs;
238 NTSTATUS vfs_expand_msdfs_init(TALLOC_CTX *ctx)
239 {
240         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "expand_msdfs",
241                                 &vfs_expand_msdfs_fns);
242 }