r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[kai/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
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 BOOL read_target_host(const char *mapfile, pstring targethost)
41 {
42         XFILE *f;
43         pstring buf;
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 False;
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(client_addr(), buf, strlen(buf)) == 0) {
74                         found = True;
75                         break;
76                 }
77         }
78
79         x_fclose(f);
80
81         if (!found)
82                 return False;
83
84         space += 1;
85
86         while (isspace(*space))
87                 space += 1;
88
89         pstrcpy(targethost, space);
90         return True;
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 BOOL expand_msdfs_target(connection_struct* conn, pstring target)
107 {
108         pstring mapfilename;
109         char *filename_start = strchr_m(target, '@');
110         char *filename_end;
111         int filename_len;
112         pstring targethost;
113         pstring new_target;
114
115         if (filename_start == NULL) {
116                 DEBUG(10, ("No filename start in %s\n", target));
117                 return False;
118         }
119
120         filename_end = strchr_m(filename_start+1, '@');
121
122         if (filename_end == NULL) {
123                 DEBUG(10, ("No filename end in %s\n", target));
124                 return False;
125         }
126
127         filename_len = PTR_DIFF(filename_end, filename_start+1);
128         pstrcpy(mapfilename, filename_start+1);
129         mapfilename[filename_len] = '\0';
130
131         DEBUG(10, ("Expanding from table [%s]\n", mapfilename));
132
133         if (!read_target_host(mapfilename, targethost)) {
134                 DEBUG(1, ("Could not expand target host from file %s\n",
135                           mapfilename));
136                 return False;
137         }
138
139         standard_sub_advanced(lp_servicename(SNUM(conn)), conn->user,
140                               conn->connectpath, conn->gid,
141                               get_current_username(),
142                               current_user_info.domain,
143                               mapfilename, sizeof(mapfilename));
144
145         DEBUG(10, ("Expanded targethost to %s\n", targethost));
146
147         *filename_start = '\0';
148         pstrcpy(new_target, target);
149         pstrcat(new_target, targethost);
150         pstrcat(new_target, filename_end+1);
151
152         DEBUG(10, ("New DFS target: %s\n", new_target));
153         pstrcpy(target, new_target);
154         return True;
155 }
156
157 static int expand_msdfs_readlink(struct vfs_handle_struct *handle,
158                                  const char *path, char *buf, size_t bufsiz)
159 {
160         pstring target;
161         int result;
162
163         result = SMB_VFS_NEXT_READLINK(handle, path, target,
164                                        sizeof(target));
165
166         if (result < 0)
167                 return result;
168
169         target[result] = '\0';
170
171         if ((strncmp(target, "msdfs:", strlen("msdfs:")) == 0) &&
172             (strchr_m(target, '@') != NULL)) {
173                 if (!expand_msdfs_target(handle->conn, target)) {
174                         errno = ENOENT;
175                         return -1;
176                 }
177         }
178
179         safe_strcpy(buf, target, bufsiz-1);
180         return strlen(buf);
181 }
182
183 /* VFS operations structure */
184
185 static vfs_op_tuple expand_msdfs_ops[] = {      
186         {SMB_VFS_OP(expand_msdfs_readlink), SMB_VFS_OP_READLINK,
187          SMB_VFS_LAYER_TRANSPARENT},
188         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
189 };
190
191 NTSTATUS vfs_expand_msdfs_init(void);
192 NTSTATUS vfs_expand_msdfs_init(void)
193 {
194         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "expand_msdfs",
195                                 expand_msdfs_ops);
196 }