Some fixes to expand_msdfs module.
[metze/samba/wip.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 2 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, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "includes.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 BOOL read_target_host(const char *mapfile, pstring targethost)
40 {
41         XFILE *f;
42         pstring buf;
43         char *s, *space = buf;
44         BOOL found = False;
45         
46         f = x_fopen(mapfile, O_RDONLY, 0);
47
48         if (f == NULL) {
49                 DEBUG(0,("can't open IP map %s. Error %s\n",
50                          mapfile, strerror(errno) ));
51                 return False;
52         }
53
54         DEBUG(10, ("Scanning mapfile [%s]\n", mapfile));
55
56         while ((s=x_fgets(buf, sizeof(buf), f)) != NULL) {
57
58                 if (buf[strlen(buf)-1] == '\n')
59                         buf[strlen(buf)-1] = '\0';
60
61                 DEBUG(10, ("Scanning line [%s]\n", buf));
62
63                 space = strchr_m(buf, ' ');
64
65                 if (space == NULL) {
66                         DEBUG(0, ("Ignoring invalid line %s\n", buf));
67                         continue;
68                 }
69
70                 *space = '\0';
71
72                 if (strncmp(client_addr(), buf, strlen(buf)) == 0) {
73                         found = True;
74                         break;
75                 }
76         }
77
78         x_fclose(f);
79
80         if (!found)
81                 return False;
82
83         space += 1;
84
85         while (isspace(*space))
86                 space += 1;
87
88         pstrcpy(targethost, space);
89         return True;
90 }
91
92 /**********************************************************
93
94   Expand the msdfs target host using read_target_host
95   explained above. The syntax used in the msdfs link is
96
97   msdfs:@table-filename@/share
98
99   Everything between and including the two @-signs is
100   replaced by the substitution string found in the table
101   described above.
102
103 ***********************************************************/
104
105 static BOOL expand_msdfs_target(connection_struct* conn, pstring target)
106 {
107         pstring mapfilename;
108         char *filename_start = strchr_m(target, '@');
109         char *filename_end;
110         int filename_len;
111         pstring targethost;
112         pstring new_target;
113
114         if (filename_start == NULL) {
115                 DEBUG(10, ("No filename start in %s\n", target));
116                 return False;
117         }
118
119         filename_end = strchr_m(filename_start+1, '@');
120
121         if (filename_end == NULL) {
122                 DEBUG(10, ("No filename end in %s\n", target));
123                 return False;
124         }
125
126         filename_len = PTR_DIFF(filename_end, filename_start+1);
127         pstrcpy(mapfilename, filename_start+1);
128         mapfilename[filename_len] = '\0';
129
130         DEBUG(10, ("Expanding from table [%s]\n", mapfilename));
131
132         if (!read_target_host(mapfilename, targethost)) {
133                 DEBUG(1, ("Could not expand target host from file %s\n",
134                           mapfilename));
135                 return False;
136         }
137
138         standard_sub_conn(conn, mapfilename, sizeof(mapfilename));
139
140         DEBUG(10, ("Expanded targethost to %s\n", targethost));
141
142         *filename_start = '\0';
143         pstrcpy(new_target, target);
144         pstrcat(new_target, targethost);
145         pstrcat(new_target, filename_end+1);
146
147         DEBUG(10, ("New DFS target: %s\n", new_target));
148         pstrcpy(target, new_target);
149         return True;
150 }
151
152 static int expand_msdfs_readlink(struct vfs_handle_struct *handle,
153                                  struct connection_struct *conn,
154                                  const char *path, char *buf, size_t bufsiz)
155 {
156         pstring target;
157         int result;
158
159         result = SMB_VFS_NEXT_READLINK(handle, conn, path, target,
160                                        sizeof(target));
161
162         if (result < 0)
163                 return result;
164
165         target[result] = '\0';
166
167         if ((strncmp(target, "msdfs:", strlen("msdfs:")) == 0) &&
168             (strchr_m(target, '@') != NULL)) {
169                 if (!expand_msdfs_target(conn, target)) {
170                         errno = ENOENT;
171                         return -1;
172                 }
173         }
174
175         safe_strcpy(buf, target, bufsiz-1);
176         return strlen(buf);
177 }
178
179 /* VFS operations structure */
180
181 static vfs_op_tuple expand_msdfs_ops[] = {      
182         {SMB_VFS_OP(expand_msdfs_readlink), SMB_VFS_OP_READLINK,
183          SMB_VFS_LAYER_TRANSPARENT},
184         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
185 };
186
187 NTSTATUS vfs_expand_msdfs_init(void)
188 {
189         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "expand_msdfs",
190                                 expand_msdfs_ops);
191 }