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