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
{
	struct exp_flavor_info *f;
	struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;

	for (f = exp->ex_flavors; f < end; f++) {
13
		if (f->pseudoflavor == rqstp->rq_cred.cr_flavor)
14 15 16 17 18 19
			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 29
	validate_process_creds();

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

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

D
David Howells 已提交
53 54 55 56 57
		gi = groups_alloc(rqgi->ngroups);
		if (!gi)
			goto oom;

		for (i = 0; i < rqgi->ngroups; i++) {
58
			if (gid_eq(GLOBAL_ROOT_GID, GROUP_AT(rqgi, i)))
59
				GROUP_AT(gi, i) = exp->ex_anon_gid;
D
David Howells 已提交
60 61 62
			else
				GROUP_AT(gi, i) = GROUP_AT(rqgi, i);
		}
L
Linus Torvalds 已提交
63
	} else {
D
David Howells 已提交
64
		gi = get_group_info(rqgi);
L
Linus Torvalds 已提交
65
	}
D
David Howells 已提交
66

67
	if (uid_eq(new->fsuid, INVALID_UID))
D
David Howells 已提交
68
		new->fsuid = exp->ex_anon_uid;
69
	if (gid_eq(new->fsgid, INVALID_GID))
D
David Howells 已提交
70 71
		new->fsgid = exp->ex_anon_gid;

72
	set_groups(new, gi);
D
David Howells 已提交
73 74
	put_group_info(gi);

75
	if (!uid_eq(new->fsuid, GLOBAL_ROOT_UID))
D
David Howells 已提交
76 77 78 79
		new->cap_effective = cap_drop_nfsd_set(new->cap_effective);
	else
		new->cap_effective = cap_raise_nfsd_set(new->cap_effective,
							new->cap_permitted);
80
	validate_process_creds();
81
	put_cred(override_creds(new));
82
	put_cred(new);
83
	validate_process_creds();
84
	return 0;
D
David Howells 已提交
85 86 87

oom:
	abort_creds(new);
88
	return -ENOMEM;
L
Linus Torvalds 已提交
89
}
90