auth.c 2.0 KB
Newer Older
1
/* Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> */
L
Linus Torvalds 已提交
2 3

#include <linux/sched.h>
4
#include "nfsd.h"
H
Harvey Harrison 已提交
5
#include "auth.h"
L
Linus Torvalds 已提交
6

J
J. Bruce Fields 已提交
7
int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
8 9 10 11 12 13 14 15 16 17 18 19
{
	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 已提交
20 21
int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
{
D
David Howells 已提交
22 23 24
	struct group_info *rqgi;
	struct group_info *gi;
	struct cred *new;
L
Linus Torvalds 已提交
25
	int i;
26
	int flags = nfsexp_flags(rqstp, exp);
L
Linus Torvalds 已提交
27 28
	int ret;

29 30
	validate_process_creds();

31 32
	/* discard any old override before preparing the new set */
	revert_creds(get_cred(current->real_cred));
D
David Howells 已提交
33 34 35 36 37 38 39 40 41
	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;

42
	if (flags & NFSEXP_ALLSQUASH) {
D
David Howells 已提交
43 44 45
		new->fsuid = exp->ex_anon_uid;
		new->fsgid = exp->ex_anon_gid;
		gi = groups_alloc(0);
46 47
		if (!gi)
			goto oom;
48
	} else if (flags & NFSEXP_ROOTSQUASH) {
D
David Howells 已提交
49 50 51 52
		if (!new->fsuid)
			new->fsuid = exp->ex_anon_uid;
		if (!new->fsgid)
			new->fsgid = exp->ex_anon_gid;
L
Linus Torvalds 已提交
53

D
David Howells 已提交
54 55 56 57 58 59 60 61 62 63
		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 已提交
64
	} else {
D
David Howells 已提交
65
		gi = get_group_info(rqgi);
L
Linus Torvalds 已提交
66
	}
D
David Howells 已提交
67 68 69 70 71 72 73 74

	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);
D
David Howells 已提交
75
	if (ret < 0)
D
David Howells 已提交
76 77
		goto error;

D
David Howells 已提交
78
	if (new->fsuid)
D
David Howells 已提交
79 80 81 82
		new->cap_effective = cap_drop_nfsd_set(new->cap_effective);
	else
		new->cap_effective = cap_raise_nfsd_set(new->cap_effective,
							new->cap_permitted);
83
	validate_process_creds();
84
	put_cred(override_creds(new));
85
	put_cred(new);
86
	validate_process_creds();
87
	return 0;
D
David Howells 已提交
88 89 90 91 92

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