cifs_dfs_ref.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 ([email protected])
  9. * Steve French ([email protected])
  10. */
  11. #include <linux/dcache.h>
  12. #include <linux/mount.h>
  13. #include <linux/namei.h>
  14. #include <linux/slab.h>
  15. #include <linux/vfs.h>
  16. #include <linux/fs.h>
  17. #include <linux/inet.h>
  18. #include "cifsglob.h"
  19. #include "cifsproto.h"
  20. #include "cifsfs.h"
  21. #include "dns_resolve.h"
  22. #include "cifs_debug.h"
  23. #include "cifs_unicode.h"
  24. #include "dfs_cache.h"
  25. #include "fs_context.h"
  26. static LIST_HEAD(cifs_dfs_automount_list);
  27. static void cifs_dfs_expire_automounts(struct work_struct *work);
  28. static DECLARE_DELAYED_WORK(cifs_dfs_automount_task,
  29. cifs_dfs_expire_automounts);
  30. static int cifs_dfs_mountpoint_expiry_timeout = 500 * HZ;
  31. static void cifs_dfs_expire_automounts(struct work_struct *work)
  32. {
  33. struct list_head *list = &cifs_dfs_automount_list;
  34. mark_mounts_for_expiry(list);
  35. if (!list_empty(list))
  36. schedule_delayed_work(&cifs_dfs_automount_task,
  37. cifs_dfs_mountpoint_expiry_timeout);
  38. }
  39. void cifs_dfs_release_automount_timer(void)
  40. {
  41. BUG_ON(!list_empty(&cifs_dfs_automount_list));
  42. cancel_delayed_work_sync(&cifs_dfs_automount_task);
  43. }
  44. /**
  45. * cifs_build_devname - build a devicename from a UNC and optional prepath
  46. * @nodename: pointer to UNC string
  47. * @prepath: pointer to prefixpath (or NULL if there isn't one)
  48. *
  49. * Build a new cifs devicename after chasing a DFS referral. Allocate a buffer
  50. * big enough to hold the final thing. Copy the UNC from the nodename, and
  51. * concatenate the prepath onto the end of it if there is one.
  52. *
  53. * Returns pointer to the built string, or a ERR_PTR. Caller is responsible
  54. * for freeing the returned string.
  55. */
  56. static char *
  57. cifs_build_devname(char *nodename, const char *prepath)
  58. {
  59. size_t pplen;
  60. size_t unclen;
  61. char *dev;
  62. char *pos;
  63. /* skip over any preceding delimiters */
  64. nodename += strspn(nodename, "\\");
  65. if (!*nodename)
  66. return ERR_PTR(-EINVAL);
  67. /* get length of UNC and set pos to last char */
  68. unclen = strlen(nodename);
  69. pos = nodename + unclen - 1;
  70. /* trim off any trailing delimiters */
  71. while (*pos == '\\') {
  72. --pos;
  73. --unclen;
  74. }
  75. /* allocate a buffer:
  76. * +2 for preceding "//"
  77. * +1 for delimiter between UNC and prepath
  78. * +1 for trailing NULL
  79. */
  80. pplen = prepath ? strlen(prepath) : 0;
  81. dev = kmalloc(2 + unclen + 1 + pplen + 1, GFP_KERNEL);
  82. if (!dev)
  83. return ERR_PTR(-ENOMEM);
  84. pos = dev;
  85. /* add the initial "//" */
  86. *pos = '/';
  87. ++pos;
  88. *pos = '/';
  89. ++pos;
  90. /* copy in the UNC portion from referral */
  91. memcpy(pos, nodename, unclen);
  92. pos += unclen;
  93. /* copy the prefixpath remainder (if there is one) */
  94. if (pplen) {
  95. *pos = '/';
  96. ++pos;
  97. memcpy(pos, prepath, pplen);
  98. pos += pplen;
  99. }
  100. /* NULL terminator */
  101. *pos = '\0';
  102. convert_delimiter(dev, '/');
  103. return dev;
  104. }
  105. /**
  106. * cifs_compose_mount_options - creates mount options for referral
  107. * @sb_mountdata: parent/root DFS mount options (template)
  108. * @fullpath: full path in UNC format
  109. * @ref: optional server's referral
  110. * @devname: return the built cifs device name if passed pointer not NULL
  111. * creates mount options for submount based on template options sb_mountdata
  112. * and replacing unc,ip,prefixpath options with ones we've got form ref_unc.
  113. *
  114. * Returns: pointer to new mount options or ERR_PTR.
  115. * Caller is responsible for freeing returned value if it is not error.
  116. */
  117. char *cifs_compose_mount_options(const char *sb_mountdata,
  118. const char *fullpath,
  119. const struct dfs_info3_param *ref,
  120. char **devname)
  121. {
  122. int rc;
  123. char *name;
  124. char *mountdata = NULL;
  125. const char *prepath = NULL;
  126. int md_len;
  127. char *tkn_e;
  128. char *srvIP = NULL;
  129. char sep = ',';
  130. int off, noff;
  131. if (sb_mountdata == NULL)
  132. return ERR_PTR(-EINVAL);
  133. if (ref) {
  134. if (WARN_ON_ONCE(!ref->node_name || ref->path_consumed < 0))
  135. return ERR_PTR(-EINVAL);
  136. if (strlen(fullpath) - ref->path_consumed) {
  137. prepath = fullpath + ref->path_consumed;
  138. /* skip initial delimiter */
  139. if (*prepath == '/' || *prepath == '\\')
  140. prepath++;
  141. }
  142. name = cifs_build_devname(ref->node_name, prepath);
  143. if (IS_ERR(name)) {
  144. rc = PTR_ERR(name);
  145. name = NULL;
  146. goto compose_mount_options_err;
  147. }
  148. } else {
  149. name = cifs_build_devname((char *)fullpath, NULL);
  150. if (IS_ERR(name)) {
  151. rc = PTR_ERR(name);
  152. name = NULL;
  153. goto compose_mount_options_err;
  154. }
  155. }
  156. rc = dns_resolve_server_name_to_ip(name, &srvIP, NULL);
  157. if (rc < 0) {
  158. cifs_dbg(FYI, "%s: Failed to resolve server part of %s to IP: %d\n",
  159. __func__, name, rc);
  160. goto compose_mount_options_err;
  161. }
  162. /*
  163. * In most cases, we'll be building a shorter string than the original,
  164. * but we do have to assume that the address in the ip= option may be
  165. * much longer than the original. Add the max length of an address
  166. * string to the length of the original string to allow for worst case.
  167. */
  168. md_len = strlen(sb_mountdata) + INET6_ADDRSTRLEN;
  169. mountdata = kzalloc(md_len + sizeof("ip=") + 1, GFP_KERNEL);
  170. if (mountdata == NULL) {
  171. rc = -ENOMEM;
  172. goto compose_mount_options_err;
  173. }
  174. /* copy all options except of unc,ip,prefixpath */
  175. off = 0;
  176. if (strncmp(sb_mountdata, "sep=", 4) == 0) {
  177. sep = sb_mountdata[4];
  178. strncpy(mountdata, sb_mountdata, 5);
  179. off += 5;
  180. }
  181. do {
  182. tkn_e = strchr(sb_mountdata + off, sep);
  183. if (tkn_e == NULL)
  184. noff = strlen(sb_mountdata + off);
  185. else
  186. noff = tkn_e - (sb_mountdata + off) + 1;
  187. if (strncasecmp(sb_mountdata + off, "cruid=", 6) == 0) {
  188. off += noff;
  189. continue;
  190. }
  191. if (strncasecmp(sb_mountdata + off, "unc=", 4) == 0) {
  192. off += noff;
  193. continue;
  194. }
  195. if (strncasecmp(sb_mountdata + off, "ip=", 3) == 0) {
  196. off += noff;
  197. continue;
  198. }
  199. if (strncasecmp(sb_mountdata + off, "prefixpath=", 11) == 0) {
  200. off += noff;
  201. continue;
  202. }
  203. strncat(mountdata, sb_mountdata + off, noff);
  204. off += noff;
  205. } while (tkn_e);
  206. strcat(mountdata, sb_mountdata + off);
  207. mountdata[md_len] = '\0';
  208. /* copy new IP and ref share name */
  209. if (mountdata[strlen(mountdata) - 1] != sep)
  210. strncat(mountdata, &sep, 1);
  211. strcat(mountdata, "ip=");
  212. strcat(mountdata, srvIP);
  213. if (devname)
  214. *devname = name;
  215. else
  216. kfree(name);
  217. /*cifs_dbg(FYI, "%s: parent mountdata: %s\n", __func__, sb_mountdata);*/
  218. /*cifs_dbg(FYI, "%s: submount mountdata: %s\n", __func__, mountdata );*/
  219. compose_mount_options_out:
  220. kfree(srvIP);
  221. return mountdata;
  222. compose_mount_options_err:
  223. kfree(mountdata);
  224. mountdata = ERR_PTR(rc);
  225. kfree(name);
  226. goto compose_mount_options_out;
  227. }
  228. /*
  229. * Create a vfsmount that we can automount
  230. */
  231. static struct vfsmount *cifs_dfs_do_automount(struct path *path)
  232. {
  233. int rc;
  234. struct dentry *mntpt = path->dentry;
  235. struct fs_context *fc;
  236. struct cifs_sb_info *cifs_sb;
  237. void *page = NULL;
  238. struct smb3_fs_context *ctx, *cur_ctx;
  239. struct smb3_fs_context tmp;
  240. char *full_path;
  241. struct vfsmount *mnt;
  242. if (IS_ROOT(mntpt))
  243. return ERR_PTR(-ESTALE);
  244. /*
  245. * The MSDFS spec states that paths in DFS referral requests and
  246. * responses must be prefixed by a single '\' character instead of
  247. * the double backslashes usually used in the UNC. This function
  248. * gives us the latter, so we must adjust the result.
  249. */
  250. cifs_sb = CIFS_SB(mntpt->d_sb);
  251. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
  252. return ERR_PTR(-EREMOTE);
  253. cur_ctx = cifs_sb->ctx;
  254. fc = fs_context_for_submount(path->mnt->mnt_sb->s_type, mntpt);
  255. if (IS_ERR(fc))
  256. return ERR_CAST(fc);
  257. ctx = smb3_fc2context(fc);
  258. page = alloc_dentry_path();
  259. /* always use tree name prefix */
  260. full_path = build_path_from_dentry_optional_prefix(mntpt, page, true);
  261. if (IS_ERR(full_path)) {
  262. mnt = ERR_CAST(full_path);
  263. goto out;
  264. }
  265. convert_delimiter(full_path, '/');
  266. cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
  267. tmp = *cur_ctx;
  268. tmp.source = full_path;
  269. tmp.UNC = tmp.prepath = NULL;
  270. rc = smb3_fs_context_dup(ctx, &tmp);
  271. if (rc) {
  272. mnt = ERR_PTR(rc);
  273. goto out;
  274. }
  275. rc = smb3_parse_devname(full_path, ctx);
  276. if (!rc)
  277. mnt = fc_mount(fc);
  278. else
  279. mnt = ERR_PTR(rc);
  280. out:
  281. put_fs_context(fc);
  282. free_dentry_path(page);
  283. return mnt;
  284. }
  285. /*
  286. * Attempt to automount the referral
  287. */
  288. struct vfsmount *cifs_dfs_d_automount(struct path *path)
  289. {
  290. struct vfsmount *newmnt;
  291. cifs_dbg(FYI, "%s: %pd\n", __func__, path->dentry);
  292. newmnt = cifs_dfs_do_automount(path);
  293. if (IS_ERR(newmnt)) {
  294. cifs_dbg(FYI, "leaving %s [automount failed]\n" , __func__);
  295. return newmnt;
  296. }
  297. mntget(newmnt); /* prevent immediate expiration */
  298. mnt_set_expiry(newmnt, &cifs_dfs_automount_list);
  299. schedule_delayed_work(&cifs_dfs_automount_task,
  300. cifs_dfs_mountpoint_expiry_timeout);
  301. cifs_dbg(FYI, "leaving %s [ok]\n" , __func__);
  302. return newmnt;
  303. }
  304. const struct inode_operations cifs_dfs_referral_inode_operations = {
  305. };