nfs4idmap.c 15 KB

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