blob: 21928056e35ecad1c8aa5cb656d9d495df6a3022 [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>
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -070012#include <linux/nfsd/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#define CAP_NFSD_MASK (CAP_FS_MASK|CAP_TO_MASK(CAP_SYS_RESOURCE))
15
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -070016int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fields1269bc62007-07-17 04:04:52 -070017{
18 struct exp_flavor_info *f;
19 struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
20
21 for (f = exp->ex_flavors; f < end; f++) {
22 if (f->pseudoflavor == rqstp->rq_flavor)
23 return f->flags;
24 }
25 return exp->ex_flags;
26
27}
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
30{
NeilBrown54cceeb2006-04-10 22:55:30 -070031 struct svc_cred cred = rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 int i;
J. Bruce Fields1269bc62007-07-17 04:04:52 -070033 int flags = nfsexp_flags(rqstp, exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 int ret;
35
J. Bruce Fields1269bc62007-07-17 04:04:52 -070036 if (flags & NFSEXP_ALLSQUASH) {
NeilBrown54cceeb2006-04-10 22:55:30 -070037 cred.cr_uid = exp->ex_anon_uid;
38 cred.cr_gid = exp->ex_anon_gid;
39 cred.cr_group_info = groups_alloc(0);
J. Bruce Fields1269bc62007-07-17 04:04:52 -070040 } else if (flags & NFSEXP_ROOTSQUASH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 struct group_info *gi;
NeilBrown54cceeb2006-04-10 22:55:30 -070042 if (!cred.cr_uid)
43 cred.cr_uid = exp->ex_anon_uid;
44 if (!cred.cr_gid)
45 cred.cr_gid = exp->ex_anon_gid;
46 gi = groups_alloc(cred.cr_group_info->ngroups);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if (gi)
NeilBrown54cceeb2006-04-10 22:55:30 -070048 for (i = 0; i < cred.cr_group_info->ngroups; i++) {
49 if (!GROUP_AT(cred.cr_group_info, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 GROUP_AT(gi, i) = exp->ex_anon_gid;
51 else
NeilBrown54cceeb2006-04-10 22:55:30 -070052 GROUP_AT(gi, i) = GROUP_AT(cred.cr_group_info, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 }
NeilBrown54cceeb2006-04-10 22:55:30 -070054 cred.cr_group_info = gi;
55 } else
56 get_group_info(cred.cr_group_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
NeilBrown54cceeb2006-04-10 22:55:30 -070058 if (cred.cr_uid != (uid_t) -1)
59 current->fsuid = cred.cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 else
61 current->fsuid = exp->ex_anon_uid;
NeilBrown54cceeb2006-04-10 22:55:30 -070062 if (cred.cr_gid != (gid_t) -1)
63 current->fsgid = cred.cr_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 else
65 current->fsgid = exp->ex_anon_gid;
66
NeilBrown54cceeb2006-04-10 22:55:30 -070067 if (!cred.cr_group_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return -ENOMEM;
NeilBrown54cceeb2006-04-10 22:55:30 -070069 ret = set_current_groups(cred.cr_group_info);
70 put_group_info(cred.cr_group_info);
71 if ((cred.cr_uid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 cap_t(current->cap_effective) &= ~CAP_NFSD_MASK;
73 } else {
74 cap_t(current->cap_effective) |= (CAP_NFSD_MASK &
75 current->cap_permitted);
76 }
77 return ret;
78}