nfs4idmap.c 15 KB

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