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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/nfsd/nfsd.h> |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 9 | #include "auth.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
J. Bruce Fields | c7d5140 | 2007-07-19 01:49:20 -0700 | [diff] [blame] | 11 | int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp) |
J. Bruce Fields | 1269bc6 | 2007-07-17 04:04:52 -0700 | [diff] [blame] | 12 | { |
| 13 | struct exp_flavor_info *f; |
| 14 | struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors; |
| 15 | |
| 16 | for (f = exp->ex_flavors; f < end; f++) { |
| 17 | if (f->pseudoflavor == rqstp->rq_flavor) |
| 18 | return f->flags; |
| 19 | } |
| 20 | return exp->ex_flags; |
| 21 | |
| 22 | } |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp) |
| 25 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 26 | struct group_info *rqgi; |
| 27 | struct group_info *gi; |
| 28 | struct cred *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | int i; |
J. Bruce Fields | 1269bc6 | 2007-07-17 04:04:52 -0700 | [diff] [blame] | 30 | int flags = nfsexp_flags(rqstp, exp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | int ret; |
| 32 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 33 | validate_process_creds(); |
| 34 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 35 | /* discard any old override before preparing the new set */ |
| 36 | revert_creds(get_cred(current->real_cred)); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 37 | new = prepare_creds(); |
| 38 | if (!new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | return -ENOMEM; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 40 | |
| 41 | new->fsuid = rqstp->rq_cred.cr_uid; |
| 42 | new->fsgid = rqstp->rq_cred.cr_gid; |
| 43 | |
| 44 | rqgi = rqstp->rq_cred.cr_group_info; |
| 45 | |
| 46 | if (flags & NFSEXP_ALLSQUASH) { |
| 47 | new->fsuid = exp->ex_anon_uid; |
| 48 | new->fsgid = exp->ex_anon_gid; |
| 49 | gi = groups_alloc(0); |
J. Bruce Fields | bf935a7 | 2009-01-20 19:32:59 -0500 | [diff] [blame] | 50 | if (!gi) |
| 51 | goto oom; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 52 | } else if (flags & NFSEXP_ROOTSQUASH) { |
| 53 | if (!new->fsuid) |
| 54 | new->fsuid = exp->ex_anon_uid; |
| 55 | if (!new->fsgid) |
| 56 | new->fsgid = exp->ex_anon_gid; |
| 57 | |
| 58 | gi = groups_alloc(rqgi->ngroups); |
| 59 | if (!gi) |
| 60 | goto oom; |
| 61 | |
| 62 | for (i = 0; i < rqgi->ngroups; i++) { |
| 63 | if (!GROUP_AT(rqgi, i)) |
| 64 | GROUP_AT(gi, i) = exp->ex_anon_gid; |
| 65 | else |
| 66 | GROUP_AT(gi, i) = GROUP_AT(rqgi, i); |
| 67 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } else { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 69 | gi = get_group_info(rqgi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 71 | |
| 72 | if (new->fsuid == (uid_t) -1) |
| 73 | new->fsuid = exp->ex_anon_uid; |
| 74 | if (new->fsgid == (gid_t) -1) |
| 75 | new->fsgid = exp->ex_anon_gid; |
| 76 | |
| 77 | ret = set_groups(new, gi); |
| 78 | put_group_info(gi); |
David Howells | f05ef8d | 2009-01-05 17:19:37 +0000 | [diff] [blame] | 79 | if (ret < 0) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 80 | goto error; |
| 81 | |
David Howells | f05ef8d | 2009-01-05 17:19:37 +0000 | [diff] [blame] | 82 | if (new->fsuid) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 83 | new->cap_effective = cap_drop_nfsd_set(new->cap_effective); |
| 84 | else |
| 85 | new->cap_effective = cap_raise_nfsd_set(new->cap_effective, |
| 86 | new->cap_permitted); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 87 | validate_process_creds(); |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 88 | put_cred(override_creds(new)); |
J. Bruce Fields | b914152 | 2009-01-20 19:34:22 -0500 | [diff] [blame] | 89 | put_cred(new); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 90 | validate_process_creds(); |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 91 | return 0; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 92 | |
| 93 | oom: |
| 94 | ret = -ENOMEM; |
| 95 | error: |
| 96 | abort_creds(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | return ret; |
| 98 | } |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 99 | |