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>
39 #include <linux/slab.h>
40 #include <linux/nfs_idmap.h>
42 static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
47 if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
49 memcpy(buf, name, namelen);
51 if (strict_strtoul(buf, 0, &val) != 0)
57 static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
59 return snprintf(buf, buflen, "%u", id);
62 #ifdef CONFIG_NFS_USE_NEW_IDMAPPER
64 #include <linux/cred.h>
65 #include <linux/sunrpc/sched.h>
66 #include <linux/nfs4.h>
67 #include <linux/nfs_fs_sb.h>
68 #include <linux/keyctl.h>
69 #include <linux/key-type.h>
70 #include <linux/rcupdate.h>
71 #include <linux/err.h>
73 #include <keys/user-type.h>
75 #define NFS_UINT_MAXLEN 11
77 const struct cred *id_resolver_cache;
79 struct key_type key_type_id_resolver = {
80 .name = "id_resolver",
81 .instantiate = user_instantiate,
83 .revoke = user_revoke,
84 .destroy = user_destroy,
85 .describe = user_describe,
89 int nfs_idmap_init(void)
95 printk(KERN_NOTICE "Registering the %s key type\n", key_type_id_resolver.name);
97 cred = prepare_kernel_cred(NULL);
101 keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
102 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
103 KEY_USR_VIEW | KEY_USR_READ,
104 KEY_ALLOC_NOT_IN_QUOTA);
105 if (IS_ERR(keyring)) {
106 ret = PTR_ERR(keyring);
107 goto failed_put_cred;
110 ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
114 ret = register_key_type(&key_type_id_resolver);
118 cred->thread_keyring = keyring;
119 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
120 id_resolver_cache = cred;
130 void nfs_idmap_quit(void)
132 key_revoke(id_resolver_cache->thread_keyring);
133 unregister_key_type(&key_type_id_resolver);
134 put_cred(id_resolver_cache);
138 * Assemble the description to pass to request_key()
139 * This function will allocate a new string and update dest to point
140 * at it. The caller is responsible for freeing dest.
142 * On error 0 is returned. Otherwise, the length of dest is returned.
144 static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
145 const char *type, size_t typelen, char **desc)
148 size_t desclen = typelen + namelen + 2;
150 *desc = kmalloc(desclen, GFP_KERNEL);
155 memcpy(cp, type, typelen);
159 memcpy(cp, name, namelen);
165 static ssize_t nfs_idmap_request_key(const char *name, size_t namelen,
166 const char *type, void *data, size_t data_size)
168 const struct cred *saved_cred;
171 struct user_key_payload *payload;
174 ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
178 saved_cred = override_creds(id_resolver_cache);
179 rkey = request_key(&key_type_id_resolver, desc, "");
180 revert_creds(saved_cred);
188 rkey->perm |= KEY_USR_VIEW;
190 ret = key_validate(rkey);
194 payload = rcu_dereference(rkey->payload.data);
195 if (IS_ERR_OR_NULL(payload)) {
196 ret = PTR_ERR(payload);
200 ret = payload->datalen;
201 if (ret > 0 && ret <= data_size)
202 memcpy(data, payload->data, ret);
215 static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, size_t buflen)
217 char id_str[NFS_UINT_MAXLEN];
221 id_len = snprintf(id_str, sizeof(id_str), "%u", id);
222 ret = nfs_idmap_request_key(id_str, id_len, type, buf, buflen);
229 static int nfs_idmap_lookup_id(const char *name, size_t namelen,
230 const char *type, __u32 *id)
232 char id_str[NFS_UINT_MAXLEN];
237 data_size = nfs_idmap_request_key(name, namelen, type, id_str, NFS_UINT_MAXLEN);
238 if (data_size <= 0) {
241 ret = strict_strtol(id_str, 10, &id_long);
242 *id = (__u32)id_long;
247 int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
249 if (nfs_map_string_to_numeric(name, namelen, uid))
251 return nfs_idmap_lookup_id(name, namelen, "uid", uid);
254 int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid)
256 if (nfs_map_string_to_numeric(name, namelen, gid))
258 return nfs_idmap_lookup_id(name, namelen, "gid", gid);
261 int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
265 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
266 ret = nfs_idmap_lookup_name(uid, "user", buf, buflen);
268 ret = nfs_map_numeric_to_string(uid, buf, buflen);
271 int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
275 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
276 ret = nfs_idmap_lookup_name(gid, "group", buf, buflen);
278 ret = nfs_map_numeric_to_string(gid, buf, buflen);
282 #else /* CONFIG_NFS_USE_NEW_IDMAPPER not defined */
284 #include <linux/module.h>
285 #include <linux/mutex.h>
286 #include <linux/init.h>
287 #include <linux/socket.h>
288 #include <linux/in.h>
289 #include <linux/sched.h>
290 #include <linux/sunrpc/clnt.h>
291 #include <linux/workqueue.h>
292 #include <linux/sunrpc/rpc_pipe_fs.h>
294 #include <linux/nfs_fs.h>
298 #define IDMAP_HASH_SZ 128
300 /* Default cache timeout is 10 minutes */
301 unsigned int nfs_idmap_cache_timeout = 600 * HZ;
303 static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
306 int num = simple_strtol(val, &endp, 0);
308 if (endp == val || *endp || num < 0 || jif < num)
310 *((int *)kp->arg) = jif;
314 module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
315 &nfs_idmap_cache_timeout, 0644);
317 struct idmap_hashent {
318 unsigned long ih_expires;
324 struct idmap_hashtable {
326 struct idmap_hashent *h_entries;
330 struct dentry *idmap_dentry;
331 wait_queue_head_t idmap_wq;
332 struct idmap_msg idmap_im;
333 struct mutex idmap_lock; /* Serializes upcalls */
334 struct mutex idmap_im_lock; /* Protects the hashtable */
335 struct idmap_hashtable idmap_user_hash;
336 struct idmap_hashtable idmap_group_hash;
339 static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
341 static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
343 static unsigned int fnvhash32(const void *, size_t);
345 static const struct rpc_pipe_ops idmap_upcall_ops = {
346 .upcall = rpc_pipe_generic_upcall,
347 .downcall = idmap_pipe_downcall,
348 .destroy_msg = idmap_pipe_destroy_msg,
352 nfs_idmap_new(struct nfs_client *clp)
357 BUG_ON(clp->cl_idmap != NULL);
359 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
363 idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_path.dentry,
364 "idmap", idmap, &idmap_upcall_ops, 0);
365 if (IS_ERR(idmap->idmap_dentry)) {
366 error = PTR_ERR(idmap->idmap_dentry);
371 mutex_init(&idmap->idmap_lock);
372 mutex_init(&idmap->idmap_im_lock);
373 init_waitqueue_head(&idmap->idmap_wq);
374 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
375 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
377 clp->cl_idmap = idmap;
382 idmap_alloc_hashtable(struct idmap_hashtable *h)
384 if (h->h_entries != NULL)
386 h->h_entries = kcalloc(IDMAP_HASH_SZ,
387 sizeof(*h->h_entries),
392 idmap_free_hashtable(struct idmap_hashtable *h)
396 if (h->h_entries == NULL)
398 for (i = 0; i < IDMAP_HASH_SZ; i++)
399 kfree(h->h_entries[i].ih_name);
404 nfs_idmap_delete(struct nfs_client *clp)
406 struct idmap *idmap = clp->cl_idmap;
410 rpc_unlink(idmap->idmap_dentry);
411 clp->cl_idmap = NULL;
412 idmap_free_hashtable(&idmap->idmap_user_hash);
413 idmap_free_hashtable(&idmap->idmap_group_hash);
418 * Helper routines for manipulating the hashtable
420 static inline struct idmap_hashent *
421 idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
423 if (h->h_entries == NULL)
425 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
428 static struct idmap_hashent *
429 idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
431 struct idmap_hashent *he = idmap_name_hash(h, name, len);
435 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
437 if (time_after(jiffies, he->ih_expires))
442 static inline struct idmap_hashent *
443 idmap_id_hash(struct idmap_hashtable* h, __u32 id)
445 if (h->h_entries == NULL)
447 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
450 static struct idmap_hashent *
451 idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
453 struct idmap_hashent *he = idmap_id_hash(h, id);
457 if (he->ih_id != id || he->ih_namelen == 0)
459 if (time_after(jiffies, he->ih_expires))
465 * Routines for allocating new entries in the hashtable.
466 * For now, we just have 1 entry per bucket, so it's all
469 static inline struct idmap_hashent *
470 idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
472 idmap_alloc_hashtable(h);
473 return idmap_name_hash(h, name, len);
476 static inline struct idmap_hashent *
477 idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
479 idmap_alloc_hashtable(h);
480 return idmap_id_hash(h, id);
484 idmap_update_entry(struct idmap_hashent *he, const char *name,
485 size_t namelen, __u32 id)
487 char *str = kmalloc(namelen + 1, GFP_KERNEL);
492 memcpy(str, name, namelen);
495 he->ih_namelen = namelen;
496 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
503 nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
504 const char *name, size_t namelen, __u32 *id)
506 struct rpc_pipe_msg msg;
507 struct idmap_msg *im;
508 struct idmap_hashent *he;
509 DECLARE_WAITQUEUE(wq, current);
512 im = &idmap->idmap_im;
515 * String sanity checks
516 * Note that the userland daemon expects NUL terminated strings
521 if (name[namelen-1] != '\0')
525 if (namelen >= IDMAP_NAMESZ)
528 mutex_lock(&idmap->idmap_lock);
529 mutex_lock(&idmap->idmap_im_lock);
531 he = idmap_lookup_name(h, name, namelen);
538 memset(im, 0, sizeof(*im));
539 memcpy(im->im_name, name, namelen);
541 im->im_type = h->h_type;
542 im->im_conv = IDMAP_CONV_NAMETOID;
544 memset(&msg, 0, sizeof(msg));
546 msg.len = sizeof(*im);
548 add_wait_queue(&idmap->idmap_wq, &wq);
549 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
550 remove_wait_queue(&idmap->idmap_wq, &wq);
554 set_current_state(TASK_UNINTERRUPTIBLE);
555 mutex_unlock(&idmap->idmap_im_lock);
557 __set_current_state(TASK_RUNNING);
558 remove_wait_queue(&idmap->idmap_wq, &wq);
559 mutex_lock(&idmap->idmap_im_lock);
561 if (im->im_status & IDMAP_STATUS_SUCCESS) {
567 memset(im, 0, sizeof(*im));
568 mutex_unlock(&idmap->idmap_im_lock);
569 mutex_unlock(&idmap->idmap_lock);
577 nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
578 __u32 id, char *name)
580 struct rpc_pipe_msg msg;
581 struct idmap_msg *im;
582 struct idmap_hashent *he;
583 DECLARE_WAITQUEUE(wq, current);
587 im = &idmap->idmap_im;
589 mutex_lock(&idmap->idmap_lock);
590 mutex_lock(&idmap->idmap_im_lock);
592 he = idmap_lookup_id(h, id);
594 memcpy(name, he->ih_name, he->ih_namelen);
595 ret = he->ih_namelen;
599 memset(im, 0, sizeof(*im));
600 im->im_type = h->h_type;
601 im->im_conv = IDMAP_CONV_IDTONAME;
604 memset(&msg, 0, sizeof(msg));
606 msg.len = sizeof(*im);
608 add_wait_queue(&idmap->idmap_wq, &wq);
610 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
611 remove_wait_queue(&idmap->idmap_wq, &wq);
615 set_current_state(TASK_UNINTERRUPTIBLE);
616 mutex_unlock(&idmap->idmap_im_lock);
618 __set_current_state(TASK_RUNNING);
619 remove_wait_queue(&idmap->idmap_wq, &wq);
620 mutex_lock(&idmap->idmap_im_lock);
622 if (im->im_status & IDMAP_STATUS_SUCCESS) {
623 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
625 memcpy(name, im->im_name, len);
630 memset(im, 0, sizeof(*im));
631 mutex_unlock(&idmap->idmap_im_lock);
632 mutex_unlock(&idmap->idmap_lock);
637 idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
639 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
640 struct idmap *idmap = (struct idmap *)rpci->private;
641 struct idmap_msg im_in, *im = &idmap->idmap_im;
642 struct idmap_hashtable *h;
643 struct idmap_hashent *he = NULL;
647 if (mlen != sizeof(im_in))
650 if (copy_from_user(&im_in, src, mlen) != 0)
653 mutex_lock(&idmap->idmap_im_lock);
656 im->im_status = im_in.im_status;
657 /* If we got an error, terminate now, and wake up pending upcalls */
658 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
659 wake_up(&idmap->idmap_wq);
663 /* Sanity checking of strings */
665 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
666 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
669 switch (im_in.im_type) {
670 case IDMAP_TYPE_USER:
671 h = &idmap->idmap_user_hash;
673 case IDMAP_TYPE_GROUP:
674 h = &idmap->idmap_group_hash;
680 switch (im_in.im_conv) {
681 case IDMAP_CONV_IDTONAME:
682 /* Did we match the current upcall? */
683 if (im->im_conv == IDMAP_CONV_IDTONAME
684 && im->im_type == im_in.im_type
685 && im->im_id == im_in.im_id) {
686 /* Yes: copy string, including the terminating '\0' */
687 memcpy(im->im_name, im_in.im_name, namelen_in);
688 im->im_name[namelen_in] = '\0';
689 wake_up(&idmap->idmap_wq);
691 he = idmap_alloc_id(h, im_in.im_id);
693 case IDMAP_CONV_NAMETOID:
694 /* Did we match the current upcall? */
695 if (im->im_conv == IDMAP_CONV_NAMETOID
696 && im->im_type == im_in.im_type
697 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
698 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
699 im->im_id = im_in.im_id;
700 wake_up(&idmap->idmap_wq);
702 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
708 /* If the entry is valid, also copy it to the cache */
710 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
713 mutex_unlock(&idmap->idmap_im_lock);
718 idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
720 struct idmap_msg *im = msg->data;
721 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
725 mutex_lock(&idmap->idmap_im_lock);
726 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
727 wake_up(&idmap->idmap_wq);
728 mutex_unlock(&idmap->idmap_im_lock);
732 * Fowler/Noll/Vo hash
733 * http://www.isthe.com/chongo/tech/comp/fnv/
736 #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
737 #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
739 static unsigned int fnvhash32(const void *buf, size_t buflen)
741 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
742 unsigned int hash = FNV_1_32;
744 for (p = buf; p < end; p++) {
746 hash ^= (unsigned int)*p;
752 int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
754 struct idmap *idmap = server->nfs_client->cl_idmap;
756 if (nfs_map_string_to_numeric(name, namelen, uid))
758 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
761 int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
763 struct idmap *idmap = server->nfs_client->cl_idmap;
765 if (nfs_map_string_to_numeric(name, namelen, uid))
767 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
770 int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
772 struct idmap *idmap = server->nfs_client->cl_idmap;
775 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
776 ret = nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
778 ret = nfs_map_numeric_to_string(uid, buf, buflen);
781 int nfs_map_gid_to_group(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
783 struct idmap *idmap = server->nfs_client->cl_idmap;
786 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
787 ret = nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
789 ret = nfs_map_numeric_to_string(uid, buf, buflen);
793 #endif /* CONFIG_NFS_USE_NEW_IDMAPPER */