blob: d13403e33622eaade5d483d4453f7d58ff4b9a8f [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
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -070014int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fields1269bc62007-07-17 04:04:52 -070015{
16 struct exp_flavor_info *f;
17 struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
18
19 for (f = exp->ex_flavors; f < end; f++) {
20 if (f->pseudoflavor == rqstp->rq_flavor)
21 return f->flags;
22 }
23 return exp->ex_flags;
24
25}
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
28{
NeilBrown54cceeb2006-04-10 22:55:30 -070029 struct svc_cred cred = rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 int i;
J. Bruce Fields1269bc62007-07-17 04:04:52 -070031 int flags = nfsexp_flags(rqstp, exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 int ret;
33
J. Bruce Fields1269bc62007-07-17 04:04:52 -070034 if (flags & NFSEXP_ALLSQUASH) {
NeilBrown54cceeb2006-04-10 22:55:30 -070035 cred.cr_uid = exp->ex_anon_uid;
36 cred.cr_gid = exp->ex_anon_gid;
37 cred.cr_group_info = groups_alloc(0);
J. Bruce Fields1269bc62007-07-17 04:04:52 -070038 } else if (flags & NFSEXP_ROOTSQUASH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct group_info *gi;
NeilBrown54cceeb2006-04-10 22:55:30 -070040 if (!cred.cr_uid)
41 cred.cr_uid = exp->ex_anon_uid;
42 if (!cred.cr_gid)
43 cred.cr_gid = exp->ex_anon_gid;
44 gi = groups_alloc(cred.cr_group_info->ngroups);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (gi)
NeilBrown54cceeb2006-04-10 22:55:30 -070046 for (i = 0; i < cred.cr_group_info->ngroups; i++) {
47 if (!GROUP_AT(cred.cr_group_info, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 GROUP_AT(gi, i) = exp->ex_anon_gid;
49 else
NeilBrown54cceeb2006-04-10 22:55:30 -070050 GROUP_AT(gi, i) = GROUP_AT(cred.cr_group_info, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
NeilBrown54cceeb2006-04-10 22:55:30 -070052 cred.cr_group_info = gi;
53 } else
54 get_group_info(cred.cr_group_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
NeilBrown54cceeb2006-04-10 22:55:30 -070056 if (cred.cr_uid != (uid_t) -1)
57 current->fsuid = cred.cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 else
59 current->fsuid = exp->ex_anon_uid;
NeilBrown54cceeb2006-04-10 22:55:30 -070060 if (cred.cr_gid != (gid_t) -1)
61 current->fsgid = cred.cr_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 else
63 current->fsgid = exp->ex_anon_gid;
64
NeilBrown54cceeb2006-04-10 22:55:30 -070065 if (!cred.cr_group_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return -ENOMEM;
NeilBrown54cceeb2006-04-10 22:55:30 -070067 ret = set_current_groups(cred.cr_group_info);
68 put_group_info(cred.cr_group_info);
69 if ((cred.cr_uid)) {
Andrew Morgane338d262008-02-04 22:29:42 -080070 current->cap_effective =
71 cap_drop_nfsd_set(current->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 } else {
Andrew Morgane338d262008-02-04 22:29:42 -080073 current->cap_effective =
74 cap_raise_nfsd_set(current->cap_effective,
75 current->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77 return ret;
78}