auth.c 1.9 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)
{
30
	struct svc_cred	cred = rqstp->rq_cred;
L
Linus Torvalds 已提交
31
	int i;
32
	int flags = nfsexp_flags(rqstp, exp);
L
Linus Torvalds 已提交
33 34
	int ret;

35
	if (flags & NFSEXP_ALLSQUASH) {
36 37 38
		cred.cr_uid = exp->ex_anon_uid;
		cred.cr_gid = exp->ex_anon_gid;
		cred.cr_group_info = groups_alloc(0);
39
	} else if (flags & NFSEXP_ROOTSQUASH) {
L
Linus Torvalds 已提交
40
		struct group_info *gi;
41 42 43 44 45
		if (!cred.cr_uid)
			cred.cr_uid = exp->ex_anon_uid;
		if (!cred.cr_gid)
			cred.cr_gid = exp->ex_anon_gid;
		gi = groups_alloc(cred.cr_group_info->ngroups);
L
Linus Torvalds 已提交
46
		if (gi)
47 48
			for (i = 0; i < cred.cr_group_info->ngroups; i++) {
				if (!GROUP_AT(cred.cr_group_info, i))
L
Linus Torvalds 已提交
49 50
					GROUP_AT(gi, i) = exp->ex_anon_gid;
				else
51
					GROUP_AT(gi, i) = GROUP_AT(cred.cr_group_info, i);
L
Linus Torvalds 已提交
52
			}
53 54 55
		cred.cr_group_info = gi;
	} else
		get_group_info(cred.cr_group_info);
L
Linus Torvalds 已提交
56

57 58
	if (cred.cr_uid != (uid_t) -1)
		current->fsuid = cred.cr_uid;
L
Linus Torvalds 已提交
59 60
	else
		current->fsuid = exp->ex_anon_uid;
61 62
	if (cred.cr_gid != (gid_t) -1)
		current->fsgid = cred.cr_gid;
L
Linus Torvalds 已提交
63 64 65
	else
		current->fsgid = exp->ex_anon_gid;

66
	if (!cred.cr_group_info)
L
Linus Torvalds 已提交
67
		return -ENOMEM;
68 69 70
	ret = set_current_groups(cred.cr_group_info);
	put_group_info(cred.cr_group_info);
	if ((cred.cr_uid)) {
71 72
		current->cap_effective =
			cap_drop_nfsd_set(current->cap_effective);
L
Linus Torvalds 已提交
73
	} else {
74 75 76
		current->cap_effective =
			cap_raise_nfsd_set(current->cap_effective,
					   current->cap_permitted);
L
Linus Torvalds 已提交
77 78 79
	}
	return ret;
}