blob: 808fc03a6fbd07a4678d1ba0e28a43eb9d2c1ecd [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>
Harvey Harrisona254b242008-02-20 12:49:00 -080013#include "auth.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -070015int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fields1269bc62007-07-17 04:04:52 -070016{
17 struct exp_flavor_info *f;
18 struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
19
20 for (f = exp->ex_flavors; f < end; f++) {
21 if (f->pseudoflavor == rqstp->rq_flavor)
22 return f->flags;
23 }
24 return exp->ex_flags;
25
26}
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
29{
David Howellsb6dff3e2008-11-14 10:39:16 +110030 struct cred *act_as = current->cred ;
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)
David Howellsb6dff3e2008-11-14 10:39:16 +110059 act_as->fsuid = cred.cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 else
David Howellsb6dff3e2008-11-14 10:39:16 +110061 act_as->fsuid = exp->ex_anon_uid;
NeilBrown54cceeb2006-04-10 22:55:30 -070062 if (cred.cr_gid != (gid_t) -1)
David Howellsb6dff3e2008-11-14 10:39:16 +110063 act_as->fsgid = cred.cr_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 else
David Howellsb6dff3e2008-11-14 10:39:16 +110065 act_as->fsgid = exp->ex_anon_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
NeilBrown54cceeb2006-04-10 22:55:30 -070067 if (!cred.cr_group_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return -ENOMEM;
David Howellsb6dff3e2008-11-14 10:39:16 +110069 ret = set_groups(act_as, cred.cr_group_info);
NeilBrown54cceeb2006-04-10 22:55:30 -070070 put_group_info(cred.cr_group_info);
71 if ((cred.cr_uid)) {
David Howellsb6dff3e2008-11-14 10:39:16 +110072 act_as->cap_effective =
73 cap_drop_nfsd_set(act_as->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 } else {
David Howellsb6dff3e2008-11-14 10:39:16 +110075 act_as->cap_effective =
76 cap_raise_nfsd_set(act_as->cap_effective,
77 act_as->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79 return ret;
80}
David Howellsb6dff3e2008-11-14 10:39:16 +110081