idmap.c 20 KB

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