nfs4idmap.c 15 KB

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