nfs4idmap.c 19 KB

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