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