2 * Mapping of UID/GIDs to name and vice versa.
4 * Copyright (c) 2002, 2003 The Regents of the University of
5 * Michigan. All rights reserved.
7 * Marius Aamodt Eriksen <marius@umich.edu>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
35 #include <linux/module.h>
36 #include <linux/nfsd_idmap.h>
37 #include <linux/seq_file.h>
38 #include <linux/sched.h>
45 * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
49 #define IDMAP_TYPE_USER 0
50 #define IDMAP_TYPE_GROUP 1
54 int type
; /* User / Group */
56 char name
[IDMAP_NAMESZ
];
57 char authname
[IDMAP_NAMESZ
];
60 /* Common entry handling */
62 #define ENT_HASHBITS 8
63 #define ENT_HASHMAX (1 << ENT_HASHBITS)
64 #define ENT_HASHMASK (ENT_HASHMAX - 1)
67 ent_init(struct cache_head
*cnew
, struct cache_head
*citm
)
69 struct ent
*new = container_of(cnew
, struct ent
, h
);
70 struct ent
*itm
= container_of(citm
, struct ent
, h
);
73 new->type
= itm
->type
;
75 strlcpy(new->name
, itm
->name
, sizeof(new->name
));
76 strlcpy(new->authname
, itm
->authname
, sizeof(new->name
));
80 ent_put(struct kref
*ref
)
82 struct ent
*map
= container_of(ref
, struct ent
, h
.ref
);
86 static struct cache_head
*
89 struct ent
*e
= kmalloc(sizeof(*e
), GFP_KERNEL
);
100 static struct cache_head
*idtoname_table
[ENT_HASHMAX
];
103 idtoname_hash(struct ent
*ent
)
107 hash
= hash_str(ent
->authname
, ENT_HASHBITS
);
108 hash
= hash_long(hash
^ ent
->id
, ENT_HASHBITS
);
110 /* Flip LSB for user/group */
111 if (ent
->type
== IDMAP_TYPE_GROUP
)
118 idtoname_request(struct cache_detail
*cd
, struct cache_head
*ch
, char **bpp
,
121 struct ent
*ent
= container_of(ch
, struct ent
, h
);
124 qword_add(bpp
, blen
, ent
->authname
);
125 snprintf(idstr
, sizeof(idstr
), "%u", ent
->id
);
126 qword_add(bpp
, blen
, ent
->type
== IDMAP_TYPE_GROUP
? "group" : "user");
127 qword_add(bpp
, blen
, idstr
);
133 idtoname_upcall(struct cache_detail
*cd
, struct cache_head
*ch
)
135 return sunrpc_cache_pipe_upcall(cd
, ch
, idtoname_request
);
139 idtoname_match(struct cache_head
*ca
, struct cache_head
*cb
)
141 struct ent
*a
= container_of(ca
, struct ent
, h
);
142 struct ent
*b
= container_of(cb
, struct ent
, h
);
144 return (a
->id
== b
->id
&& a
->type
== b
->type
&&
145 strcmp(a
->authname
, b
->authname
) == 0);
149 idtoname_show(struct seq_file
*m
, struct cache_detail
*cd
, struct cache_head
*h
)
154 seq_puts(m
, "#domain type id [name]\n");
157 ent
= container_of(h
, struct ent
, h
);
158 seq_printf(m
, "%s %s %u", ent
->authname
,
159 ent
->type
== IDMAP_TYPE_GROUP
? "group" : "user",
161 if (test_bit(CACHE_VALID
, &h
->flags
))
162 seq_printf(m
, " %s", ent
->name
);
168 warn_no_idmapd(struct cache_detail
*detail
, int has_died
)
170 printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
171 has_died
? "died" : "not been started");
175 static int idtoname_parse(struct cache_detail
*, char *, int);
176 static struct ent
*idtoname_lookup(struct ent
*);
177 static struct ent
*idtoname_update(struct ent
*, struct ent
*);
179 static struct cache_detail idtoname_cache
= {
180 .owner
= THIS_MODULE
,
181 .hash_size
= ENT_HASHMAX
,
182 .hash_table
= idtoname_table
,
183 .name
= "nfs4.idtoname",
184 .cache_put
= ent_put
,
185 .cache_upcall
= idtoname_upcall
,
186 .cache_parse
= idtoname_parse
,
187 .cache_show
= idtoname_show
,
188 .warn_no_listener
= warn_no_idmapd
,
189 .match
= idtoname_match
,
196 idtoname_parse(struct cache_detail
*cd
, char *buf
, int buflen
)
198 struct ent ent
, *res
;
203 if (buf
[buflen
- 1] != '\n')
205 buf
[buflen
- 1]= '\0';
207 buf1
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
211 memset(&ent
, 0, sizeof(ent
));
213 /* Authentication name */
214 if (qword_get(&buf
, buf1
, PAGE_SIZE
) <= 0)
216 memcpy(ent
.authname
, buf1
, sizeof(ent
.authname
));
219 if (qword_get(&buf
, buf1
, PAGE_SIZE
) <= 0)
221 ent
.type
= strcmp(buf1
, "user") == 0 ?
222 IDMAP_TYPE_USER
: IDMAP_TYPE_GROUP
;
225 if (qword_get(&buf
, buf1
, PAGE_SIZE
) <= 0)
227 ent
.id
= simple_strtoul(buf1
, &bp
, 10);
232 ent
.h
.expiry_time
= get_expiry(&buf
);
233 if (ent
.h
.expiry_time
== 0)
237 res
= idtoname_lookup(&ent
);
243 len
= qword_get(&buf
, buf1
, PAGE_SIZE
);
247 set_bit(CACHE_NEGATIVE
, &ent
.h
.flags
);
248 else if (len
>= IDMAP_NAMESZ
)
251 memcpy(ent
.name
, buf1
, sizeof(ent
.name
));
253 res
= idtoname_update(&ent
, res
);
257 cache_put(&res
->h
, &idtoname_cache
);
268 idtoname_lookup(struct ent
*item
)
270 struct cache_head
*ch
= sunrpc_cache_lookup(&idtoname_cache
,
272 idtoname_hash(item
));
274 return container_of(ch
, struct ent
, h
);
280 idtoname_update(struct ent
*new, struct ent
*old
)
282 struct cache_head
*ch
= sunrpc_cache_update(&idtoname_cache
,
286 return container_of(ch
, struct ent
, h
);
296 static struct cache_head
*nametoid_table
[ENT_HASHMAX
];
299 nametoid_hash(struct ent
*ent
)
301 return hash_str(ent
->name
, ENT_HASHBITS
);
305 nametoid_request(struct cache_detail
*cd
, struct cache_head
*ch
, char **bpp
,
308 struct ent
*ent
= container_of(ch
, struct ent
, h
);
310 qword_add(bpp
, blen
, ent
->authname
);
311 qword_add(bpp
, blen
, ent
->type
== IDMAP_TYPE_GROUP
? "group" : "user");
312 qword_add(bpp
, blen
, ent
->name
);
318 nametoid_upcall(struct cache_detail
*cd
, struct cache_head
*ch
)
320 return sunrpc_cache_pipe_upcall(cd
, ch
, nametoid_request
);
324 nametoid_match(struct cache_head
*ca
, struct cache_head
*cb
)
326 struct ent
*a
= container_of(ca
, struct ent
, h
);
327 struct ent
*b
= container_of(cb
, struct ent
, h
);
329 return (a
->type
== b
->type
&& strcmp(a
->name
, b
->name
) == 0 &&
330 strcmp(a
->authname
, b
->authname
) == 0);
334 nametoid_show(struct seq_file
*m
, struct cache_detail
*cd
, struct cache_head
*h
)
339 seq_puts(m
, "#domain type name [id]\n");
342 ent
= container_of(h
, struct ent
, h
);
343 seq_printf(m
, "%s %s %s", ent
->authname
,
344 ent
->type
== IDMAP_TYPE_GROUP
? "group" : "user",
346 if (test_bit(CACHE_VALID
, &h
->flags
))
347 seq_printf(m
, " %u", ent
->id
);
352 static struct ent
*nametoid_lookup(struct ent
*);
353 static struct ent
*nametoid_update(struct ent
*, struct ent
*);
354 static int nametoid_parse(struct cache_detail
*, char *, int);
356 static struct cache_detail nametoid_cache
= {
357 .owner
= THIS_MODULE
,
358 .hash_size
= ENT_HASHMAX
,
359 .hash_table
= nametoid_table
,
360 .name
= "nfs4.nametoid",
361 .cache_put
= ent_put
,
362 .cache_upcall
= nametoid_upcall
,
363 .cache_parse
= nametoid_parse
,
364 .cache_show
= nametoid_show
,
365 .warn_no_listener
= warn_no_idmapd
,
366 .match
= nametoid_match
,
373 nametoid_parse(struct cache_detail
*cd
, char *buf
, int buflen
)
375 struct ent ent
, *res
;
379 if (buf
[buflen
- 1] != '\n')
381 buf
[buflen
- 1]= '\0';
383 buf1
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
387 memset(&ent
, 0, sizeof(ent
));
389 /* Authentication name */
390 if (qword_get(&buf
, buf1
, PAGE_SIZE
) <= 0)
392 memcpy(ent
.authname
, buf1
, sizeof(ent
.authname
));
395 if (qword_get(&buf
, buf1
, PAGE_SIZE
) <= 0)
397 ent
.type
= strcmp(buf1
, "user") == 0 ?
398 IDMAP_TYPE_USER
: IDMAP_TYPE_GROUP
;
401 error
= qword_get(&buf
, buf1
, PAGE_SIZE
);
402 if (error
<= 0 || error
>= IDMAP_NAMESZ
)
404 memcpy(ent
.name
, buf1
, sizeof(ent
.name
));
407 ent
.h
.expiry_time
= get_expiry(&buf
);
408 if (ent
.h
.expiry_time
== 0)
412 error
= get_int(&buf
, &ent
.id
);
413 if (error
== -EINVAL
)
415 if (error
== -ENOENT
)
416 set_bit(CACHE_NEGATIVE
, &ent
.h
.flags
);
419 res
= nametoid_lookup(&ent
);
422 res
= nametoid_update(&ent
, res
);
426 cache_put(&res
->h
, &nametoid_cache
);
436 nametoid_lookup(struct ent
*item
)
438 struct cache_head
*ch
= sunrpc_cache_lookup(&nametoid_cache
,
440 nametoid_hash(item
));
442 return container_of(ch
, struct ent
, h
);
448 nametoid_update(struct ent
*new, struct ent
*old
)
450 struct cache_head
*ch
= sunrpc_cache_update(&nametoid_cache
,
454 return container_of(ch
, struct ent
, h
);
464 nfsd_idmap_init(void)
468 rv
= cache_register(&idtoname_cache
);
471 rv
= cache_register(&nametoid_cache
);
473 cache_unregister(&idtoname_cache
);
478 nfsd_idmap_shutdown(void)
480 cache_unregister(&idtoname_cache
);
481 cache_unregister(&nametoid_cache
);
485 * Deferred request handling
488 struct idmap_defer_req
{
489 struct cache_req req
;
490 struct cache_deferred_req deferred_req
;
491 wait_queue_head_t waitq
;
496 put_mdr(struct idmap_defer_req
*mdr
)
498 if (atomic_dec_and_test(&mdr
->count
))
503 get_mdr(struct idmap_defer_req
*mdr
)
505 atomic_inc(&mdr
->count
);
509 idmap_revisit(struct cache_deferred_req
*dreq
, int toomany
)
511 struct idmap_defer_req
*mdr
=
512 container_of(dreq
, struct idmap_defer_req
, deferred_req
);
514 wake_up(&mdr
->waitq
);
518 static struct cache_deferred_req
*
519 idmap_defer(struct cache_req
*req
)
521 struct idmap_defer_req
*mdr
=
522 container_of(req
, struct idmap_defer_req
, req
);
524 mdr
->deferred_req
.revisit
= idmap_revisit
;
526 return (&mdr
->deferred_req
);
530 do_idmap_lookup(struct ent
*(*lookup_fn
)(struct ent
*), struct ent
*key
,
531 struct cache_detail
*detail
, struct ent
**item
,
532 struct idmap_defer_req
*mdr
)
534 *item
= lookup_fn(key
);
537 return cache_check(detail
, &(*item
)->h
, &mdr
->req
);
541 do_idmap_lookup_nowait(struct ent
*(*lookup_fn
)(struct ent
*),
542 struct ent
*key
, struct cache_detail
*detail
,
547 *item
= lookup_fn(key
);
551 if (!test_bit(CACHE_VALID
, &(*item
)->h
.flags
)
552 || (*item
)->h
.expiry_time
< get_seconds()
553 || detail
->flush_time
> (*item
)->h
.last_refresh
)
556 if (test_bit(CACHE_NEGATIVE
, &(*item
)->h
.flags
))
560 cache_put(&(*item
)->h
, detail
);
567 idmap_lookup(struct svc_rqst
*rqstp
,
568 struct ent
*(*lookup_fn
)(struct ent
*), struct ent
*key
,
569 struct cache_detail
*detail
, struct ent
**item
)
571 struct idmap_defer_req
*mdr
;
574 mdr
= kzalloc(sizeof(*mdr
), GFP_KERNEL
);
577 atomic_set(&mdr
->count
, 1);
578 init_waitqueue_head(&mdr
->waitq
);
579 mdr
->req
.defer
= idmap_defer
;
580 ret
= do_idmap_lookup(lookup_fn
, key
, detail
, item
, mdr
);
581 if (ret
== -EAGAIN
) {
582 wait_event_interruptible_timeout(mdr
->waitq
,
583 test_bit(CACHE_VALID
, &(*item
)->h
.flags
), 1 * HZ
);
584 ret
= do_idmap_lookup_nowait(lookup_fn
, key
, detail
, item
);
591 rqst_authname(struct svc_rqst
*rqstp
)
593 struct auth_domain
*clp
;
595 clp
= rqstp
->rq_gssclient
? rqstp
->rq_gssclient
: rqstp
->rq_client
;
600 idmap_name_to_id(struct svc_rqst
*rqstp
, int type
, const char *name
, u32 namelen
,
603 struct ent
*item
, key
= {
608 if (namelen
+ 1 > sizeof(key
.name
))
610 memcpy(key
.name
, name
, namelen
);
611 key
.name
[namelen
] = '\0';
612 strlcpy(key
.authname
, rqst_authname(rqstp
), sizeof(key
.authname
));
613 ret
= idmap_lookup(rqstp
, nametoid_lookup
, &key
, &nametoid_cache
, &item
);
615 ret
= -ESRCH
; /* nfserr_badname */
619 cache_put(&item
->h
, &nametoid_cache
);
624 idmap_id_to_name(struct svc_rqst
*rqstp
, int type
, uid_t id
, char *name
)
626 struct ent
*item
, key
= {
632 strlcpy(key
.authname
, rqst_authname(rqstp
), sizeof(key
.authname
));
633 ret
= idmap_lookup(rqstp
, idtoname_lookup
, &key
, &idtoname_cache
, &item
);
635 return sprintf(name
, "%u", id
);
638 ret
= strlen(item
->name
);
639 BUG_ON(ret
> IDMAP_NAMESZ
);
640 memcpy(name
, item
->name
, ret
);
641 cache_put(&item
->h
, &idtoname_cache
);
646 nfsd_map_name_to_uid(struct svc_rqst
*rqstp
, const char *name
, size_t namelen
,
649 return idmap_name_to_id(rqstp
, IDMAP_TYPE_USER
, name
, namelen
, id
);
653 nfsd_map_name_to_gid(struct svc_rqst
*rqstp
, const char *name
, size_t namelen
,
656 return idmap_name_to_id(rqstp
, IDMAP_TYPE_GROUP
, name
, namelen
, id
);
660 nfsd_map_uid_to_name(struct svc_rqst
*rqstp
, __u32 id
, char *name
)
662 return idmap_id_to_name(rqstp
, IDMAP_TYPE_USER
, id
, name
);
666 nfsd_map_gid_to_name(struct svc_rqst
*rqstp
, __u32 id
, char *name
)
668 return idmap_id_to_name(rqstp
, IDMAP_TYPE_GROUP
, id
, name
);