nfs4idmap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 <net/net_namespace.h>
  39. #include "idmap.h"
  40. #include "nfsd.h"
  41. /*
  42. * Turn off idmapping when using AUTH_SYS.
  43. */
  44. static bool nfs4_disable_idmapping = true;
  45. module_param(nfs4_disable_idmapping, bool, 0644);
  46. MODULE_PARM_DESC(nfs4_disable_idmapping,
  47. "Turn off server's NFSv4 idmapping when using 'sec=sys'");
  48. /*
  49. * Cache entry
  50. */
  51. /*
  52. * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
  53. * that.
  54. */
  55. #define IDMAP_TYPE_USER 0
  56. #define IDMAP_TYPE_GROUP 1
  57. struct ent {
  58. struct cache_head h;
  59. int type; /* User / Group */
  60. uid_t id;
  61. char name[IDMAP_NAMESZ];
  62. char authname[IDMAP_NAMESZ];
  63. };
  64. /* Common entry handling */
  65. #define ENT_HASHBITS 8
  66. #define ENT_HASHMAX (1 << ENT_HASHBITS)
  67. static void
  68. ent_init(struct cache_head *cnew, struct cache_head *citm)
  69. {
  70. struct ent *new = container_of(cnew, struct ent, h);
  71. struct ent *itm = container_of(citm, struct ent, h);
  72. new->id = itm->id;
  73. new->type = itm->type;
  74. strlcpy(new->name, itm->name, sizeof(new->name));
  75. strlcpy(new->authname, itm->authname, sizeof(new->name));
  76. }
  77. static void
  78. ent_put(struct kref *ref)
  79. {
  80. struct ent *map = container_of(ref, struct ent, h.ref);
  81. kfree(map);
  82. }
  83. static struct cache_head *
  84. ent_alloc(void)
  85. {
  86. struct ent *e = kmalloc(sizeof(*e), GFP_KERNEL);
  87. if (e)
  88. return &e->h;
  89. else
  90. return NULL;
  91. }
  92. /*
  93. * ID -> Name cache
  94. */
  95. static struct cache_head *idtoname_table[ENT_HASHMAX];
  96. static uint32_t
  97. idtoname_hash(struct ent *ent)
  98. {
  99. uint32_t hash;
  100. hash = hash_str(ent->authname, ENT_HASHBITS);
  101. hash = hash_long(hash ^ ent->id, ENT_HASHBITS);
  102. /* Flip LSB for user/group */
  103. if (ent->type == IDMAP_TYPE_GROUP)
  104. hash ^= 1;
  105. return hash;
  106. }
  107. static void
  108. idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
  109. int *blen)
  110. {
  111. struct ent *ent = container_of(ch, struct ent, h);
  112. char idstr[11];
  113. qword_add(bpp, blen, ent->authname);
  114. snprintf(idstr, sizeof(idstr), "%u", ent->id);
  115. qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
  116. qword_add(bpp, blen, idstr);
  117. (*bpp)[-1] = '\n';
  118. }
  119. static int
  120. idtoname_upcall(struct cache_detail *cd, struct cache_head *ch)
  121. {
  122. return sunrpc_cache_pipe_upcall(cd, ch, idtoname_request);
  123. }
  124. static int
  125. idtoname_match(struct cache_head *ca, struct cache_head *cb)
  126. {
  127. struct ent *a = container_of(ca, struct ent, h);
  128. struct ent *b = container_of(cb, struct ent, h);
  129. return (a->id == b->id && a->type == b->type &&
  130. strcmp(a->authname, b->authname) == 0);
  131. }
  132. static int
  133. idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
  134. {
  135. struct ent *ent;
  136. if (h == NULL) {
  137. seq_puts(m, "#domain type id [name]\n");
  138. return 0;
  139. }
  140. ent = container_of(h, struct ent, h);
  141. seq_printf(m, "%s %s %u", ent->authname,
  142. ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
  143. ent->id);
  144. if (test_bit(CACHE_VALID, &h->flags))
  145. seq_printf(m, " %s", ent->name);
  146. seq_printf(m, "\n");
  147. return 0;
  148. }
  149. static void
  150. warn_no_idmapd(struct cache_detail *detail, int has_died)
  151. {
  152. printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
  153. has_died ? "died" : "not been started");
  154. }
  155. static int idtoname_parse(struct cache_detail *, char *, int);
  156. static struct ent *idtoname_lookup(struct ent *);
  157. static struct ent *idtoname_update(struct ent *, struct ent *);
  158. static struct cache_detail idtoname_cache = {
  159. .owner = THIS_MODULE,
  160. .hash_size = ENT_HASHMAX,
  161. .hash_table = idtoname_table,
  162. .name = "nfs4.idtoname",
  163. .cache_put = ent_put,
  164. .cache_upcall = idtoname_upcall,
  165. .cache_parse = idtoname_parse,
  166. .cache_show = idtoname_show,
  167. .warn_no_listener = warn_no_idmapd,
  168. .match = idtoname_match,
  169. .init = ent_init,
  170. .update = ent_init,
  171. .alloc = ent_alloc,
  172. };
  173. static int
  174. idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
  175. {
  176. struct ent ent, *res;
  177. char *buf1, *bp;
  178. int len;
  179. int error = -EINVAL;
  180. if (buf[buflen - 1] != '\n')
  181. return (-EINVAL);
  182. buf[buflen - 1]= '\0';
  183. buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
  184. if (buf1 == NULL)
  185. return (-ENOMEM);
  186. memset(&ent, 0, sizeof(ent));
  187. /* Authentication name */
  188. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  189. goto out;
  190. memcpy(ent.authname, buf1, sizeof(ent.authname));
  191. /* Type */
  192. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  193. goto out;
  194. ent.type = strcmp(buf1, "user") == 0 ?
  195. IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
  196. /* ID */
  197. if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  198. goto out;
  199. ent.id = simple_strtoul(buf1, &bp, 10);
  200. if (bp == buf1)
  201. goto out;
  202. /* expiry */
  203. ent.h.expiry_time = get_expiry(&buf);
  204. if (ent.h.expiry_time == 0)
  205. goto out;
  206. error = -ENOMEM;
  207. res = idtoname_lookup(&ent);
  208. if (!res)
  209. goto out;
  210. /* Name */
  211. error = -EINVAL;
  212. len = qword_get(&buf, buf1, PAGE_SIZE);
  213. if (len < 0)
  214. goto out;
  215. if (len == 0)
  216. set_bit(CACHE_NEGATIVE, &ent.h.flags);
  217. else if (len >= IDMAP_NAMESZ)
  218. goto out;
  219. else
  220. memcpy(ent.name, buf1, sizeof(ent.name));
  221. error = -ENOMEM;
  222. res = idtoname_update(&ent, res);
  223. if (res == NULL)
  224. goto out;
  225. cache_put(&res->h, &idtoname_cache);
  226. error = 0;
  227. out:
  228. kfree(buf1);
  229. return error;
  230. }
  231. static struct ent *
  232. idtoname_lookup(struct ent *item)
  233. {
  234. struct cache_head *ch = sunrpc_cache_lookup(&idtoname_cache,
  235. &item->h,
  236. idtoname_hash(item));
  237. if (ch)
  238. return container_of(ch, struct ent, h);
  239. else
  240. return NULL;
  241. }
  242. static struct ent *
  243. idtoname_update(struct ent *new, struct ent *old)
  244. {
  245. struct cache_head *ch = sunrpc_cache_update(&idtoname_cache,
  246. &new->h, &old->h,
  247. idtoname_hash(new));
  248. if (ch)
  249. return container_of(ch, struct ent, h);
  250. else
  251. return NULL;
  252. }
  253. /*
  254. * Name -> ID cache
  255. */
  256. static struct cache_head *nametoid_table[ENT_HASHMAX];
  257. static inline int
  258. nametoid_hash(struct ent *ent)
  259. {
  260. return hash_str(ent->name, ENT_HASHBITS);
  261. }
  262. static void
  263. nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
  264. int *blen)
  265. {
  266. struct ent *ent = container_of(ch, struct ent, h);
  267. qword_add(bpp, blen, ent->authname);
  268. qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
  269. qword_add(bpp, blen, ent->name);
  270. (*bpp)[-1] = '\n';
  271. }
  272. static int
  273. nametoid_upcall(struct cache_detail *cd, struct cache_head *ch)
  274. {
  275. return sunrpc_cache_pipe_upcall(cd, ch, nametoid_request);
  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, " %u", 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_upcall = nametoid_upcall,
  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. int
  396. nfsd_idmap_init(void)
  397. {
  398. int rv;
  399. rv = cache_register_net(&idtoname_cache, &init_net);
  400. if (rv)
  401. return rv;
  402. rv = cache_register_net(&nametoid_cache, &init_net);
  403. if (rv)
  404. cache_unregister_net(&idtoname_cache, &init_net);
  405. return rv;
  406. }
  407. void
  408. nfsd_idmap_shutdown(void)
  409. {
  410. cache_unregister_net(&idtoname_cache, &init_net);
  411. cache_unregister_net(&nametoid_cache, &init_net);
  412. }
  413. static int
  414. idmap_lookup(struct svc_rqst *rqstp,
  415. struct ent *(*lookup_fn)(struct ent *), struct ent *key,
  416. struct cache_detail *detail, struct ent **item)
  417. {
  418. int ret;
  419. *item = lookup_fn(key);
  420. if (!*item)
  421. return -ENOMEM;
  422. retry:
  423. ret = cache_check(detail, &(*item)->h, &rqstp->rq_chandle);
  424. if (ret == -ETIMEDOUT) {
  425. struct ent *prev_item = *item;
  426. *item = lookup_fn(key);
  427. if (*item != prev_item)
  428. goto retry;
  429. cache_put(&(*item)->h, detail);
  430. }
  431. return ret;
  432. }
  433. static char *
  434. rqst_authname(struct svc_rqst *rqstp)
  435. {
  436. struct auth_domain *clp;
  437. clp = rqstp->rq_gssclient ? rqstp->rq_gssclient : rqstp->rq_client;
  438. return clp->name;
  439. }
  440. static __be32
  441. idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
  442. uid_t *id)
  443. {
  444. struct ent *item, key = {
  445. .type = type,
  446. };
  447. int ret;
  448. if (namelen + 1 > sizeof(key.name))
  449. return nfserr_badowner;
  450. memcpy(key.name, name, namelen);
  451. key.name[namelen] = '\0';
  452. strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
  453. ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item);
  454. if (ret == -ENOENT)
  455. return nfserr_badowner;
  456. if (ret)
  457. return nfserrno(ret);
  458. *id = item->id;
  459. cache_put(&item->h, &nametoid_cache);
  460. return 0;
  461. }
  462. static int
  463. idmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name)
  464. {
  465. struct ent *item, key = {
  466. .id = id,
  467. .type = type,
  468. };
  469. int ret;
  470. strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
  471. ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item);
  472. if (ret == -ENOENT)
  473. return sprintf(name, "%u", id);
  474. if (ret)
  475. return ret;
  476. ret = strlen(item->name);
  477. BUG_ON(ret > IDMAP_NAMESZ);
  478. memcpy(name, item->name, ret);
  479. cache_put(&item->h, &idtoname_cache);
  480. return ret;
  481. }
  482. static bool
  483. numeric_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen, uid_t *id)
  484. {
  485. int ret;
  486. char buf[11];
  487. if (namelen + 1 > sizeof(buf))
  488. /* too long to represent a 32-bit id: */
  489. return false;
  490. /* Just to make sure it's null-terminated: */
  491. memcpy(buf, name, namelen);
  492. buf[namelen] = '\0';
  493. ret = kstrtouint(buf, 10, id);
  494. return ret == 0;
  495. }
  496. static __be32
  497. do_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen, uid_t *id)
  498. {
  499. if (nfs4_disable_idmapping && rqstp->rq_flavor < RPC_AUTH_GSS)
  500. if (numeric_name_to_id(rqstp, type, name, namelen, id))
  501. return 0;
  502. /*
  503. * otherwise, fall through and try idmapping, for
  504. * backwards compatibility with clients sending names:
  505. */
  506. return idmap_name_to_id(rqstp, type, name, namelen, id);
  507. }
  508. static int
  509. do_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name)
  510. {
  511. if (nfs4_disable_idmapping && rqstp->rq_flavor < RPC_AUTH_GSS)
  512. return sprintf(name, "%u", id);
  513. return idmap_id_to_name(rqstp, type, id, name);
  514. }
  515. __be32
  516. nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
  517. __u32 *id)
  518. {
  519. return do_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id);
  520. }
  521. __be32
  522. nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
  523. __u32 *id)
  524. {
  525. return do_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id);
  526. }
  527. int
  528. nfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
  529. {
  530. return do_id_to_name(rqstp, IDMAP_TYPE_USER, id, name);
  531. }
  532. int
  533. nfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
  534. {
  535. return do_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name);
  536. }