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