idmap.c 19 KB

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