4 * Mapping of UID/GIDs to name and vice versa.
6 * Copyright (c) 2002, 2003 The Regents of the University of
7 * Michigan. All rights reserved.
9 * Marius Aamodt Eriksen <marius@umich.edu>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
37 #include <linux/module.h>
38 #include <linux/nfsd_idmap.h>
39 #include <linux/seq_file.h>
40 #include <linux/sched.h>
47 * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
51 #define IDMAP_TYPE_USER 0
52 #define IDMAP_TYPE_GROUP 1
56 int type; /* User / Group */
58 char name[IDMAP_NAMESZ];
59 char authname[IDMAP_NAMESZ];
62 /* Common entry handling */
64 #define ENT_HASHBITS 8
65 #define ENT_HASHMAX (1 << ENT_HASHBITS)
66 #define ENT_HASHMASK (ENT_HASHMAX - 1)
69 ent_init(struct cache_head *cnew, struct cache_head *citm)
71 struct ent *new = container_of(cnew, struct ent, h);
72 struct ent *itm = container_of(citm, struct ent, h);
75 new->type = itm->type;
77 strlcpy(new->name, itm->name, sizeof(new->name));
78 strlcpy(new->authname, itm->authname, sizeof(new->name));
82 ent_put(struct kref *ref)
84 struct ent *map = container_of(ref, struct ent, h.ref);
88 static struct cache_head *
91 struct ent *e = kmalloc(sizeof(*e), GFP_KERNEL);
102 static struct cache_head *idtoname_table[ENT_HASHMAX];
105 idtoname_hash(struct ent *ent)
109 hash = hash_str(ent->authname, ENT_HASHBITS);
110 hash = hash_long(hash ^ ent->id, ENT_HASHBITS);
112 /* Flip LSB for user/group */
113 if (ent->type == IDMAP_TYPE_GROUP)
120 idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
123 struct ent *ent = container_of(ch, struct ent, h);
126 qword_add(bpp, blen, ent->authname);
127 snprintf(idstr, sizeof(idstr), "%u", ent->id);
128 qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
129 qword_add(bpp, blen, idstr);
135 idtoname_upcall(struct cache_detail *cd, struct cache_head *ch)
137 return sunrpc_cache_pipe_upcall(cd, ch, idtoname_request);
141 idtoname_match(struct cache_head *ca, struct cache_head *cb)
143 struct ent *a = container_of(ca, struct ent, h);
144 struct ent *b = container_of(cb, struct ent, h);
146 return (a->id == b->id && a->type == b->type &&
147 strcmp(a->authname, b->authname) == 0);
151 idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
156 seq_puts(m, "#domain type id [name]\n");
159 ent = container_of(h, struct ent, h);
160 seq_printf(m, "%s %s %u", ent->authname,
161 ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
163 if (test_bit(CACHE_VALID, &h->flags))
164 seq_printf(m, " %s", ent->name);
170 warn_no_idmapd(struct cache_detail *detail, int has_died)
172 printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
173 has_died ? "died" : "not been started");
177 static int idtoname_parse(struct cache_detail *, char *, int);
178 static struct ent *idtoname_lookup(struct ent *);
179 static struct ent *idtoname_update(struct ent *, struct ent *);
181 static struct cache_detail idtoname_cache = {
182 .owner = THIS_MODULE,
183 .hash_size = ENT_HASHMAX,
184 .hash_table = idtoname_table,
185 .name = "nfs4.idtoname",
186 .cache_put = ent_put,
187 .cache_upcall = idtoname_upcall,
188 .cache_parse = idtoname_parse,
189 .cache_show = idtoname_show,
190 .warn_no_listener = warn_no_idmapd,
191 .match = idtoname_match,
198 idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
200 struct ent ent, *res;
205 if (buf[buflen - 1] != '\n')
207 buf[buflen - 1]= '\0';
209 buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
213 memset(&ent, 0, sizeof(ent));
215 /* Authentication name */
216 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
218 memcpy(ent.authname, buf1, sizeof(ent.authname));
221 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
223 ent.type = strcmp(buf1, "user") == 0 ?
224 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
227 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
229 ent.id = simple_strtoul(buf1, &bp, 10);
234 ent.h.expiry_time = get_expiry(&buf);
235 if (ent.h.expiry_time == 0)
239 res = idtoname_lookup(&ent);
245 len = qword_get(&buf, buf1, PAGE_SIZE);
249 set_bit(CACHE_NEGATIVE, &ent.h.flags);
250 else if (len >= IDMAP_NAMESZ)
253 memcpy(ent.name, buf1, sizeof(ent.name));
255 res = idtoname_update(&ent, res);
259 cache_put(&res->h, &idtoname_cache);
270 idtoname_lookup(struct ent *item)
272 struct cache_head *ch = sunrpc_cache_lookup(&idtoname_cache,
274 idtoname_hash(item));
276 return container_of(ch, struct ent, h);
282 idtoname_update(struct ent *new, struct ent *old)
284 struct cache_head *ch = sunrpc_cache_update(&idtoname_cache,
288 return container_of(ch, struct ent, h);
298 static struct cache_head *nametoid_table[ENT_HASHMAX];
301 nametoid_hash(struct ent *ent)
303 return hash_str(ent->name, ENT_HASHBITS);
307 nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
310 struct ent *ent = container_of(ch, struct ent, h);
312 qword_add(bpp, blen, ent->authname);
313 qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
314 qword_add(bpp, blen, ent->name);
320 nametoid_upcall(struct cache_detail *cd, struct cache_head *ch)
322 return sunrpc_cache_pipe_upcall(cd, ch, nametoid_request);
326 nametoid_match(struct cache_head *ca, struct cache_head *cb)
328 struct ent *a = container_of(ca, struct ent, h);
329 struct ent *b = container_of(cb, struct ent, h);
331 return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
332 strcmp(a->authname, b->authname) == 0);
336 nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
341 seq_puts(m, "#domain type name [id]\n");
344 ent = container_of(h, struct ent, h);
345 seq_printf(m, "%s %s %s", ent->authname,
346 ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
348 if (test_bit(CACHE_VALID, &h->flags))
349 seq_printf(m, " %u", ent->id);
354 static struct ent *nametoid_lookup(struct ent *);
355 static struct ent *nametoid_update(struct ent *, struct ent *);
356 static int nametoid_parse(struct cache_detail *, char *, int);
358 static struct cache_detail nametoid_cache = {
359 .owner = THIS_MODULE,
360 .hash_size = ENT_HASHMAX,
361 .hash_table = nametoid_table,
362 .name = "nfs4.nametoid",
363 .cache_put = ent_put,
364 .cache_upcall = nametoid_upcall,
365 .cache_parse = nametoid_parse,
366 .cache_show = nametoid_show,
367 .warn_no_listener = warn_no_idmapd,
368 .match = nametoid_match,
375 nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
377 struct ent ent, *res;
381 if (buf[buflen - 1] != '\n')
383 buf[buflen - 1]= '\0';
385 buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
389 memset(&ent, 0, sizeof(ent));
391 /* Authentication name */
392 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
394 memcpy(ent.authname, buf1, sizeof(ent.authname));
397 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
399 ent.type = strcmp(buf1, "user") == 0 ?
400 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
403 error = qword_get(&buf, buf1, PAGE_SIZE);
404 if (error <= 0 || error >= IDMAP_NAMESZ)
406 memcpy(ent.name, buf1, sizeof(ent.name));
409 ent.h.expiry_time = get_expiry(&buf);
410 if (ent.h.expiry_time == 0)
414 error = get_int(&buf, &ent.id);
415 if (error == -EINVAL)
417 if (error == -ENOENT)
418 set_bit(CACHE_NEGATIVE, &ent.h.flags);
421 res = nametoid_lookup(&ent);
424 res = nametoid_update(&ent, res);
428 cache_put(&res->h, &nametoid_cache);
438 nametoid_lookup(struct ent *item)
440 struct cache_head *ch = sunrpc_cache_lookup(&nametoid_cache,
442 nametoid_hash(item));
444 return container_of(ch, struct ent, h);
450 nametoid_update(struct ent *new, struct ent *old)
452 struct cache_head *ch = sunrpc_cache_update(&nametoid_cache,
456 return container_of(ch, struct ent, h);
466 nfsd_idmap_init(void)
470 rv = cache_register(&idtoname_cache);
473 rv = cache_register(&nametoid_cache);
475 cache_unregister(&idtoname_cache);
480 nfsd_idmap_shutdown(void)
482 cache_unregister(&idtoname_cache);
483 cache_unregister(&nametoid_cache);
487 * Deferred request handling
490 struct idmap_defer_req {
491 struct cache_req req;
492 struct cache_deferred_req deferred_req;
493 wait_queue_head_t waitq;
498 put_mdr(struct idmap_defer_req *mdr)
500 if (atomic_dec_and_test(&mdr->count))
505 get_mdr(struct idmap_defer_req *mdr)
507 atomic_inc(&mdr->count);
511 idmap_revisit(struct cache_deferred_req *dreq, int toomany)
513 struct idmap_defer_req *mdr =
514 container_of(dreq, struct idmap_defer_req, deferred_req);
516 wake_up(&mdr->waitq);
520 static struct cache_deferred_req *
521 idmap_defer(struct cache_req *req)
523 struct idmap_defer_req *mdr =
524 container_of(req, struct idmap_defer_req, req);
526 mdr->deferred_req.revisit = idmap_revisit;
528 return (&mdr->deferred_req);
532 do_idmap_lookup(struct ent *(*lookup_fn)(struct ent *), struct ent *key,
533 struct cache_detail *detail, struct ent **item,
534 struct idmap_defer_req *mdr)
536 *item = lookup_fn(key);
539 return cache_check(detail, &(*item)->h, &mdr->req);
543 do_idmap_lookup_nowait(struct ent *(*lookup_fn)(struct ent *),
544 struct ent *key, struct cache_detail *detail,
549 *item = lookup_fn(key);
553 if (!test_bit(CACHE_VALID, &(*item)->h.flags)
554 || (*item)->h.expiry_time < get_seconds()
555 || detail->flush_time > (*item)->h.last_refresh)
558 if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
562 cache_put(&(*item)->h, detail);
569 idmap_lookup(struct svc_rqst *rqstp,
570 struct ent *(*lookup_fn)(struct ent *), struct ent *key,
571 struct cache_detail *detail, struct ent **item)
573 struct idmap_defer_req *mdr;
576 mdr = kzalloc(sizeof(*mdr), GFP_KERNEL);
579 atomic_set(&mdr->count, 1);
580 init_waitqueue_head(&mdr->waitq);
581 mdr->req.defer = idmap_defer;
582 ret = do_idmap_lookup(lookup_fn, key, detail, item, mdr);
583 if (ret == -EAGAIN) {
584 wait_event_interruptible_timeout(mdr->waitq,
585 test_bit(CACHE_VALID, &(*item)->h.flags), 1 * HZ);
586 ret = do_idmap_lookup_nowait(lookup_fn, key, detail, item);
593 rqst_authname(struct svc_rqst *rqstp)
595 struct auth_domain *clp;
597 clp = rqstp->rq_gssclient ? rqstp->rq_gssclient : rqstp->rq_client;
602 idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
605 struct ent *item, key = {
610 if (namelen + 1 > sizeof(key.name))
612 memcpy(key.name, name, namelen);
613 key.name[namelen] = '\0';
614 strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
615 ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item);
617 ret = -ESRCH; /* nfserr_badname */
621 cache_put(&item->h, &nametoid_cache);
626 idmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name)
628 struct ent *item, key = {
634 strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
635 ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item);
637 return sprintf(name, "%u", id);
640 ret = strlen(item->name);
641 BUG_ON(ret > IDMAP_NAMESZ);
642 memcpy(name, item->name, ret);
643 cache_put(&item->h, &idtoname_cache);
648 nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
651 return idmap_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id);
655 nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
658 return idmap_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id);
662 nfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
664 return idmap_id_to_name(rqstp, IDMAP_TYPE_USER, id, name);
668 nfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
670 return idmap_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name);