nfs4idmap.c 17 KB

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