s4:dsdb/password_hash: split out a password_hash_needed() function
[Samba.git] / source3 / modules / vfs_expand_msdfs.c
blobeaf96e0ae09986aaa1239993afd4495f9f221099
1 /*
2 * Expand msdfs targets based on client IP
4 * Copyright (C) Volker Lendecke, 2004
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.
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.
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/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23 #include "../librpc/gen_ndr/ndr_netlogon.h"
24 #include "smbd/globals.h"
25 #include "auth.h"
26 #include "../lib/tsocket/tsocket.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_VFS
31 /**********************************************************
32 Under mapfile we expect a table of the following format:
34 IP-Prefix whitespace expansion
36 For example:
37 192.168.234 local.samba.org
38 192.168 remote.samba.org
39 default.samba.org
41 This is to redirect a DFS client to a host close to it.
42 ***********************************************************/
44 static char *read_target_host(TALLOC_CTX *ctx, const char *mapfile,
45 const char *clientaddr)
47 XFILE *f;
48 char buf[1024];
49 char *space = buf;
50 bool found = false;
52 f = x_fopen(mapfile, O_RDONLY, 0);
54 if (f == NULL) {
55 DEBUG(0,("can't open IP map %s. Error %s\n",
56 mapfile, strerror(errno) ));
57 return NULL;
60 DEBUG(10, ("Scanning mapfile [%s]\n", mapfile));
62 while (x_fgets(buf, sizeof(buf), f) != NULL) {
64 if ((strlen(buf) > 0) && (buf[strlen(buf)-1] == '\n'))
65 buf[strlen(buf)-1] = '\0';
67 DEBUG(10, ("Scanning line [%s]\n", buf));
69 space = strchr_m(buf, ' ');
71 if (space == NULL) {
72 DEBUG(0, ("Ignoring invalid line %s\n", buf));
73 continue;
76 *space = '\0';
78 if (strncmp(clientaddr, buf, strlen(buf)) == 0) {
79 found = true;
80 break;
84 x_fclose(f);
86 if (!found) {
87 return NULL;
90 space += 1;
92 while (isspace(*space))
93 space += 1;
95 return talloc_strdup(ctx, space);
98 /**********************************************************
100 Expand the msdfs target host using read_target_host
101 explained above. The syntax used in the msdfs link is
103 msdfs:@table-filename@/share
105 Everything between and including the two @-signs is
106 replaced by the substitution string found in the table
107 described above.
109 ***********************************************************/
111 static char *expand_msdfs_target(TALLOC_CTX *ctx,
112 connection_struct *conn,
113 char *target)
115 char *mapfilename = NULL;
116 char *filename_start = strchr_m(target, '@');
117 char *filename_end = NULL;
118 int filename_len = 0;
119 char *targethost = NULL;
120 char *new_target = NULL;
121 char *raddr;
123 if (filename_start == NULL) {
124 DEBUG(10, ("No filename start in %s\n", target));
125 return NULL;
128 filename_end = strchr_m(filename_start+1, '@');
130 if (filename_end == NULL) {
131 DEBUG(10, ("No filename end in %s\n", target));
132 return NULL;
135 filename_len = PTR_DIFF(filename_end, filename_start+1);
136 mapfilename = talloc_strdup(ctx, filename_start+1);
137 if (!mapfilename) {
138 return NULL;
140 mapfilename[filename_len] = '\0';
142 DEBUG(10, ("Expanding from table [%s]\n", mapfilename));
144 raddr = tsocket_address_inet_addr_string(conn->sconn->remote_address,
145 ctx);
146 if (raddr == NULL) {
147 return NULL;
150 targethost = read_target_host(
151 ctx, raddr, mapfilename);
152 if (targethost == NULL) {
153 DEBUG(1, ("Could not expand target host from file %s\n",
154 mapfilename));
155 return NULL;
158 targethost = talloc_sub_advanced(ctx,
159 lp_servicename(talloc_tos(), SNUM(conn)),
160 conn->session_info->unix_info->unix_name,
161 conn->connectpath,
162 conn->session_info->unix_token->gid,
163 conn->session_info->unix_info->sanitized_username,
164 conn->session_info->info->domain_name,
165 targethost);
167 DEBUG(10, ("Expanded targethost to %s\n", targethost));
169 /* Replace the part between '@...@' */
170 *filename_start = '\0';
171 new_target = talloc_asprintf(ctx,
172 "%s%s%s",
173 target,
174 targethost,
175 filename_end+1);
176 if (!new_target) {
177 return NULL;
180 DEBUG(10, ("New DFS target: %s\n", new_target));
181 return new_target;
184 static int expand_msdfs_readlink(struct vfs_handle_struct *handle,
185 const char *path, char *buf, size_t bufsiz)
187 TALLOC_CTX *ctx = talloc_tos();
188 int result;
189 char *target = talloc_array(ctx, char, PATH_MAX+1);
190 size_t len;
192 if (!target) {
193 errno = ENOMEM;
194 return -1;
196 if (bufsiz == 0) {
197 errno = EINVAL;
198 return -1;
201 result = SMB_VFS_NEXT_READLINK(handle, path, target,
202 PATH_MAX);
204 if (result <= 0)
205 return result;
207 target[result] = '\0';
209 if ((strncmp(target, "msdfs:", 6) == 0) &&
210 (strchr_m(target, '@') != NULL)) {
211 target = expand_msdfs_target(ctx, handle->conn, target);
212 if (!target) {
213 errno = ENOENT;
214 return -1;
218 len = MIN(bufsiz, strlen(target));
220 memcpy(buf, target, len);
222 TALLOC_FREE(target);
223 return len;
226 static struct vfs_fn_pointers vfs_expand_msdfs_fns = {
227 .readlink_fn = expand_msdfs_readlink
230 NTSTATUS vfs_expand_msdfs_init(void);
231 NTSTATUS vfs_expand_msdfs_init(void)
233 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "expand_msdfs",
234 &vfs_expand_msdfs_fns);