nfs4idmap.c 16.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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 36
/*
 *  Mapping of UID/GIDs to name and vice versa.
 *
 *  Copyright (c) 2002, 2003 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.
 */

#include <linux/module.h>
#include <linux/seq_file.h>
37
#include <linux/sched.h>
38
#include <linux/slab.h>
39
#include <linux/sunrpc/svc_xprt.h>
40
#include <net/net_namespace.h>
41
#include "idmap.h"
42
#include "nfsd.h"
43
#include "netns.h"
Linus Torvalds's avatar
Linus Torvalds committed
44

45 46 47 48 49 50 51 52
/*
 * Turn off idmapping when using AUTH_SYS.
 */
static bool nfs4_disable_idmapping = true;
module_param(nfs4_disable_idmapping, bool, 0644);
MODULE_PARM_DESC(nfs4_disable_idmapping,
		"Turn off server's NFSv4 idmapping when using 'sec=sys'");

Linus Torvalds's avatar
Linus Torvalds committed
53 54 55 56 57 58 59 60 61 62 63 64
/*
 * Cache entry
 */

/*
 * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
 * that.
 */

struct ent {
	struct cache_head h;
	int               type;		       /* User / Group */
65
	u32               id;
Linus Torvalds's avatar
Linus Torvalds committed
66 67
	char              name[IDMAP_NAMESZ];
	char              authname[IDMAP_NAMESZ];
68
	struct rcu_head	  rcu_head;
Linus Torvalds's avatar
Linus Torvalds committed
69 70 71 72 73 74 75
};

/* Common entry handling */

#define ENT_HASHBITS          8
#define ENT_HASHMAX           (1 << ENT_HASHBITS)

76 77
static void
ent_init(struct cache_head *cnew, struct cache_head *citm)
Linus Torvalds's avatar
Linus Torvalds committed
78
{
79 80 81
	struct ent *new = container_of(cnew, struct ent, h);
	struct ent *itm = container_of(citm, struct ent, h);

Linus Torvalds's avatar
Linus Torvalds committed
82 83 84 85
	new->id = itm->id;
	new->type = itm->type;

	strlcpy(new->name, itm->name, sizeof(new->name));
Joe Perches's avatar
Joe Perches committed
86
	strlcpy(new->authname, itm->authname, sizeof(new->authname));
Linus Torvalds's avatar
Linus Torvalds committed
87 88
}

89
static void
90
ent_put(struct kref *ref)
Linus Torvalds's avatar
Linus Torvalds committed
91
{
92
	struct ent *map = container_of(ref, struct ent, h.ref);
93
	kfree_rcu(map, rcu_head);
Linus Torvalds's avatar
Linus Torvalds committed
94 95
}

96 97 98 99 100 101 102 103 104 105
static struct cache_head *
ent_alloc(void)
{
	struct ent *e = kmalloc(sizeof(*e), GFP_KERNEL);
	if (e)
		return &e->h;
	else
		return NULL;
}

Linus Torvalds's avatar
Linus Torvalds committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/*
 * ID -> Name cache
 */

static uint32_t
idtoname_hash(struct ent *ent)
{
	uint32_t hash;

	hash = hash_str(ent->authname, ENT_HASHBITS);
	hash = hash_long(hash ^ ent->id, ENT_HASHBITS);

	/* Flip LSB for user/group */
	if (ent->type == IDMAP_TYPE_GROUP)
		hash ^= 1;

	return hash;
}

125 126 127 128 129 130
static int
idtoname_upcall(struct cache_detail *cd, struct cache_head *h)
{
	return sunrpc_cache_pipe_upcall_timeout(cd, h);
}

Linus Torvalds's avatar
Linus Torvalds committed
131 132 133 134 135 136 137 138
static void
idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
    int *blen)
{
 	struct ent *ent = container_of(ch, struct ent, h);
	char idstr[11];

	qword_add(bpp, blen, ent->authname);
139
	snprintf(idstr, sizeof(idstr), "%u", ent->id);
Linus Torvalds's avatar
Linus Torvalds committed
140 141 142 143 144 145
	qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
	qword_add(bpp, blen, idstr);

	(*bpp)[-1] = '\n';
}

146 147
static int
idtoname_match(struct cache_head *ca, struct cache_head *cb)
Linus Torvalds's avatar
Linus Torvalds committed
148
{
149 150 151
	struct ent *a = container_of(ca, struct ent, h);
	struct ent *b = container_of(cb, struct ent, h);

Linus Torvalds's avatar
Linus Torvalds committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165
	return (a->id == b->id && a->type == b->type &&
	    strcmp(a->authname, b->authname) == 0);
}

static int
idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
{
	struct ent *ent;

	if (h == NULL) {
		seq_puts(m, "#domain type id [name]\n");
		return 0;
	}
	ent = container_of(h, struct ent, h);
166
	seq_printf(m, "%s %s %u", ent->authname,
Linus Torvalds's avatar
Linus Torvalds committed
167 168 169 170 171 172 173 174 175
			ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
			ent->id);
	if (test_bit(CACHE_VALID, &h->flags))
		seq_printf(m, " %s", ent->name);
	seq_printf(m, "\n");
	return 0;
}

static void
176
warn_no_idmapd(struct cache_detail *detail, int has_died)
Linus Torvalds's avatar
Linus Torvalds committed
177 178
{
	printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
179
			has_died ? "died" : "not been started");
Linus Torvalds's avatar
Linus Torvalds committed
180 181 182 183
}


static int         idtoname_parse(struct cache_detail *, char *, int);
184 185 186
static struct ent *idtoname_lookup(struct cache_detail *, struct ent *);
static struct ent *idtoname_update(struct cache_detail *, struct ent *,
				   struct ent *);
Linus Torvalds's avatar
Linus Torvalds committed
187

188
static const struct cache_detail idtoname_cache_template = {
189
	.owner		= THIS_MODULE,
Linus Torvalds's avatar
Linus Torvalds committed
190 191 192
	.hash_size	= ENT_HASHMAX,
	.name		= "nfs4.idtoname",
	.cache_put	= ent_put,
193
	.cache_upcall	= idtoname_upcall,
194
	.cache_request	= idtoname_request,
Linus Torvalds's avatar
Linus Torvalds committed
195 196 197
	.cache_parse	= idtoname_parse,
	.cache_show	= idtoname_show,
	.warn_no_listener = warn_no_idmapd,
198 199 200 201
	.match		= idtoname_match,
	.init		= ent_init,
	.update		= ent_init,
	.alloc		= ent_alloc,
Linus Torvalds's avatar
Linus Torvalds committed
202 203
};

Harvey Harrison's avatar
Harvey Harrison committed
204
static int
Linus Torvalds's avatar
Linus Torvalds committed
205 206 207 208
idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
{
	struct ent ent, *res;
	char *buf1, *bp;
209
	int len;
Linus Torvalds's avatar
Linus Torvalds committed
210 211 212 213 214 215 216 217 218 219 220 221 222
	int error = -EINVAL;

	if (buf[buflen - 1] != '\n')
		return (-EINVAL);
	buf[buflen - 1]= '\0';

	buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if (buf1 == NULL)
		return (-ENOMEM);

	memset(&ent, 0, sizeof(ent));

	/* Authentication name */
223 224
	len = qword_get(&buf, buf1, PAGE_SIZE);
	if (len <= 0 || len >= IDMAP_NAMESZ)
Linus Torvalds's avatar
Linus Torvalds committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
		goto out;
	memcpy(ent.authname, buf1, sizeof(ent.authname));

	/* Type */
	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
		goto out;
	ent.type = strcmp(buf1, "user") == 0 ?
		IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;

	/* ID */
	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
		goto out;
	ent.id = simple_strtoul(buf1, &bp, 10);
	if (bp == buf1)
		goto out;

	/* expiry */
	ent.h.expiry_time = get_expiry(&buf);
	if (ent.h.expiry_time == 0)
		goto out;

246
	error = -ENOMEM;
247
	res = idtoname_lookup(cd, &ent);
248 249 250
	if (!res)
		goto out;

Linus Torvalds's avatar
Linus Torvalds committed
251
	/* Name */
252 253
	error = -EINVAL;
	len = qword_get(&buf, buf1, PAGE_SIZE);
254
	if (len < 0 || len >= IDMAP_NAMESZ)
Linus Torvalds's avatar
Linus Torvalds committed
255
		goto out;
256
	if (len == 0)
Linus Torvalds's avatar
Linus Torvalds committed
257
		set_bit(CACHE_NEGATIVE, &ent.h.flags);
258
	else
Linus Torvalds's avatar
Linus Torvalds committed
259 260
		memcpy(ent.name, buf1, sizeof(ent.name));
	error = -ENOMEM;
261
	res = idtoname_update(cd, &ent, res);
262
	if (res == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
263 264
		goto out;

265
	cache_put(&res->h, cd);
Linus Torvalds's avatar
Linus Torvalds committed
266 267 268 269 270 271
	error = 0;
out:
	kfree(buf1);
	return error;
}

272
static struct ent *
273
idtoname_lookup(struct cache_detail *cd, struct ent *item)
274
{
275 276
	struct cache_head *ch = sunrpc_cache_lookup_rcu(cd, &item->h,
							idtoname_hash(item));
277 278 279 280 281 282 283
	if (ch)
		return container_of(ch, struct ent, h);
	else
		return NULL;
}

static struct ent *
284
idtoname_update(struct cache_detail *cd, struct ent *new, struct ent *old)
285
{
286
	struct cache_head *ch = sunrpc_cache_update(cd, &new->h, &old->h,
287 288 289 290 291 292 293
						    idtoname_hash(new));
	if (ch)
		return container_of(ch, struct ent, h);
	else
		return NULL;
}

Linus Torvalds's avatar
Linus Torvalds committed
294 295 296 297 298 299 300 301 302 303 304

/*
 * Name -> ID cache
 */

static inline int
nametoid_hash(struct ent *ent)
{
	return hash_str(ent->name, ENT_HASHBITS);
}

305 306 307 308 309 310
static int
nametoid_upcall(struct cache_detail *cd, struct cache_head *h)
{
	return sunrpc_cache_pipe_upcall_timeout(cd, h);
}

311
static void
Linus Torvalds's avatar
Linus Torvalds committed
312 313 314 315 316 317 318 319 320 321 322 323
nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
    int *blen)
{
 	struct ent *ent = container_of(ch, struct ent, h);

	qword_add(bpp, blen, ent->authname);
	qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
	qword_add(bpp, blen, ent->name);

	(*bpp)[-1] = '\n';
}

324 325
static int
nametoid_match(struct cache_head *ca, struct cache_head *cb)
Linus Torvalds's avatar
Linus Torvalds committed
326
{
327 328 329
	struct ent *a = container_of(ca, struct ent, h);
	struct ent *b = container_of(cb, struct ent, h);

Linus Torvalds's avatar
Linus Torvalds committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
	return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
	    strcmp(a->authname, b->authname) == 0);
}

static int
nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
{
	struct ent *ent;

	if (h == NULL) {
		seq_puts(m, "#domain type name [id]\n");
		return 0;
	}
	ent = container_of(h, struct ent, h);
	seq_printf(m, "%s %s %s", ent->authname,
			ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
			ent->name);
	if (test_bit(CACHE_VALID, &h->flags))
348
		seq_printf(m, " %u", ent->id);
Linus Torvalds's avatar
Linus Torvalds committed
349 350 351 352
	seq_printf(m, "\n");
	return 0;
}

353 354 355
static struct ent *nametoid_lookup(struct cache_detail *, struct ent *);
static struct ent *nametoid_update(struct cache_detail *, struct ent *,
				   struct ent *);
356
static int         nametoid_parse(struct cache_detail *, char *, int);
Linus Torvalds's avatar
Linus Torvalds committed
357

358
static const struct cache_detail nametoid_cache_template = {
359
	.owner		= THIS_MODULE,
Linus Torvalds's avatar
Linus Torvalds committed
360 361 362
	.hash_size	= ENT_HASHMAX,
	.name		= "nfs4.nametoid",
	.cache_put	= ent_put,
363
	.cache_upcall	= nametoid_upcall,
364
	.cache_request	= nametoid_request,
Linus Torvalds's avatar
Linus Torvalds committed
365 366 367
	.cache_parse	= nametoid_parse,
	.cache_show	= nametoid_show,
	.warn_no_listener = warn_no_idmapd,
368 369 370 371
	.match		= nametoid_match,
	.init		= ent_init,
	.update		= ent_init,
	.alloc		= ent_alloc,
Linus Torvalds's avatar
Linus Torvalds committed
372 373
};

374
static int
Linus Torvalds's avatar
Linus Torvalds committed
375 376 377 378
nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
{
	struct ent ent, *res;
	char *buf1;
379
	int len, error = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
380 381 382 383 384 385 386 387 388 389 390 391

	if (buf[buflen - 1] != '\n')
		return (-EINVAL);
	buf[buflen - 1]= '\0';

	buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if (buf1 == NULL)
		return (-ENOMEM);

	memset(&ent, 0, sizeof(ent));

	/* Authentication name */
392 393
	len = qword_get(&buf, buf1, PAGE_SIZE);
	if (len <= 0 || len >= IDMAP_NAMESZ)
Linus Torvalds's avatar
Linus Torvalds committed
394 395 396 397 398 399 400 401 402 403
		goto out;
	memcpy(ent.authname, buf1, sizeof(ent.authname));

	/* Type */
	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
		goto out;
	ent.type = strcmp(buf1, "user") == 0 ?
		IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;

	/* Name */
404 405
	len = qword_get(&buf, buf1, PAGE_SIZE);
	if (len <= 0 || len >= IDMAP_NAMESZ)
Linus Torvalds's avatar
Linus Torvalds committed
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
		goto out;
	memcpy(ent.name, buf1, sizeof(ent.name));

	/* expiry */
	ent.h.expiry_time = get_expiry(&buf);
	if (ent.h.expiry_time == 0)
		goto out;

	/* ID */
	error = get_int(&buf, &ent.id);
	if (error == -EINVAL)
		goto out;
	if (error == -ENOENT)
		set_bit(CACHE_NEGATIVE, &ent.h.flags);

	error = -ENOMEM;
422
	res = nametoid_lookup(cd, &ent);
423 424
	if (res == NULL)
		goto out;
425
	res = nametoid_update(cd, &ent, res);
426
	if (res == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
427 428
		goto out;

429
	cache_put(&res->h, cd);
Linus Torvalds's avatar
Linus Torvalds committed
430 431 432 433 434 435
	error = 0;
out:
	kfree(buf1);
	return (error);
}

436 437

static struct ent *
438
nametoid_lookup(struct cache_detail *cd, struct ent *item)
439
{
440 441
	struct cache_head *ch = sunrpc_cache_lookup_rcu(cd, &item->h,
							nametoid_hash(item));
442 443 444 445 446 447 448
	if (ch)
		return container_of(ch, struct ent, h);
	else
		return NULL;
}

static struct ent *
449
nametoid_update(struct cache_detail *cd, struct ent *new, struct ent *old)
450
{
451
	struct cache_head *ch = sunrpc_cache_update(cd, &new->h, &old->h,
452 453 454 455 456 457
						    nametoid_hash(new));
	if (ch)
		return container_of(ch, struct ent, h);
	else
		return NULL;
}
Linus Torvalds's avatar
Linus Torvalds committed
458 459 460 461 462

/*
 * Exported API
 */

463
int
464
nfsd_idmap_init(struct net *net)
Linus Torvalds's avatar
Linus Torvalds committed
465
{
466
	int rv;
467
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
468

469 470 471 472
	nn->idtoname_cache = cache_create_net(&idtoname_cache_template, net);
	if (IS_ERR(nn->idtoname_cache))
		return PTR_ERR(nn->idtoname_cache);
	rv = cache_register_net(nn->idtoname_cache, net);
473
	if (rv)
474
		goto destroy_idtoname_cache;
475 476
	nn->nametoid_cache = cache_create_net(&nametoid_cache_template, net);
	if (IS_ERR(nn->nametoid_cache)) {
477
		rv = PTR_ERR(nn->nametoid_cache);
478
		goto unregister_idtoname_cache;
479 480 481 482
	}
	rv = cache_register_net(nn->nametoid_cache, net);
	if (rv)
		goto destroy_nametoid_cache;
483 484
	return 0;

485 486
destroy_nametoid_cache:
	cache_destroy_net(nn->nametoid_cache, net);
487 488 489 490
unregister_idtoname_cache:
	cache_unregister_net(nn->idtoname_cache, net);
destroy_idtoname_cache:
	cache_destroy_net(nn->idtoname_cache, net);
491
	return rv;
Linus Torvalds's avatar
Linus Torvalds committed
492 493 494
}

void
495
nfsd_idmap_shutdown(struct net *net)
Linus Torvalds's avatar
Linus Torvalds committed
496
{
497 498 499
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);

	cache_unregister_net(nn->idtoname_cache, net);
500
	cache_unregister_net(nn->nametoid_cache, net);
501
	cache_destroy_net(nn->idtoname_cache, net);
502
	cache_destroy_net(nn->nametoid_cache, net);
Linus Torvalds's avatar
Linus Torvalds committed
503 504 505 506
}

static int
idmap_lookup(struct svc_rqst *rqstp,
507 508
		struct ent *(*lookup_fn)(struct cache_detail *, struct ent *),
		struct ent *key, struct cache_detail *detail, struct ent **item)
Linus Torvalds's avatar
Linus Torvalds committed
509 510 511
{
	int ret;

512
	*item = lookup_fn(detail, key);
513
	if (!*item)
Linus Torvalds's avatar
Linus Torvalds committed
514
		return -ENOMEM;
515 516 517 518 519
 retry:
	ret = cache_check(detail, &(*item)->h, &rqstp->rq_chandle);

	if (ret == -ETIMEDOUT) {
		struct ent *prev_item = *item;
520
		*item = lookup_fn(detail, key);
521 522 523
		if (*item != prev_item)
			goto retry;
		cache_put(&(*item)->h, detail);
Linus Torvalds's avatar
Linus Torvalds committed
524 525 526 527
	}
	return ret;
}

528 529 530 531 532 533 534 535 536
static char *
rqst_authname(struct svc_rqst *rqstp)
{
	struct auth_domain *clp;

	clp = rqstp->rq_gssclient ? rqstp->rq_gssclient : rqstp->rq_client;
	return clp->name;
}

537
static __be32
Linus Torvalds's avatar
Linus Torvalds committed
538
idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
539
		u32 *id)
Linus Torvalds's avatar
Linus Torvalds committed
540 541 542 543 544
{
	struct ent *item, key = {
		.type = type,
	};
	int ret;
545
	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds's avatar
Linus Torvalds committed
546 547

	if (namelen + 1 > sizeof(key.name))
548
		return nfserr_badowner;
Linus Torvalds's avatar
Linus Torvalds committed
549 550
	memcpy(key.name, name, namelen);
	key.name[namelen] = '\0';
551
	strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
552
	ret = idmap_lookup(rqstp, nametoid_lookup, &key, nn->nametoid_cache, &item);
Linus Torvalds's avatar
Linus Torvalds committed
553
	if (ret == -ENOENT)
554
		return nfserr_badowner;
Linus Torvalds's avatar
Linus Torvalds committed
555
	if (ret)
556
		return nfserrno(ret);
Linus Torvalds's avatar
Linus Torvalds committed
557
	*id = item->id;
558
	cache_put(&item->h, nn->nametoid_cache);
Linus Torvalds's avatar
Linus Torvalds committed
559 560 561
	return 0;
}

562
static __be32 encode_ascii_id(struct xdr_stream *xdr, u32 id)
563 564 565
{
	char buf[11];
	int len;
566
	__be32 *p;
567 568

	len = sprintf(buf, "%u", id);
569 570
	p = xdr_reserve_space(xdr, len + 4);
	if (!p)
571
		return nfserr_resource;
572
	p = xdr_encode_opaque(p, buf, len);
573 574 575
	return 0;
}

576 577
static __be32 idmap_id_to_name(struct xdr_stream *xdr,
			       struct svc_rqst *rqstp, int type, u32 id)
Linus Torvalds's avatar
Linus Torvalds committed
578 579 580 581 582
{
	struct ent *item, key = {
		.id = id,
		.type = type,
	};
583
	__be32 *p;
Linus Torvalds's avatar
Linus Torvalds committed
584
	int ret;
585
	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds's avatar
Linus Torvalds committed
586

587
	strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
588
	ret = idmap_lookup(rqstp, idtoname_lookup, &key, nn->idtoname_cache, &item);
Linus Torvalds's avatar
Linus Torvalds committed
589
	if (ret == -ENOENT)
590
		return encode_ascii_id(xdr, id);
Linus Torvalds's avatar
Linus Torvalds committed
591
	if (ret)
592
		return nfserrno(ret);
Linus Torvalds's avatar
Linus Torvalds committed
593
	ret = strlen(item->name);
594
	WARN_ON_ONCE(ret > IDMAP_NAMESZ);
595 596
	p = xdr_reserve_space(xdr, ret + 4);
	if (!p)
597
		return nfserr_resource;
598
	p = xdr_encode_opaque(p, item->name, ret);
599
	cache_put(&item->h, nn->idtoname_cache);
600
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
601 602
}

603
static bool
604
numeric_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen, u32 *id)
605 606 607 608 609 610 611 612 613 614
{
	int ret;
	char buf[11];

	if (namelen + 1 > sizeof(buf))
		/* too long to represent a 32-bit id: */
		return false;
	/* Just to make sure it's null-terminated: */
	memcpy(buf, name, namelen);
	buf[namelen] = '\0';
615
	ret = kstrtouint(buf, 10, id);
616 617 618 619
	return ret == 0;
}

static __be32
620
do_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen, u32 *id)
621
{
622
	if (nfs4_disable_idmapping && rqstp->rq_cred.cr_flavor < RPC_AUTH_GSS)
623 624 625 626 627 628 629 630 631
		if (numeric_name_to_id(rqstp, type, name, namelen, id))
			return 0;
		/*
		 * otherwise, fall through and try idmapping, for
		 * backwards compatibility with clients sending names:
		 */
	return idmap_name_to_id(rqstp, type, name, namelen, id);
}

632 633
static __be32 encode_name_from_id(struct xdr_stream *xdr,
				  struct svc_rqst *rqstp, int type, u32 id)
634
{
635
	if (nfs4_disable_idmapping && rqstp->rq_cred.cr_flavor < RPC_AUTH_GSS)
636 637
		return encode_ascii_id(xdr, id);
	return idmap_id_to_name(xdr, rqstp, type, id);
638 639
}

640
__be32
Linus Torvalds's avatar
Linus Torvalds committed
641
nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
642
		kuid_t *uid)
Linus Torvalds's avatar
Linus Torvalds committed
643
{
644 645
	__be32 status;
	u32 id = -1;
646 647 648 649

	if (name == NULL || namelen == 0)
		return nfserr_inval;

650
	status = do_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, &id);
651
	*uid = make_kuid(nfsd_user_namespace(rqstp), id);
652 653 654
	if (!uid_valid(*uid))
		status = nfserr_badowner;
	return status;
Linus Torvalds's avatar
Linus Torvalds committed
655 656
}

657
__be32
Linus Torvalds's avatar
Linus Torvalds committed
658
nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
659
		kgid_t *gid)
Linus Torvalds's avatar
Linus Torvalds committed
660
{
661 662
	__be32 status;
	u32 id = -1;
663 664 665 666

	if (name == NULL || namelen == 0)
		return nfserr_inval;

667
	status = do_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, &id);
668
	*gid = make_kgid(nfsd_user_namespace(rqstp), id);
669 670 671
	if (!gid_valid(*gid))
		status = nfserr_badowner;
	return status;
Linus Torvalds's avatar
Linus Torvalds committed
672 673
}

674 675
__be32 nfsd4_encode_user(struct xdr_stream *xdr, struct svc_rqst *rqstp,
			 kuid_t uid)
Linus Torvalds's avatar
Linus Torvalds committed
676
{
677
	u32 id = from_kuid_munged(nfsd_user_namespace(rqstp), uid);
678
	return encode_name_from_id(xdr, rqstp, IDMAP_TYPE_USER, id);
Linus Torvalds's avatar
Linus Torvalds committed
679 680
}

681 682
__be32 nfsd4_encode_group(struct xdr_stream *xdr, struct svc_rqst *rqstp,
			  kgid_t gid)
Linus Torvalds's avatar
Linus Torvalds committed
683
{
684
	u32 id = from_kgid_munged(nfsd_user_namespace(rqstp), gid);
685
	return encode_name_from_id(xdr, rqstp, IDMAP_TYPE_GROUP, id);
Linus Torvalds's avatar
Linus Torvalds committed
686
}