build: ensure compiler flags are properly detected
[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 #include "../librpc/gen_ndr/ndr_netlogon.h"
22 #include "smbd/globals.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_VFS
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                               const char *clientaddr)
42 {
43         XFILE *f;
44         char buf[1024];
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 NULL;
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(clientaddr, buf, strlen(buf)) == 0) {
75                         found = true;
76                         break;
77                 }
78         }
79
80         x_fclose(f);
81
82         if (!found) {
83                 return NULL;
84         }
85
86         space += 1;
87
88         while (isspace(*space))
89                 space += 1;
90
91         return talloc_strdup(ctx, space);
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 char *expand_msdfs_target(TALLOC_CTX *ctx,
108                                 connection_struct *conn,
109                                 char *target)
110 {
111         char *mapfilename = NULL;
112         char *filename_start = strchr_m(target, '@');
113         char *filename_end = NULL;
114         int filename_len = 0;
115         char *targethost = NULL;
116         char *new_target = NULL;
117
118         if (filename_start == NULL) {
119                 DEBUG(10, ("No filename start in %s\n", target));
120                 return NULL;
121         }
122
123         filename_end = strchr_m(filename_start+1, '@');
124
125         if (filename_end == NULL) {
126                 DEBUG(10, ("No filename end in %s\n", target));
127                 return NULL;
128         }
129
130         filename_len = PTR_DIFF(filename_end, filename_start+1);
131         mapfilename = talloc_strdup(ctx, filename_start+1);
132         if (!mapfilename) {
133                 return NULL;
134         }
135         mapfilename[filename_len] = '\0';
136
137         DEBUG(10, ("Expanding from table [%s]\n", mapfilename));
138
139         targethost = read_target_host(
140                 ctx, conn->sconn->client_id.addr, mapfilename);
141         if (targethost == NULL) {
142                 DEBUG(1, ("Could not expand target host from file %s\n",
143                           mapfilename));
144                 return NULL;
145         }
146
147         targethost = talloc_sub_advanced(ctx,
148                                 lp_servicename(SNUM(conn)),
149                                 conn->session_info->unix_name,
150                                 conn->connectpath,
151                                 conn->session_info->utok.gid,
152                                 conn->session_info->sanitized_username,
153                                 conn->session_info->info3->base.domain.string,
154                                 targethost);
155
156         DEBUG(10, ("Expanded targethost to %s\n", targethost));
157
158         /* Replace the part between '@...@' */
159         *filename_start = '\0';
160         new_target = talloc_asprintf(ctx,
161                                 "%s%s%s",
162                                 target,
163                                 targethost,
164                                 filename_end+1);
165         if (!new_target) {
166                 return NULL;
167         }
168
169         DEBUG(10, ("New DFS target: %s\n", new_target));
170         return new_target;
171 }
172
173 static int expand_msdfs_readlink(struct vfs_handle_struct *handle,
174                                  const char *path, char *buf, size_t bufsiz)
175 {
176         TALLOC_CTX *ctx = talloc_tos();
177         int result;
178         char *target = TALLOC_ARRAY(ctx, char, PATH_MAX+1);
179         size_t len;
180
181         if (!target) {
182                 errno = ENOMEM;
183                 return -1;
184         }
185         if (bufsiz == 0) {
186                 errno = EINVAL;
187                 return -1;
188         }
189
190         result = SMB_VFS_NEXT_READLINK(handle, path, target,
191                                        PATH_MAX);
192
193         if (result <= 0)
194                 return result;
195
196         target[result] = '\0';
197
198         if ((strncmp(target, "msdfs:", 6) == 0) &&
199             (strchr_m(target, '@') != NULL)) {
200                 target = expand_msdfs_target(ctx, handle->conn, target);
201                 if (!target) {
202                         errno = ENOENT;
203                         return -1;
204                 }
205         }
206
207         len = MIN(bufsiz, strlen(target));
208
209         memcpy(buf, target, len);
210
211         TALLOC_FREE(target);
212         return len;
213 }
214
215 static struct vfs_fn_pointers vfs_expand_msdfs_fns = {
216         .vfs_readlink = expand_msdfs_readlink
217 };
218
219 NTSTATUS vfs_expand_msdfs_init(void);
220 NTSTATUS vfs_expand_msdfs_init(void)
221 {
222         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "expand_msdfs",
223                                 &vfs_expand_msdfs_fns);
224 }