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/init.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/sunrpc/cache.h>
49 #include <linux/nfsd_idmap.h>
50 #include <linux/list.h>
51 #include <linux/time.h>
52 #include <linux/seq_file.h>
53 #include <linux/sunrpc/svcauth.h>
60 * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
64 #define IDMAP_TYPE_USER 0
65 #define IDMAP_TYPE_GROUP 1
69 int type; /* User / Group */
71 char name[IDMAP_NAMESZ];
72 char authname[IDMAP_NAMESZ];
75 /* Common entry handling */
77 #define ENT_HASHBITS 8
78 #define ENT_HASHMAX (1 << ENT_HASHBITS)
79 #define ENT_HASHMASK (ENT_HASHMAX - 1)
82 ent_init(struct cache_head *cnew, struct cache_head *citm)
84 struct ent *new = container_of(cnew, struct ent, h);
85 struct ent *itm = container_of(citm, struct ent, h);
88 new->type = itm->type;
90 strlcpy(new->name, itm->name, sizeof(new->name));
91 strlcpy(new->authname, itm->authname, sizeof(new->name));
95 ent_put(struct kref *ref)
97 struct ent *map = container_of(ref, struct ent, h.ref);
101 static struct cache_head *
104 struct ent *e = kmalloc(sizeof(*e), GFP_KERNEL);
115 static struct cache_head *idtoname_table[ENT_HASHMAX];
118 idtoname_hash(struct ent *ent)
122 hash = hash_str(ent->authname, ENT_HASHBITS);
123 hash = hash_long(hash ^ ent->id, ENT_HASHBITS);
125 /* Flip LSB for user/group */
126 if (ent->type == IDMAP_TYPE_GROUP)
133 idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
136 struct ent *ent = container_of(ch, struct ent, h);
139 qword_add(bpp, blen, ent->authname);
140 snprintf(idstr, sizeof(idstr), "%u", ent->id);
141 qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
142 qword_add(bpp, blen, idstr);
148 idtoname_upcall(struct cache_detail *cd, struct cache_head *ch)
150 return sunrpc_cache_pipe_upcall(cd, ch, idtoname_request);
154 idtoname_match(struct cache_head *ca, struct cache_head *cb)
156 struct ent *a = container_of(ca, struct ent, h);
157 struct ent *b = container_of(cb, struct ent, h);
159 return (a->id == b->id && a->type == b->type &&
160 strcmp(a->authname, b->authname) == 0);
164 idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
169 seq_puts(m, "#domain type id [name]\n");
172 ent = container_of(h, struct ent, h);
173 seq_printf(m, "%s %s %u", ent->authname,
174 ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
176 if (test_bit(CACHE_VALID, &h->flags))
177 seq_printf(m, " %s", ent->name);
183 warn_no_idmapd(struct cache_detail *detail, int has_died)
185 printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
186 has_died ? "died" : "not been started");
190 static int idtoname_parse(struct cache_detail *, char *, int);
191 static struct ent *idtoname_lookup(struct ent *);
192 static struct ent *idtoname_update(struct ent *, struct ent *);
194 static struct cache_detail idtoname_cache = {
195 .owner = THIS_MODULE,
196 .hash_size = ENT_HASHMAX,
197 .hash_table = idtoname_table,
198 .name = "nfs4.idtoname",
199 .cache_put = ent_put,
200 .cache_upcall = idtoname_upcall,
201 .cache_parse = idtoname_parse,
202 .cache_show = idtoname_show,
203 .warn_no_listener = warn_no_idmapd,
204 .match = idtoname_match,
211 idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
213 struct ent ent, *res;
218 if (buf[buflen - 1] != '\n')
220 buf[buflen - 1]= '\0';
222 buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
226 memset(&ent, 0, sizeof(ent));
228 /* Authentication name */
229 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
231 memcpy(ent.authname, buf1, sizeof(ent.authname));
234 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
236 ent.type = strcmp(buf1, "user") == 0 ?
237 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
240 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
242 ent.id = simple_strtoul(buf1, &bp, 10);
247 ent.h.expiry_time = get_expiry(&buf);
248 if (ent.h.expiry_time == 0)
252 res = idtoname_lookup(&ent);
258 len = qword_get(&buf, buf1, PAGE_SIZE);
262 set_bit(CACHE_NEGATIVE, &ent.h.flags);
263 else if (len >= IDMAP_NAMESZ)
266 memcpy(ent.name, buf1, sizeof(ent.name));
268 res = idtoname_update(&ent, res);
272 cache_put(&res->h, &idtoname_cache);
283 idtoname_lookup(struct ent *item)
285 struct cache_head *ch = sunrpc_cache_lookup(&idtoname_cache,
287 idtoname_hash(item));
289 return container_of(ch, struct ent, h);
295 idtoname_update(struct ent *new, struct ent *old)
297 struct cache_head *ch = sunrpc_cache_update(&idtoname_cache,
301 return container_of(ch, struct ent, h);
311 static struct cache_head *nametoid_table[ENT_HASHMAX];
314 nametoid_hash(struct ent *ent)
316 return hash_str(ent->name, ENT_HASHBITS);
320 nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
323 struct ent *ent = container_of(ch, struct ent, h);
325 qword_add(bpp, blen, ent->authname);
326 qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
327 qword_add(bpp, blen, ent->name);
333 nametoid_upcall(struct cache_detail *cd, struct cache_head *ch)
335 return sunrpc_cache_pipe_upcall(cd, ch, nametoid_request);
339 nametoid_match(struct cache_head *ca, struct cache_head *cb)
341 struct ent *a = container_of(ca, struct ent, h);
342 struct ent *b = container_of(cb, struct ent, h);
344 return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
345 strcmp(a->authname, b->authname) == 0);
349 nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
354 seq_puts(m, "#domain type name [id]\n");
357 ent = container_of(h, struct ent, h);
358 seq_printf(m, "%s %s %s", ent->authname,
359 ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
361 if (test_bit(CACHE_VALID, &h->flags))
362 seq_printf(m, " %u", ent->id);
367 static struct ent *nametoid_lookup(struct ent *);
368 static struct ent *nametoid_update(struct ent *, struct ent *);
369 static int nametoid_parse(struct cache_detail *, char *, int);
371 static struct cache_detail nametoid_cache = {
372 .owner = THIS_MODULE,
373 .hash_size = ENT_HASHMAX,
374 .hash_table = nametoid_table,
375 .name = "nfs4.nametoid",
376 .cache_put = ent_put,
377 .cache_upcall = nametoid_upcall,
378 .cache_parse = nametoid_parse,
379 .cache_show = nametoid_show,
380 .warn_no_listener = warn_no_idmapd,
381 .match = nametoid_match,
388 nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
390 struct ent ent, *res;
394 if (buf[buflen - 1] != '\n')
396 buf[buflen - 1]= '\0';
398 buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
402 memset(&ent, 0, sizeof(ent));
404 /* Authentication name */
405 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
407 memcpy(ent.authname, buf1, sizeof(ent.authname));
410 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
412 ent.type = strcmp(buf1, "user") == 0 ?
413 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
416 error = qword_get(&buf, buf1, PAGE_SIZE);
417 if (error <= 0 || error >= IDMAP_NAMESZ)
419 memcpy(ent.name, buf1, sizeof(ent.name));
422 ent.h.expiry_time = get_expiry(&buf);
423 if (ent.h.expiry_time == 0)
427 error = get_int(&buf, &ent.id);
428 if (error == -EINVAL)
430 if (error == -ENOENT)
431 set_bit(CACHE_NEGATIVE, &ent.h.flags);
434 res = nametoid_lookup(&ent);
437 res = nametoid_update(&ent, res);
441 cache_put(&res->h, &nametoid_cache);
451 nametoid_lookup(struct ent *item)
453 struct cache_head *ch = sunrpc_cache_lookup(&nametoid_cache,
455 nametoid_hash(item));
457 return container_of(ch, struct ent, h);
463 nametoid_update(struct ent *new, struct ent *old)
465 struct cache_head *ch = sunrpc_cache_update(&nametoid_cache,
469 return container_of(ch, struct ent, h);
479 nfsd_idmap_init(void)
483 rv = cache_register(&idtoname_cache);
486 rv = cache_register(&nametoid_cache);
488 cache_unregister(&idtoname_cache);
493 nfsd_idmap_shutdown(void)
495 cache_unregister(&idtoname_cache);
496 cache_unregister(&nametoid_cache);
500 * Deferred request handling
503 struct idmap_defer_req {
504 struct cache_req req;
505 struct cache_deferred_req deferred_req;
506 wait_queue_head_t waitq;
511 put_mdr(struct idmap_defer_req *mdr)
513 if (atomic_dec_and_test(&mdr->count))
518 get_mdr(struct idmap_defer_req *mdr)
520 atomic_inc(&mdr->count);
524 idmap_revisit(struct cache_deferred_req *dreq, int toomany)
526 struct idmap_defer_req *mdr =
527 container_of(dreq, struct idmap_defer_req, deferred_req);
529 wake_up(&mdr->waitq);
533 static struct cache_deferred_req *
534 idmap_defer(struct cache_req *req)
536 struct idmap_defer_req *mdr =
537 container_of(req, struct idmap_defer_req, req);
539 mdr->deferred_req.revisit = idmap_revisit;
541 return (&mdr->deferred_req);
545 do_idmap_lookup(struct ent *(*lookup_fn)(struct ent *), struct ent *key,
546 struct cache_detail *detail, struct ent **item,
547 struct idmap_defer_req *mdr)
549 *item = lookup_fn(key);
552 return cache_check(detail, &(*item)->h, &mdr->req);
556 do_idmap_lookup_nowait(struct ent *(*lookup_fn)(struct ent *),
557 struct ent *key, struct cache_detail *detail,
562 *item = lookup_fn(key);
566 if (!test_bit(CACHE_VALID, &(*item)->h.flags)
567 || (*item)->h.expiry_time < get_seconds()
568 || detail->flush_time > (*item)->h.last_refresh)
571 if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
575 cache_put(&(*item)->h, detail);
582 idmap_lookup(struct svc_rqst *rqstp,
583 struct ent *(*lookup_fn)(struct ent *), struct ent *key,
584 struct cache_detail *detail, struct ent **item)
586 struct idmap_defer_req *mdr;
589 mdr = kzalloc(sizeof(*mdr), GFP_KERNEL);
592 atomic_set(&mdr->count, 1);
593 init_waitqueue_head(&mdr->waitq);
594 mdr->req.defer = idmap_defer;
595 ret = do_idmap_lookup(lookup_fn, key, detail, item, mdr);
596 if (ret == -EAGAIN) {
597 wait_event_interruptible_timeout(mdr->waitq,
598 test_bit(CACHE_VALID, &(*item)->h.flags), 1 * HZ);
599 ret = do_idmap_lookup_nowait(lookup_fn, key, detail, item);
606 rqst_authname(struct svc_rqst *rqstp)
608 struct auth_domain *clp;
610 clp = rqstp->rq_gssclient ? rqstp->rq_gssclient : rqstp->rq_client;
615 idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
618 struct ent *item, key = {
623 if (namelen + 1 > sizeof(key.name))
625 memcpy(key.name, name, namelen);
626 key.name[namelen] = '\0';
627 strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
628 ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item);
630 ret = -ESRCH; /* nfserr_badname */
634 cache_put(&item->h, &nametoid_cache);
639 idmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name)
641 struct ent *item, key = {
647 strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
648 ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item);
650 return sprintf(name, "%u", id);
653 ret = strlen(item->name);
654 BUG_ON(ret > IDMAP_NAMESZ);
655 memcpy(name, item->name, ret);
656 cache_put(&item->h, &idtoname_cache);
661 nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
664 return idmap_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id);
668 nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
671 return idmap_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id);
675 nfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
677 return idmap_id_to_name(rqstp, IDMAP_TYPE_USER, id, name);
681 nfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
683 return idmap_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name);