nfs4idmap.c 15 KB

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