4 * UID and GID to name mapping for clients.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
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.
36 #include <linux/types.h>
37 #include <linux/string.h>
38 #include <linux/kernel.h>
40 static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
45 if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
47 memcpy(buf, name, namelen);
49 if (strict_strtoul(buf, 0, &val) != 0)
55 #ifdef CONFIG_NFS_USE_NEW_IDMAPPER
57 #include <linux/slab.h>
58 #include <linux/cred.h>
59 #include <linux/nfs_idmap.h>
60 #include <linux/keyctl.h>
61 #include <linux/key-type.h>
62 #include <linux/rcupdate.h>
63 #include <linux/err.h>
65 #include <keys/user-type.h>
67 #define NFS_UINT_MAXLEN 11
69 const struct cred *id_resolver_cache;
71 struct key_type key_type_id_resolver = {
72 .name = "id_resolver",
73 .instantiate = user_instantiate,
75 .revoke = user_revoke,
76 .destroy = user_destroy,
77 .describe = user_describe,
81 int nfs_idmap_init(void)
87 printk(KERN_NOTICE "Registering the %s key type\n", key_type_id_resolver.name);
89 cred = prepare_kernel_cred(NULL);
93 keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
94 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
95 KEY_USR_VIEW | KEY_USR_READ,
96 KEY_ALLOC_NOT_IN_QUOTA);
97 if (IS_ERR(keyring)) {
98 ret = PTR_ERR(keyring);
102 ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
106 ret = register_key_type(&key_type_id_resolver);
110 cred->thread_keyring = keyring;
111 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
112 id_resolver_cache = cred;
122 void nfs_idmap_quit(void)
124 key_revoke(id_resolver_cache->thread_keyring);
125 unregister_key_type(&key_type_id_resolver);
126 put_cred(id_resolver_cache);
130 * Assemble the description to pass to request_key()
131 * This function will allocate a new string and update dest to point
132 * at it. The caller is responsible for freeing dest.
134 * On error 0 is returned. Otherwise, the length of dest is returned.
136 static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
137 const char *type, size_t typelen, char **desc)
140 size_t desclen = typelen + namelen + 2;
142 *desc = kmalloc(desclen, GFP_KERNEL);
147 memcpy(cp, type, typelen);
151 memcpy(cp, name, namelen);
157 static ssize_t nfs_idmap_request_key(const char *name, size_t namelen,
158 const char *type, void *data, size_t data_size)
160 const struct cred *saved_cred;
163 struct user_key_payload *payload;
166 ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
170 saved_cred = override_creds(id_resolver_cache);
171 rkey = request_key(&key_type_id_resolver, desc, "");
172 revert_creds(saved_cred);
180 rkey->perm |= KEY_USR_VIEW;
182 ret = key_validate(rkey);
186 payload = rcu_dereference(rkey->payload.data);
187 if (IS_ERR_OR_NULL(payload)) {
188 ret = PTR_ERR(payload);
192 ret = payload->datalen;
193 if (ret > 0 && ret <= data_size)
194 memcpy(data, payload->data, ret);
207 static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, size_t buflen)
209 char id_str[NFS_UINT_MAXLEN];
213 id_len = snprintf(id_str, sizeof(id_str), "%u", id);
214 ret = nfs_idmap_request_key(id_str, id_len, type, buf, buflen);
221 static int nfs_idmap_lookup_id(const char *name, size_t namelen,
222 const char *type, __u32 *id)
224 char id_str[NFS_UINT_MAXLEN];
229 data_size = nfs_idmap_request_key(name, namelen, type, id_str, NFS_UINT_MAXLEN);
230 if (data_size <= 0) {
233 ret = strict_strtol(id_str, 10, &id_long);
234 *id = (__u32)id_long;
239 int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
241 if (nfs_map_string_to_numeric(name, namelen, uid))
243 return nfs_idmap_lookup_id(name, namelen, "uid", uid);
246 int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *gid)
248 if (nfs_map_string_to_numeric(name, namelen, gid))
250 return nfs_idmap_lookup_id(name, namelen, "gid", gid);
253 int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen)
255 return nfs_idmap_lookup_name(uid, "user", buf, buflen);
257 int nfs_map_gid_to_group(struct nfs_client *clp, __u32 gid, char *buf, size_t buflen)
259 return nfs_idmap_lookup_name(gid, "group", buf, buflen);
262 #else /* CONFIG_NFS_USE_NEW_IDMAPPER not defined */
264 #include <linux/module.h>
265 #include <linux/mutex.h>
266 #include <linux/init.h>
267 #include <linux/slab.h>
268 #include <linux/socket.h>
269 #include <linux/in.h>
270 #include <linux/sched.h>
272 #include <linux/sunrpc/clnt.h>
273 #include <linux/workqueue.h>
274 #include <linux/sunrpc/rpc_pipe_fs.h>
276 #include <linux/nfs_fs.h>
278 #include <linux/nfs_idmap.h>
281 #define IDMAP_HASH_SZ 128
283 /* Default cache timeout is 10 minutes */
284 unsigned int nfs_idmap_cache_timeout = 600 * HZ;
286 static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
289 int num = simple_strtol(val, &endp, 0);
291 if (endp == val || *endp || num < 0 || jif < num)
293 *((int *)kp->arg) = jif;
297 module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
298 &nfs_idmap_cache_timeout, 0644);
300 struct idmap_hashent {
301 unsigned long ih_expires;
304 char ih_name[IDMAP_NAMESZ];
307 struct idmap_hashtable {
309 struct idmap_hashent h_entries[IDMAP_HASH_SZ];
313 struct dentry *idmap_dentry;
314 wait_queue_head_t idmap_wq;
315 struct idmap_msg idmap_im;
316 struct mutex idmap_lock; /* Serializes upcalls */
317 struct mutex idmap_im_lock; /* Protects the hashtable */
318 struct idmap_hashtable idmap_user_hash;
319 struct idmap_hashtable idmap_group_hash;
322 static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *,
323 char __user *, size_t);
324 static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
326 static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
328 static unsigned int fnvhash32(const void *, size_t);
330 static const struct rpc_pipe_ops idmap_upcall_ops = {
331 .upcall = idmap_pipe_upcall,
332 .downcall = idmap_pipe_downcall,
333 .destroy_msg = idmap_pipe_destroy_msg,
337 nfs_idmap_new(struct nfs_client *clp)
342 BUG_ON(clp->cl_idmap != NULL);
344 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
348 idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_path.dentry,
349 "idmap", idmap, &idmap_upcall_ops, 0);
350 if (IS_ERR(idmap->idmap_dentry)) {
351 error = PTR_ERR(idmap->idmap_dentry);
356 mutex_init(&idmap->idmap_lock);
357 mutex_init(&idmap->idmap_im_lock);
358 init_waitqueue_head(&idmap->idmap_wq);
359 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
360 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
362 clp->cl_idmap = idmap;
367 nfs_idmap_delete(struct nfs_client *clp)
369 struct idmap *idmap = clp->cl_idmap;
373 rpc_unlink(idmap->idmap_dentry);
374 clp->cl_idmap = NULL;
379 * Helper routines for manipulating the hashtable
381 static inline struct idmap_hashent *
382 idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
384 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
387 static struct idmap_hashent *
388 idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
390 struct idmap_hashent *he = idmap_name_hash(h, name, len);
392 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
394 if (time_after(jiffies, he->ih_expires))
399 static inline struct idmap_hashent *
400 idmap_id_hash(struct idmap_hashtable* h, __u32 id)
402 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
405 static struct idmap_hashent *
406 idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
408 struct idmap_hashent *he = idmap_id_hash(h, id);
409 if (he->ih_id != id || he->ih_namelen == 0)
411 if (time_after(jiffies, he->ih_expires))
417 * Routines for allocating new entries in the hashtable.
418 * For now, we just have 1 entry per bucket, so it's all
421 static inline struct idmap_hashent *
422 idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
424 return idmap_name_hash(h, name, len);
427 static inline struct idmap_hashent *
428 idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
430 return idmap_id_hash(h, id);
434 idmap_update_entry(struct idmap_hashent *he, const char *name,
435 size_t namelen, __u32 id)
438 memcpy(he->ih_name, name, namelen);
439 he->ih_name[namelen] = '\0';
440 he->ih_namelen = namelen;
441 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
448 nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
449 const char *name, size_t namelen, __u32 *id)
451 struct rpc_pipe_msg msg;
452 struct idmap_msg *im;
453 struct idmap_hashent *he;
454 DECLARE_WAITQUEUE(wq, current);
457 im = &idmap->idmap_im;
460 * String sanity checks
461 * Note that the userland daemon expects NUL terminated strings
466 if (name[namelen-1] != '\0')
470 if (namelen >= IDMAP_NAMESZ)
473 mutex_lock(&idmap->idmap_lock);
474 mutex_lock(&idmap->idmap_im_lock);
476 he = idmap_lookup_name(h, name, namelen);
483 memset(im, 0, sizeof(*im));
484 memcpy(im->im_name, name, namelen);
486 im->im_type = h->h_type;
487 im->im_conv = IDMAP_CONV_NAMETOID;
489 memset(&msg, 0, sizeof(msg));
491 msg.len = sizeof(*im);
493 add_wait_queue(&idmap->idmap_wq, &wq);
494 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
495 remove_wait_queue(&idmap->idmap_wq, &wq);
499 set_current_state(TASK_UNINTERRUPTIBLE);
500 mutex_unlock(&idmap->idmap_im_lock);
502 __set_current_state(TASK_RUNNING);
503 remove_wait_queue(&idmap->idmap_wq, &wq);
504 mutex_lock(&idmap->idmap_im_lock);
506 if (im->im_status & IDMAP_STATUS_SUCCESS) {
512 memset(im, 0, sizeof(*im));
513 mutex_unlock(&idmap->idmap_im_lock);
514 mutex_unlock(&idmap->idmap_lock);
522 nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
523 __u32 id, char *name)
525 struct rpc_pipe_msg msg;
526 struct idmap_msg *im;
527 struct idmap_hashent *he;
528 DECLARE_WAITQUEUE(wq, current);
532 im = &idmap->idmap_im;
534 mutex_lock(&idmap->idmap_lock);
535 mutex_lock(&idmap->idmap_im_lock);
537 he = idmap_lookup_id(h, id);
539 memcpy(name, he->ih_name, he->ih_namelen);
540 ret = he->ih_namelen;
544 memset(im, 0, sizeof(*im));
545 im->im_type = h->h_type;
546 im->im_conv = IDMAP_CONV_IDTONAME;
549 memset(&msg, 0, sizeof(msg));
551 msg.len = sizeof(*im);
553 add_wait_queue(&idmap->idmap_wq, &wq);
555 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
556 remove_wait_queue(&idmap->idmap_wq, &wq);
560 set_current_state(TASK_UNINTERRUPTIBLE);
561 mutex_unlock(&idmap->idmap_im_lock);
563 __set_current_state(TASK_RUNNING);
564 remove_wait_queue(&idmap->idmap_wq, &wq);
565 mutex_lock(&idmap->idmap_im_lock);
567 if (im->im_status & IDMAP_STATUS_SUCCESS) {
568 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
570 memcpy(name, im->im_name, len);
575 memset(im, 0, sizeof(*im));
576 mutex_unlock(&idmap->idmap_im_lock);
577 mutex_unlock(&idmap->idmap_lock);
581 /* RPC pipefs upcall/downcall routines */
583 idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
584 char __user *dst, size_t buflen)
586 char *data = (char *)msg->data + msg->copied;
587 size_t mlen = min(msg->len, buflen);
590 left = copy_to_user(dst, data, mlen);
592 msg->errno = -EFAULT;
603 idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
605 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
606 struct idmap *idmap = (struct idmap *)rpci->private;
607 struct idmap_msg im_in, *im = &idmap->idmap_im;
608 struct idmap_hashtable *h;
609 struct idmap_hashent *he = NULL;
613 if (mlen != sizeof(im_in))
616 if (copy_from_user(&im_in, src, mlen) != 0)
619 mutex_lock(&idmap->idmap_im_lock);
622 im->im_status = im_in.im_status;
623 /* If we got an error, terminate now, and wake up pending upcalls */
624 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
625 wake_up(&idmap->idmap_wq);
629 /* Sanity checking of strings */
631 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
632 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
635 switch (im_in.im_type) {
636 case IDMAP_TYPE_USER:
637 h = &idmap->idmap_user_hash;
639 case IDMAP_TYPE_GROUP:
640 h = &idmap->idmap_group_hash;
646 switch (im_in.im_conv) {
647 case IDMAP_CONV_IDTONAME:
648 /* Did we match the current upcall? */
649 if (im->im_conv == IDMAP_CONV_IDTONAME
650 && im->im_type == im_in.im_type
651 && im->im_id == im_in.im_id) {
652 /* Yes: copy string, including the terminating '\0' */
653 memcpy(im->im_name, im_in.im_name, namelen_in);
654 im->im_name[namelen_in] = '\0';
655 wake_up(&idmap->idmap_wq);
657 he = idmap_alloc_id(h, im_in.im_id);
659 case IDMAP_CONV_NAMETOID:
660 /* Did we match the current upcall? */
661 if (im->im_conv == IDMAP_CONV_NAMETOID
662 && im->im_type == im_in.im_type
663 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
664 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
665 im->im_id = im_in.im_id;
666 wake_up(&idmap->idmap_wq);
668 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
674 /* If the entry is valid, also copy it to the cache */
676 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
679 mutex_unlock(&idmap->idmap_im_lock);
684 idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
686 struct idmap_msg *im = msg->data;
687 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
691 mutex_lock(&idmap->idmap_im_lock);
692 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
693 wake_up(&idmap->idmap_wq);
694 mutex_unlock(&idmap->idmap_im_lock);
698 * Fowler/Noll/Vo hash
699 * http://www.isthe.com/chongo/tech/comp/fnv/
702 #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
703 #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
705 static unsigned int fnvhash32(const void *buf, size_t buflen)
707 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
708 unsigned int hash = FNV_1_32;
710 for (p = buf; p < end; p++) {
712 hash ^= (unsigned int)*p;
718 int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
720 struct idmap *idmap = clp->cl_idmap;
722 if (nfs_map_string_to_numeric(name, namelen, uid))
724 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
727 int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
729 struct idmap *idmap = clp->cl_idmap;
731 if (nfs_map_string_to_numeric(name, namelen, uid))
733 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
736 int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen)
738 struct idmap *idmap = clp->cl_idmap;
740 return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
742 int nfs_map_gid_to_group(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen)
744 struct idmap *idmap = clp->cl_idmap;
746 return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
749 #endif /* CONFIG_NFS_USE_NEW_IDMAPPER */