idmap.c 21 KB

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