Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Fields | c7d5140 | 2007-07-19 01:49:20 -0700 | [diff] [blame] | 12 | #include <linux/nfsd/export.h> |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 13 | #include "auth.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
J. Bruce Fields | c7d5140 | 2007-07-19 01:49:20 -0700 | [diff] [blame] | 15 | int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp) |
J. Bruce Fields | 1269bc6 | 2007-07-17 04:04:52 -0700 | [diff] [blame] | 16 | { |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp) |
| 29 | { |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 30 | struct svc_cred cred = rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | int i; |
J. Bruce Fields | 1269bc6 | 2007-07-17 04:04:52 -0700 | [diff] [blame] | 32 | int flags = nfsexp_flags(rqstp, exp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | int ret; |
| 34 | |
J. Bruce Fields | 1269bc6 | 2007-07-17 04:04:52 -0700 | [diff] [blame] | 35 | if (flags & NFSEXP_ALLSQUASH) { |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 36 | 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 Fields | 1269bc6 | 2007-07-17 04:04:52 -0700 | [diff] [blame] | 39 | } else if (flags & NFSEXP_ROOTSQUASH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | struct group_info *gi; |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 41 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | if (gi) |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 47 | for (i = 0; i < cred.cr_group_info->ngroups; i++) { |
| 48 | if (!GROUP_AT(cred.cr_group_info, i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | GROUP_AT(gi, i) = exp->ex_anon_gid; |
| 50 | else |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 51 | GROUP_AT(gi, i) = GROUP_AT(cred.cr_group_info, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 53 | cred.cr_group_info = gi; |
| 54 | } else |
| 55 | get_group_info(cred.cr_group_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 57 | if (cred.cr_uid != (uid_t) -1) |
| 58 | current->fsuid = cred.cr_uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | else |
| 60 | current->fsuid = exp->ex_anon_uid; |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 61 | if (cred.cr_gid != (gid_t) -1) |
| 62 | current->fsgid = cred.cr_gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | else |
| 64 | current->fsgid = exp->ex_anon_gid; |
| 65 | |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 66 | if (!cred.cr_group_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | return -ENOMEM; |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 68 | ret = set_current_groups(cred.cr_group_info); |
| 69 | put_group_info(cred.cr_group_info); |
| 70 | if ((cred.cr_uid)) { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 71 | current->cap_effective = |
| 72 | cap_drop_nfsd_set(current->cap_effective); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } else { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 74 | current->cap_effective = |
| 75 | cap_raise_nfsd_set(current->cap_effective, |
| 76 | current->cap_permitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | return ret; |
| 79 | } |