nfs4idmap.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * Mapping of UID/GIDs to name and vice versa.
  3. *
  4. * Copyright (c) 2002, 2003 The Regents of the University of
  5. * Michigan. All rights reserved.
  6. *
  7. * Marius Aamodt Eriksen <marius@umich.edu>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  23. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  24. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  29. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <linux/module.h>
  35. #include <linux/seq_file.h>
  36. #include <linux/sched.h>
  37. #include <linux/slab.h>
  38. #include <linux/sunrpc/svc_xprt.h>
  39. #include <net/net_namespace.h>
  40. #include "idmap.h"
  41. #include "nfsd.h"
  42. #include "netns.h"
  43. #include "vfs.h"
  44. /*
  45. * Turn off idmapping when using AUTH_SYS.
  46. */
  47. static bool nfs4_disable_idmapping = true;
  48. module_param(nfs4_disable_idmapping, bool, 0644);
  49. MODULE_PARM_DESC(nfs4_disable_idmapping,
  50. "Turn off server's NFSv4 idmapping when using 'sec=sys'");
  51. /*
  52. * Cache entry
  53. */
  54. /*
  55. * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
  56. * that.
  57. */
  58. struct ent {
  59. struct cache_head h;
  60. int type; /* User / Group */
  61. u32 id;
  62. char name[IDMAP_NAMESZ];
  63. char authname[IDMAP_NAMESZ];
  64. struct rcu_head rcu_head;
  65. };
  66. /* Common entry handling */
  67. #define ENT_HASHBITS 8
  68. #define ENT_HASHMAX (1 << ENT_HASHBITS)
  69. static void
  70. ent_init(struct cache_head *cnew, struct cache_head *citm)
  71. {
  72. struct ent *new = container_of(cnew, struct ent, h);
  73. struct ent *itm = container_of(citm, struct ent, h);
  74. new->id = itm->id;
  75. new->type = itm->type;
  76. strscpy(new->name, itm->name, sizeof(new->name));
  77. strscpy(new->authname, itm->authname, sizeof(new->authname));
  78. }
  79. static void
  80. ent_put(struct kref *ref)
  81. {
  82. struct ent *map = container_of(ref, struct ent, h.ref);
  83. kfree_rcu(map, rcu_head);
  84. }
  85. static struct cache_head *
  86. ent_alloc(void)
  87. {
  88. struct ent *e = kmalloc(sizeof(*e), GFP_KERNEL);
  89. if (e)
  90. return &e->h;
  91. else
  92. return NULL;
  93. }
  94. /*
  95. * ID -> Name cache
  96. */
  97. static uint32_t
  98. idtoname_hash(struct ent *ent)
  99. {
  100. uint32_t hash;
  101. hash = hash_str(ent->authname, ENT_HASHBITS);
  102. hash = hash_long(hash ^ ent->id, ENT_HASHBITS);
  103. /* Flip LSB for user/group */
  104. if (ent->type == IDMAP_TYPE_GROUP)
  105. hash ^= 1;
  106. return hash;
  107. }
  108. static int
  109. idtoname_upcall(struct cache_detail *cd, struct cache_head *h)
  110. {
  111. return sunrpc_cache_pipe_upcall_timeout(cd, h);
  112. }
  113. static void
  114. idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
  115. int *blen)
  116. {
  117. struct ent *ent = container_of(ch, struct ent, h);
  118. char idstr[11];
  119. qword_add(bpp, blen, ent->authname);
  120. snprintf(idstr, sizeof(idstr), "%u", ent->id);
  121. qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
  122. qword_add(bpp, blen, idstr);
  123. (*bpp)[-1] = '\n';
  124. }
  125. static int
  126. idtoname_match(struct cache_head *ca, struct cache_head *cb)
  127. {
  128. struct ent *a = container_of(ca, struct ent, h);
  129. struct ent *b = container_of(cb, struct ent, h);
  130. return (a->id == b->id && a->type == b->type &&
  131. strcmp(a->authname, b->authname) == 0);
  132. }
  133. static int
  134. idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
  135. {
  136. struct ent *ent;
  137. if (h == NULL) {
  138. seq_puts(m, "#domain type id [name]\n");
  139. return 0;
  140. }
  141. ent = container_of(h, struct ent, h);
  142. seq_printf(m, "%s %s %u", ent->authname,
  143. ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
  144. ent->id);
  145. if (test_bit(CACHE_VALID, &h->flags))
  146. seq_printf(m, " %s", ent->name);
  147. seq_putc(m, '\n');
  148. return 0;
  149. }
  150. static void
  151. warn_no_idmapd(struct cache_detail *detail, int has_died)
  152. {
  153. printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
  154. has_died ? "died" : "not been started");
  155. }
  156. static int idtoname_parse(struct cache_detail *, char *, int);
  157. static struct ent *idtoname_lookup(struct cache_detail *, struct ent *);
  158. static struct ent *idtoname_update(struct cache_detail *, struct ent *,
  159. struct ent *);
  160. static const struct cache_detail idtoname_cache_template = {
  161. .owner = THIS_MODULE,
  162. .hash_size = ENT_HASHMAX,
  163. .name = "nfs4.idtoname",
  164. .cache_put = ent_put,
  165. .cache_upcall = idtoname_upcall,
  166. .cache_request = idtoname_request,
  167. .cache_parse = idtoname_parse,
  168. .cache_show = idtoname_show,
  169. .warn_no_listener = warn_no_idmapd,
  170. .match = idtoname_match,
  171. .init = ent_init,
  172. .update = ent_init,
  173. .alloc = ent_alloc,
  174. };
  175. static int
  176. idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
  177. {
  178. struct ent ent, *res;
  179. char *buf1, *bp;
  180. int len;
  181. int error = -EINVAL;
  182. if (buf[buflen - 1] != '\n')
  183. return (-EINVAL);
  184. buf[buflen - 1]= '\0';
  185. buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
  186. if (buf1 == NULL)
  187. return (-ENOMEM);
  188. memset(&ent, 0, sizeof(ent));
  189. /* Authentication name */
  190. len = qword_get(&buf, buf1, PAGE_SIZE);
  191. if (len <= 0 || len >= IDMAP_NAMESZ)
  192. goto out;
  193. memcpy(ent.authname, buf1, sizeof(ent.authname));
  194. /* Type */
  195. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  196. goto out;
  197. ent.type = strcmp(buf1, "user") == 0 ?
  198. IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
  199. /* ID */
  200. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  201. goto out;
  202. ent.id = simple_strtoul(buf1, &bp, 10);
  203. if (bp == buf1)
  204. goto out;
  205. /* expiry */
  206. error = get_expiry(&buf, &ent.h.expiry_time);
  207. if (error)
  208. goto out;
  209. error = -ENOMEM;
  210. res = idtoname_lookup(cd, &ent);
  211. if (!res)
  212. goto out;
  213. /* Name */
  214. error = -EINVAL;
  215. len = qword_get(&buf, buf1, PAGE_SIZE);
  216. if (len < 0 || len >= IDMAP_NAMESZ)
  217. goto out;
  218. if (len == 0)
  219. set_bit(CACHE_NEGATIVE, &ent.h.flags);
  220. else
  221. memcpy(ent.name, buf1, sizeof(ent.name));
  222. error = -ENOMEM;
  223. res = idtoname_update(cd, &ent, res);
  224. if (res == NULL)
  225. goto out;
  226. cache_put(&res->h, cd);
  227. error = 0;
  228. out:
  229. kfree(buf1);
  230. return error;
  231. }
  232. static struct ent *
  233. idtoname_lookup(struct cache_detail *cd, struct ent *item)
  234. {
  235. struct cache_head *ch = sunrpc_cache_lookup_rcu(cd, &item->h,
  236. idtoname_hash(item));
  237. if (ch)
  238. return container_of(ch, struct ent, h);
  239. else
  240. return NULL;
  241. }
  242. static struct ent *
  243. idtoname_update(struct cache_detail *cd, struct ent *new, struct ent *old)
  244. {
  245. struct cache_head *ch = sunrpc_cache_update(cd, &new->h, &old->h,
  246. idtoname_hash(new));
  247. if (ch)
  248. return container_of(ch, struct ent, h);
  249. else
  250. return NULL;
  251. }
  252. /*
  253. * Name -> ID cache
  254. */
  255. static inline int
  256. nametoid_hash(struct ent *ent)
  257. {
  258. return hash_str(ent->name, ENT_HASHBITS);
  259. }
  260. static int
  261. nametoid_upcall(struct cache_detail *cd, struct cache_head *h)
  262. {
  263. return sunrpc_cache_pipe_upcall_timeout(cd, h);
  264. }
  265. static void
  266. nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
  267. int *blen)
  268. {
  269. struct ent *ent = container_of(ch, struct ent, h);
  270. qword_add(bpp, blen, ent->authname);
  271. qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
  272. qword_add(bpp, blen, ent->name);
  273. (*bpp)[-1] = '\n';
  274. }
  275. static int
  276. nametoid_match(struct cache_head *ca, struct cache_head *cb)
  277. {
  278. struct ent *a = container_of(ca, struct ent, h);
  279. struct ent *b = container_of(cb, struct ent, h);
  280. return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
  281. strcmp(a->authname, b->authname) == 0);
  282. }
  283. static int
  284. nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
  285. {
  286. struct ent *ent;
  287. if (h == NULL) {
  288. seq_puts(m, "#domain type name [id]\n");
  289. return 0;
  290. }
  291. ent = container_of(h, struct ent, h);
  292. seq_printf(m, "%s %s %s", ent->authname,
  293. ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
  294. ent->name);
  295. if (test_bit(CACHE_VALID, &h->flags))
  296. seq_printf(m, " %u", ent->id);
  297. seq_putc(m, '\n');
  298. return 0;
  299. }
  300. static struct ent *nametoid_lookup(struct cache_detail *, struct ent *);
  301. static struct ent *nametoid_update(struct cache_detail *, struct ent *,
  302. struct ent *);
  303. static int nametoid_parse(struct cache_detail *, char *, int);
  304. static const struct cache_detail nametoid_cache_template = {
  305. .owner = THIS_MODULE,
  306. .hash_size = ENT_HASHMAX,
  307. .name = "nfs4.nametoid",
  308. .cache_put = ent_put,
  309. .cache_upcall = nametoid_upcall,
  310. .cache_request = nametoid_request,
  311. .cache_parse = nametoid_parse,
  312. .cache_show = nametoid_show,
  313. .warn_no_listener = warn_no_idmapd,
  314. .match = nametoid_match,
  315. .init = ent_init,
  316. .update = ent_init,
  317. .alloc = ent_alloc,
  318. };
  319. static int
  320. nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
  321. {
  322. struct ent ent, *res;
  323. char *buf1;
  324. int len, error = -EINVAL;
  325. if (buf[buflen - 1] != '\n')
  326. return (-EINVAL);
  327. buf[buflen - 1]= '\0';
  328. buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
  329. if (buf1 == NULL)
  330. return (-ENOMEM);
  331. memset(&ent, 0, sizeof(ent));
  332. /* Authentication name */
  333. len = qword_get(&buf, buf1, PAGE_SIZE);
  334. if (len <= 0 || len >= IDMAP_NAMESZ)
  335. goto out;
  336. memcpy(ent.authname, buf1, sizeof(ent.authname));
  337. /* Type */
  338. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  339. goto out;
  340. ent.type = strcmp(buf1, "user") == 0 ?
  341. IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
  342. /* Name */
  343. len = qword_get(&buf, buf1, PAGE_SIZE);
  344. if (len <= 0 || len >= IDMAP_NAMESZ)
  345. goto out;
  346. memcpy(ent.name, buf1, sizeof(ent.name));
  347. /* expiry */
  348. error = get_expiry(&buf, &ent.h.expiry_time);
  349. if (error)
  350. goto out;
  351. /* ID */
  352. error = get_int(&buf, &ent.id);
  353. if (error == -EINVAL)
  354. goto out;
  355. if (error == -ENOENT)
  356. set_bit(CACHE_NEGATIVE, &ent.h.flags);
  357. error = -ENOMEM;
  358. res = nametoid_lookup(cd, &ent);
  359. if (res == NULL)
  360. goto out;
  361. res = nametoid_update(cd, &ent, res);
  362. if (res == NULL)
  363. goto out;
  364. cache_put(&res->h, cd);
  365. error = 0;
  366. out:
  367. kfree(buf1);
  368. return (error);
  369. }
  370. static struct ent *
  371. nametoid_lookup(struct cache_detail *cd, struct ent *item)
  372. {
  373. struct cache_head *ch = sunrpc_cache_lookup_rcu(cd, &item->h,
  374. nametoid_hash(item));
  375. if (ch)
  376. return container_of(ch, struct ent, h);
  377. else
  378. return NULL;
  379. }
  380. static struct ent *
  381. nametoid_update(struct cache_detail *cd, struct ent *new, struct ent *old)
  382. {
  383. struct cache_head *ch = sunrpc_cache_update(cd, &new->h, &old->h,
  384. nametoid_hash(new));
  385. if (ch)
  386. return container_of(ch, struct ent, h);
  387. else
  388. return NULL;
  389. }
  390. /*
  391. * Exported API
  392. */
  393. int
  394. nfsd_idmap_init(struct net *net)
  395. {
  396. int rv;
  397. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  398. nn->idtoname_cache = cache_create_net(&idtoname_cache_template, net);
  399. if (IS_ERR(nn->idtoname_cache))
  400. return PTR_ERR(nn->idtoname_cache);
  401. rv = cache_register_net(nn->idtoname_cache, net);
  402. if (rv)
  403. goto destroy_idtoname_cache;
  404. nn->nametoid_cache = cache_create_net(&nametoid_cache_template, net);
  405. if (IS_ERR(nn->nametoid_cache)) {
  406. rv = PTR_ERR(nn->nametoid_cache);
  407. goto unregister_idtoname_cache;
  408. }
  409. rv = cache_register_net(nn->nametoid_cache, net);
  410. if (rv)
  411. goto destroy_nametoid_cache;
  412. return 0;
  413. destroy_nametoid_cache:
  414. cache_destroy_net(nn->nametoid_cache, net);
  415. unregister_idtoname_cache:
  416. cache_unregister_net(nn->idtoname_cache, net);
  417. destroy_idtoname_cache:
  418. cache_destroy_net(nn->idtoname_cache, net);
  419. return rv;
  420. }
  421. void
  422. nfsd_idmap_shutdown(struct net *net)
  423. {
  424. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  425. cache_unregister_net(nn->idtoname_cache, net);
  426. cache_unregister_net(nn->nametoid_cache, net);
  427. cache_destroy_net(nn->idtoname_cache, net);
  428. cache_destroy_net(nn->nametoid_cache, net);
  429. }
  430. static int
  431. idmap_lookup(struct svc_rqst *rqstp,
  432. struct ent *(*lookup_fn)(struct cache_detail *, struct ent *),
  433. struct ent *key, struct cache_detail *detail, struct ent **item)
  434. {
  435. int ret;
  436. *item = lookup_fn(detail, key);
  437. if (!*item)
  438. return -ENOMEM;
  439. retry:
  440. ret = cache_check(detail, &(*item)->h, &rqstp->rq_chandle);
  441. if (ret == -ETIMEDOUT) {
  442. struct ent *prev_item = *item;
  443. *item = lookup_fn(detail, key);
  444. if (*item != prev_item)
  445. goto retry;
  446. cache_put(&(*item)->h, detail);
  447. }
  448. return ret;
  449. }
  450. static char *
  451. rqst_authname(struct svc_rqst *rqstp)
  452. {
  453. struct auth_domain *clp;
  454. clp = rqstp->rq_gssclient ? rqstp->rq_gssclient : rqstp->rq_client;
  455. return clp->name;
  456. }
  457. static __be32
  458. idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
  459. u32 *id)
  460. {
  461. struct ent *item, key = {
  462. .type = type,
  463. };
  464. int ret;
  465. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  466. if (namelen + 1 > sizeof(key.name))
  467. return nfserr_badowner;
  468. memcpy(key.name, name, namelen);
  469. key.name[namelen] = '\0';
  470. strscpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
  471. ret = idmap_lookup(rqstp, nametoid_lookup, &key, nn->nametoid_cache, &item);
  472. if (ret == -ENOENT)
  473. return nfserr_badowner;
  474. if (ret)
  475. return nfserrno(ret);
  476. *id = item->id;
  477. cache_put(&item->h, nn->nametoid_cache);
  478. return 0;
  479. }
  480. static __be32 encode_ascii_id(struct xdr_stream *xdr, u32 id)
  481. {
  482. char buf[11];
  483. int len;
  484. __be32 *p;
  485. len = sprintf(buf, "%u", id);
  486. p = xdr_reserve_space(xdr, len + 4);
  487. if (!p)
  488. return nfserr_resource;
  489. p = xdr_encode_opaque(p, buf, len);
  490. return 0;
  491. }
  492. static __be32 idmap_id_to_name(struct xdr_stream *xdr,
  493. struct svc_rqst *rqstp, int type, u32 id)
  494. {
  495. struct ent *item, key = {
  496. .id = id,
  497. .type = type,
  498. };
  499. __be32 status = nfs_ok;
  500. __be32 *p;
  501. int ret;
  502. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  503. strscpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
  504. ret = idmap_lookup(rqstp, idtoname_lookup, &key, nn->idtoname_cache, &item);
  505. if (ret == -ENOENT)
  506. return encode_ascii_id(xdr, id);
  507. if (ret)
  508. return nfserrno(ret);
  509. ret = strlen(item->name);
  510. WARN_ON_ONCE(ret > IDMAP_NAMESZ);
  511. p = xdr_reserve_space(xdr, ret + 4);
  512. if (unlikely(!p)) {
  513. status = nfserr_resource;
  514. goto out_put;
  515. }
  516. xdr_encode_opaque(p, item->name, ret);
  517. out_put:
  518. cache_put(&item->h, nn->idtoname_cache);
  519. return status;
  520. }
  521. static bool
  522. numeric_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen, u32 *id)
  523. {
  524. int ret;
  525. char buf[11];
  526. if (namelen + 1 > sizeof(buf))
  527. /* too long to represent a 32-bit id: */
  528. return false;
  529. /* Just to make sure it's null-terminated: */
  530. memcpy(buf, name, namelen);
  531. buf[namelen] = '\0';
  532. ret = kstrtouint(buf, 10, id);
  533. return ret == 0;
  534. }
  535. static __be32
  536. do_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen, u32 *id)
  537. {
  538. if (nfs4_disable_idmapping && rqstp->rq_cred.cr_flavor < RPC_AUTH_GSS)
  539. if (numeric_name_to_id(rqstp, type, name, namelen, id))
  540. return 0;
  541. /*
  542. * otherwise, fall through and try idmapping, for
  543. * backwards compatibility with clients sending names:
  544. */
  545. return idmap_name_to_id(rqstp, type, name, namelen, id);
  546. }
  547. static __be32 encode_name_from_id(struct xdr_stream *xdr,
  548. struct svc_rqst *rqstp, int type, u32 id)
  549. {
  550. if (nfs4_disable_idmapping && rqstp->rq_cred.cr_flavor < RPC_AUTH_GSS)
  551. return encode_ascii_id(xdr, id);
  552. return idmap_id_to_name(xdr, rqstp, type, id);
  553. }
  554. __be32
  555. nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
  556. kuid_t *uid)
  557. {
  558. __be32 status;
  559. u32 id = -1;
  560. if (name == NULL || namelen == 0)
  561. return nfserr_inval;
  562. status = do_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, &id);
  563. *uid = make_kuid(nfsd_user_namespace(rqstp), id);
  564. if (!uid_valid(*uid))
  565. status = nfserr_badowner;
  566. return status;
  567. }
  568. __be32
  569. nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
  570. kgid_t *gid)
  571. {
  572. __be32 status;
  573. u32 id = -1;
  574. if (name == NULL || namelen == 0)
  575. return nfserr_inval;
  576. status = do_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, &id);
  577. *gid = make_kgid(nfsd_user_namespace(rqstp), id);
  578. if (!gid_valid(*gid))
  579. status = nfserr_badowner;
  580. return status;
  581. }
  582. __be32 nfsd4_encode_user(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  583. kuid_t uid)
  584. {
  585. u32 id = from_kuid_munged(nfsd_user_namespace(rqstp), uid);
  586. return encode_name_from_id(xdr, rqstp, IDMAP_TYPE_USER, id);
  587. }
  588. __be32 nfsd4_encode_group(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  589. kgid_t gid)
  590. {
  591. u32 id = from_kgid_munged(nfsd_user_namespace(rqstp), gid);
  592. return encode_name_from_id(xdr, rqstp, IDMAP_TYPE_GROUP, id);
  593. }