nfs4idmap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. #define DefineSimpleCacheLookupMap(STRUCT, FUNC) \
  73. DefineCacheLookup(struct STRUCT, h, FUNC##_lookup, \
  74. (struct STRUCT *item, int set), /*no setup */, \
  75. & FUNC##_cache, FUNC##_hash(item), FUNC##_match(item, tmp), \
  76. STRUCT##_init(new, item), STRUCT##_update(tmp, item), 0)
  77. /* Common entry handling */
  78. #define ENT_HASHBITS 8
  79. #define ENT_HASHMAX (1 << ENT_HASHBITS)
  80. #define ENT_HASHMASK (ENT_HASHMAX - 1)
  81. static inline void
  82. ent_init(struct ent *new, struct ent *itm)
  83. {
  84. new->id = itm->id;
  85. new->type = itm->type;
  86. strlcpy(new->name, itm->name, sizeof(new->name));
  87. strlcpy(new->authname, itm->authname, sizeof(new->name));
  88. }
  89. static inline void
  90. ent_update(struct ent *new, struct ent *itm)
  91. {
  92. ent_init(new, itm);
  93. }
  94. static void
  95. ent_put(struct cache_head *ch, struct cache_detail *cd)
  96. {
  97. if (cache_put(ch, cd)) {
  98. struct ent *map = container_of(ch, struct ent, h);
  99. kfree(map);
  100. }
  101. }
  102. /*
  103. * ID -> Name cache
  104. */
  105. static struct cache_head *idtoname_table[ENT_HASHMAX];
  106. static uint32_t
  107. idtoname_hash(struct ent *ent)
  108. {
  109. uint32_t hash;
  110. hash = hash_str(ent->authname, ENT_HASHBITS);
  111. hash = hash_long(hash ^ ent->id, ENT_HASHBITS);
  112. /* Flip LSB for user/group */
  113. if (ent->type == IDMAP_TYPE_GROUP)
  114. hash ^= 1;
  115. return hash;
  116. }
  117. static void
  118. idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
  119. int *blen)
  120. {
  121. struct ent *ent = container_of(ch, struct ent, h);
  122. char idstr[11];
  123. qword_add(bpp, blen, ent->authname);
  124. snprintf(idstr, sizeof(idstr), "%d", ent->id);
  125. qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
  126. qword_add(bpp, blen, idstr);
  127. (*bpp)[-1] = '\n';
  128. }
  129. static inline int
  130. idtoname_match(struct ent *a, struct ent *b)
  131. {
  132. return (a->id == b->id && a->type == b->type &&
  133. strcmp(a->authname, b->authname) == 0);
  134. }
  135. static int
  136. idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
  137. {
  138. struct ent *ent;
  139. if (h == NULL) {
  140. seq_puts(m, "#domain type id [name]\n");
  141. return 0;
  142. }
  143. ent = container_of(h, struct ent, h);
  144. seq_printf(m, "%s %s %d", ent->authname,
  145. ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
  146. ent->id);
  147. if (test_bit(CACHE_VALID, &h->flags))
  148. seq_printf(m, " %s", ent->name);
  149. seq_printf(m, "\n");
  150. return 0;
  151. }
  152. static void
  153. warn_no_idmapd(struct cache_detail *detail)
  154. {
  155. printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
  156. detail->last_close? "died" : "not been started");
  157. }
  158. static int idtoname_parse(struct cache_detail *, char *, int);
  159. static struct ent *idtoname_lookup(struct ent *, int);
  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. };
  171. int
  172. idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
  173. {
  174. struct ent ent, *res;
  175. char *buf1, *bp;
  176. int error = -EINVAL;
  177. if (buf[buflen - 1] != '\n')
  178. return (-EINVAL);
  179. buf[buflen - 1]= '\0';
  180. buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
  181. if (buf1 == NULL)
  182. return (-ENOMEM);
  183. memset(&ent, 0, sizeof(ent));
  184. /* Authentication name */
  185. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  186. goto out;
  187. memcpy(ent.authname, buf1, sizeof(ent.authname));
  188. /* Type */
  189. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  190. goto out;
  191. ent.type = strcmp(buf1, "user") == 0 ?
  192. IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
  193. /* ID */
  194. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  195. goto out;
  196. ent.id = simple_strtoul(buf1, &bp, 10);
  197. if (bp == buf1)
  198. goto out;
  199. /* expiry */
  200. ent.h.expiry_time = get_expiry(&buf);
  201. if (ent.h.expiry_time == 0)
  202. goto out;
  203. /* Name */
  204. error = qword_get(&buf, buf1, PAGE_SIZE);
  205. if (error == -EINVAL)
  206. goto out;
  207. if (error == -ENOENT)
  208. set_bit(CACHE_NEGATIVE, &ent.h.flags);
  209. else {
  210. if (error >= IDMAP_NAMESZ) {
  211. error = -EINVAL;
  212. goto out;
  213. }
  214. memcpy(ent.name, buf1, sizeof(ent.name));
  215. }
  216. error = -ENOMEM;
  217. if ((res = idtoname_lookup(&ent, 1)) == NULL)
  218. goto out;
  219. ent_put(&res->h, &idtoname_cache);
  220. error = 0;
  221. out:
  222. kfree(buf1);
  223. return error;
  224. }
  225. static DefineSimpleCacheLookupMap(ent, idtoname);
  226. /*
  227. * Name -> ID cache
  228. */
  229. static struct cache_head *nametoid_table[ENT_HASHMAX];
  230. static inline int
  231. nametoid_hash(struct ent *ent)
  232. {
  233. return hash_str(ent->name, ENT_HASHBITS);
  234. }
  235. static void
  236. nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
  237. int *blen)
  238. {
  239. struct ent *ent = container_of(ch, struct ent, h);
  240. qword_add(bpp, blen, ent->authname);
  241. qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
  242. qword_add(bpp, blen, ent->name);
  243. (*bpp)[-1] = '\n';
  244. }
  245. static inline int
  246. nametoid_match(struct ent *a, struct ent *b)
  247. {
  248. return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
  249. strcmp(a->authname, b->authname) == 0);
  250. }
  251. static int
  252. nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
  253. {
  254. struct ent *ent;
  255. if (h == NULL) {
  256. seq_puts(m, "#domain type name [id]\n");
  257. return 0;
  258. }
  259. ent = container_of(h, struct ent, h);
  260. seq_printf(m, "%s %s %s", ent->authname,
  261. ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
  262. ent->name);
  263. if (test_bit(CACHE_VALID, &h->flags))
  264. seq_printf(m, " %d", ent->id);
  265. seq_printf(m, "\n");
  266. return 0;
  267. }
  268. static struct ent *nametoid_lookup(struct ent *, int);
  269. static int nametoid_parse(struct cache_detail *, char *, int);
  270. static struct cache_detail nametoid_cache = {
  271. .owner = THIS_MODULE,
  272. .hash_size = ENT_HASHMAX,
  273. .hash_table = nametoid_table,
  274. .name = "nfs4.nametoid",
  275. .cache_put = ent_put,
  276. .cache_request = nametoid_request,
  277. .cache_parse = nametoid_parse,
  278. .cache_show = nametoid_show,
  279. .warn_no_listener = warn_no_idmapd,
  280. };
  281. static int
  282. nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
  283. {
  284. struct ent ent, *res;
  285. char *buf1;
  286. int error = -EINVAL;
  287. if (buf[buflen - 1] != '\n')
  288. return (-EINVAL);
  289. buf[buflen - 1]= '\0';
  290. buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
  291. if (buf1 == NULL)
  292. return (-ENOMEM);
  293. memset(&ent, 0, sizeof(ent));
  294. /* Authentication name */
  295. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  296. goto out;
  297. memcpy(ent.authname, buf1, sizeof(ent.authname));
  298. /* Type */
  299. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  300. goto out;
  301. ent.type = strcmp(buf1, "user") == 0 ?
  302. IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
  303. /* Name */
  304. error = qword_get(&buf, buf1, PAGE_SIZE);
  305. if (error <= 0 || error >= IDMAP_NAMESZ)
  306. goto out;
  307. memcpy(ent.name, buf1, sizeof(ent.name));
  308. /* expiry */
  309. ent.h.expiry_time = get_expiry(&buf);
  310. if (ent.h.expiry_time == 0)
  311. goto out;
  312. /* ID */
  313. error = get_int(&buf, &ent.id);
  314. if (error == -EINVAL)
  315. goto out;
  316. if (error == -ENOENT)
  317. set_bit(CACHE_NEGATIVE, &ent.h.flags);
  318. error = -ENOMEM;
  319. if ((res = nametoid_lookup(&ent, 1)) == NULL)
  320. goto out;
  321. ent_put(&res->h, &nametoid_cache);
  322. error = 0;
  323. out:
  324. kfree(buf1);
  325. return (error);
  326. }
  327. static DefineSimpleCacheLookupMap(ent, nametoid);
  328. /*
  329. * Exported API
  330. */
  331. void
  332. nfsd_idmap_init(void)
  333. {
  334. cache_register(&idtoname_cache);
  335. cache_register(&nametoid_cache);
  336. }
  337. void
  338. nfsd_idmap_shutdown(void)
  339. {
  340. if (cache_unregister(&idtoname_cache))
  341. printk(KERN_ERR "nfsd: failed to unregister idtoname cache\n");
  342. if (cache_unregister(&nametoid_cache))
  343. printk(KERN_ERR "nfsd: failed to unregister nametoid cache\n");
  344. }
  345. /*
  346. * Deferred request handling
  347. */
  348. struct idmap_defer_req {
  349. struct cache_req req;
  350. struct cache_deferred_req deferred_req;
  351. wait_queue_head_t waitq;
  352. atomic_t count;
  353. };
  354. static inline void
  355. put_mdr(struct idmap_defer_req *mdr)
  356. {
  357. if (atomic_dec_and_test(&mdr->count))
  358. kfree(mdr);
  359. }
  360. static inline void
  361. get_mdr(struct idmap_defer_req *mdr)
  362. {
  363. atomic_inc(&mdr->count);
  364. }
  365. static void
  366. idmap_revisit(struct cache_deferred_req *dreq, int toomany)
  367. {
  368. struct idmap_defer_req *mdr =
  369. container_of(dreq, struct idmap_defer_req, deferred_req);
  370. wake_up(&mdr->waitq);
  371. put_mdr(mdr);
  372. }
  373. static struct cache_deferred_req *
  374. idmap_defer(struct cache_req *req)
  375. {
  376. struct idmap_defer_req *mdr =
  377. container_of(req, struct idmap_defer_req, req);
  378. mdr->deferred_req.revisit = idmap_revisit;
  379. get_mdr(mdr);
  380. return (&mdr->deferred_req);
  381. }
  382. static inline int
  383. do_idmap_lookup(struct ent *(*lookup_fn)(struct ent *, int), struct ent *key,
  384. struct cache_detail *detail, struct ent **item,
  385. struct idmap_defer_req *mdr)
  386. {
  387. *item = lookup_fn(key, 0);
  388. if (!*item)
  389. return -ENOMEM;
  390. return cache_check(detail, &(*item)->h, &mdr->req);
  391. }
  392. static inline int
  393. do_idmap_lookup_nowait(struct ent *(*lookup_fn)(struct ent *, int),
  394. struct ent *key, struct cache_detail *detail,
  395. struct ent **item)
  396. {
  397. int ret = -ENOMEM;
  398. *item = lookup_fn(key, 0);
  399. if (!*item)
  400. goto out_err;
  401. ret = -ETIMEDOUT;
  402. if (!test_bit(CACHE_VALID, &(*item)->h.flags)
  403. || (*item)->h.expiry_time < get_seconds()
  404. || detail->flush_time > (*item)->h.last_refresh)
  405. goto out_put;
  406. ret = -ENOENT;
  407. if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
  408. goto out_put;
  409. return 0;
  410. out_put:
  411. ent_put(&(*item)->h, detail);
  412. out_err:
  413. *item = NULL;
  414. return ret;
  415. }
  416. static int
  417. idmap_lookup(struct svc_rqst *rqstp,
  418. struct ent *(*lookup_fn)(struct ent *, int), struct ent *key,
  419. struct cache_detail *detail, struct ent **item)
  420. {
  421. struct idmap_defer_req *mdr;
  422. int ret;
  423. mdr = kmalloc(sizeof(*mdr), GFP_KERNEL);
  424. if (!mdr)
  425. return -ENOMEM;
  426. memset(mdr, 0, sizeof(*mdr));
  427. atomic_set(&mdr->count, 1);
  428. init_waitqueue_head(&mdr->waitq);
  429. mdr->req.defer = idmap_defer;
  430. ret = do_idmap_lookup(lookup_fn, key, detail, item, mdr);
  431. if (ret == -EAGAIN) {
  432. wait_event_interruptible_timeout(mdr->waitq,
  433. test_bit(CACHE_VALID, &(*item)->h.flags), 1 * HZ);
  434. ret = do_idmap_lookup_nowait(lookup_fn, key, detail, item);
  435. }
  436. put_mdr(mdr);
  437. return ret;
  438. }
  439. static int
  440. idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
  441. uid_t *id)
  442. {
  443. struct ent *item, key = {
  444. .type = type,
  445. };
  446. int ret;
  447. if (namelen + 1 > sizeof(key.name))
  448. return -EINVAL;
  449. memcpy(key.name, name, namelen);
  450. key.name[namelen] = '\0';
  451. strlcpy(key.authname, rqstp->rq_client->name, sizeof(key.authname));
  452. ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item);
  453. if (ret == -ENOENT)
  454. ret = -ESRCH; /* nfserr_badname */
  455. if (ret)
  456. return ret;
  457. *id = item->id;
  458. ent_put(&item->h, &nametoid_cache);
  459. return 0;
  460. }
  461. static int
  462. idmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name)
  463. {
  464. struct ent *item, key = {
  465. .id = id,
  466. .type = type,
  467. };
  468. int ret;
  469. strlcpy(key.authname, rqstp->rq_client->name, sizeof(key.authname));
  470. ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item);
  471. if (ret == -ENOENT)
  472. return sprintf(name, "%u", id);
  473. if (ret)
  474. return ret;
  475. ret = strlen(item->name);
  476. BUG_ON(ret > IDMAP_NAMESZ);
  477. memcpy(name, item->name, ret);
  478. ent_put(&item->h, &idtoname_cache);
  479. return ret;
  480. }
  481. int
  482. nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
  483. __u32 *id)
  484. {
  485. return idmap_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id);
  486. }
  487. int
  488. nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
  489. __u32 *id)
  490. {
  491. return idmap_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id);
  492. }
  493. int
  494. nfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
  495. {
  496. return idmap_id_to_name(rqstp, IDMAP_TYPE_USER, id, name);
  497. }
  498. int
  499. nfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
  500. {
  501. return idmap_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name);
  502. }