idmap.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * fs/nfs/idmap.c
  3. *
  4. * UID and GID to name mapping for clients.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Marius Aamodt Eriksen <marius@umich.edu>
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <linux/types.h>
  37. #include <linux/parser.h>
  38. #include <linux/fs.h>
  39. #include <linux/nfs_idmap.h>
  40. #include <net/net_namespace.h>
  41. #include <linux/sunrpc/rpc_pipe_fs.h>
  42. #include <linux/nfs_fs.h>
  43. #include <linux/nfs_fs_sb.h>
  44. #include <linux/key.h>
  45. #include <linux/keyctl.h>
  46. #include <linux/key-type.h>
  47. #include <keys/user-type.h>
  48. #include <linux/module.h>
  49. #include "internal.h"
  50. #include "netns.h"
  51. #define NFS_UINT_MAXLEN 11
  52. /* Default cache timeout is 10 minutes */
  53. unsigned int nfs_idmap_cache_timeout = 600;
  54. static const struct cred *id_resolver_cache;
  55. static struct key_type key_type_id_resolver_legacy;
  56. struct idmap {
  57. struct rpc_pipe *idmap_pipe;
  58. struct key_construction *idmap_key_cons;
  59. struct mutex idmap_mutex;
  60. };
  61. struct idmap_legacy_upcalldata {
  62. struct rpc_pipe_msg pipe_msg;
  63. struct idmap_msg idmap_msg;
  64. struct idmap *idmap;
  65. };
  66. /**
  67. * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
  68. * @fattr: fully initialised struct nfs_fattr
  69. * @owner_name: owner name string cache
  70. * @group_name: group name string cache
  71. */
  72. void nfs_fattr_init_names(struct nfs_fattr *fattr,
  73. struct nfs4_string *owner_name,
  74. struct nfs4_string *group_name)
  75. {
  76. fattr->owner_name = owner_name;
  77. fattr->group_name = group_name;
  78. }
  79. static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
  80. {
  81. fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
  82. kfree(fattr->owner_name->data);
  83. }
  84. static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
  85. {
  86. fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
  87. kfree(fattr->group_name->data);
  88. }
  89. static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
  90. {
  91. struct nfs4_string *owner = fattr->owner_name;
  92. __u32 uid;
  93. if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
  94. return false;
  95. if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
  96. fattr->uid = uid;
  97. fattr->valid |= NFS_ATTR_FATTR_OWNER;
  98. }
  99. return true;
  100. }
  101. static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
  102. {
  103. struct nfs4_string *group = fattr->group_name;
  104. __u32 gid;
  105. if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
  106. return false;
  107. if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
  108. fattr->gid = gid;
  109. fattr->valid |= NFS_ATTR_FATTR_GROUP;
  110. }
  111. return true;
  112. }
  113. /**
  114. * nfs_fattr_free_names - free up the NFSv4 owner and group strings
  115. * @fattr: a fully initialised nfs_fattr structure
  116. */
  117. void nfs_fattr_free_names(struct nfs_fattr *fattr)
  118. {
  119. if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
  120. nfs_fattr_free_owner_name(fattr);
  121. if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
  122. nfs_fattr_free_group_name(fattr);
  123. }
  124. /**
  125. * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
  126. * @server: pointer to the filesystem nfs_server structure
  127. * @fattr: a fully initialised nfs_fattr structure
  128. *
  129. * This helper maps the cached NFSv4 owner/group strings in fattr into
  130. * their numeric uid/gid equivalents, and then frees the cached strings.
  131. */
  132. void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
  133. {
  134. if (nfs_fattr_map_owner_name(server, fattr))
  135. nfs_fattr_free_owner_name(fattr);
  136. if (nfs_fattr_map_group_name(server, fattr))
  137. nfs_fattr_free_group_name(fattr);
  138. }
  139. static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
  140. {
  141. unsigned long val;
  142. char buf[16];
  143. if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
  144. return 0;
  145. memcpy(buf, name, namelen);
  146. buf[namelen] = '\0';
  147. if (kstrtoul(buf, 0, &val) != 0)
  148. return 0;
  149. *res = val;
  150. return 1;
  151. }
  152. static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
  153. {
  154. return snprintf(buf, buflen, "%u", id);
  155. }
  156. static struct key_type key_type_id_resolver = {
  157. .name = "id_resolver",
  158. .instantiate = user_instantiate,
  159. .match = user_match,
  160. .revoke = user_revoke,
  161. .destroy = user_destroy,
  162. .describe = user_describe,
  163. .read = user_read,
  164. };
  165. static int nfs_idmap_init_keyring(void)
  166. {
  167. struct cred *cred;
  168. struct key *keyring;
  169. int ret = 0;
  170. printk(KERN_NOTICE "NFS: Registering the %s key type\n",
  171. key_type_id_resolver.name);
  172. cred = prepare_kernel_cred(NULL);
  173. if (!cred)
  174. return -ENOMEM;
  175. keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
  176. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  177. KEY_USR_VIEW | KEY_USR_READ,
  178. KEY_ALLOC_NOT_IN_QUOTA);
  179. if (IS_ERR(keyring)) {
  180. ret = PTR_ERR(keyring);
  181. goto failed_put_cred;
  182. }
  183. ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
  184. if (ret < 0)
  185. goto failed_put_key;
  186. ret = register_key_type(&key_type_id_resolver);
  187. if (ret < 0)
  188. goto failed_put_key;
  189. ret = register_key_type(&key_type_id_resolver_legacy);
  190. if (ret < 0)
  191. goto failed_reg_legacy;
  192. set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
  193. cred->thread_keyring = keyring;
  194. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  195. id_resolver_cache = cred;
  196. return 0;
  197. failed_reg_legacy:
  198. unregister_key_type(&key_type_id_resolver);
  199. failed_put_key:
  200. key_put(keyring);
  201. failed_put_cred:
  202. put_cred(cred);
  203. return ret;
  204. }
  205. static void nfs_idmap_quit_keyring(void)
  206. {
  207. key_revoke(id_resolver_cache->thread_keyring);
  208. unregister_key_type(&key_type_id_resolver);
  209. unregister_key_type(&key_type_id_resolver_legacy);
  210. put_cred(id_resolver_cache);
  211. }
  212. /*
  213. * Assemble the description to pass to request_key()
  214. * This function will allocate a new string and update dest to point
  215. * at it. The caller is responsible for freeing dest.
  216. *
  217. * On error 0 is returned. Otherwise, the length of dest is returned.
  218. */
  219. static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
  220. const char *type, size_t typelen, char **desc)
  221. {
  222. char *cp;
  223. size_t desclen = typelen + namelen + 2;
  224. *desc = kmalloc(desclen, GFP_KERNEL);
  225. if (!*desc)
  226. return -ENOMEM;
  227. cp = *desc;
  228. memcpy(cp, type, typelen);
  229. cp += typelen;
  230. *cp++ = ':';
  231. memcpy(cp, name, namelen);
  232. cp += namelen;
  233. *cp = '\0';
  234. return desclen;
  235. }
  236. static ssize_t nfs_idmap_request_key(struct key_type *key_type,
  237. const char *name, size_t namelen,
  238. const char *type, void *data,
  239. size_t data_size, struct idmap *idmap)
  240. {
  241. const struct cred *saved_cred;
  242. struct key *rkey;
  243. char *desc;
  244. struct user_key_payload *payload;
  245. ssize_t ret;
  246. ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
  247. if (ret <= 0)
  248. goto out;
  249. saved_cred = override_creds(id_resolver_cache);
  250. if (idmap)
  251. rkey = request_key_with_auxdata(key_type, desc, "", 0, idmap);
  252. else
  253. rkey = request_key(&key_type_id_resolver, desc, "");
  254. revert_creds(saved_cred);
  255. kfree(desc);
  256. if (IS_ERR(rkey)) {
  257. ret = PTR_ERR(rkey);
  258. goto out;
  259. }
  260. rcu_read_lock();
  261. rkey->perm |= KEY_USR_VIEW;
  262. ret = key_validate(rkey);
  263. if (ret < 0)
  264. goto out_up;
  265. payload = rcu_dereference(rkey->payload.data);
  266. if (IS_ERR_OR_NULL(payload)) {
  267. ret = PTR_ERR(payload);
  268. goto out_up;
  269. }
  270. ret = payload->datalen;
  271. if (ret > 0 && ret <= data_size)
  272. memcpy(data, payload->data, ret);
  273. else
  274. ret = -EINVAL;
  275. out_up:
  276. rcu_read_unlock();
  277. key_put(rkey);
  278. out:
  279. return ret;
  280. }
  281. static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
  282. const char *type, void *data,
  283. size_t data_size, struct idmap *idmap)
  284. {
  285. ssize_t ret = nfs_idmap_request_key(&key_type_id_resolver,
  286. name, namelen, type, data,
  287. data_size, NULL);
  288. if (ret < 0) {
  289. mutex_lock(&idmap->idmap_mutex);
  290. ret = nfs_idmap_request_key(&key_type_id_resolver_legacy,
  291. name, namelen, type, data,
  292. data_size, idmap);
  293. idmap->idmap_key_cons = NULL;
  294. mutex_unlock(&idmap->idmap_mutex);
  295. }
  296. return ret;
  297. }
  298. /* ID -> Name */
  299. static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
  300. size_t buflen, struct idmap *idmap)
  301. {
  302. char id_str[NFS_UINT_MAXLEN];
  303. int id_len;
  304. ssize_t ret;
  305. id_len = snprintf(id_str, sizeof(id_str), "%u", id);
  306. ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
  307. if (ret < 0)
  308. return -EINVAL;
  309. return ret;
  310. }
  311. /* Name -> ID */
  312. static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
  313. __u32 *id, struct idmap *idmap)
  314. {
  315. char id_str[NFS_UINT_MAXLEN];
  316. long id_long;
  317. ssize_t data_size;
  318. int ret = 0;
  319. data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
  320. if (data_size <= 0) {
  321. ret = -EINVAL;
  322. } else {
  323. ret = kstrtol(id_str, 10, &id_long);
  324. *id = (__u32)id_long;
  325. }
  326. return ret;
  327. }
  328. /* idmap classic begins here */
  329. module_param(nfs_idmap_cache_timeout, int, 0644);
  330. enum {
  331. Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
  332. };
  333. static const match_table_t nfs_idmap_tokens = {
  334. { Opt_find_uid, "uid:%s" },
  335. { Opt_find_gid, "gid:%s" },
  336. { Opt_find_user, "user:%s" },
  337. { Opt_find_group, "group:%s" },
  338. { Opt_find_err, NULL }
  339. };
  340. static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
  341. static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
  342. size_t);
  343. static void idmap_release_pipe(struct inode *);
  344. static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
  345. static const struct rpc_pipe_ops idmap_upcall_ops = {
  346. .upcall = rpc_pipe_generic_upcall,
  347. .downcall = idmap_pipe_downcall,
  348. .release_pipe = idmap_release_pipe,
  349. .destroy_msg = idmap_pipe_destroy_msg,
  350. };
  351. static struct key_type key_type_id_resolver_legacy = {
  352. .name = "id_legacy",
  353. .instantiate = user_instantiate,
  354. .match = user_match,
  355. .revoke = user_revoke,
  356. .destroy = user_destroy,
  357. .describe = user_describe,
  358. .read = user_read,
  359. .request_key = nfs_idmap_legacy_upcall,
  360. };
  361. static void __nfs_idmap_unregister(struct rpc_pipe *pipe)
  362. {
  363. if (pipe->dentry)
  364. rpc_unlink(pipe->dentry);
  365. }
  366. static int __nfs_idmap_register(struct dentry *dir,
  367. struct idmap *idmap,
  368. struct rpc_pipe *pipe)
  369. {
  370. struct dentry *dentry;
  371. dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
  372. if (IS_ERR(dentry))
  373. return PTR_ERR(dentry);
  374. pipe->dentry = dentry;
  375. return 0;
  376. }
  377. static void nfs_idmap_unregister(struct nfs_client *clp,
  378. struct rpc_pipe *pipe)
  379. {
  380. struct net *net = clp->net;
  381. struct super_block *pipefs_sb;
  382. pipefs_sb = rpc_get_sb_net(net);
  383. if (pipefs_sb) {
  384. __nfs_idmap_unregister(pipe);
  385. rpc_put_sb_net(net);
  386. }
  387. }
  388. static int nfs_idmap_register(struct nfs_client *clp,
  389. struct idmap *idmap,
  390. struct rpc_pipe *pipe)
  391. {
  392. struct net *net = clp->net;
  393. struct super_block *pipefs_sb;
  394. int err = 0;
  395. pipefs_sb = rpc_get_sb_net(net);
  396. if (pipefs_sb) {
  397. if (clp->cl_rpcclient->cl_dentry)
  398. err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
  399. idmap, pipe);
  400. rpc_put_sb_net(net);
  401. }
  402. return err;
  403. }
  404. int
  405. nfs_idmap_new(struct nfs_client *clp)
  406. {
  407. struct idmap *idmap;
  408. struct rpc_pipe *pipe;
  409. int error;
  410. BUG_ON(clp->cl_idmap != NULL);
  411. idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
  412. if (idmap == NULL)
  413. return -ENOMEM;
  414. pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
  415. if (IS_ERR(pipe)) {
  416. error = PTR_ERR(pipe);
  417. kfree(idmap);
  418. return error;
  419. }
  420. error = nfs_idmap_register(clp, idmap, pipe);
  421. if (error) {
  422. rpc_destroy_pipe_data(pipe);
  423. kfree(idmap);
  424. return error;
  425. }
  426. idmap->idmap_pipe = pipe;
  427. mutex_init(&idmap->idmap_mutex);
  428. clp->cl_idmap = idmap;
  429. return 0;
  430. }
  431. void
  432. nfs_idmap_delete(struct nfs_client *clp)
  433. {
  434. struct idmap *idmap = clp->cl_idmap;
  435. if (!idmap)
  436. return;
  437. nfs_idmap_unregister(clp, idmap->idmap_pipe);
  438. rpc_destroy_pipe_data(idmap->idmap_pipe);
  439. clp->cl_idmap = NULL;
  440. kfree(idmap);
  441. }
  442. static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event,
  443. struct super_block *sb)
  444. {
  445. int err = 0;
  446. switch (event) {
  447. case RPC_PIPEFS_MOUNT:
  448. BUG_ON(clp->cl_rpcclient->cl_dentry == NULL);
  449. err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
  450. clp->cl_idmap,
  451. clp->cl_idmap->idmap_pipe);
  452. break;
  453. case RPC_PIPEFS_UMOUNT:
  454. if (clp->cl_idmap->idmap_pipe) {
  455. struct dentry *parent;
  456. parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
  457. __nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
  458. /*
  459. * Note: This is a dirty hack. SUNRPC hook has been
  460. * called already but simple_rmdir() call for the
  461. * directory returned with error because of idmap pipe
  462. * inside. Thus now we have to remove this directory
  463. * here.
  464. */
  465. if (rpc_rmdir(parent))
  466. printk(KERN_ERR "NFS: %s: failed to remove "
  467. "clnt dir!\n", __func__);
  468. }
  469. break;
  470. default:
  471. printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__,
  472. event);
  473. return -ENOTSUPP;
  474. }
  475. return err;
  476. }
  477. static struct nfs_client *nfs_get_client_for_event(struct net *net, int event)
  478. {
  479. struct nfs_net *nn = net_generic(net, nfs_net_id);
  480. struct dentry *cl_dentry;
  481. struct nfs_client *clp;
  482. spin_lock(&nn->nfs_client_lock);
  483. list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
  484. if (clp->rpc_ops != &nfs_v4_clientops)
  485. continue;
  486. cl_dentry = clp->cl_idmap->idmap_pipe->dentry;
  487. if (((event == RPC_PIPEFS_MOUNT) && cl_dentry) ||
  488. ((event == RPC_PIPEFS_UMOUNT) && !cl_dentry))
  489. continue;
  490. atomic_inc(&clp->cl_count);
  491. spin_unlock(&nn->nfs_client_lock);
  492. return clp;
  493. }
  494. spin_unlock(&nn->nfs_client_lock);
  495. return NULL;
  496. }
  497. static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
  498. void *ptr)
  499. {
  500. struct super_block *sb = ptr;
  501. struct nfs_client *clp;
  502. int error = 0;
  503. if (!try_module_get(THIS_MODULE))
  504. return 0;
  505. while ((clp = nfs_get_client_for_event(sb->s_fs_info, event))) {
  506. error = __rpc_pipefs_event(clp, event, sb);
  507. nfs_put_client(clp);
  508. if (error)
  509. break;
  510. }
  511. module_put(THIS_MODULE);
  512. return error;
  513. }
  514. #define PIPEFS_NFS_PRIO 1
  515. static struct notifier_block nfs_idmap_block = {
  516. .notifier_call = rpc_pipefs_event,
  517. .priority = SUNRPC_PIPEFS_NFS_PRIO,
  518. };
  519. int nfs_idmap_init(void)
  520. {
  521. int ret;
  522. ret = nfs_idmap_init_keyring();
  523. if (ret != 0)
  524. goto out;
  525. ret = rpc_pipefs_notifier_register(&nfs_idmap_block);
  526. if (ret != 0)
  527. nfs_idmap_quit_keyring();
  528. out:
  529. return ret;
  530. }
  531. void nfs_idmap_quit(void)
  532. {
  533. rpc_pipefs_notifier_unregister(&nfs_idmap_block);
  534. nfs_idmap_quit_keyring();
  535. }
  536. static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
  537. struct idmap_msg *im,
  538. struct rpc_pipe_msg *msg)
  539. {
  540. substring_t substr;
  541. int token, ret;
  542. memset(im, 0, sizeof(*im));
  543. memset(msg, 0, sizeof(*msg));
  544. im->im_type = IDMAP_TYPE_GROUP;
  545. token = match_token(desc, nfs_idmap_tokens, &substr);
  546. switch (token) {
  547. case Opt_find_uid:
  548. im->im_type = IDMAP_TYPE_USER;
  549. case Opt_find_gid:
  550. im->im_conv = IDMAP_CONV_NAMETOID;
  551. ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
  552. break;
  553. case Opt_find_user:
  554. im->im_type = IDMAP_TYPE_USER;
  555. case Opt_find_group:
  556. im->im_conv = IDMAP_CONV_IDTONAME;
  557. ret = match_int(&substr, &im->im_id);
  558. break;
  559. default:
  560. ret = -EINVAL;
  561. goto out;
  562. }
  563. msg->data = im;
  564. msg->len = sizeof(struct idmap_msg);
  565. out:
  566. return ret;
  567. }
  568. static int nfs_idmap_legacy_upcall(struct key_construction *cons,
  569. const char *op,
  570. void *aux)
  571. {
  572. struct idmap_legacy_upcalldata *data;
  573. struct rpc_pipe_msg *msg;
  574. struct idmap_msg *im;
  575. struct idmap *idmap = (struct idmap *)aux;
  576. struct key *key = cons->key;
  577. int ret = -ENOMEM;
  578. /* msg and im are freed in idmap_pipe_destroy_msg */
  579. data = kmalloc(sizeof(*data), GFP_KERNEL);
  580. if (!data)
  581. goto out1;
  582. msg = &data->pipe_msg;
  583. im = &data->idmap_msg;
  584. data->idmap = idmap;
  585. ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
  586. if (ret < 0)
  587. goto out2;
  588. BUG_ON(idmap->idmap_key_cons != NULL);
  589. idmap->idmap_key_cons = cons;
  590. ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
  591. if (ret < 0)
  592. goto out3;
  593. return ret;
  594. out3:
  595. idmap->idmap_key_cons = NULL;
  596. out2:
  597. kfree(data);
  598. out1:
  599. complete_request_key(cons, ret);
  600. return ret;
  601. }
  602. static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data)
  603. {
  604. return key_instantiate_and_link(key, data, strlen(data) + 1,
  605. id_resolver_cache->thread_keyring,
  606. authkey);
  607. }
  608. static int nfs_idmap_read_message(struct idmap_msg *im, struct key *key, struct key *authkey)
  609. {
  610. char id_str[NFS_UINT_MAXLEN];
  611. int ret = -EINVAL;
  612. switch (im->im_conv) {
  613. case IDMAP_CONV_NAMETOID:
  614. sprintf(id_str, "%d", im->im_id);
  615. ret = nfs_idmap_instantiate(key, authkey, id_str);
  616. break;
  617. case IDMAP_CONV_IDTONAME:
  618. ret = nfs_idmap_instantiate(key, authkey, im->im_name);
  619. break;
  620. }
  621. return ret;
  622. }
  623. static ssize_t
  624. idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  625. {
  626. struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
  627. struct idmap *idmap = (struct idmap *)rpci->private;
  628. struct key_construction *cons;
  629. struct idmap_msg im;
  630. size_t namelen_in;
  631. int ret;
  632. /* If instantiation is successful, anyone waiting for key construction
  633. * will have been woken up and someone else may now have used
  634. * idmap_key_cons - so after this point we may no longer touch it.
  635. */
  636. cons = ACCESS_ONCE(idmap->idmap_key_cons);
  637. idmap->idmap_key_cons = NULL;
  638. if (mlen != sizeof(im)) {
  639. ret = -ENOSPC;
  640. goto out;
  641. }
  642. if (copy_from_user(&im, src, mlen) != 0) {
  643. ret = -EFAULT;
  644. goto out;
  645. }
  646. if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
  647. ret = -ENOKEY;
  648. goto out;
  649. }
  650. namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
  651. if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
  652. ret = -EINVAL;
  653. goto out;
  654. }
  655. ret = nfs_idmap_read_message(&im, cons->key, cons->authkey);
  656. if (ret >= 0) {
  657. key_set_timeout(cons->key, nfs_idmap_cache_timeout);
  658. ret = mlen;
  659. }
  660. out:
  661. complete_request_key(cons, ret);
  662. return ret;
  663. }
  664. static void
  665. idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  666. {
  667. struct idmap_legacy_upcalldata *data = container_of(msg,
  668. struct idmap_legacy_upcalldata,
  669. pipe_msg);
  670. struct idmap *idmap = data->idmap;
  671. struct key_construction *cons;
  672. if (msg->errno) {
  673. cons = ACCESS_ONCE(idmap->idmap_key_cons);
  674. idmap->idmap_key_cons = NULL;
  675. complete_request_key(cons, msg->errno);
  676. }
  677. /* Free memory allocated in nfs_idmap_legacy_upcall() */
  678. kfree(data);
  679. }
  680. static void
  681. idmap_release_pipe(struct inode *inode)
  682. {
  683. struct rpc_inode *rpci = RPC_I(inode);
  684. struct idmap *idmap = (struct idmap *)rpci->private;
  685. idmap->idmap_key_cons = NULL;
  686. }
  687. int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
  688. {
  689. struct idmap *idmap = server->nfs_client->cl_idmap;
  690. if (nfs_map_string_to_numeric(name, namelen, uid))
  691. return 0;
  692. return nfs_idmap_lookup_id(name, namelen, "uid", uid, idmap);
  693. }
  694. int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid)
  695. {
  696. struct idmap *idmap = server->nfs_client->cl_idmap;
  697. if (nfs_map_string_to_numeric(name, namelen, gid))
  698. return 0;
  699. return nfs_idmap_lookup_id(name, namelen, "gid", gid, idmap);
  700. }
  701. int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
  702. {
  703. struct idmap *idmap = server->nfs_client->cl_idmap;
  704. int ret = -EINVAL;
  705. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  706. ret = nfs_idmap_lookup_name(uid, "user", buf, buflen, idmap);
  707. if (ret < 0)
  708. ret = nfs_map_numeric_to_string(uid, buf, buflen);
  709. return ret;
  710. }
  711. int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
  712. {
  713. struct idmap *idmap = server->nfs_client->cl_idmap;
  714. int ret = -EINVAL;
  715. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  716. ret = nfs_idmap_lookup_name(gid, "group", buf, buflen, idmap);
  717. if (ret < 0)
  718. ret = nfs_map_numeric_to_string(gid, buf, buflen);
  719. return ret;
  720. }