nfs4idmap.c 15 KB

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