blob: 294992e9bf69e18b17a52964dd0af23841894f26 [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{
NeilBrown54cceeb2006-04-10 22:55:30 -070030 struct svc_cred cred = rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 int i;
J. Bruce Fields1269bc62007-07-17 04:04:52 -070032 int flags = nfsexp_flags(rqstp, exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 int ret;
34
J. Bruce Fields1269bc62007-07-17 04:04:52 -070035 if (flags & NFSEXP_ALLSQUASH) {
NeilBrown54cceeb2006-04-10 22:55:30 -070036 cred.cr_uid = exp->ex_anon_uid;
37 cred.cr_gid = exp->ex_anon_gid;
38 cred.cr_group_info = groups_alloc(0);
J. Bruce Fields1269bc62007-07-17 04:04:52 -070039 } else if (flags & NFSEXP_ROOTSQUASH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct group_info *gi;
NeilBrown54cceeb2006-04-10 22:55:30 -070041 if (!cred.cr_uid)
42 cred.cr_uid = exp->ex_anon_uid;
43 if (!cred.cr_gid)
44 cred.cr_gid = exp->ex_anon_gid;
45 gi = groups_alloc(cred.cr_group_info->ngroups);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 if (gi)
NeilBrown54cceeb2006-04-10 22:55:30 -070047 for (i = 0; i < cred.cr_group_info->ngroups; i++) {
48 if (!GROUP_AT(cred.cr_group_info, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 GROUP_AT(gi, i) = exp->ex_anon_gid;
50 else
NeilBrown54cceeb2006-04-10 22:55:30 -070051 GROUP_AT(gi, i) = GROUP_AT(cred.cr_group_info, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
NeilBrown54cceeb2006-04-10 22:55:30 -070053 cred.cr_group_info = gi;
54 } else
55 get_group_info(cred.cr_group_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
NeilBrown54cceeb2006-04-10 22:55:30 -070057 if (cred.cr_uid != (uid_t) -1)
58 current->fsuid = cred.cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 else
60 current->fsuid = exp->ex_anon_uid;
NeilBrown54cceeb2006-04-10 22:55:30 -070061 if (cred.cr_gid != (gid_t) -1)
62 current->fsgid = cred.cr_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 else
64 current->fsgid = exp->ex_anon_gid;
65
NeilBrown54cceeb2006-04-10 22:55:30 -070066 if (!cred.cr_group_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return -ENOMEM;
NeilBrown54cceeb2006-04-10 22:55:30 -070068 ret = set_current_groups(cred.cr_group_info);
69 put_group_info(cred.cr_group_info);
70 if ((cred.cr_uid)) {
Andrew Morgane338d262008-02-04 22:29:42 -080071 current->cap_effective =
72 cap_drop_nfsd_set(current->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 } else {
Andrew Morgane338d262008-02-04 22:29:42 -080074 current->cap_effective =
75 cap_raise_nfsd_set(current->cap_effective,
76 current->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78 return ret;
79}