nfs4idmap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. .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. };
  170. int
  171. idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
  172. {
  173. struct ent ent, *res;
  174. char *buf1, *bp;
  175. int error = -EINVAL;
  176. if (buf[buflen - 1] != '\n')
  177. return (-EINVAL);
  178. buf[buflen - 1]= '\0';
  179. buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
  180. if (buf1 == NULL)
  181. return (-ENOMEM);
  182. memset(&ent, 0, sizeof(ent));
  183. /* Authentication name */
  184. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  185. goto out;
  186. memcpy(ent.authname, buf1, sizeof(ent.authname));
  187. /* Type */
  188. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  189. goto out;
  190. ent.type = strcmp(buf1, "user") == 0 ?
  191. IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
  192. /* ID */
  193. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  194. goto out;
  195. ent.id = simple_strtoul(buf1, &bp, 10);
  196. if (bp == buf1)
  197. goto out;
  198. /* expiry */
  199. ent.h.expiry_time = get_expiry(&buf);
  200. if (ent.h.expiry_time == 0)
  201. goto out;
  202. /* Name */
  203. error = qword_get(&buf, buf1, PAGE_SIZE);
  204. if (error == -EINVAL)
  205. goto out;
  206. if (error == -ENOENT)
  207. set_bit(CACHE_NEGATIVE, &ent.h.flags);
  208. else {
  209. if (error >= IDMAP_NAMESZ) {
  210. error = -EINVAL;
  211. goto out;
  212. }
  213. memcpy(ent.name, buf1, sizeof(ent.name));
  214. }
  215. error = -ENOMEM;
  216. if ((res = idtoname_lookup(&ent, 1)) == NULL)
  217. goto out;
  218. ent_put(&res->h, &idtoname_cache);
  219. error = 0;
  220. out:
  221. kfree(buf1);
  222. return error;
  223. }
  224. static DefineSimpleCacheLookupMap(ent, idtoname);
  225. /*
  226. * Name -> ID cache
  227. */
  228. static struct cache_head *nametoid_table[ENT_HASHMAX];
  229. static inline int
  230. nametoid_hash(struct ent *ent)
  231. {
  232. return hash_str(ent->name, ENT_HASHBITS);
  233. }
  234. static void
  235. nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
  236. int *blen)
  237. {
  238. struct ent *ent = container_of(ch, struct ent, h);
  239. qword_add(bpp, blen, ent->authname);
  240. qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
  241. qword_add(bpp, blen, ent->name);
  242. (*bpp)[-1] = '\n';
  243. }
  244. static inline int
  245. nametoid_match(struct ent *a, struct ent *b)
  246. {
  247. return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
  248. strcmp(a->authname, b->authname) == 0);
  249. }
  250. static int
  251. nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
  252. {
  253. struct ent *ent;
  254. if (h == NULL) {
  255. seq_puts(m, "#domain type name [id]\n");
  256. return 0;
  257. }
  258. ent = container_of(h, struct ent, h);
  259. seq_printf(m, "%s %s %s", ent->authname,
  260. ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
  261. ent->name);
  262. if (test_bit(CACHE_VALID, &h->flags))
  263. seq_printf(m, " %d", ent->id);
  264. seq_printf(m, "\n");
  265. return 0;
  266. }
  267. static struct ent *nametoid_lookup(struct ent *, int);
  268. static int nametoid_parse(struct cache_detail *, char *, int);
  269. static struct cache_detail nametoid_cache = {
  270. .hash_size = ENT_HASHMAX,
  271. .hash_table = nametoid_table,
  272. .name = "nfs4.nametoid",
  273. .cache_put = ent_put,
  274. .cache_request = nametoid_request,
  275. .cache_parse = nametoid_parse,
  276. .cache_show = nametoid_show,
  277. .warn_no_listener = warn_no_idmapd,
  278. };
  279. static int
  280. nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
  281. {
  282. struct ent ent, *res;
  283. char *buf1;
  284. int error = -EINVAL;
  285. if (buf[buflen - 1] != '\n')
  286. return (-EINVAL);
  287. buf[buflen - 1]= '\0';
  288. buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
  289. if (buf1 == NULL)
  290. return (-ENOMEM);
  291. memset(&ent, 0, sizeof(ent));
  292. /* Authentication name */
  293. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  294. goto out;
  295. memcpy(ent.authname, buf1, sizeof(ent.authname));
  296. /* Type */
  297. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  298. goto out;
  299. ent.type = strcmp(buf1, "user") == 0 ?
  300. IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
  301. /* Name */
  302. error = qword_get(&buf, buf1, PAGE_SIZE);
  303. if (error <= 0 || error >= IDMAP_NAMESZ)
  304. goto out;
  305. memcpy(ent.name, buf1, sizeof(ent.name));
  306. /* expiry */
  307. ent.h.expiry_time = get_expiry(&buf);
  308. if (ent.h.expiry_time == 0)
  309. goto out;
  310. /* ID */
  311. error = get_int(&buf, &ent.id);
  312. if (error == -EINVAL)
  313. goto out;
  314. if (error == -ENOENT)
  315. set_bit(CACHE_NEGATIVE, &ent.h.flags);
  316. error = -ENOMEM;
  317. if ((res = nametoid_lookup(&ent, 1)) == NULL)
  318. goto out;
  319. ent_put(&res->h, &nametoid_cache);
  320. error = 0;
  321. out:
  322. kfree(buf1);
  323. return (error);
  324. }
  325. static DefineSimpleCacheLookupMap(ent, nametoid);
  326. /*
  327. * Exported API
  328. */
  329. void
  330. nfsd_idmap_init(void)
  331. {
  332. cache_register(&idtoname_cache);
  333. cache_register(&nametoid_cache);
  334. }
  335. void
  336. nfsd_idmap_shutdown(void)
  337. {
  338. cache_unregister(&idtoname_cache);
  339. cache_unregister(&nametoid_cache);
  340. }
  341. /*
  342. * Deferred request handling
  343. */
  344. struct idmap_defer_req {
  345. struct cache_req req;
  346. struct cache_deferred_req deferred_req;
  347. wait_queue_head_t waitq;
  348. atomic_t count;
  349. };
  350. static inline void
  351. put_mdr(struct idmap_defer_req *mdr)
  352. {
  353. if (atomic_dec_and_test(&mdr->count))
  354. kfree(mdr);
  355. }
  356. static inline void
  357. get_mdr(struct idmap_defer_req *mdr)
  358. {
  359. atomic_inc(&mdr->count);
  360. }
  361. static void
  362. idmap_revisit(struct cache_deferred_req *dreq, int toomany)
  363. {
  364. struct idmap_defer_req *mdr =
  365. container_of(dreq, struct idmap_defer_req, deferred_req);
  366. wake_up(&mdr->waitq);
  367. put_mdr(mdr);
  368. }
  369. static struct cache_deferred_req *
  370. idmap_defer(struct cache_req *req)
  371. {
  372. struct idmap_defer_req *mdr =
  373. container_of(req, struct idmap_defer_req, req);
  374. mdr->deferred_req.revisit = idmap_revisit;
  375. get_mdr(mdr);
  376. return (&mdr->deferred_req);
  377. }
  378. static inline int
  379. do_idmap_lookup(struct ent *(*lookup_fn)(struct ent *, int), struct ent *key,
  380. struct cache_detail *detail, struct ent **item,
  381. struct idmap_defer_req *mdr)
  382. {
  383. *item = lookup_fn(key, 0);
  384. if (!*item)
  385. return -ENOMEM;
  386. return cache_check(detail, &(*item)->h, &mdr->req);
  387. }
  388. static inline int
  389. do_idmap_lookup_nowait(struct ent *(*lookup_fn)(struct ent *, int),
  390. struct ent *key, struct cache_detail *detail,
  391. struct ent **item)
  392. {
  393. int ret = -ENOMEM;
  394. *item = lookup_fn(key, 0);
  395. if (!*item)
  396. goto out_err;
  397. ret = -ETIMEDOUT;
  398. if (!test_bit(CACHE_VALID, &(*item)->h.flags)
  399. || (*item)->h.expiry_time < get_seconds()
  400. || detail->flush_time > (*item)->h.last_refresh)
  401. goto out_put;
  402. ret = -ENOENT;
  403. if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
  404. goto out_put;
  405. return 0;
  406. out_put:
  407. ent_put(&(*item)->h, detail);
  408. out_err:
  409. *item = NULL;
  410. return ret;
  411. }
  412. static int
  413. idmap_lookup(struct svc_rqst *rqstp,
  414. struct ent *(*lookup_fn)(struct ent *, int), struct ent *key,
  415. struct cache_detail *detail, struct ent **item)
  416. {
  417. struct idmap_defer_req *mdr;
  418. int ret;
  419. mdr = kmalloc(sizeof(*mdr), GFP_KERNEL);
  420. if (!mdr)
  421. return -ENOMEM;
  422. memset(mdr, 0, sizeof(*mdr));
  423. atomic_set(&mdr->count, 1);
  424. init_waitqueue_head(&mdr->waitq);
  425. mdr->req.defer = idmap_defer;
  426. ret = do_idmap_lookup(lookup_fn, key, detail, item, mdr);
  427. if (ret == -EAGAIN) {
  428. wait_event_interruptible_timeout(mdr->waitq,
  429. test_bit(CACHE_VALID, &(*item)->h.flags), 1 * HZ);
  430. ret = do_idmap_lookup_nowait(lookup_fn, key, detail, item);
  431. }
  432. put_mdr(mdr);
  433. return ret;
  434. }
  435. static int
  436. idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
  437. uid_t *id)
  438. {
  439. struct ent *item, key = {
  440. .type = type,
  441. };
  442. int ret;
  443. if (namelen + 1 > sizeof(key.name))
  444. return -EINVAL;
  445. memcpy(key.name, name, namelen);
  446. key.name[namelen] = '\0';
  447. strlcpy(key.authname, rqstp->rq_client->name, sizeof(key.authname));
  448. ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item);
  449. if (ret == -ENOENT)
  450. ret = -ESRCH; /* nfserr_badname */
  451. if (ret)
  452. return ret;
  453. *id = item->id;
  454. ent_put(&item->h, &nametoid_cache);
  455. return 0;
  456. }
  457. static int
  458. idmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name)
  459. {
  460. struct ent *item, key = {
  461. .id = id,
  462. .type = type,
  463. };
  464. int ret;
  465. strlcpy(key.authname, rqstp->rq_client->name, sizeof(key.authname));
  466. ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item);
  467. if (ret == -ENOENT)
  468. return sprintf(name, "%u", id);
  469. if (ret)
  470. return ret;
  471. ret = strlen(item->name);
  472. BUG_ON(ret > IDMAP_NAMESZ);
  473. memcpy(name, item->name, ret);
  474. ent_put(&item->h, &idtoname_cache);
  475. return ret;
  476. }
  477. int
  478. nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
  479. __u32 *id)
  480. {
  481. return idmap_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id);
  482. }
  483. int
  484. nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
  485. __u32 *id)
  486. {
  487. return idmap_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id);
  488. }
  489. int
  490. nfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
  491. {
  492. return idmap_id_to_name(rqstp, IDMAP_TYPE_USER, id, name);
  493. }
  494. int
  495. nfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
  496. {
  497. return idmap_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name);
  498. }