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