r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[tprouty/samba.git] / source / 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, 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 extern userdom_struct current_user_info;
27
28 /**********************************************************
29   Under mapfile we expect a table of the following format:
30
31   IP-Prefix whitespace expansion
32
33   For example:
34   192.168.234 local.samba.org
35   192.168     remote.samba.org
36               default.samba.org
37
38   This is to redirect a DFS client to a host close to it.
39 ***********************************************************/
40
41 static BOOL read_target_host(const char *mapfile, pstring targethost)
42 {
43         XFILE *f;
44         pstring buf;
45         char *space = buf;
46         BOOL found = False;
47         
48         f = x_fopen(mapfile, O_RDONLY, 0);
49
50         if (f == NULL) {
51                 DEBUG(0,("can't open IP map %s. Error %s\n",
52                          mapfile, strerror(errno) ));
53                 return False;
54         }
55
56         DEBUG(10, ("Scanning mapfile [%s]\n", mapfile));
57
58         while (x_fgets(buf, sizeof(buf), f) != NULL) {
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(), buf, strlen(buf)) == 0) {
75                         found = True;
76                         break;
77                 }
78         }
79
80         x_fclose(f);
81
82         if (!found)
83                 return False;
84
85         space += 1;
86
87         while (isspace(*space))
88                 space += 1;
89
90         pstrcpy(targethost, space);
91         return True;
92 }
93
94 /**********************************************************
95
96   Expand the msdfs target host using read_target_host
97   explained above. The syntax used in the msdfs link is
98
99   msdfs:@table-filename@/share
100
101   Everything between and including the two @-signs is
102   replaced by the substitution string found in the table
103   described above.
104
105 ***********************************************************/
106
107 static BOOL expand_msdfs_target(connection_struct* conn, pstring target)
108 {
109         pstring mapfilename;
110         char *filename_start = strchr_m(target, '@');
111         char *filename_end;
112         int filename_len;
113         pstring targethost;
114         pstring new_target;
115
116         if (filename_start == NULL) {
117                 DEBUG(10, ("No filename start in %s\n", target));
118                 return False;
119         }
120
121         filename_end = strchr_m(filename_start+1, '@');
122
123         if (filename_end == NULL) {
124                 DEBUG(10, ("No filename end in %s\n", target));
125                 return False;
126         }
127
128         filename_len = PTR_DIFF(filename_end, filename_start+1);
129         pstrcpy(mapfilename, filename_start+1);
130         mapfilename[filename_len] = '\0';
131
132         DEBUG(10, ("Expanding from table [%s]\n", mapfilename));
133
134         if (!read_target_host(mapfilename, targethost)) {
135                 DEBUG(1, ("Could not expand target host from file %s\n",
136                           mapfilename));
137                 return False;
138         }
139
140         standard_sub_advanced(lp_servicename(SNUM(conn)), conn->user,
141                               conn->connectpath, conn->gid,
142                               get_current_username(),
143                               current_user_info.domain,
144                               mapfilename, sizeof(mapfilename));
145
146         DEBUG(10, ("Expanded targethost to %s\n", targethost));
147
148         *filename_start = '\0';
149         pstrcpy(new_target, target);
150         pstrcat(new_target, targethost);
151         pstrcat(new_target, filename_end+1);
152
153         DEBUG(10, ("New DFS target: %s\n", new_target));
154         pstrcpy(target, new_target);
155         return True;
156 }
157
158 static int expand_msdfs_readlink(struct vfs_handle_struct *handle,
159                                  const char *path, char *buf, size_t bufsiz)
160 {
161         pstring target;
162         int result;
163
164         result = SMB_VFS_NEXT_READLINK(handle, path, target,
165                                        sizeof(target));
166
167         if (result < 0)
168                 return result;
169
170         target[result] = '\0';
171
172         if ((strncmp(target, "msdfs:", strlen("msdfs:")) == 0) &&
173             (strchr_m(target, '@') != NULL)) {
174                 if (!expand_msdfs_target(handle->conn, target)) {
175                         errno = ENOENT;
176                         return -1;
177                 }
178         }
179
180         safe_strcpy(buf, target, bufsiz-1);
181         return strlen(buf);
182 }
183
184 /* VFS operations structure */
185
186 static vfs_op_tuple expand_msdfs_ops[] = {      
187         {SMB_VFS_OP(expand_msdfs_readlink), SMB_VFS_OP_READLINK,
188          SMB_VFS_LAYER_TRANSPARENT},
189         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
190 };
191
192 NTSTATUS vfs_expand_msdfs_init(void);
193 NTSTATUS vfs_expand_msdfs_init(void)
194 {
195         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "expand_msdfs",
196                                 expand_msdfs_ops);
197 }