idmap.c 21 KB

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