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> |
| 12 | |
| 13 | #define CAP_NFSD_MASK (CAP_FS_MASK|CAP_TO_MASK(CAP_SYS_RESOURCE)) |
| 14 | |
| 15 | int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp) |
| 16 | { |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 17 | struct svc_cred cred = rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | int i; |
| 19 | int ret; |
| 20 | |
| 21 | if (exp->ex_flags & NFSEXP_ALLSQUASH) { |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 22 | cred.cr_uid = exp->ex_anon_uid; |
| 23 | cred.cr_gid = exp->ex_anon_gid; |
| 24 | cred.cr_group_info = groups_alloc(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | } else if (exp->ex_flags & NFSEXP_ROOTSQUASH) { |
| 26 | struct group_info *gi; |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 27 | if (!cred.cr_uid) |
| 28 | cred.cr_uid = exp->ex_anon_uid; |
| 29 | if (!cred.cr_gid) |
| 30 | cred.cr_gid = exp->ex_anon_gid; |
| 31 | gi = groups_alloc(cred.cr_group_info->ngroups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | if (gi) |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 33 | for (i = 0; i < cred.cr_group_info->ngroups; i++) { |
| 34 | if (!GROUP_AT(cred.cr_group_info, i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | GROUP_AT(gi, i) = exp->ex_anon_gid; |
| 36 | else |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 37 | GROUP_AT(gi, i) = GROUP_AT(cred.cr_group_info, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 39 | cred.cr_group_info = gi; |
| 40 | } else |
| 41 | get_group_info(cred.cr_group_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 43 | if (cred.cr_uid != (uid_t) -1) |
| 44 | current->fsuid = cred.cr_uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | else |
| 46 | current->fsuid = exp->ex_anon_uid; |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 47 | if (cred.cr_gid != (gid_t) -1) |
| 48 | current->fsgid = cred.cr_gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | else |
| 50 | current->fsgid = exp->ex_anon_gid; |
| 51 | |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 52 | if (!cred.cr_group_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | return -ENOMEM; |
NeilBrown | 54cceeb | 2006-04-10 22:55:30 -0700 | [diff] [blame] | 54 | ret = set_current_groups(cred.cr_group_info); |
| 55 | put_group_info(cred.cr_group_info); |
| 56 | if ((cred.cr_uid)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | cap_t(current->cap_effective) &= ~CAP_NFSD_MASK; |
| 58 | } else { |
| 59 | cap_t(current->cap_effective) |= (CAP_NFSD_MASK & |
| 60 | current->cap_permitted); |
| 61 | } |
| 62 | return ret; |
| 63 | } |