blob: 6e92b0fe5323483cd6ce3bd25fd6d5a211d9589b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfsd/auth.c
3 *
4 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
5 */
6
7#include <linux/types.h>
8#include <linux/sched.h>
9#include <linux/sunrpc/svc.h>
10#include <linux/sunrpc/svcauth.h>
11#include <linux/nfsd/nfsd.h>
12
13#define CAP_NFSD_MASK (CAP_FS_MASK|CAP_TO_MASK(CAP_SYS_RESOURCE))
14
15int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
16{
NeilBrown54cceeb2006-04-10 22:55:30 -070017 struct svc_cred cred = rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 int i;
19 int ret;
20
21 if (exp->ex_flags & NFSEXP_ALLSQUASH) {
NeilBrown54cceeb2006-04-10 22:55:30 -070022 cred.cr_uid = exp->ex_anon_uid;
23 cred.cr_gid = exp->ex_anon_gid;
24 cred.cr_group_info = groups_alloc(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 } else if (exp->ex_flags & NFSEXP_ROOTSQUASH) {
26 struct group_info *gi;
NeilBrown54cceeb2006-04-10 22:55:30 -070027 if (!cred.cr_uid)
28 cred.cr_uid = exp->ex_anon_uid;
29 if (!cred.cr_gid)
30 cred.cr_gid = exp->ex_anon_gid;
31 gi = groups_alloc(cred.cr_group_info->ngroups);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 if (gi)
NeilBrown54cceeb2006-04-10 22:55:30 -070033 for (i = 0; i < cred.cr_group_info->ngroups; i++) {
34 if (!GROUP_AT(cred.cr_group_info, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 GROUP_AT(gi, i) = exp->ex_anon_gid;
36 else
NeilBrown54cceeb2006-04-10 22:55:30 -070037 GROUP_AT(gi, i) = GROUP_AT(cred.cr_group_info, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 }
NeilBrown54cceeb2006-04-10 22:55:30 -070039 cred.cr_group_info = gi;
40 } else
41 get_group_info(cred.cr_group_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
NeilBrown54cceeb2006-04-10 22:55:30 -070043 if (cred.cr_uid != (uid_t) -1)
44 current->fsuid = cred.cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 else
46 current->fsuid = exp->ex_anon_uid;
NeilBrown54cceeb2006-04-10 22:55:30 -070047 if (cred.cr_gid != (gid_t) -1)
48 current->fsgid = cred.cr_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 else
50 current->fsgid = exp->ex_anon_gid;
51
NeilBrown54cceeb2006-04-10 22:55:30 -070052 if (!cred.cr_group_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return -ENOMEM;
NeilBrown54cceeb2006-04-10 22:55:30 -070054 ret = set_current_groups(cred.cr_group_info);
55 put_group_info(cred.cr_group_info);
56 if ((cred.cr_uid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 cap_t(current->cap_effective) &= ~CAP_NFSD_MASK;
58 } else {
59 cap_t(current->cap_effective) |= (CAP_NFSD_MASK &
60 current->cap_permitted);
61 }
62 return ret;
63}