nfs4idmap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * Mapping of UID/GIDs to name and vice versa.
  3. *
  4. * Copyright (c) 2002, 2003 The Regents of the University of
  5. * Michigan. All rights reserved.
  6. *
  7. * Marius Aamodt Eriksen <marius@umich.edu>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  23. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  24. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  29. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <linux/module.h>
  35. #include <linux/seq_file.h>
  36. #include <linux/sched.h>
  37. #include <linux/slab.h>
  38. #include "idmap.h"
  39. #include "nfsd.h"
  40. /*
  41. * Cache entry
  42. */
  43. /*
  44. * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
  45. * that.
  46. */
  47. #define IDMAP_TYPE_USER 0
  48. #define IDMAP_TYPE_GROUP 1
  49. struct ent {
  50. struct cache_head h;
  51. int type; /* User / Group */
  52. uid_t id;
  53. char name[IDMAP_NAMESZ];
  54. char authname[IDMAP_NAMESZ];
  55. };
  56. /* Common entry handling */
  57. #define ENT_HASHBITS 8
  58. #define ENT_HASHMAX (1 << ENT_HASHBITS)
  59. static void
  60. ent_init(struct cache_head *cnew, struct cache_head *citm)
  61. {
  62. struct ent *new = container_of(cnew, struct ent, h);
  63. struct ent *itm = container_of(citm, struct ent, h);
  64. new->id = itm->id;
  65. new->type = itm->type;
  66. strlcpy(new->name, itm->name, sizeof(new->name));
  67. strlcpy(new->authname, itm->authname, sizeof(new->name));
  68. }
  69. static void
  70. ent_put(struct kref *ref)
  71. {
  72. struct ent *map = container_of(ref, struct ent, h.ref);
  73. kfree(map);
  74. }
  75. static struct cache_head *
  76. ent_alloc(void)
  77. {
  78. struct ent *e = kmalloc(sizeof(*e), GFP_KERNEL);
  79. if (e)
  80. return &e->h;
  81. else
  82. return NULL;
  83. }
  84. /*
  85. * ID -> Name cache
  86. */
  87. static struct cache_head *idtoname_table[ENT_HASHMAX];
  88. static uint32_t
  89. idtoname_hash(struct ent *ent)
  90. {
  91. uint32_t hash;
  92. hash = hash_str(ent->authname, ENT_HASHBITS);
  93. hash = hash_long(hash ^ ent->id, ENT_HASHBITS);
  94. /* Flip LSB for user/group */
  95. if (ent->type == IDMAP_TYPE_GROUP)
  96. hash ^= 1;
  97. return hash;
  98. }
  99. static void
  100. idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
  101. int *blen)
  102. {
  103. struct ent *ent = container_of(ch, struct ent, h);
  104. char idstr[11];
  105. qword_add(bpp, blen, ent->authname);
  106. snprintf(idstr, sizeof(idstr), "%u", ent->id);
  107. qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
  108. qword_add(bpp, blen, idstr);
  109. (*bpp)[-1] = '\n';
  110. }
  111. static int
  112. idtoname_upcall(struct cache_detail *cd, struct cache_head *ch)
  113. {
  114. return sunrpc_cache_pipe_upcall(cd, ch, idtoname_request);
  115. }
  116. static int
  117. idtoname_match(struct cache_head *ca, struct cache_head *cb)
  118. {
  119. struct ent *a = container_of(ca, struct ent, h);
  120. struct ent *b = container_of(cb, struct ent, h);
  121. return (a->id == b->id && a->type == b->type &&
  122. strcmp(a->authname, b->authname) == 0);
  123. }
  124. static int
  125. idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
  126. {
  127. struct ent *ent;
  128. if (h == NULL) {
  129. seq_puts(m, "#domain type id [name]\n");
  130. return 0;
  131. }
  132. ent = container_of(h, struct ent, h);
  133. seq_printf(m, "%s %s %u", ent->authname,
  134. ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
  135. ent->id);
  136. if (test_bit(CACHE_VALID, &h->flags))
  137. seq_printf(m, " %s", ent->name);
  138. seq_printf(m, "\n");
  139. return 0;
  140. }
  141. static void
  142. warn_no_idmapd(struct cache_detail *detail, int has_died)
  143. {
  144. printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
  145. has_died ? "died" : "not been started");
  146. }
  147. static int idtoname_parse(struct cache_detail *, char *, int);
  148. static struct ent *idtoname_lookup(struct ent *);
  149. static struct ent *idtoname_update(struct ent *, struct ent *);
  150. static struct cache_detail idtoname_cache = {
  151. .owner = THIS_MODULE,
  152. .hash_size = ENT_HASHMAX,
  153. .hash_table = idtoname_table,
  154. .name = "nfs4.idtoname",
  155. .cache_put = ent_put,
  156. .cache_upcall = idtoname_upcall,
  157. .cache_parse = idtoname_parse,
  158. .cache_show = idtoname_show,
  159. .warn_no_listener = warn_no_idmapd,
  160. .match = idtoname_match,
  161. .init = ent_init,
  162. .update = ent_init,
  163. .alloc = ent_alloc,
  164. };
  165. static int
  166. idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
  167. {
  168. struct ent ent, *res;
  169. char *buf1, *bp;
  170. int len;
  171. int error = -EINVAL;
  172. if (buf[buflen - 1] != '\n')
  173. return (-EINVAL);
  174. buf[buflen - 1]= '\0';
  175. buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
  176. if (buf1 == NULL)
  177. return (-ENOMEM);
  178. memset(&ent, 0, sizeof(ent));
  179. /* Authentication name */
  180. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  181. goto out;
  182. memcpy(ent.authname, buf1, sizeof(ent.authname));
  183. /* Type */
  184. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  185. goto out;
  186. ent.type = strcmp(buf1, "user") == 0 ?
  187. IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
  188. /* ID */
  189. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  190. goto out;
  191. ent.id = simple_strtoul(buf1, &bp, 10);
  192. if (bp == buf1)
  193. goto out;
  194. /* expiry */
  195. ent.h.expiry_time = get_expiry(&buf);
  196. if (ent.h.expiry_time == 0)
  197. goto out;
  198. error = -ENOMEM;
  199. res = idtoname_lookup(&ent);
  200. if (!res)
  201. goto out;
  202. /* Name */
  203. error = -EINVAL;
  204. len = qword_get(&buf, buf1, PAGE_SIZE);
  205. if (len < 0)
  206. goto out;
  207. if (len == 0)
  208. set_bit(CACHE_NEGATIVE, &ent.h.flags);
  209. else if (len >= IDMAP_NAMESZ)
  210. goto out;
  211. else
  212. memcpy(ent.name, buf1, sizeof(ent.name));
  213. error = -ENOMEM;
  214. res = idtoname_update(&ent, res);
  215. if (res == NULL)
  216. goto out;
  217. cache_put(&res->h, &idtoname_cache);
  218. error = 0;
  219. out:
  220. kfree(buf1);
  221. return error;
  222. }
  223. static struct ent *
  224. idtoname_lookup(struct ent *item)
  225. {
  226. struct cache_head *ch = sunrpc_cache_lookup(&idtoname_cache,
  227. &item->h,
  228. idtoname_hash(item));
  229. if (ch)
  230. return container_of(ch, struct ent, h);
  231. else
  232. return NULL;
  233. }
  234. static struct ent *
  235. idtoname_update(struct ent *new, struct ent *old)
  236. {
  237. struct cache_head *ch = sunrpc_cache_update(&idtoname_cache,
  238. &new->h, &old->h,
  239. idtoname_hash(new));
  240. if (ch)
  241. return container_of(ch, struct ent, h);
  242. else
  243. return NULL;
  244. }
  245. /*
  246. * Name -> ID cache
  247. */
  248. static struct cache_head *nametoid_table[ENT_HASHMAX];
  249. static inline int
  250. nametoid_hash(struct ent *ent)
  251. {
  252. return hash_str(ent->name, ENT_HASHBITS);
  253. }
  254. static void
  255. nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
  256. int *blen)
  257. {
  258. struct ent *ent = container_of(ch, struct ent, h);
  259. qword_add(bpp, blen, ent->authname);
  260. qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
  261. qword_add(bpp, blen, ent->name);
  262. (*bpp)[-1] = '\n';
  263. }
  264. static int
  265. nametoid_upcall(struct cache_detail *cd, struct cache_head *ch)
  266. {
  267. return sunrpc_cache_pipe_upcall(cd, ch, nametoid_request);
  268. }
  269. static int
  270. nametoid_match(struct cache_head *ca, struct cache_head *cb)
  271. {
  272. struct ent *a = container_of(ca, struct ent, h);
  273. struct ent *b = container_of(cb, struct ent, h);
  274. return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
  275. strcmp(a->authname, b->authname) == 0);
  276. }
  277. static int
  278. nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
  279. {
  280. struct ent *ent;
  281. if (h == NULL) {
  282. seq_puts(m, "#domain type name [id]\n");
  283. return 0;
  284. }
  285. ent = container_of(h, struct ent, h);
  286. seq_printf(m, "%s %s %s", ent->authname,
  287. ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
  288. ent->name);
  289. if (test_bit(CACHE_VALID, &h->flags))
  290. seq_printf(m, " %u", ent->id);
  291. seq_printf(m, "\n");
  292. return 0;
  293. }
  294. static struct ent *nametoid_lookup(struct ent *);
  295. static struct ent *nametoid_update(struct ent *, struct ent *);
  296. static int nametoid_parse(struct cache_detail *, char *, int);
  297. static struct cache_detail nametoid_cache = {
  298. .owner = THIS_MODULE,
  299. .hash_size = ENT_HASHMAX,
  300. .hash_table = nametoid_table,
  301. .name = "nfs4.nametoid",
  302. .cache_put = ent_put,
  303. .cache_upcall = nametoid_upcall,
  304. .cache_parse = nametoid_parse,
  305. .cache_show = nametoid_show,
  306. .warn_no_listener = warn_no_idmapd,
  307. .match = nametoid_match,
  308. .init = ent_init,
  309. .update = ent_init,
  310. .alloc = ent_alloc,
  311. };
  312. static int
  313. nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
  314. {
  315. struct ent ent, *res;
  316. char *buf1;
  317. int error = -EINVAL;
  318. if (buf[buflen - 1] != '\n')
  319. return (-EINVAL);
  320. buf[buflen - 1]= '\0';
  321. buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
  322. if (buf1 == NULL)
  323. return (-ENOMEM);
  324. memset(&ent, 0, sizeof(ent));
  325. /* Authentication name */
  326. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  327. goto out;
  328. memcpy(ent.authname, buf1, sizeof(ent.authname));
  329. /* Type */
  330. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  331. goto out;
  332. ent.type = strcmp(buf1, "user") == 0 ?
  333. IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
  334. /* Name */
  335. error = qword_get(&buf, buf1, PAGE_SIZE);
  336. if (error <= 0 || error >= IDMAP_NAMESZ)
  337. goto out;
  338. memcpy(ent.name, buf1, sizeof(ent.name));
  339. /* expiry */
  340. ent.h.expiry_time = get_expiry(&buf);
  341. if (ent.h.expiry_time == 0)
  342. goto out;
  343. /* ID */
  344. error = get_int(&buf, &ent.id);
  345. if (error == -EINVAL)
  346. goto out;
  347. if (error == -ENOENT)
  348. set_bit(CACHE_NEGATIVE, &ent.h.flags);
  349. error = -ENOMEM;
  350. res = nametoid_lookup(&ent);
  351. if (res == NULL)
  352. goto out;
  353. res = nametoid_update(&ent, res);
  354. if (res == NULL)
  355. goto out;
  356. cache_put(&res->h, &nametoid_cache);
  357. error = 0;
  358. out:
  359. kfree(buf1);
  360. return (error);
  361. }
  362. static struct ent *
  363. nametoid_lookup(struct ent *item)
  364. {
  365. struct cache_head *ch = sunrpc_cache_lookup(&nametoid_cache,
  366. &item->h,
  367. nametoid_hash(item));
  368. if (ch)
  369. return container_of(ch, struct ent, h);
  370. else
  371. return NULL;
  372. }
  373. static struct ent *
  374. nametoid_update(struct ent *new, struct ent *old)
  375. {
  376. struct cache_head *ch = sunrpc_cache_update(&nametoid_cache,
  377. &new->h, &old->h,
  378. nametoid_hash(new));
  379. if (ch)
  380. return container_of(ch, struct ent, h);
  381. else
  382. return NULL;
  383. }
  384. /*
  385. * Exported API
  386. */
  387. int
  388. nfsd_idmap_init(void)
  389. {
  390. int rv;
  391. rv = cache_register(&idtoname_cache);
  392. if (rv)
  393. return rv;
  394. rv = cache_register(&nametoid_cache);
  395. if (rv)
  396. cache_unregister(&idtoname_cache);
  397. return rv;
  398. }
  399. void
  400. nfsd_idmap_shutdown(void)
  401. {
  402. cache_unregister(&idtoname_cache);
  403. cache_unregister(&nametoid_cache);
  404. }
  405. static int
  406. idmap_lookup(struct svc_rqst *rqstp,
  407. struct ent *(*lookup_fn)(struct ent *), struct ent *key,
  408. struct cache_detail *detail, struct ent **item)
  409. {
  410. int ret;
  411. *item = lookup_fn(key);
  412. if (!*item)
  413. return -ENOMEM;
  414. retry:
  415. ret = cache_check(detail, &(*item)->h, &rqstp->rq_chandle);
  416. if (ret == -ETIMEDOUT) {
  417. struct ent *prev_item = *item;
  418. *item = lookup_fn(key);
  419. if (*item != prev_item)
  420. goto retry;
  421. cache_put(&(*item)->h, detail);
  422. }
  423. return ret;
  424. }
  425. static char *
  426. rqst_authname(struct svc_rqst *rqstp)
  427. {
  428. struct auth_domain *clp;
  429. clp = rqstp->rq_gssclient ? rqstp->rq_gssclient : rqstp->rq_client;
  430. return clp->name;
  431. }
  432. static __be32
  433. idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
  434. uid_t *id)
  435. {
  436. struct ent *item, key = {
  437. .type = type,
  438. };
  439. int ret;
  440. if (namelen + 1 > sizeof(key.name))
  441. return nfserr_badowner;
  442. memcpy(key.name, name, namelen);
  443. key.name[namelen] = '\0';
  444. strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
  445. ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item);
  446. if (ret == -ENOENT)
  447. return nfserr_badowner;
  448. if (ret)
  449. return nfserrno(ret);
  450. *id = item->id;
  451. cache_put(&item->h, &nametoid_cache);
  452. return 0;
  453. }
  454. static int
  455. idmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name)
  456. {
  457. struct ent *item, key = {
  458. .id = id,
  459. .type = type,
  460. };
  461. int ret;
  462. strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
  463. ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item);
  464. if (ret == -ENOENT)
  465. return sprintf(name, "%u", id);
  466. if (ret)
  467. return ret;
  468. ret = strlen(item->name);
  469. BUG_ON(ret > IDMAP_NAMESZ);
  470. memcpy(name, item->name, ret);
  471. cache_put(&item->h, &idtoname_cache);
  472. return ret;
  473. }
  474. __be32
  475. nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
  476. __u32 *id)
  477. {
  478. return idmap_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id);
  479. }
  480. __be32
  481. nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
  482. __u32 *id)
  483. {
  484. return idmap_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id);
  485. }
  486. int
  487. nfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
  488. {
  489. return idmap_id_to_name(rqstp, IDMAP_TYPE_USER, id, name);
  490. }
  491. int
  492. nfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
  493. {
  494. return idmap_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name);
  495. }