auth.c 2.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * linux/fs/nfsd/auth.c
 *
 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
 */

#include <linux/types.h>
#include <linux/sched.h>
#include <linux/sunrpc/svc.h>
#include <linux/sunrpc/svcauth.h>
#include <linux/nfsd/nfsd.h>
J
J. Bruce Fields 已提交
12
#include <linux/nfsd/export.h>
H
Harvey Harrison 已提交
13
#include "auth.h"
L
Linus Torvalds 已提交
14

J
J. Bruce Fields 已提交
15
int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
16 17 18 19 20 21 22 23 24 25 26 27
{
	struct exp_flavor_info *f;
	struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;

	for (f = exp->ex_flavors; f < end; f++) {
		if (f->pseudoflavor == rqstp->rq_flavor)
			return f->flags;
	}
	return exp->ex_flags;

}

L
Linus Torvalds 已提交
28 29
int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
{
D
David Howells 已提交
30 31 32
	struct group_info *rqgi;
	struct group_info *gi;
	struct cred *new;
L
Linus Torvalds 已提交
33
	int i;
34
	int flags = nfsexp_flags(rqstp, exp);
L
Linus Torvalds 已提交
35 36
	int ret;

37 38
	/* discard any old override before preparing the new set */
	revert_creds(get_cred(current->real_cred));
D
David Howells 已提交
39 40 41 42 43 44 45 46 47
	new = prepare_creds();
	if (!new)
		return -ENOMEM;

	new->fsuid = rqstp->rq_cred.cr_uid;
	new->fsgid = rqstp->rq_cred.cr_gid;

	rqgi = rqstp->rq_cred.cr_group_info;

48
	if (flags & NFSEXP_ALLSQUASH) {
D
David Howells 已提交
49 50 51
		new->fsuid = exp->ex_anon_uid;
		new->fsgid = exp->ex_anon_gid;
		gi = groups_alloc(0);
52
	} else if (flags & NFSEXP_ROOTSQUASH) {
D
David Howells 已提交
53 54 55 56
		if (!new->fsuid)
			new->fsuid = exp->ex_anon_uid;
		if (!new->fsgid)
			new->fsgid = exp->ex_anon_gid;
L
Linus Torvalds 已提交
57

D
David Howells 已提交
58 59 60 61 62 63 64 65 66 67
		gi = groups_alloc(rqgi->ngroups);
		if (!gi)
			goto oom;

		for (i = 0; i < rqgi->ngroups; i++) {
			if (!GROUP_AT(rqgi, i))
				GROUP_AT(gi, i) = exp->ex_anon_gid;
			else
				GROUP_AT(gi, i) = GROUP_AT(rqgi, i);
		}
L
Linus Torvalds 已提交
68
	} else {
D
David Howells 已提交
69
		gi = get_group_info(rqgi);
L
Linus Torvalds 已提交
70
	}
D
David Howells 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

	if (new->fsuid == (uid_t) -1)
		new->fsuid = exp->ex_anon_uid;
	if (new->fsgid == (gid_t) -1)
		new->fsgid = exp->ex_anon_gid;

	ret = set_groups(new, gi);
	put_group_info(gi);
	if (!ret)
		goto error;

	if (new->uid)
		new->cap_effective = cap_drop_nfsd_set(new->cap_effective);
	else
		new->cap_effective = cap_raise_nfsd_set(new->cap_effective,
							new->cap_permitted);
87 88
	put_cred(override_creds(new));
	return 0;
D
David Howells 已提交
89 90 91 92 93

oom:
	ret = -ENOMEM;
error:
	abort_creds(new);
L
Linus Torvalds 已提交
94 95
	return ret;
}
96