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/config.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
42 #include <linux/utsname.h>
43 #include <linux/errno.h>
44 #include <linux/string.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/smp_lock.h>
51 #include <linux/sunrpc/cache.h>
52 #include <linux/nfsd_idmap.h>
53 #include <linux/list.h>
54 #include <linux/sched.h>
55 #include <linux/time.h>
56 #include <linux/seq_file.h>
57 #include <linux/sunrpc/svcauth.h>
64 * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
68 #define IDMAP_TYPE_USER 0
69 #define IDMAP_TYPE_GROUP 1
73 int type
; /* User / Group */
75 char name
[IDMAP_NAMESZ
];
76 char authname
[IDMAP_NAMESZ
];
79 /* Common entry handling */
81 #define ENT_HASHBITS 8
82 #define ENT_HASHMAX (1 << ENT_HASHBITS)
83 #define ENT_HASHMASK (ENT_HASHMAX - 1)
86 ent_init(struct cache_head
*cnew
, struct cache_head
*citm
)
88 struct ent
*new = container_of(cnew
, struct ent
, h
);
89 struct ent
*itm
= container_of(citm
, struct ent
, h
);
92 new->type
= itm
->type
;
94 strlcpy(new->name
, itm
->name
, sizeof(new->name
));
95 strlcpy(new->authname
, itm
->authname
, sizeof(new->name
));
99 ent_put(struct kref
*ref
)
101 struct ent
*map
= container_of(ref
, struct ent
, h
.ref
);
105 static struct cache_head
*
108 struct ent
*e
= kmalloc(sizeof(*e
), GFP_KERNEL
);
119 static struct cache_head
*idtoname_table
[ENT_HASHMAX
];
122 idtoname_hash(struct ent
*ent
)
126 hash
= hash_str(ent
->authname
, ENT_HASHBITS
);
127 hash
= hash_long(hash
^ ent
->id
, ENT_HASHBITS
);
129 /* Flip LSB for user/group */
130 if (ent
->type
== IDMAP_TYPE_GROUP
)
137 idtoname_request(struct cache_detail
*cd
, struct cache_head
*ch
, char **bpp
,
140 struct ent
*ent
= container_of(ch
, struct ent
, h
);
143 qword_add(bpp
, blen
, ent
->authname
);
144 snprintf(idstr
, sizeof(idstr
), "%d", ent
->id
);
145 qword_add(bpp
, blen
, ent
->type
== IDMAP_TYPE_GROUP
? "group" : "user");
146 qword_add(bpp
, blen
, idstr
);
152 idtoname_match(struct cache_head
*ca
, struct cache_head
*cb
)
154 struct ent
*a
= container_of(ca
, struct ent
, h
);
155 struct ent
*b
= container_of(cb
, struct ent
, h
);
157 return (a
->id
== b
->id
&& a
->type
== b
->type
&&
158 strcmp(a
->authname
, b
->authname
) == 0);
162 idtoname_show(struct seq_file
*m
, struct cache_detail
*cd
, struct cache_head
*h
)
167 seq_puts(m
, "#domain type id [name]\n");
170 ent
= container_of(h
, struct ent
, h
);
171 seq_printf(m
, "%s %s %d", ent
->authname
,
172 ent
->type
== IDMAP_TYPE_GROUP
? "group" : "user",
174 if (test_bit(CACHE_VALID
, &h
->flags
))
175 seq_printf(m
, " %s", ent
->name
);
181 warn_no_idmapd(struct cache_detail
*detail
)
183 printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
184 detail
->last_close
? "died" : "not been started");
188 static int idtoname_parse(struct cache_detail
*, char *, int);
189 static struct ent
*idtoname_lookup(struct ent
*);
190 static struct ent
*idtoname_update(struct ent
*, struct ent
*);
192 static struct cache_detail idtoname_cache
= {
193 .owner
= THIS_MODULE
,
194 .hash_size
= ENT_HASHMAX
,
195 .hash_table
= idtoname_table
,
196 .name
= "nfs4.idtoname",
197 .cache_put
= ent_put
,
198 .cache_request
= idtoname_request
,
199 .cache_parse
= idtoname_parse
,
200 .cache_show
= idtoname_show
,
201 .warn_no_listener
= warn_no_idmapd
,
202 .match
= idtoname_match
,
209 idtoname_parse(struct cache_detail
*cd
, char *buf
, int buflen
)
211 struct ent ent
, *res
;
215 if (buf
[buflen
- 1] != '\n')
217 buf
[buflen
- 1]= '\0';
219 buf1
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
223 memset(&ent
, 0, sizeof(ent
));
225 /* Authentication name */
226 if (qword_get(&buf
, buf1
, PAGE_SIZE
) <= 0)
228 memcpy(ent
.authname
, buf1
, sizeof(ent
.authname
));
231 if (qword_get(&buf
, buf1
, PAGE_SIZE
) <= 0)
233 ent
.type
= strcmp(buf1
, "user") == 0 ?
234 IDMAP_TYPE_USER
: IDMAP_TYPE_GROUP
;
237 if (qword_get(&buf
, buf1
, PAGE_SIZE
) <= 0)
239 ent
.id
= simple_strtoul(buf1
, &bp
, 10);
244 ent
.h
.expiry_time
= get_expiry(&buf
);
245 if (ent
.h
.expiry_time
== 0)
249 res
= idtoname_lookup(&ent
);
254 error
= qword_get(&buf
, buf1
, PAGE_SIZE
);
255 if (error
== -EINVAL
)
257 if (error
== -ENOENT
)
258 set_bit(CACHE_NEGATIVE
, &ent
.h
.flags
);
260 if (error
>= IDMAP_NAMESZ
) {
264 memcpy(ent
.name
, buf1
, sizeof(ent
.name
));
267 res
= idtoname_update(&ent
, res
);
271 cache_put(&res
->h
, &idtoname_cache
);
282 idtoname_lookup(struct ent
*item
)
284 struct cache_head
*ch
= sunrpc_cache_lookup(&idtoname_cache
,
286 idtoname_hash(item
));
288 return container_of(ch
, struct ent
, h
);
294 idtoname_update(struct ent
*new, struct ent
*old
)
296 struct cache_head
*ch
= sunrpc_cache_update(&idtoname_cache
,
300 return container_of(ch
, struct ent
, h
);
310 static struct cache_head
*nametoid_table
[ENT_HASHMAX
];
313 nametoid_hash(struct ent
*ent
)
315 return hash_str(ent
->name
, ENT_HASHBITS
);
319 nametoid_request(struct cache_detail
*cd
, struct cache_head
*ch
, char **bpp
,
322 struct ent
*ent
= container_of(ch
, struct ent
, h
);
324 qword_add(bpp
, blen
, ent
->authname
);
325 qword_add(bpp
, blen
, ent
->type
== IDMAP_TYPE_GROUP
? "group" : "user");
326 qword_add(bpp
, blen
, ent
->name
);
332 nametoid_match(struct cache_head
*ca
, struct cache_head
*cb
)
334 struct ent
*a
= container_of(ca
, struct ent
, h
);
335 struct ent
*b
= container_of(cb
, struct ent
, h
);
337 return (a
->type
== b
->type
&& strcmp(a
->name
, b
->name
) == 0 &&
338 strcmp(a
->authname
, b
->authname
) == 0);
342 nametoid_show(struct seq_file
*m
, struct cache_detail
*cd
, struct cache_head
*h
)
347 seq_puts(m
, "#domain type name [id]\n");
350 ent
= container_of(h
, struct ent
, h
);
351 seq_printf(m
, "%s %s %s", ent
->authname
,
352 ent
->type
== IDMAP_TYPE_GROUP
? "group" : "user",
354 if (test_bit(CACHE_VALID
, &h
->flags
))
355 seq_printf(m
, " %d", ent
->id
);
360 static struct ent
*nametoid_lookup(struct ent
*);
361 static struct ent
*nametoid_update(struct ent
*, struct ent
*);
362 static int nametoid_parse(struct cache_detail
*, char *, int);
364 static struct cache_detail nametoid_cache
= {
365 .owner
= THIS_MODULE
,
366 .hash_size
= ENT_HASHMAX
,
367 .hash_table
= nametoid_table
,
368 .name
= "nfs4.nametoid",
369 .cache_put
= ent_put
,
370 .cache_request
= nametoid_request
,
371 .cache_parse
= nametoid_parse
,
372 .cache_show
= nametoid_show
,
373 .warn_no_listener
= warn_no_idmapd
,
374 .match
= nametoid_match
,
381 nametoid_parse(struct cache_detail
*cd
, char *buf
, int buflen
)
383 struct ent ent
, *res
;
387 if (buf
[buflen
- 1] != '\n')
389 buf
[buflen
- 1]= '\0';
391 buf1
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
395 memset(&ent
, 0, sizeof(ent
));
397 /* Authentication name */
398 if (qword_get(&buf
, buf1
, PAGE_SIZE
) <= 0)
400 memcpy(ent
.authname
, buf1
, sizeof(ent
.authname
));
403 if (qword_get(&buf
, buf1
, PAGE_SIZE
) <= 0)
405 ent
.type
= strcmp(buf1
, "user") == 0 ?
406 IDMAP_TYPE_USER
: IDMAP_TYPE_GROUP
;
409 error
= qword_get(&buf
, buf1
, PAGE_SIZE
);
410 if (error
<= 0 || error
>= IDMAP_NAMESZ
)
412 memcpy(ent
.name
, buf1
, sizeof(ent
.name
));
415 ent
.h
.expiry_time
= get_expiry(&buf
);
416 if (ent
.h
.expiry_time
== 0)
420 error
= get_int(&buf
, &ent
.id
);
421 if (error
== -EINVAL
)
423 if (error
== -ENOENT
)
424 set_bit(CACHE_NEGATIVE
, &ent
.h
.flags
);
427 res
= nametoid_lookup(&ent
);
430 res
= nametoid_update(&ent
, res
);
434 cache_put(&res
->h
, &nametoid_cache
);
444 nametoid_lookup(struct ent
*item
)
446 struct cache_head
*ch
= sunrpc_cache_lookup(&nametoid_cache
,
448 nametoid_hash(item
));
450 return container_of(ch
, struct ent
, h
);
456 nametoid_update(struct ent
*new, struct ent
*old
)
458 struct cache_head
*ch
= sunrpc_cache_update(&nametoid_cache
,
462 return container_of(ch
, struct ent
, h
);
472 nfsd_idmap_init(void)
474 cache_register(&idtoname_cache
);
475 cache_register(&nametoid_cache
);
479 nfsd_idmap_shutdown(void)
481 if (cache_unregister(&idtoname_cache
))
482 printk(KERN_ERR
"nfsd: failed to unregister idtoname cache\n");
483 if (cache_unregister(&nametoid_cache
))
484 printk(KERN_ERR
"nfsd: failed to unregister nametoid cache\n");
488 * Deferred request handling
491 struct idmap_defer_req
{
492 struct cache_req req
;
493 struct cache_deferred_req deferred_req
;
494 wait_queue_head_t waitq
;
499 put_mdr(struct idmap_defer_req
*mdr
)
501 if (atomic_dec_and_test(&mdr
->count
))
506 get_mdr(struct idmap_defer_req
*mdr
)
508 atomic_inc(&mdr
->count
);
512 idmap_revisit(struct cache_deferred_req
*dreq
, int toomany
)
514 struct idmap_defer_req
*mdr
=
515 container_of(dreq
, struct idmap_defer_req
, deferred_req
);
517 wake_up(&mdr
->waitq
);
521 static struct cache_deferred_req
*
522 idmap_defer(struct cache_req
*req
)
524 struct idmap_defer_req
*mdr
=
525 container_of(req
, struct idmap_defer_req
, req
);
527 mdr
->deferred_req
.revisit
= idmap_revisit
;
529 return (&mdr
->deferred_req
);
533 do_idmap_lookup(struct ent
*(*lookup_fn
)(struct ent
*), struct ent
*key
,
534 struct cache_detail
*detail
, struct ent
**item
,
535 struct idmap_defer_req
*mdr
)
537 *item
= lookup_fn(key
);
540 return cache_check(detail
, &(*item
)->h
, &mdr
->req
);
544 do_idmap_lookup_nowait(struct ent
*(*lookup_fn
)(struct ent
*),
545 struct ent
*key
, struct cache_detail
*detail
,
550 *item
= lookup_fn(key
);
554 if (!test_bit(CACHE_VALID
, &(*item
)->h
.flags
)
555 || (*item
)->h
.expiry_time
< get_seconds()
556 || detail
->flush_time
> (*item
)->h
.last_refresh
)
559 if (test_bit(CACHE_NEGATIVE
, &(*item
)->h
.flags
))
563 cache_put(&(*item
)->h
, detail
);
570 idmap_lookup(struct svc_rqst
*rqstp
,
571 struct ent
*(*lookup_fn
)(struct ent
*), struct ent
*key
,
572 struct cache_detail
*detail
, struct ent
**item
)
574 struct idmap_defer_req
*mdr
;
577 mdr
= kmalloc(sizeof(*mdr
), GFP_KERNEL
);
580 memset(mdr
, 0, sizeof(*mdr
));
581 atomic_set(&mdr
->count
, 1);
582 init_waitqueue_head(&mdr
->waitq
);
583 mdr
->req
.defer
= idmap_defer
;
584 ret
= do_idmap_lookup(lookup_fn
, key
, detail
, item
, mdr
);
585 if (ret
== -EAGAIN
) {
586 wait_event_interruptible_timeout(mdr
->waitq
,
587 test_bit(CACHE_VALID
, &(*item
)->h
.flags
), 1 * HZ
);
588 ret
= do_idmap_lookup_nowait(lookup_fn
, key
, detail
, item
);
595 idmap_name_to_id(struct svc_rqst
*rqstp
, int type
, const char *name
, u32 namelen
,
598 struct ent
*item
, key
= {
603 if (namelen
+ 1 > sizeof(key
.name
))
605 memcpy(key
.name
, name
, namelen
);
606 key
.name
[namelen
] = '\0';
607 strlcpy(key
.authname
, rqstp
->rq_client
->name
, sizeof(key
.authname
));
608 ret
= idmap_lookup(rqstp
, nametoid_lookup
, &key
, &nametoid_cache
, &item
);
610 ret
= -ESRCH
; /* nfserr_badname */
614 cache_put(&item
->h
, &nametoid_cache
);
619 idmap_id_to_name(struct svc_rqst
*rqstp
, int type
, uid_t id
, char *name
)
621 struct ent
*item
, key
= {
627 strlcpy(key
.authname
, rqstp
->rq_client
->name
, sizeof(key
.authname
));
628 ret
= idmap_lookup(rqstp
, idtoname_lookup
, &key
, &idtoname_cache
, &item
);
630 return sprintf(name
, "%u", id
);
633 ret
= strlen(item
->name
);
634 BUG_ON(ret
> IDMAP_NAMESZ
);
635 memcpy(name
, item
->name
, ret
);
636 cache_put(&item
->h
, &idtoname_cache
);
641 nfsd_map_name_to_uid(struct svc_rqst
*rqstp
, const char *name
, size_t namelen
,
644 return idmap_name_to_id(rqstp
, IDMAP_TYPE_USER
, name
, namelen
, id
);
648 nfsd_map_name_to_gid(struct svc_rqst
*rqstp
, const char *name
, size_t namelen
,
651 return idmap_name_to_id(rqstp
, IDMAP_TYPE_GROUP
, name
, namelen
, id
);
655 nfsd_map_uid_to_name(struct svc_rqst
*rqstp
, __u32 id
, char *name
)
657 return idmap_id_to_name(rqstp
, IDMAP_TYPE_USER
, id
, name
);
661 nfsd_map_gid_to_name(struct svc_rqst
*rqstp
, __u32 id
, char *name
)
663 return idmap_id_to_name(rqstp
, IDMAP_TYPE_GROUP
, id
, name
);