idmap.c 19 KB

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