1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Contains the CIFS DFS referral mounting routines used for handling
4  *   traversal via DFS junction point
5  *
6  *   Copyright (c) 2007 Igor Mammedov
7  *   Copyright (C) International Business Machines  Corp., 2008
8  *   Author(s): Igor Mammedov (niallain@gmail.com)
9  *		Steve French (sfrench@us.ibm.com)
10  */
11 
12 #include <linux/dcache.h>
13 #include <linux/mount.h>
14 #include <linux/namei.h>
15 #include <linux/slab.h>
16 #include <linux/vfs.h>
17 #include <linux/fs.h>
18 #include <linux/inet.h>
19 #include "cifsglob.h"
20 #include "cifsproto.h"
21 #include "cifsfs.h"
22 #include "dns_resolve.h"
23 #include "cifs_debug.h"
24 #include "cifs_unicode.h"
25 #include "dfs_cache.h"
26 #include "fs_context.h"
27 
28 static LIST_HEAD(cifs_dfs_automount_list);
29 
30 static void cifs_dfs_expire_automounts(struct work_struct *work);
31 static DECLARE_DELAYED_WORK(cifs_dfs_automount_task,
32 			    cifs_dfs_expire_automounts);
33 static int cifs_dfs_mountpoint_expiry_timeout = 500 * HZ;
34 
cifs_dfs_expire_automounts(struct work_struct * work)35 static void cifs_dfs_expire_automounts(struct work_struct *work)
36 {
37 	struct list_head *list = &cifs_dfs_automount_list;
38 
39 	mark_mounts_for_expiry(list);
40 	if (!list_empty(list))
41 		schedule_delayed_work(&cifs_dfs_automount_task,
42 				      cifs_dfs_mountpoint_expiry_timeout);
43 }
44 
cifs_dfs_release_automount_timer(void)45 void cifs_dfs_release_automount_timer(void)
46 {
47 	BUG_ON(!list_empty(&cifs_dfs_automount_list));
48 	cancel_delayed_work_sync(&cifs_dfs_automount_task);
49 }
50 
51 /**
52  * cifs_build_devname - build a devicename from a UNC and optional prepath
53  * @nodename:	pointer to UNC string
54  * @prepath:	pointer to prefixpath (or NULL if there isn't one)
55  *
56  * Build a new cifs devicename after chasing a DFS referral. Allocate a buffer
57  * big enough to hold the final thing. Copy the UNC from the nodename, and
58  * concatenate the prepath onto the end of it if there is one.
59  *
60  * Returns pointer to the built string, or a ERR_PTR. Caller is responsible
61  * for freeing the returned string.
62  */
63 static char *
cifs_build_devname(char * nodename,const char * prepath)64 cifs_build_devname(char *nodename, const char *prepath)
65 {
66 	size_t pplen;
67 	size_t unclen;
68 	char *dev;
69 	char *pos;
70 
71 	/* skip over any preceding delimiters */
72 	nodename += strspn(nodename, "\\");
73 	if (!*nodename)
74 		return ERR_PTR(-EINVAL);
75 
76 	/* get length of UNC and set pos to last char */
77 	unclen = strlen(nodename);
78 	pos = nodename + unclen - 1;
79 
80 	/* trim off any trailing delimiters */
81 	while (*pos == '\\') {
82 		--pos;
83 		--unclen;
84 	}
85 
86 	/* allocate a buffer:
87 	 * +2 for preceding "//"
88 	 * +1 for delimiter between UNC and prepath
89 	 * +1 for trailing NULL
90 	 */
91 	pplen = prepath ? strlen(prepath) : 0;
92 	dev = kmalloc(2 + unclen + 1 + pplen + 1, GFP_KERNEL);
93 	if (!dev)
94 		return ERR_PTR(-ENOMEM);
95 
96 	pos = dev;
97 	/* add the initial "//" */
98 	*pos = '/';
99 	++pos;
100 	*pos = '/';
101 	++pos;
102 
103 	/* copy in the UNC portion from referral */
104 	memcpy(pos, nodename, unclen);
105 	pos += unclen;
106 
107 	/* copy the prefixpath remainder (if there is one) */
108 	if (pplen) {
109 		*pos = '/';
110 		++pos;
111 		memcpy(pos, prepath, pplen);
112 		pos += pplen;
113 	}
114 
115 	/* NULL terminator */
116 	*pos = '\0';
117 
118 	convert_delimiter(dev, '/');
119 	return dev;
120 }
121 
122 
123 /**
124  * cifs_compose_mount_options	-	creates mount options for referral
125  * @sb_mountdata:	parent/root DFS mount options (template)
126  * @fullpath:		full path in UNC format
127  * @ref:		optional server's referral
128  *
129  * creates mount options for submount based on template options sb_mountdata
130  * and replacing unc,ip,prefixpath options with ones we've got form ref_unc.
131  *
132  * Returns: pointer to new mount options or ERR_PTR.
133  * Caller is responsible for freeing returned value if it is not error.
134  */
cifs_compose_mount_options(const char * sb_mountdata,const char * fullpath,const struct dfs_info3_param * ref,char ** devname)135 char *cifs_compose_mount_options(const char *sb_mountdata,
136 				 const char *fullpath,
137 				 const struct dfs_info3_param *ref,
138 				 char **devname)
139 {
140 	int rc;
141 	char *name;
142 	char *mountdata = NULL;
143 	const char *prepath = NULL;
144 	int md_len;
145 	char *tkn_e;
146 	char *srvIP = NULL;
147 	char sep = ',';
148 	int off, noff;
149 
150 	if (sb_mountdata == NULL)
151 		return ERR_PTR(-EINVAL);
152 
153 	if (ref) {
154 		if (strlen(fullpath) - ref->path_consumed) {
155 			prepath = fullpath + ref->path_consumed;
156 			/* skip initial delimiter */
157 			if (*prepath == '/' || *prepath == '\\')
158 				prepath++;
159 		}
160 
161 		name = cifs_build_devname(ref->node_name, prepath);
162 		if (IS_ERR(name)) {
163 			rc = PTR_ERR(name);
164 			name = NULL;
165 			goto compose_mount_options_err;
166 		}
167 	} else {
168 		name = cifs_build_devname((char *)fullpath, NULL);
169 		if (IS_ERR(name)) {
170 			rc = PTR_ERR(name);
171 			name = NULL;
172 			goto compose_mount_options_err;
173 		}
174 	}
175 
176 	rc = dns_resolve_server_name_to_ip(name, &srvIP);
177 	if (rc < 0) {
178 		cifs_dbg(FYI, "%s: Failed to resolve server part of %s to IP: %d\n",
179 			 __func__, name, rc);
180 		goto compose_mount_options_err;
181 	}
182 
183 	/*
184 	 * In most cases, we'll be building a shorter string than the original,
185 	 * but we do have to assume that the address in the ip= option may be
186 	 * much longer than the original. Add the max length of an address
187 	 * string to the length of the original string to allow for worst case.
188 	 */
189 	md_len = strlen(sb_mountdata) + INET6_ADDRSTRLEN;
190 	mountdata = kzalloc(md_len + sizeof("ip=") + 1, GFP_KERNEL);
191 	if (mountdata == NULL) {
192 		rc = -ENOMEM;
193 		goto compose_mount_options_err;
194 	}
195 
196 	/* copy all options except of unc,ip,prefixpath */
197 	off = 0;
198 	if (strncmp(sb_mountdata, "sep=", 4) == 0) {
199 			sep = sb_mountdata[4];
200 			strncpy(mountdata, sb_mountdata, 5);
201 			off += 5;
202 	}
203 
204 	do {
205 		tkn_e = strchr(sb_mountdata + off, sep);
206 		if (tkn_e == NULL)
207 			noff = strlen(sb_mountdata + off);
208 		else
209 			noff = tkn_e - (sb_mountdata + off) + 1;
210 
211 		if (strncasecmp(sb_mountdata + off, "unc=", 4) == 0) {
212 			off += noff;
213 			continue;
214 		}
215 		if (strncasecmp(sb_mountdata + off, "ip=", 3) == 0) {
216 			off += noff;
217 			continue;
218 		}
219 		if (strncasecmp(sb_mountdata + off, "prefixpath=", 11) == 0) {
220 			off += noff;
221 			continue;
222 		}
223 		strncat(mountdata, sb_mountdata + off, noff);
224 		off += noff;
225 	} while (tkn_e);
226 	strcat(mountdata, sb_mountdata + off);
227 	mountdata[md_len] = '\0';
228 
229 	/* copy new IP and ref share name */
230 	if (mountdata[strlen(mountdata) - 1] != sep)
231 		strncat(mountdata, &sep, 1);
232 	strcat(mountdata, "ip=");
233 	strcat(mountdata, srvIP);
234 
235 	if (devname)
236 		*devname = name;
237 	else
238 		kfree(name);
239 
240 	/*cifs_dbg(FYI, "%s: parent mountdata: %s\n", __func__, sb_mountdata);*/
241 	/*cifs_dbg(FYI, "%s: submount mountdata: %s\n", __func__, mountdata );*/
242 
243 compose_mount_options_out:
244 	kfree(srvIP);
245 	return mountdata;
246 
247 compose_mount_options_err:
248 	kfree(mountdata);
249 	mountdata = ERR_PTR(rc);
250 	kfree(name);
251 	goto compose_mount_options_out;
252 }
253 
254 /**
255  * cifs_dfs_do_mount - mounts specified path using DFS full path
256  *
257  * Always pass down @fullpath to smb3_do_mount() so we can use the root server
258  * to perform failover in case we failed to connect to the first target in the
259  * referral.
260  *
261  * @mntpt:		directory entry for the path we are trying to automount
262  * @cifs_sb:		parent/root superblock
263  * @fullpath:		full path in UNC format
264  */
cifs_dfs_do_mount(struct dentry * mntpt,struct cifs_sb_info * cifs_sb,const char * fullpath)265 static struct vfsmount *cifs_dfs_do_mount(struct dentry *mntpt,
266 					  struct cifs_sb_info *cifs_sb,
267 					  const char *fullpath)
268 {
269 	struct vfsmount *mnt;
270 	char *mountdata;
271 	char *devname;
272 
273 	devname = kstrdup(fullpath, GFP_KERNEL);
274 	if (!devname)
275 		return ERR_PTR(-ENOMEM);
276 
277 	convert_delimiter(devname, '/');
278 
279 	/* TODO: change to call fs_context_for_mount(), fill in context directly, call fc_mount */
280 
281 	/* See afs_mntpt_do_automount in fs/afs/mntpt.c for an example */
282 
283 	/* strip first '\' from fullpath */
284 	mountdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options,
285 					       fullpath + 1, NULL, NULL);
286 	if (IS_ERR(mountdata)) {
287 		kfree(devname);
288 		return (struct vfsmount *)mountdata;
289 	}
290 
291 	mnt = vfs_submount(mntpt, &cifs_fs_type, devname, mountdata);
292 	kfree(mountdata);
293 	kfree(devname);
294 	return mnt;
295 }
296 
297 /*
298  * Create a vfsmount that we can automount
299  */
cifs_dfs_do_automount(struct dentry * mntpt)300 static struct vfsmount *cifs_dfs_do_automount(struct dentry *mntpt)
301 {
302 	struct cifs_sb_info *cifs_sb;
303 	struct cifs_ses *ses;
304 	struct cifs_tcon *tcon;
305 	void *page;
306 	char *full_path, *root_path;
307 	unsigned int xid;
308 	int rc;
309 	struct vfsmount *mnt;
310 
311 	cifs_dbg(FYI, "in %s\n", __func__);
312 	BUG_ON(IS_ROOT(mntpt));
313 
314 	/*
315 	 * The MSDFS spec states that paths in DFS referral requests and
316 	 * responses must be prefixed by a single '\' character instead of
317 	 * the double backslashes usually used in the UNC. This function
318 	 * gives us the latter, so we must adjust the result.
319 	 */
320 	mnt = ERR_PTR(-ENOMEM);
321 
322 	cifs_sb = CIFS_SB(mntpt->d_sb);
323 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) {
324 		mnt = ERR_PTR(-EREMOTE);
325 		goto cdda_exit;
326 	}
327 
328 	page = alloc_dentry_path();
329 	/* always use tree name prefix */
330 	full_path = build_path_from_dentry_optional_prefix(mntpt, page, true);
331 	if (IS_ERR(full_path)) {
332 		mnt = ERR_CAST(full_path);
333 		goto free_full_path;
334 	}
335 
336 	convert_delimiter(full_path, '\\');
337 
338 	cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
339 
340 	if (!cifs_sb_master_tlink(cifs_sb)) {
341 		cifs_dbg(FYI, "%s: master tlink is NULL\n", __func__);
342 		goto free_full_path;
343 	}
344 
345 	tcon = cifs_sb_master_tcon(cifs_sb);
346 	if (!tcon) {
347 		cifs_dbg(FYI, "%s: master tcon is NULL\n", __func__);
348 		goto free_full_path;
349 	}
350 
351 	root_path = kstrdup(tcon->treeName, GFP_KERNEL);
352 	if (!root_path) {
353 		mnt = ERR_PTR(-ENOMEM);
354 		goto free_full_path;
355 	}
356 	cifs_dbg(FYI, "%s: root path: %s\n", __func__, root_path);
357 
358 	ses = tcon->ses;
359 	xid = get_xid();
360 
361 	/*
362 	 * If DFS root has been expired, then unconditionally fetch it again to
363 	 * refresh DFS referral cache.
364 	 */
365 	rc = dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
366 			    root_path + 1, NULL, NULL);
367 	if (!rc) {
368 		rc = dfs_cache_find(xid, ses, cifs_sb->local_nls,
369 				    cifs_remap(cifs_sb), full_path + 1,
370 				    NULL, NULL);
371 	}
372 
373 	free_xid(xid);
374 
375 	if (rc) {
376 		mnt = ERR_PTR(rc);
377 		goto free_root_path;
378 	}
379 	/*
380 	 * OK - we were able to get and cache a referral for @full_path.
381 	 *
382 	 * Now, pass it down to cifs_mount() and it will retry every available
383 	 * node server in case of failures - no need to do it here.
384 	 */
385 	mnt = cifs_dfs_do_mount(mntpt, cifs_sb, full_path);
386 	cifs_dbg(FYI, "%s: cifs_dfs_do_mount:%s , mnt:%p\n", __func__,
387 		 full_path + 1, mnt);
388 
389 free_root_path:
390 	kfree(root_path);
391 free_full_path:
392 	free_dentry_path(page);
393 cdda_exit:
394 	cifs_dbg(FYI, "leaving %s\n" , __func__);
395 	return mnt;
396 }
397 
398 /*
399  * Attempt to automount the referral
400  */
cifs_dfs_d_automount(struct path * path)401 struct vfsmount *cifs_dfs_d_automount(struct path *path)
402 {
403 	struct vfsmount *newmnt;
404 
405 	cifs_dbg(FYI, "in %s\n", __func__);
406 
407 	newmnt = cifs_dfs_do_automount(path->dentry);
408 	if (IS_ERR(newmnt)) {
409 		cifs_dbg(FYI, "leaving %s [automount failed]\n" , __func__);
410 		return newmnt;
411 	}
412 
413 	mntget(newmnt); /* prevent immediate expiration */
414 	mnt_set_expiry(newmnt, &cifs_dfs_automount_list);
415 	schedule_delayed_work(&cifs_dfs_automount_task,
416 			      cifs_dfs_mountpoint_expiry_timeout);
417 	cifs_dbg(FYI, "leaving %s [ok]\n" , __func__);
418 	return newmnt;
419 }
420 
421 const struct inode_operations cifs_dfs_referral_inode_operations = {
422 };
423