idmap.c 20.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * fs/nfs/idmap.c
 *
 *  UID and GID to name mapping for clients.
 *
 *  Copyright (c) 2002 The Regents of the University of Michigan.
 *  All rights reserved.
 *
 *  Marius Aamodt Eriksen <marius@umich.edu>
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *  3. Neither the name of the University nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
36
#include <linux/types.h>
37 38
#include <linux/parser.h>
#include <linux/fs.h>
39
#include <linux/nfs_idmap.h>
40 41
#include <net/net_namespace.h>
#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 46 47 48
#include <linux/keyctl.h>
#include <linux/key-type.h>
#include <keys/user-type.h>
#include <linux/module.h>
49

50
#include "internal.h"
51
#include "netns.h"
52 53 54

#define NFS_UINT_MAXLEN 11

55 56
static const struct cred *id_resolver_cache;
static struct key_type key_type_id_resolver_legacy;
57

58 59 60 61 62
struct idmap {
	struct rpc_pipe		*idmap_pipe;
	struct key_construction	*idmap_key_cons;
	struct mutex		idmap_mutex;
};
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

/**
 * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
 * @fattr: fully initialised struct nfs_fattr
 * @owner_name: owner name string cache
 * @group_name: group name string cache
 */
void nfs_fattr_init_names(struct nfs_fattr *fattr,
		struct nfs4_string *owner_name,
		struct nfs4_string *group_name)
{
	fattr->owner_name = owner_name;
	fattr->group_name = group_name;
}

static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
{
	fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
	kfree(fattr->owner_name->data);
}

static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
{
	fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
	kfree(fattr->group_name->data);
}

static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
{
	struct nfs4_string *owner = fattr->owner_name;
	__u32 uid;

	if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
		return false;
	if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
		fattr->uid = uid;
		fattr->valid |= NFS_ATTR_FATTR_OWNER;
	}
	return true;
}

static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
{
	struct nfs4_string *group = fattr->group_name;
	__u32 gid;

	if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
		return false;
	if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
		fattr->gid = gid;
		fattr->valid |= NFS_ATTR_FATTR_GROUP;
	}
	return true;
}

/**
 * nfs_fattr_free_names - free up the NFSv4 owner and group strings
 * @fattr: a fully initialised nfs_fattr structure
 */
void nfs_fattr_free_names(struct nfs_fattr *fattr)
{
	if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
		nfs_fattr_free_owner_name(fattr);
	if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
		nfs_fattr_free_group_name(fattr);
}

/**
 * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
 * @server: pointer to the filesystem nfs_server structure
 * @fattr: a fully initialised nfs_fattr structure
 *
 * This helper maps the cached NFSv4 owner/group strings in fattr into
 * their numeric uid/gid equivalents, and then frees the cached strings.
 */
void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
{
	if (nfs_fattr_map_owner_name(server, fattr))
		nfs_fattr_free_owner_name(fattr);
	if (nfs_fattr_map_group_name(server, fattr))
		nfs_fattr_free_group_name(fattr);
}
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
{
	unsigned long val;
	char buf[16];

	if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
		return 0;
	memcpy(buf, name, namelen);
	buf[namelen] = '\0';
	if (strict_strtoul(buf, 0, &val) != 0)
		return 0;
	*res = val;
	return 1;
}
L
Linus Torvalds 已提交
160

161 162 163 164 165
static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
{
	return snprintf(buf, buflen, "%u", id);
}

166
static struct key_type key_type_id_resolver = {
B
Bryan Schumaker 已提交
167 168 169 170 171 172 173 174 175
	.name		= "id_resolver",
	.instantiate	= user_instantiate,
	.match		= user_match,
	.revoke		= user_revoke,
	.destroy	= user_destroy,
	.describe	= user_describe,
	.read		= user_read,
};

176
static int nfs_idmap_init_keyring(void)
B
Bryan Schumaker 已提交
177 178 179 180 181
{
	struct cred *cred;
	struct key *keyring;
	int ret = 0;

182 183
	printk(KERN_NOTICE "NFS: Registering the %s key type\n",
		key_type_id_resolver.name);
B
Bryan Schumaker 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

	cred = prepare_kernel_cred(NULL);
	if (!cred)
		return -ENOMEM;

	keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
			     (KEY_POS_ALL & ~KEY_POS_SETATTR) |
			     KEY_USR_VIEW | KEY_USR_READ,
			     KEY_ALLOC_NOT_IN_QUOTA);
	if (IS_ERR(keyring)) {
		ret = PTR_ERR(keyring);
		goto failed_put_cred;
	}

	ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
	if (ret < 0)
		goto failed_put_key;

	ret = register_key_type(&key_type_id_resolver);
	if (ret < 0)
		goto failed_put_key;

206 207 208 209
	ret = register_key_type(&key_type_id_resolver_legacy);
	if (ret < 0)
		goto failed_reg_legacy;

210
	set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
B
Bryan Schumaker 已提交
211 212 213 214 215
	cred->thread_keyring = keyring;
	cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
	id_resolver_cache = cred;
	return 0;

216 217
failed_reg_legacy:
	unregister_key_type(&key_type_id_resolver);
B
Bryan Schumaker 已提交
218 219 220 221 222 223 224
failed_put_key:
	key_put(keyring);
failed_put_cred:
	put_cred(cred);
	return ret;
}

225
static void nfs_idmap_quit_keyring(void)
B
Bryan Schumaker 已提交
226 227 228
{
	key_revoke(id_resolver_cache->thread_keyring);
	unregister_key_type(&key_type_id_resolver);
229
	unregister_key_type(&key_type_id_resolver_legacy);
B
Bryan Schumaker 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	put_cred(id_resolver_cache);
}

/*
 * Assemble the description to pass to request_key()
 * This function will allocate a new string and update dest to point
 * at it.  The caller is responsible for freeing dest.
 *
 * On error 0 is returned.  Otherwise, the length of dest is returned.
 */
static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
				const char *type, size_t typelen, char **desc)
{
	char *cp;
	size_t desclen = typelen + namelen + 2;

	*desc = kmalloc(desclen, GFP_KERNEL);
D
Dan Carpenter 已提交
247
	if (!*desc)
B
Bryan Schumaker 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260
		return -ENOMEM;

	cp = *desc;
	memcpy(cp, type, typelen);
	cp += typelen;
	*cp++ = ':';

	memcpy(cp, name, namelen);
	cp += namelen;
	*cp = '\0';
	return desclen;
}

261 262 263 264
static ssize_t nfs_idmap_request_key(struct key_type *key_type,
				     const char *name, size_t namelen,
				     const char *type, void *data,
				     size_t data_size, struct idmap *idmap)
B
Bryan Schumaker 已提交
265 266 267 268 269 270 271 272 273 274 275 276
{
	const struct cred *saved_cred;
	struct key *rkey;
	char *desc;
	struct user_key_payload *payload;
	ssize_t ret;

	ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
	if (ret <= 0)
		goto out;

	saved_cred = override_creds(id_resolver_cache);
277 278 279 280
	if (idmap)
		rkey = request_key_with_auxdata(key_type, desc, "", 0, idmap);
	else
		rkey = request_key(&key_type_id_resolver, desc, "");
B
Bryan Schumaker 已提交
281
	revert_creds(saved_cred);
282

B
Bryan Schumaker 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	kfree(desc);
	if (IS_ERR(rkey)) {
		ret = PTR_ERR(rkey);
		goto out;
	}

	rcu_read_lock();
	rkey->perm |= KEY_USR_VIEW;

	ret = key_validate(rkey);
	if (ret < 0)
		goto out_up;

	payload = rcu_dereference(rkey->payload.data);
	if (IS_ERR_OR_NULL(payload)) {
		ret = PTR_ERR(payload);
		goto out_up;
	}

	ret = payload->datalen;
	if (ret > 0 && ret <= data_size)
		memcpy(data, payload->data, ret);
	else
		ret = -EINVAL;

out_up:
	rcu_read_unlock();
	key_put(rkey);
out:
	return ret;
}

315 316 317 318 319 320 321 322
static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
				 const char *type, void *data,
				 size_t data_size, struct idmap *idmap)
{
	ssize_t ret = nfs_idmap_request_key(&key_type_id_resolver,
					    name, namelen, type, data,
					    data_size, NULL);
	if (ret < 0) {
323
		mutex_lock(&idmap->idmap_mutex);
324 325 326
		ret = nfs_idmap_request_key(&key_type_id_resolver_legacy,
					    name, namelen, type, data,
					    data_size, idmap);
327
		mutex_unlock(&idmap->idmap_mutex);
328 329 330
	}
	return ret;
}
B
Bryan Schumaker 已提交
331 332

/* ID -> Name */
333 334
static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
				     size_t buflen, struct idmap *idmap)
B
Bryan Schumaker 已提交
335 336 337 338 339 340
{
	char id_str[NFS_UINT_MAXLEN];
	int id_len;
	ssize_t ret;

	id_len = snprintf(id_str, sizeof(id_str), "%u", id);
341
	ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
B
Bryan Schumaker 已提交
342 343 344 345 346 347
	if (ret < 0)
		return -EINVAL;
	return ret;
}

/* Name -> ID */
348 349
static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
			       __u32 *id, struct idmap *idmap)
B
Bryan Schumaker 已提交
350 351 352 353 354 355
{
	char id_str[NFS_UINT_MAXLEN];
	long id_long;
	ssize_t data_size;
	int ret = 0;

356
	data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
B
Bryan Schumaker 已提交
357 358 359 360 361 362 363 364 365
	if (data_size <= 0) {
		ret = -EINVAL;
	} else {
		ret = strict_strtol(id_str, 10, &id_long);
		*id = (__u32)id_long;
	}
	return ret;
}

366
/* idmap classic begins here */
367

368 369
enum {
	Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
L
Linus Torvalds 已提交
370 371
};

372 373 374 375 376 377
static const match_table_t nfs_idmap_tokens = {
	{ Opt_find_uid, "uid:%s" },
	{ Opt_find_gid, "gid:%s" },
	{ Opt_find_user, "user:%s" },
	{ Opt_find_group, "group:%s" },
	{ Opt_find_err, NULL }
L
Linus Torvalds 已提交
378 379
};

380
static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
C
Chuck Lever 已提交
381 382 383
static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
				   size_t);
static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
L
Linus Torvalds 已提交
384

385
static const struct rpc_pipe_ops idmap_upcall_ops = {
386
	.upcall		= rpc_pipe_generic_upcall,
C
Chuck Lever 已提交
387 388
	.downcall	= idmap_pipe_downcall,
	.destroy_msg	= idmap_pipe_destroy_msg,
L
Linus Torvalds 已提交
389 390
};

391
static struct key_type key_type_id_resolver_legacy = {
392
	.name		= "id_legacy",
393 394 395 396 397 398 399 400 401
	.instantiate	= user_instantiate,
	.match		= user_match,
	.revoke		= user_revoke,
	.destroy	= user_destroy,
	.describe	= user_describe,
	.read		= user_read,
	.request_key	= nfs_idmap_legacy_upcall,
};

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
static void __nfs_idmap_unregister(struct rpc_pipe *pipe)
{
	if (pipe->dentry)
		rpc_unlink(pipe->dentry);
}

static int __nfs_idmap_register(struct dentry *dir,
				     struct idmap *idmap,
				     struct rpc_pipe *pipe)
{
	struct dentry *dentry;

	dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);
	pipe->dentry = dentry;
	return 0;
}

static void nfs_idmap_unregister(struct nfs_client *clp,
				      struct rpc_pipe *pipe)
{
424
	struct net *net = clp->cl_net;
425 426 427 428 429 430 431 432 433 434 435 436 437
	struct super_block *pipefs_sb;

	pipefs_sb = rpc_get_sb_net(net);
	if (pipefs_sb) {
		__nfs_idmap_unregister(pipe);
		rpc_put_sb_net(net);
	}
}

static int nfs_idmap_register(struct nfs_client *clp,
				   struct idmap *idmap,
				   struct rpc_pipe *pipe)
{
438
	struct net *net = clp->cl_net;
439 440 441 442 443 444 445 446 447 448 449 450 451
	struct super_block *pipefs_sb;
	int err = 0;

	pipefs_sb = rpc_get_sb_net(net);
	if (pipefs_sb) {
		if (clp->cl_rpcclient->cl_dentry)
			err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
						   idmap, pipe);
		rpc_put_sb_net(net);
	}
	return err;
}

452
int
453
nfs_idmap_new(struct nfs_client *clp)
L
Linus Torvalds 已提交
454 455
{
	struct idmap *idmap;
456
	struct rpc_pipe *pipe;
457
	int error;
L
Linus Torvalds 已提交
458

459
	BUG_ON(clp->cl_idmap != NULL);
460

C
Chuck Lever 已提交
461 462 463
	idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
	if (idmap == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
464

465 466 467
	pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
	if (IS_ERR(pipe)) {
		error = PTR_ERR(pipe);
L
Linus Torvalds 已提交
468
		kfree(idmap);
469
		return error;
L
Linus Torvalds 已提交
470
	}
471 472
	error = nfs_idmap_register(clp, idmap, pipe);
	if (error) {
473 474 475 476 477
		rpc_destroy_pipe_data(pipe);
		kfree(idmap);
		return error;
	}
	idmap->idmap_pipe = pipe;
478
	mutex_init(&idmap->idmap_mutex);
L
Linus Torvalds 已提交
479 480

	clp->cl_idmap = idmap;
481
	return 0;
L
Linus Torvalds 已提交
482 483 484
}

void
485
nfs_idmap_delete(struct nfs_client *clp)
L
Linus Torvalds 已提交
486 487 488 489 490
{
	struct idmap *idmap = clp->cl_idmap;

	if (!idmap)
		return;
491
	nfs_idmap_unregister(clp, idmap->idmap_pipe);
492
	rpc_destroy_pipe_data(idmap->idmap_pipe);
L
Linus Torvalds 已提交
493 494 495 496
	clp->cl_idmap = NULL;
	kfree(idmap);
}

497 498
static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event,
			      struct super_block *sb)
L
Linus Torvalds 已提交
499
{
500
	int err = 0;
L
Linus Torvalds 已提交
501

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
	switch (event) {
	case RPC_PIPEFS_MOUNT:
		BUG_ON(clp->cl_rpcclient->cl_dentry == NULL);
		err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
						clp->cl_idmap,
						clp->cl_idmap->idmap_pipe);
		break;
	case RPC_PIPEFS_UMOUNT:
		if (clp->cl_idmap->idmap_pipe) {
			struct dentry *parent;

			parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
			__nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
			/*
			 * Note: This is a dirty hack. SUNRPC hook has been
			 * called already but simple_rmdir() call for the
			 * directory returned with error because of idmap pipe
			 * inside. Thus now we have to remove this directory
			 * here.
			 */
			if (rpc_rmdir(parent))
523 524
				printk(KERN_ERR "NFS: %s: failed to remove "
					"clnt dir!\n", __func__);
525 526 527
		}
		break;
	default:
528 529
		printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__,
			event);
530 531 532 533 534
		return -ENOTSUPP;
	}
	return err;
}

535
static struct nfs_client *nfs_get_client_for_event(struct net *net, int event)
536
{
537 538
	struct nfs_net *nn = net_generic(net, nfs_net_id);
	struct dentry *cl_dentry;
539
	struct nfs_client *clp;
540
	int err;
541

542
restart:
543
	spin_lock(&nn->nfs_client_lock);
544
	list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
545 546 547 548 549 550 551 552 553 554 555 556 557
		/* Wait for initialisation to finish */
		if (clp->cl_cons_state == NFS_CS_INITING) {
			atomic_inc(&clp->cl_count);
			spin_unlock(&nn->nfs_client_lock);
			err = nfs_wait_client_init_complete(clp);
			nfs_put_client(clp);
			if (err)
				return NULL;
			goto restart;
		}
		/* Skip nfs_clients that failed to initialise */
		if (clp->cl_cons_state < 0)
			continue;
558
		smp_rmb();
559 560
		if (clp->rpc_ops != &nfs_v4_clientops)
			continue;
561 562 563 564 565 566 567 568 569 570
		cl_dentry = clp->cl_idmap->idmap_pipe->dentry;
		if (((event == RPC_PIPEFS_MOUNT) && cl_dentry) ||
		    ((event == RPC_PIPEFS_UMOUNT) && !cl_dentry))
			continue;
		atomic_inc(&clp->cl_count);
		spin_unlock(&nn->nfs_client_lock);
		return clp;
	}
	spin_unlock(&nn->nfs_client_lock);
	return NULL;
L
Linus Torvalds 已提交
571 572
}

573 574
static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
			    void *ptr)
L
Linus Torvalds 已提交
575
{
576 577 578
	struct super_block *sb = ptr;
	struct nfs_client *clp;
	int error = 0;
L
Linus Torvalds 已提交
579

580 581 582
	if (!try_module_get(THIS_MODULE))
		return 0;

583
	while ((clp = nfs_get_client_for_event(sb->s_fs_info, event))) {
584
		error = __rpc_pipefs_event(clp, event, sb);
585
		nfs_put_client(clp);
586 587 588
		if (error)
			break;
	}
589
	module_put(THIS_MODULE);
590
	return error;
L
Linus Torvalds 已提交
591 592
}

593 594 595 596 597 598
#define PIPEFS_NFS_PRIO		1

static struct notifier_block nfs_idmap_block = {
	.notifier_call	= rpc_pipefs_event,
	.priority	= SUNRPC_PIPEFS_NFS_PRIO,
};
L
Linus Torvalds 已提交
599

600
int nfs_idmap_init(void)
L
Linus Torvalds 已提交
601
{
602 603 604 605 606 607 608 609 610
	int ret;
	ret = nfs_idmap_init_keyring();
	if (ret != 0)
		goto out;
	ret = rpc_pipefs_notifier_register(&nfs_idmap_block);
	if (ret != 0)
		nfs_idmap_quit_keyring();
out:
	return ret;
L
Linus Torvalds 已提交
611 612
}

613
void nfs_idmap_quit(void)
L
Linus Torvalds 已提交
614
{
615
	rpc_pipefs_notifier_unregister(&nfs_idmap_block);
616
	nfs_idmap_quit_keyring();
L
Linus Torvalds 已提交
617 618
}

619 620
static int nfs_idmap_prepare_message(char *desc, struct idmap_msg *im,
				     struct rpc_pipe_msg *msg)
L
Linus Torvalds 已提交
621
{
622 623
	substring_t substr;
	int token, ret;
L
Linus Torvalds 已提交
624

625 626
	memset(im,  0, sizeof(*im));
	memset(msg, 0, sizeof(*msg));
L
Linus Torvalds 已提交
627

628 629
	im->im_type = IDMAP_TYPE_GROUP;
	token = match_token(desc, nfs_idmap_tokens, &substr);
L
Linus Torvalds 已提交
630

631 632 633 634 635 636 637
	switch (token) {
	case Opt_find_uid:
		im->im_type = IDMAP_TYPE_USER;
	case Opt_find_gid:
		im->im_conv = IDMAP_CONV_NAMETOID;
		ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
		break;
L
Linus Torvalds 已提交
638

639 640 641 642 643 644
	case Opt_find_user:
		im->im_type = IDMAP_TYPE_USER;
	case Opt_find_group:
		im->im_conv = IDMAP_CONV_IDTONAME;
		ret = match_int(&substr, &im->im_id);
		break;
L
Linus Torvalds 已提交
645

646 647
	default:
		ret = -EINVAL;
L
Linus Torvalds 已提交
648 649 650
		goto out;
	}

651 652
	msg->data = im;
	msg->len  = sizeof(struct idmap_msg);
L
Linus Torvalds 已提交
653

654
out:
C
Chuck Lever 已提交
655
	return ret;
L
Linus Torvalds 已提交
656 657
}

658 659 660
static int nfs_idmap_legacy_upcall(struct key_construction *cons,
				   const char *op,
				   void *aux)
L
Linus Torvalds 已提交
661
{
662
	struct rpc_pipe_msg *msg;
L
Linus Torvalds 已提交
663
	struct idmap_msg *im;
664 665
	struct idmap *idmap = (struct idmap *)aux;
	struct key *key = cons->key;
666
	int ret = -ENOMEM;
L
Linus Torvalds 已提交
667

668 669
	/* msg and im are freed in idmap_pipe_destroy_msg */
	msg = kmalloc(sizeof(*msg), GFP_KERNEL);
670
	if (!msg)
671
		goto out0;
L
Linus Torvalds 已提交
672

673
	im = kmalloc(sizeof(*im), GFP_KERNEL);
674
	if (!im)
675
		goto out1;
L
Linus Torvalds 已提交
676

677 678 679
	ret = nfs_idmap_prepare_message(key->description, im, msg);
	if (ret < 0)
		goto out2;
L
Linus Torvalds 已提交
680

681
	BUG_ON(idmap->idmap_key_cons != NULL);
682
	idmap->idmap_key_cons = cons;
L
Linus Torvalds 已提交
683

684 685 686
	ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
	if (ret < 0)
		goto out2;
L
Linus Torvalds 已提交
687

688
	return ret;
689 690 691 692 693 694

out2:
	kfree(im);
out1:
	kfree(msg);
out0:
695
	complete_request_key(cons, ret);
C
Chuck Lever 已提交
696
	return ret;
L
Linus Torvalds 已提交
697 698
}

699
static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data)
L
Linus Torvalds 已提交
700
{
701 702 703 704
	return key_instantiate_and_link(key, data, strlen(data) + 1,
					id_resolver_cache->thread_keyring,
					authkey);
}
L
Linus Torvalds 已提交
705

706 707 708 709
static int nfs_idmap_read_message(struct idmap_msg *im, struct key *key, struct key *authkey)
{
	char id_str[NFS_UINT_MAXLEN];
	int ret = -EINVAL;
L
Linus Torvalds 已提交
710

711 712 713 714 715 716 717 718
	switch (im->im_conv) {
	case IDMAP_CONV_NAMETOID:
		sprintf(id_str, "%d", im->im_id);
		ret = nfs_idmap_instantiate(key, authkey, id_str);
		break;
	case IDMAP_CONV_IDTONAME:
		ret = nfs_idmap_instantiate(key, authkey, im->im_name);
		break;
L
Linus Torvalds 已提交
719 720 721 722 723 724 725 726
	}

	return ret;
}

static ssize_t
idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
{
C
Chuck Lever 已提交
727
	struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
L
Linus Torvalds 已提交
728
	struct idmap *idmap = (struct idmap *)rpci->private;
729
	struct key_construction *cons;
730
	struct idmap_msg im;
731
	size_t namelen_in;
L
Linus Torvalds 已提交
732 733
	int ret;

734 735 736 737 738 739 740
	/* If instantiation is successful, anyone waiting for key construction
	 * will have been woken up and someone else may now have used
	 * idmap_key_cons - so after this point we may no longer touch it.
	 */
	cons = ACCESS_ONCE(idmap->idmap_key_cons);
	idmap->idmap_key_cons = NULL;

741 742
	if (mlen != sizeof(im)) {
		ret = -ENOSPC;
L
Linus Torvalds 已提交
743 744 745
		goto out;
	}

746 747
	if (copy_from_user(&im, src, mlen) != 0) {
		ret = -EFAULT;
L
Linus Torvalds 已提交
748
		goto out;
749
	}
L
Linus Torvalds 已提交
750

751 752
	if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
		ret = mlen;
753
		complete_request_key(cons, -ENOKEY);
754
		goto out_incomplete;
L
Linus Torvalds 已提交
755 756
	}

757 758 759
	namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
	if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
		ret = -EINVAL;
L
Linus Torvalds 已提交
760 761 762
		goto out;
	}

763 764 765 766 767 768
	ret = nfs_idmap_read_message(&im, cons->key, cons->authkey);
	if (ret >= 0) {
		key_set_timeout(cons->key, nfs_idmap_cache_timeout);
		ret = mlen;
	}

L
Linus Torvalds 已提交
769
out:
770
	complete_request_key(cons, ret);
771
out_incomplete:
L
Linus Torvalds 已提交
772 773 774
	return ret;
}

A
Adrian Bunk 已提交
775
static void
L
Linus Torvalds 已提交
776 777
idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
{
778 779 780
	/* Free memory allocated in nfs_idmap_legacy_upcall() */
	kfree(msg->data);
	kfree(msg);
L
Linus Torvalds 已提交
781 782
}

783
int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
L
Linus Torvalds 已提交
784
{
785
	struct idmap *idmap = server->nfs_client->cl_idmap;
L
Linus Torvalds 已提交
786

787 788
	if (nfs_map_string_to_numeric(name, namelen, uid))
		return 0;
789
	return nfs_idmap_lookup_id(name, namelen, "uid", uid, idmap);
L
Linus Torvalds 已提交
790 791
}

792
int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid)
L
Linus Torvalds 已提交
793
{
794
	struct idmap *idmap = server->nfs_client->cl_idmap;
L
Linus Torvalds 已提交
795

796
	if (nfs_map_string_to_numeric(name, namelen, gid))
797
		return 0;
798
	return nfs_idmap_lookup_id(name, namelen, "gid", gid, idmap);
L
Linus Torvalds 已提交
799 800
}

801
int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
L
Linus Torvalds 已提交
802
{
803
	struct idmap *idmap = server->nfs_client->cl_idmap;
804
	int ret = -EINVAL;
L
Linus Torvalds 已提交
805

806
	if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
807
		ret = nfs_idmap_lookup_name(uid, "user", buf, buflen, idmap);
808 809 810
	if (ret < 0)
		ret = nfs_map_numeric_to_string(uid, buf, buflen);
	return ret;
L
Linus Torvalds 已提交
811
}
812
int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
L
Linus Torvalds 已提交
813
{
814
	struct idmap *idmap = server->nfs_client->cl_idmap;
815
	int ret = -EINVAL;
L
Linus Torvalds 已提交
816

817
	if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
818
		ret = nfs_idmap_lookup_name(gid, "group", buf, buflen, idmap);
819
	if (ret < 0)
820
		ret = nfs_map_numeric_to_string(gid, buf, buflen);
821
	return ret;
L
Linus Torvalds 已提交
822
}