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/parser.h>
39 #include <linux/nfs_idmap.h>
40 #include <net/net_namespace.h>
41 #include <linux/sunrpc/rpc_pipe_fs.h>
42 #include <linux/nfs_fs.h>
43 #include <linux/nfs_fs_sb.h>
44 #include <linux/key.h>
45 #include <linux/keyctl.h>
46 #include <linux/key-type.h>
47 #include <keys/user-type.h>
48 #include <linux/module.h>
53 #define NFS_UINT_MAXLEN 11
55 /* Default cache timeout is 10 minutes */
56 unsigned int nfs_idmap_cache_timeout = 600;
57 static const struct cred *id_resolver_cache;
58 static struct key_type key_type_id_resolver_legacy;
61 struct rpc_pipe *idmap_pipe;
62 struct key_construction *idmap_key_cons;
63 struct mutex idmap_mutex;
66 struct idmap_legacy_upcalldata {
67 struct rpc_pipe_msg pipe_msg;
68 struct idmap_msg idmap_msg;
73 * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
74 * @fattr: fully initialised struct nfs_fattr
75 * @owner_name: owner name string cache
76 * @group_name: group name string cache
78 void nfs_fattr_init_names(struct nfs_fattr *fattr,
79 struct nfs4_string *owner_name,
80 struct nfs4_string *group_name)
82 fattr->owner_name = owner_name;
83 fattr->group_name = group_name;
86 static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
88 fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
89 kfree(fattr->owner_name->data);
92 static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
94 fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
95 kfree(fattr->group_name->data);
98 static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
100 struct nfs4_string *owner = fattr->owner_name;
103 if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
105 if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
107 fattr->valid |= NFS_ATTR_FATTR_OWNER;
112 static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
114 struct nfs4_string *group = fattr->group_name;
117 if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
119 if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
121 fattr->valid |= NFS_ATTR_FATTR_GROUP;
127 * nfs_fattr_free_names - free up the NFSv4 owner and group strings
128 * @fattr: a fully initialised nfs_fattr structure
130 void nfs_fattr_free_names(struct nfs_fattr *fattr)
132 if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
133 nfs_fattr_free_owner_name(fattr);
134 if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
135 nfs_fattr_free_group_name(fattr);
139 * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
140 * @server: pointer to the filesystem nfs_server structure
141 * @fattr: a fully initialised nfs_fattr structure
143 * This helper maps the cached NFSv4 owner/group strings in fattr into
144 * their numeric uid/gid equivalents, and then frees the cached strings.
146 void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
148 if (nfs_fattr_map_owner_name(server, fattr))
149 nfs_fattr_free_owner_name(fattr);
150 if (nfs_fattr_map_group_name(server, fattr))
151 nfs_fattr_free_group_name(fattr);
154 static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
159 if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
161 memcpy(buf, name, namelen);
163 if (strict_strtoul(buf, 0, &val) != 0)
169 static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
171 return snprintf(buf, buflen, "%u", id);
174 static struct key_type key_type_id_resolver = {
175 .name = "id_resolver",
176 .instantiate = user_instantiate,
178 .revoke = user_revoke,
179 .destroy = user_destroy,
180 .describe = user_describe,
184 static int nfs_idmap_init_keyring(void)
190 printk(KERN_NOTICE "NFS: Registering the %s key type\n",
191 key_type_id_resolver.name);
193 cred = prepare_kernel_cred(NULL);
197 keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
198 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
199 KEY_USR_VIEW | KEY_USR_READ,
200 KEY_ALLOC_NOT_IN_QUOTA);
201 if (IS_ERR(keyring)) {
202 ret = PTR_ERR(keyring);
203 goto failed_put_cred;
206 ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
210 ret = register_key_type(&key_type_id_resolver);
214 ret = register_key_type(&key_type_id_resolver_legacy);
216 goto failed_reg_legacy;
218 set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
219 cred->thread_keyring = keyring;
220 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
221 id_resolver_cache = cred;
225 unregister_key_type(&key_type_id_resolver);
233 static void nfs_idmap_quit_keyring(void)
235 key_revoke(id_resolver_cache->thread_keyring);
236 unregister_key_type(&key_type_id_resolver);
237 unregister_key_type(&key_type_id_resolver_legacy);
238 put_cred(id_resolver_cache);
242 * Assemble the description to pass to request_key()
243 * This function will allocate a new string and update dest to point
244 * at it. The caller is responsible for freeing dest.
246 * On error 0 is returned. Otherwise, the length of dest is returned.
248 static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
249 const char *type, size_t typelen, char **desc)
252 size_t desclen = typelen + namelen + 2;
254 *desc = kmalloc(desclen, GFP_KERNEL);
259 memcpy(cp, type, typelen);
263 memcpy(cp, name, namelen);
269 static ssize_t nfs_idmap_request_key(struct key_type *key_type,
270 const char *name, size_t namelen,
271 const char *type, void *data,
272 size_t data_size, struct idmap *idmap)
274 const struct cred *saved_cred;
277 struct user_key_payload *payload;
280 ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
284 saved_cred = override_creds(id_resolver_cache);
286 rkey = request_key_with_auxdata(key_type, desc, "", 0, idmap);
288 rkey = request_key(&key_type_id_resolver, desc, "");
289 revert_creds(saved_cred);
298 rkey->perm |= KEY_USR_VIEW;
300 ret = key_validate(rkey);
304 payload = rcu_dereference(rkey->payload.data);
305 if (IS_ERR_OR_NULL(payload)) {
306 ret = PTR_ERR(payload);
310 ret = payload->datalen;
311 if (ret > 0 && ret <= data_size)
312 memcpy(data, payload->data, ret);
323 static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
324 const char *type, void *data,
325 size_t data_size, struct idmap *idmap)
327 ssize_t ret = nfs_idmap_request_key(&key_type_id_resolver,
328 name, namelen, type, data,
331 mutex_lock(&idmap->idmap_mutex);
332 ret = nfs_idmap_request_key(&key_type_id_resolver_legacy,
333 name, namelen, type, data,
335 idmap->idmap_key_cons = NULL;
336 mutex_unlock(&idmap->idmap_mutex);
342 static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
343 size_t buflen, struct idmap *idmap)
345 char id_str[NFS_UINT_MAXLEN];
349 id_len = snprintf(id_str, sizeof(id_str), "%u", id);
350 ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
357 static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
358 __u32 *id, struct idmap *idmap)
360 char id_str[NFS_UINT_MAXLEN];
365 data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
366 if (data_size <= 0) {
369 ret = strict_strtol(id_str, 10, &id_long);
370 *id = (__u32)id_long;
375 /* idmap classic begins here */
376 module_param(nfs_idmap_cache_timeout, int, 0644);
379 Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
382 static const match_table_t nfs_idmap_tokens = {
383 { Opt_find_uid, "uid:%s" },
384 { Opt_find_gid, "gid:%s" },
385 { Opt_find_user, "user:%s" },
386 { Opt_find_group, "group:%s" },
387 { Opt_find_err, NULL }
390 static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
391 static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
393 static void idmap_release_pipe(struct inode *);
394 static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
396 static const struct rpc_pipe_ops idmap_upcall_ops = {
397 .upcall = rpc_pipe_generic_upcall,
398 .downcall = idmap_pipe_downcall,
399 .release_pipe = idmap_release_pipe,
400 .destroy_msg = idmap_pipe_destroy_msg,
403 static struct key_type key_type_id_resolver_legacy = {
405 .instantiate = user_instantiate,
407 .revoke = user_revoke,
408 .destroy = user_destroy,
409 .describe = user_describe,
411 .request_key = nfs_idmap_legacy_upcall,
414 static void __nfs_idmap_unregister(struct rpc_pipe *pipe)
417 rpc_unlink(pipe->dentry);
420 static int __nfs_idmap_register(struct dentry *dir,
422 struct rpc_pipe *pipe)
424 struct dentry *dentry;
426 dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
428 return PTR_ERR(dentry);
429 pipe->dentry = dentry;
433 static void nfs_idmap_unregister(struct nfs_client *clp,
434 struct rpc_pipe *pipe)
436 struct net *net = clp->net;
437 struct super_block *pipefs_sb;
439 pipefs_sb = rpc_get_sb_net(net);
441 __nfs_idmap_unregister(pipe);
446 static int nfs_idmap_register(struct nfs_client *clp,
448 struct rpc_pipe *pipe)
450 struct net *net = clp->net;
451 struct super_block *pipefs_sb;
454 pipefs_sb = rpc_get_sb_net(net);
456 if (clp->cl_rpcclient->cl_dentry)
457 err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
465 nfs_idmap_new(struct nfs_client *clp)
468 struct rpc_pipe *pipe;
471 BUG_ON(clp->cl_idmap != NULL);
473 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
477 pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
479 error = PTR_ERR(pipe);
483 error = nfs_idmap_register(clp, idmap, pipe);
485 rpc_destroy_pipe_data(pipe);
489 idmap->idmap_pipe = pipe;
490 mutex_init(&idmap->idmap_mutex);
492 clp->cl_idmap = idmap;
497 nfs_idmap_delete(struct nfs_client *clp)
499 struct idmap *idmap = clp->cl_idmap;
503 nfs_idmap_unregister(clp, idmap->idmap_pipe);
504 rpc_destroy_pipe_data(idmap->idmap_pipe);
505 clp->cl_idmap = NULL;
509 static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event,
510 struct super_block *sb)
515 case RPC_PIPEFS_MOUNT:
516 BUG_ON(clp->cl_rpcclient->cl_dentry == NULL);
517 err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
519 clp->cl_idmap->idmap_pipe);
521 case RPC_PIPEFS_UMOUNT:
522 if (clp->cl_idmap->idmap_pipe) {
523 struct dentry *parent;
525 parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
526 __nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
528 * Note: This is a dirty hack. SUNRPC hook has been
529 * called already but simple_rmdir() call for the
530 * directory returned with error because of idmap pipe
531 * inside. Thus now we have to remove this directory
534 if (rpc_rmdir(parent))
535 printk(KERN_ERR "NFS: %s: failed to remove "
536 "clnt dir!\n", __func__);
540 printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__,
547 static struct nfs_client *nfs_get_client_for_event(struct net *net, int event)
549 struct nfs_net *nn = net_generic(net, nfs_net_id);
550 struct dentry *cl_dentry;
551 struct nfs_client *clp;
553 spin_lock(&nn->nfs_client_lock);
554 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
555 if (clp->rpc_ops != &nfs_v4_clientops)
557 cl_dentry = clp->cl_idmap->idmap_pipe->dentry;
558 if (((event == RPC_PIPEFS_MOUNT) && cl_dentry) ||
559 ((event == RPC_PIPEFS_UMOUNT) && !cl_dentry))
561 atomic_inc(&clp->cl_count);
562 spin_unlock(&nn->nfs_client_lock);
565 spin_unlock(&nn->nfs_client_lock);
569 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
572 struct super_block *sb = ptr;
573 struct nfs_client *clp;
576 if (!try_module_get(THIS_MODULE))
579 while ((clp = nfs_get_client_for_event(sb->s_fs_info, event))) {
580 error = __rpc_pipefs_event(clp, event, sb);
585 module_put(THIS_MODULE);
589 #define PIPEFS_NFS_PRIO 1
591 static struct notifier_block nfs_idmap_block = {
592 .notifier_call = rpc_pipefs_event,
593 .priority = SUNRPC_PIPEFS_NFS_PRIO,
596 int nfs_idmap_init(void)
599 ret = nfs_idmap_init_keyring();
602 ret = rpc_pipefs_notifier_register(&nfs_idmap_block);
604 nfs_idmap_quit_keyring();
609 void nfs_idmap_quit(void)
611 rpc_pipefs_notifier_unregister(&nfs_idmap_block);
612 nfs_idmap_quit_keyring();
615 static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
616 struct idmap_msg *im,
617 struct rpc_pipe_msg *msg)
622 memset(im, 0, sizeof(*im));
623 memset(msg, 0, sizeof(*msg));
625 im->im_type = IDMAP_TYPE_GROUP;
626 token = match_token(desc, nfs_idmap_tokens, &substr);
630 im->im_type = IDMAP_TYPE_USER;
632 im->im_conv = IDMAP_CONV_NAMETOID;
633 ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
637 im->im_type = IDMAP_TYPE_USER;
639 im->im_conv = IDMAP_CONV_IDTONAME;
640 ret = match_int(&substr, &im->im_id);
649 msg->len = sizeof(struct idmap_msg);
655 static int nfs_idmap_legacy_upcall(struct key_construction *cons,
659 struct idmap_legacy_upcalldata *data;
660 struct rpc_pipe_msg *msg;
661 struct idmap_msg *im;
662 struct idmap *idmap = (struct idmap *)aux;
663 struct key *key = cons->key;
666 /* msg and im are freed in idmap_pipe_destroy_msg */
667 data = kmalloc(sizeof(*data), GFP_KERNEL);
671 msg = &data->pipe_msg;
672 im = &data->idmap_msg;
675 ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
679 BUG_ON(idmap->idmap_key_cons != NULL);
680 idmap->idmap_key_cons = cons;
682 ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
689 idmap->idmap_key_cons = NULL;
693 complete_request_key(cons, ret);
697 static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data)
699 return key_instantiate_and_link(key, data, strlen(data) + 1,
700 id_resolver_cache->thread_keyring,
704 static int nfs_idmap_read_message(struct idmap_msg *im, struct key *key, struct key *authkey)
706 char id_str[NFS_UINT_MAXLEN];
709 switch (im->im_conv) {
710 case IDMAP_CONV_NAMETOID:
711 sprintf(id_str, "%d", im->im_id);
712 ret = nfs_idmap_instantiate(key, authkey, id_str);
714 case IDMAP_CONV_IDTONAME:
715 ret = nfs_idmap_instantiate(key, authkey, im->im_name);
723 idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
725 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
726 struct idmap *idmap = (struct idmap *)rpci->private;
727 struct key_construction *cons;
732 /* If instantiation is successful, anyone waiting for key construction
733 * will have been woken up and someone else may now have used
734 * idmap_key_cons - so after this point we may no longer touch it.
736 cons = ACCESS_ONCE(idmap->idmap_key_cons);
737 idmap->idmap_key_cons = NULL;
739 if (mlen != sizeof(im)) {
744 if (copy_from_user(&im, src, mlen) != 0) {
749 if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
751 complete_request_key(cons, -ENOKEY);
755 namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
756 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
761 ret = nfs_idmap_read_message(&im, cons->key, cons->authkey);
763 key_set_timeout(cons->key, nfs_idmap_cache_timeout);
768 complete_request_key(cons, ret);
774 idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
776 struct idmap_legacy_upcalldata *data = container_of(msg,
777 struct idmap_legacy_upcalldata,
779 struct idmap *idmap = data->idmap;
780 struct key_construction *cons;
782 cons = ACCESS_ONCE(idmap->idmap_key_cons);
783 idmap->idmap_key_cons = NULL;
784 complete_request_key(cons, msg->errno);
786 /* Free memory allocated in nfs_idmap_legacy_upcall() */
791 idmap_release_pipe(struct inode *inode)
793 struct rpc_inode *rpci = RPC_I(inode);
794 struct idmap *idmap = (struct idmap *)rpci->private;
795 idmap->idmap_key_cons = NULL;
798 int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
800 struct idmap *idmap = server->nfs_client->cl_idmap;
802 if (nfs_map_string_to_numeric(name, namelen, uid))
804 return nfs_idmap_lookup_id(name, namelen, "uid", uid, idmap);
807 int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid)
809 struct idmap *idmap = server->nfs_client->cl_idmap;
811 if (nfs_map_string_to_numeric(name, namelen, gid))
813 return nfs_idmap_lookup_id(name, namelen, "gid", gid, idmap);
816 int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
818 struct idmap *idmap = server->nfs_client->cl_idmap;
821 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
822 ret = nfs_idmap_lookup_name(uid, "user", buf, buflen, idmap);
824 ret = nfs_map_numeric_to_string(uid, buf, buflen);
827 int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
829 struct idmap *idmap = server->nfs_client->cl_idmap;
832 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
833 ret = nfs_idmap_lookup_name(gid, "group", buf, buflen, idmap);
835 ret = nfs_map_numeric_to_string(gid, buf, buflen);