lib: Remove global xfile.h includes
[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 #include "lib/util/xfile.h"
28
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_VFS
31
32 /**********************************************************
33   Under mapfile we expect a table of the following format:
34
35   IP-Prefix whitespace expansion
36
37   For example:
38   192.168.234 local.samba.org
39   192.168     remote.samba.org
40               default.samba.org
41
42   This is to redirect a DFS client to a host close to it.
43 ***********************************************************/
44
45 static char *read_target_host(TALLOC_CTX *ctx, const char *mapfile,
46                               const char *clientaddr)
47 {
48         XFILE *f;
49         char buf[1024];
50         char *space = buf;
51         bool found = false;
52
53         f = x_fopen(mapfile, O_RDONLY, 0);
54
55         if (f == NULL) {
56                 DEBUG(0,("can't open IP map %s. Error %s\n",
57                          mapfile, strerror(errno) ));
58                 return NULL;
59         }
60
61         DEBUG(10, ("Scanning mapfile [%s]\n", mapfile));
62
63         while (x_fgets(buf, sizeof(buf), f) != NULL) {
64
65                 if ((strlen(buf) > 0) && (buf[strlen(buf)-1] == '\n'))
66                         buf[strlen(buf)-1] = '\0';
67
68                 DEBUG(10, ("Scanning line [%s]\n", buf));
69
70                 space = strchr_m(buf, ' ');
71
72                 if (space == NULL) {
73                         DEBUG(0, ("Ignoring invalid line %s\n", buf));
74                         continue;
75                 }
76
77                 *space = '\0';
78
79                 if (strncmp(clientaddr, buf, strlen(buf)) == 0) {
80                         found = true;
81                         break;
82                 }
83         }
84
85         x_fclose(f);
86
87         if (!found) {
88                 return NULL;
89         }
90
91         space += 1;
92
93         while (isspace(*space))
94                 space += 1;
95
96         return talloc_strdup(ctx, space);
97 }
98
99 /**********************************************************
100
101   Expand the msdfs target host using read_target_host
102   explained above. The syntax used in the msdfs link is
103
104   msdfs:@table-filename@/share
105
106   Everything between and including the two @-signs is
107   replaced by the substitution string found in the table
108   described above.
109
110 ***********************************************************/
111
112 static char *expand_msdfs_target(TALLOC_CTX *ctx,
113                                 connection_struct *conn,
114                                 char *target)
115 {
116         char *mapfilename = NULL;
117         char *filename_start = strchr_m(target, '@');
118         char *filename_end = NULL;
119         int filename_len = 0;
120         char *targethost = NULL;
121         char *new_target = NULL;
122         char *raddr;
123
124         if (filename_start == NULL) {
125                 DEBUG(10, ("No filename start in %s\n", target));
126                 return NULL;
127         }
128
129         filename_end = strchr_m(filename_start+1, '@');
130
131         if (filename_end == NULL) {
132                 DEBUG(10, ("No filename end in %s\n", target));
133                 return NULL;
134         }
135
136         filename_len = PTR_DIFF(filename_end, filename_start+1);
137         mapfilename = talloc_strdup(ctx, filename_start+1);
138         if (!mapfilename) {
139                 return NULL;
140         }
141         mapfilename[filename_len] = '\0';
142
143         DEBUG(10, ("Expanding from table [%s]\n", mapfilename));
144
145         raddr = tsocket_address_inet_addr_string(conn->sconn->remote_address,
146                                                  ctx);
147         if (raddr == NULL) {
148                 return NULL;
149         }
150
151         targethost = read_target_host(
152                 ctx, raddr, mapfilename);
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_advanced(ctx,
160                                 lp_servicename(talloc_tos(), 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_readlink(struct vfs_handle_struct *handle,
186                                  const char *path, char *buf, size_t bufsiz)
187 {
188         TALLOC_CTX *ctx = talloc_tos();
189         int result;
190         char *target = talloc_array(ctx, char, PATH_MAX+1);
191         size_t len;
192
193         if (!target) {
194                 errno = ENOMEM;
195                 return -1;
196         }
197         if (bufsiz == 0) {
198                 errno = EINVAL;
199                 return -1;
200         }
201
202         result = SMB_VFS_NEXT_READLINK(handle, path, target,
203                                        PATH_MAX);
204
205         if (result <= 0)
206                 return result;
207
208         target[result] = '\0';
209
210         if ((strncmp(target, "msdfs:", 6) == 0) &&
211             (strchr_m(target, '@') != NULL)) {
212                 target = expand_msdfs_target(ctx, handle->conn, target);
213                 if (!target) {
214                         errno = ENOENT;
215                         return -1;
216                 }
217         }
218
219         len = MIN(bufsiz, strlen(target));
220
221         memcpy(buf, target, len);
222
223         TALLOC_FREE(target);
224         return len;
225 }
226
227 static struct vfs_fn_pointers vfs_expand_msdfs_fns = {
228         .readlink_fn = expand_msdfs_readlink
229 };
230
231 NTSTATUS vfs_expand_msdfs_init(void);
232 NTSTATUS vfs_expand_msdfs_init(void)
233 {
234         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "expand_msdfs",
235                                 &vfs_expand_msdfs_fns);
236 }