nfs4idmap.c 15 KB

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