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 static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
57 return snprintf(buf, buflen, "%u", id);
60 #ifdef CONFIG_NFS_USE_NEW_IDMAPPER
62 #include <linux/slab.h>
63 #include <linux/cred.h>
64 #include <linux/nfs_idmap.h>
65 #include <linux/keyctl.h>
66 #include <linux/key-type.h>
67 #include <linux/rcupdate.h>
68 #include <linux/err.h>
70 #include <keys/user-type.h>
72 #define NFS_UINT_MAXLEN 11
74 const struct cred *id_resolver_cache;
76 struct key_type key_type_id_resolver = {
77 .name = "id_resolver",
78 .instantiate = user_instantiate,
80 .revoke = user_revoke,
81 .destroy = user_destroy,
82 .describe = user_describe,
86 int nfs_idmap_init(void)
92 printk(KERN_NOTICE "Registering the %s key type\n", key_type_id_resolver.name);
94 cred = prepare_kernel_cred(NULL);
98 keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
99 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
100 KEY_USR_VIEW | KEY_USR_READ,
101 KEY_ALLOC_NOT_IN_QUOTA);
102 if (IS_ERR(keyring)) {
103 ret = PTR_ERR(keyring);
104 goto failed_put_cred;
107 ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
111 ret = register_key_type(&key_type_id_resolver);
115 cred->thread_keyring = keyring;
116 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
117 id_resolver_cache = cred;
127 void nfs_idmap_quit(void)
129 key_revoke(id_resolver_cache->thread_keyring);
130 unregister_key_type(&key_type_id_resolver);
131 put_cred(id_resolver_cache);
135 * Assemble the description to pass to request_key()
136 * This function will allocate a new string and update dest to point
137 * at it. The caller is responsible for freeing dest.
139 * On error 0 is returned. Otherwise, the length of dest is returned.
141 static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
142 const char *type, size_t typelen, char **desc)
145 size_t desclen = typelen + namelen + 2;
147 *desc = kmalloc(desclen, GFP_KERNEL);
152 memcpy(cp, type, typelen);
156 memcpy(cp, name, namelen);
162 static ssize_t nfs_idmap_request_key(const char *name, size_t namelen,
163 const char *type, void *data, size_t data_size)
165 const struct cred *saved_cred;
168 struct user_key_payload *payload;
171 ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
175 saved_cred = override_creds(id_resolver_cache);
176 rkey = request_key(&key_type_id_resolver, desc, "");
177 revert_creds(saved_cred);
185 rkey->perm |= KEY_USR_VIEW;
187 ret = key_validate(rkey);
191 payload = rcu_dereference(rkey->payload.data);
192 if (IS_ERR_OR_NULL(payload)) {
193 ret = PTR_ERR(payload);
197 ret = payload->datalen;
198 if (ret > 0 && ret <= data_size)
199 memcpy(data, payload->data, ret);
212 static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, size_t buflen)
214 char id_str[NFS_UINT_MAXLEN];
218 id_len = snprintf(id_str, sizeof(id_str), "%u", id);
219 ret = nfs_idmap_request_key(id_str, id_len, type, buf, buflen);
226 static int nfs_idmap_lookup_id(const char *name, size_t namelen,
227 const char *type, __u32 *id)
229 char id_str[NFS_UINT_MAXLEN];
234 data_size = nfs_idmap_request_key(name, namelen, type, id_str, NFS_UINT_MAXLEN);
235 if (data_size <= 0) {
238 ret = strict_strtol(id_str, 10, &id_long);
239 *id = (__u32)id_long;
244 int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
246 if (nfs_map_string_to_numeric(name, namelen, uid))
248 return nfs_idmap_lookup_id(name, namelen, "uid", uid);
251 int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid)
253 if (nfs_map_string_to_numeric(name, namelen, gid))
255 return nfs_idmap_lookup_id(name, namelen, "gid", gid);
258 int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
261 ret = nfs_idmap_lookup_name(uid, "user", buf, buflen);
263 ret = nfs_map_numeric_to_string(uid, buf, buflen);
266 int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
270 ret = nfs_idmap_lookup_name(gid, "group", buf, buflen);
272 ret = nfs_map_numeric_to_string(gid, buf, buflen);
276 #else /* CONFIG_NFS_USE_NEW_IDMAPPER not defined */
278 #include <linux/module.h>
279 #include <linux/mutex.h>
280 #include <linux/init.h>
281 #include <linux/slab.h>
282 #include <linux/socket.h>
283 #include <linux/in.h>
284 #include <linux/sched.h>
286 #include <linux/sunrpc/clnt.h>
287 #include <linux/workqueue.h>
288 #include <linux/sunrpc/rpc_pipe_fs.h>
290 #include <linux/nfs_fs.h>
292 #include <linux/nfs_idmap.h>
295 #define IDMAP_HASH_SZ 128
297 /* Default cache timeout is 10 minutes */
298 unsigned int nfs_idmap_cache_timeout = 600 * HZ;
300 static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
303 int num = simple_strtol(val, &endp, 0);
305 if (endp == val || *endp || num < 0 || jif < num)
307 *((int *)kp->arg) = jif;
311 module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
312 &nfs_idmap_cache_timeout, 0644);
314 struct idmap_hashent {
315 unsigned long ih_expires;
318 char ih_name[IDMAP_NAMESZ];
321 struct idmap_hashtable {
323 struct idmap_hashent h_entries[IDMAP_HASH_SZ];
327 struct dentry *idmap_dentry;
328 wait_queue_head_t idmap_wq;
329 struct idmap_msg idmap_im;
330 struct mutex idmap_lock; /* Serializes upcalls */
331 struct mutex idmap_im_lock; /* Protects the hashtable */
332 struct idmap_hashtable idmap_user_hash;
333 struct idmap_hashtable idmap_group_hash;
336 static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *,
337 char __user *, size_t);
338 static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
340 static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
342 static unsigned int fnvhash32(const void *, size_t);
344 static const struct rpc_pipe_ops idmap_upcall_ops = {
345 .upcall = idmap_pipe_upcall,
346 .downcall = idmap_pipe_downcall,
347 .destroy_msg = idmap_pipe_destroy_msg,
351 nfs_idmap_new(struct nfs_client *clp)
356 BUG_ON(clp->cl_idmap != NULL);
358 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
362 idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_path.dentry,
363 "idmap", idmap, &idmap_upcall_ops, 0);
364 if (IS_ERR(idmap->idmap_dentry)) {
365 error = PTR_ERR(idmap->idmap_dentry);
370 mutex_init(&idmap->idmap_lock);
371 mutex_init(&idmap->idmap_im_lock);
372 init_waitqueue_head(&idmap->idmap_wq);
373 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
374 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
376 clp->cl_idmap = idmap;
381 nfs_idmap_delete(struct nfs_client *clp)
383 struct idmap *idmap = clp->cl_idmap;
387 rpc_unlink(idmap->idmap_dentry);
388 clp->cl_idmap = NULL;
393 * Helper routines for manipulating the hashtable
395 static inline struct idmap_hashent *
396 idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
398 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
401 static struct idmap_hashent *
402 idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
404 struct idmap_hashent *he = idmap_name_hash(h, name, len);
406 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
408 if (time_after(jiffies, he->ih_expires))
413 static inline struct idmap_hashent *
414 idmap_id_hash(struct idmap_hashtable* h, __u32 id)
416 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
419 static struct idmap_hashent *
420 idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
422 struct idmap_hashent *he = idmap_id_hash(h, id);
423 if (he->ih_id != id || he->ih_namelen == 0)
425 if (time_after(jiffies, he->ih_expires))
431 * Routines for allocating new entries in the hashtable.
432 * For now, we just have 1 entry per bucket, so it's all
435 static inline struct idmap_hashent *
436 idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
438 return idmap_name_hash(h, name, len);
441 static inline struct idmap_hashent *
442 idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
444 return idmap_id_hash(h, id);
448 idmap_update_entry(struct idmap_hashent *he, const char *name,
449 size_t namelen, __u32 id)
452 memcpy(he->ih_name, name, namelen);
453 he->ih_name[namelen] = '\0';
454 he->ih_namelen = namelen;
455 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
462 nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
463 const char *name, size_t namelen, __u32 *id)
465 struct rpc_pipe_msg msg;
466 struct idmap_msg *im;
467 struct idmap_hashent *he;
468 DECLARE_WAITQUEUE(wq, current);
471 im = &idmap->idmap_im;
474 * String sanity checks
475 * Note that the userland daemon expects NUL terminated strings
480 if (name[namelen-1] != '\0')
484 if (namelen >= IDMAP_NAMESZ)
487 mutex_lock(&idmap->idmap_lock);
488 mutex_lock(&idmap->idmap_im_lock);
490 he = idmap_lookup_name(h, name, namelen);
497 memset(im, 0, sizeof(*im));
498 memcpy(im->im_name, name, namelen);
500 im->im_type = h->h_type;
501 im->im_conv = IDMAP_CONV_NAMETOID;
503 memset(&msg, 0, sizeof(msg));
505 msg.len = sizeof(*im);
507 add_wait_queue(&idmap->idmap_wq, &wq);
508 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
509 remove_wait_queue(&idmap->idmap_wq, &wq);
513 set_current_state(TASK_UNINTERRUPTIBLE);
514 mutex_unlock(&idmap->idmap_im_lock);
516 __set_current_state(TASK_RUNNING);
517 remove_wait_queue(&idmap->idmap_wq, &wq);
518 mutex_lock(&idmap->idmap_im_lock);
520 if (im->im_status & IDMAP_STATUS_SUCCESS) {
526 memset(im, 0, sizeof(*im));
527 mutex_unlock(&idmap->idmap_im_lock);
528 mutex_unlock(&idmap->idmap_lock);
536 nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
537 __u32 id, char *name)
539 struct rpc_pipe_msg msg;
540 struct idmap_msg *im;
541 struct idmap_hashent *he;
542 DECLARE_WAITQUEUE(wq, current);
546 im = &idmap->idmap_im;
548 mutex_lock(&idmap->idmap_lock);
549 mutex_lock(&idmap->idmap_im_lock);
551 he = idmap_lookup_id(h, id);
553 memcpy(name, he->ih_name, he->ih_namelen);
554 ret = he->ih_namelen;
558 memset(im, 0, sizeof(*im));
559 im->im_type = h->h_type;
560 im->im_conv = IDMAP_CONV_IDTONAME;
563 memset(&msg, 0, sizeof(msg));
565 msg.len = sizeof(*im);
567 add_wait_queue(&idmap->idmap_wq, &wq);
569 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
570 remove_wait_queue(&idmap->idmap_wq, &wq);
574 set_current_state(TASK_UNINTERRUPTIBLE);
575 mutex_unlock(&idmap->idmap_im_lock);
577 __set_current_state(TASK_RUNNING);
578 remove_wait_queue(&idmap->idmap_wq, &wq);
579 mutex_lock(&idmap->idmap_im_lock);
581 if (im->im_status & IDMAP_STATUS_SUCCESS) {
582 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
584 memcpy(name, im->im_name, len);
589 memset(im, 0, sizeof(*im));
590 mutex_unlock(&idmap->idmap_im_lock);
591 mutex_unlock(&idmap->idmap_lock);
595 /* RPC pipefs upcall/downcall routines */
597 idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
598 char __user *dst, size_t buflen)
600 char *data = (char *)msg->data + msg->copied;
601 size_t mlen = min(msg->len, buflen);
604 left = copy_to_user(dst, data, mlen);
606 msg->errno = -EFAULT;
617 idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
619 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
620 struct idmap *idmap = (struct idmap *)rpci->private;
621 struct idmap_msg im_in, *im = &idmap->idmap_im;
622 struct idmap_hashtable *h;
623 struct idmap_hashent *he = NULL;
627 if (mlen != sizeof(im_in))
630 if (copy_from_user(&im_in, src, mlen) != 0)
633 mutex_lock(&idmap->idmap_im_lock);
636 im->im_status = im_in.im_status;
637 /* If we got an error, terminate now, and wake up pending upcalls */
638 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
639 wake_up(&idmap->idmap_wq);
643 /* Sanity checking of strings */
645 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
646 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
649 switch (im_in.im_type) {
650 case IDMAP_TYPE_USER:
651 h = &idmap->idmap_user_hash;
653 case IDMAP_TYPE_GROUP:
654 h = &idmap->idmap_group_hash;
660 switch (im_in.im_conv) {
661 case IDMAP_CONV_IDTONAME:
662 /* Did we match the current upcall? */
663 if (im->im_conv == IDMAP_CONV_IDTONAME
664 && im->im_type == im_in.im_type
665 && im->im_id == im_in.im_id) {
666 /* Yes: copy string, including the terminating '\0' */
667 memcpy(im->im_name, im_in.im_name, namelen_in);
668 im->im_name[namelen_in] = '\0';
669 wake_up(&idmap->idmap_wq);
671 he = idmap_alloc_id(h, im_in.im_id);
673 case IDMAP_CONV_NAMETOID:
674 /* Did we match the current upcall? */
675 if (im->im_conv == IDMAP_CONV_NAMETOID
676 && im->im_type == im_in.im_type
677 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
678 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
679 im->im_id = im_in.im_id;
680 wake_up(&idmap->idmap_wq);
682 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
688 /* If the entry is valid, also copy it to the cache */
690 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
693 mutex_unlock(&idmap->idmap_im_lock);
698 idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
700 struct idmap_msg *im = msg->data;
701 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
705 mutex_lock(&idmap->idmap_im_lock);
706 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
707 wake_up(&idmap->idmap_wq);
708 mutex_unlock(&idmap->idmap_im_lock);
712 * Fowler/Noll/Vo hash
713 * http://www.isthe.com/chongo/tech/comp/fnv/
716 #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
717 #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
719 static unsigned int fnvhash32(const void *buf, size_t buflen)
721 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
722 unsigned int hash = FNV_1_32;
724 for (p = buf; p < end; p++) {
726 hash ^= (unsigned int)*p;
732 int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
734 struct idmap *idmap = server->nfs_client->cl_idmap;
736 if (nfs_map_string_to_numeric(name, namelen, uid))
738 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
741 int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
743 struct idmap *idmap = server->nfs_client->cl_idmap;
745 if (nfs_map_string_to_numeric(name, namelen, uid))
747 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
750 int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
752 struct idmap *idmap = server->nfs_client->cl_idmap;
755 ret = nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
757 ret = nfs_map_numeric_to_string(uid, buf, buflen);
760 int nfs_map_gid_to_group(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
762 struct idmap *idmap = server->nfs_client->cl_idmap;
765 ret = nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
767 ret = nfs_map_numeric_to_string(uid, buf, buflen);
771 #endif /* CONFIG_NFS_USE_NEW_IDMAPPER */