9d4883c08544c78d3fecc77f56e9ff925807ed39
[bbaumbach/samba-autobuild/.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
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_VFS
24
25 extern userdom_struct current_user_info;
26
27 /**********************************************************
28   Under mapfile we expect a table of the following format:
29
30   IP-Prefix whitespace expansion
31
32   For example:
33   192.168.234 local.samba.org
34   192.168     remote.samba.org
35               default.samba.org
36
37   This is to redirect a DFS client to a host close to it.
38 ***********************************************************/
39
40 static char *read_target_host(TALLOC_CTX *ctx, const char *mapfile)
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                 char addr[INET6_ADDRSTRLEN];
59
60                 if ((strlen(buf) > 0) && (buf[strlen(buf)-1] == '\n'))
61                         buf[strlen(buf)-1] = '\0';
62
63                 DEBUG(10, ("Scanning line [%s]\n", buf));
64
65                 space = strchr_m(buf, ' ');
66
67                 if (space == NULL) {
68                         DEBUG(0, ("Ignoring invalid line %s\n", buf));
69                         continue;
70                 }
71
72                 *space = '\0';
73
74                 if (strncmp(client_addr(get_client_fd(),addr,sizeof(addr)),
75                                 buf, strlen(buf)) == 0) {
76                         found = true;
77                         break;
78                 }
79         }
80
81         x_fclose(f);
82
83         if (!found) {
84                 return NULL;
85         }
86
87         space += 1;
88
89         while (isspace(*space))
90                 space += 1;
91
92         return talloc_strdup(ctx, space);
93 }
94
95 /**********************************************************
96
97   Expand the msdfs target host using read_target_host
98   explained above. The syntax used in the msdfs link is
99
100   msdfs:@table-filename@/share
101
102   Everything between and including the two @-signs is
103   replaced by the substitution string found in the table
104   described above.
105
106 ***********************************************************/
107
108 static char *expand_msdfs_target(TALLOC_CTX *ctx,
109                                 connection_struct *conn,
110                                 char *target)
111 {
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;
118
119         if (filename_start == NULL) {
120                 DEBUG(10, ("No filename start in %s\n", target));
121                 return NULL;
122         }
123
124         filename_end = strchr_m(filename_start+1, '@');
125
126         if (filename_end == NULL) {
127                 DEBUG(10, ("No filename end in %s\n", target));
128                 return NULL;
129         }
130
131         filename_len = PTR_DIFF(filename_end, filename_start+1);
132         mapfilename = talloc_strdup(ctx, filename_start+1);
133         if (!mapfilename) {
134                 return NULL;
135         }
136         mapfilename[filename_len] = '\0';
137
138         DEBUG(10, ("Expanding from table [%s]\n", mapfilename));
139
140         if ((targethost = read_target_host(ctx, mapfilename)) == NULL) {
141                 DEBUG(1, ("Could not expand target host from file %s\n",
142                           mapfilename));
143                 return NULL;
144         }
145
146         targethost = talloc_sub_advanced(ctx,
147                                 lp_servicename(SNUM(conn)),
148                                 conn->user,
149                                 conn->connectpath,
150                                 conn->gid,
151                                 get_current_username(),
152                                 current_user_info.domain,
153                                 targethost);
154
155         DEBUG(10, ("Expanded targethost to %s\n", targethost));
156
157         /* Replace the part between '@...@' */
158         *filename_start = '\0';
159         new_target = talloc_asprintf(ctx,
160                                 "%s%s%s",
161                                 target,
162                                 targethost,
163                                 filename_end+1);
164         if (!new_target) {
165                 return NULL;
166         }
167
168         DEBUG(10, ("New DFS target: %s\n", new_target));
169         return new_target;
170 }
171
172 static int expand_msdfs_readlink(struct vfs_handle_struct *handle,
173                                  const char *path, char *buf, size_t bufsiz)
174 {
175         TALLOC_CTX *ctx = talloc_tos();
176         int result;
177         char *target = TALLOC_ARRAY(ctx, char, PATH_MAX+1);
178
179         if (!target) {
180                 errno = ENOMEM;
181                 return -1;
182         }
183         result = SMB_VFS_NEXT_READLINK(handle, path, target,
184                                        PATH_MAX);
185
186         if (result < 0)
187                 return result;
188
189         target[result] = '\0';
190
191         if ((strncmp(target, "msdfs:", strlen("msdfs:")) == 0) &&
192             (strchr_m(target, '@') != NULL)) {
193                 target = expand_msdfs_target(ctx, handle->conn, target);
194                 if (!target) {
195                         errno = ENOENT;
196                         return -1;
197                 }
198         }
199
200         safe_strcpy(buf, target, bufsiz-1);
201         return strlen(buf);
202 }
203
204 /* VFS operations structure */
205
206 static vfs_op_tuple expand_msdfs_ops[] = {
207         {SMB_VFS_OP(expand_msdfs_readlink), SMB_VFS_OP_READLINK,
208          SMB_VFS_LAYER_TRANSPARENT},
209         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
210 };
211
212 NTSTATUS vfs_expand_msdfs_init(void);
213 NTSTATUS vfs_expand_msdfs_init(void)
214 {
215         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "expand_msdfs",
216                                 expand_msdfs_ops);
217 }