blob: ad354d284cf8248526c0facd53da8a12ee8dd789 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/nfsd/nfsd.h>
Harvey Harrisona254b242008-02-20 12:49:00 -08009#include "auth.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -070011int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fields1269bc62007-07-17 04:04:52 -070012{
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 Torvalds1da177e2005-04-16 15:20:36 -070024int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
25{
David Howellsd84f4f92008-11-14 10:39:23 +110026 struct group_info *rqgi;
27 struct group_info *gi;
28 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 int i;
J. Bruce Fields1269bc62007-07-17 04:04:52 -070030 int flags = nfsexp_flags(rqstp, exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 int ret;
32
David Howellse0e81732009-09-02 09:13:40 +010033 validate_process_creds();
34
David Howells3b11a1d2008-11-14 10:39:26 +110035 /* discard any old override before preparing the new set */
36 revert_creds(get_cred(current->real_cred));
David Howellsd84f4f92008-11-14 10:39:23 +110037 new = prepare_creds();
38 if (!new)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +110040
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 Fieldsbf935a72009-01-20 19:32:59 -050050 if (!gi)
51 goto oom;
David Howellsd84f4f92008-11-14 10:39:23 +110052 } 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 Torvalds1da177e2005-04-16 15:20:36 -070068 } else {
David Howellsd84f4f92008-11-14 10:39:23 +110069 gi = get_group_info(rqgi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
David Howellsd84f4f92008-11-14 10:39:23 +110071
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 Howellsf05ef8d2009-01-05 17:19:37 +000079 if (ret < 0)
David Howellsd84f4f92008-11-14 10:39:23 +110080 goto error;
81
David Howellsf05ef8d2009-01-05 17:19:37 +000082 if (new->fsuid)
David Howellsd84f4f92008-11-14 10:39:23 +110083 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 Howellse0e81732009-09-02 09:13:40 +010087 validate_process_creds();
David Howells3b11a1d2008-11-14 10:39:26 +110088 put_cred(override_creds(new));
J. Bruce Fieldsb9141522009-01-20 19:34:22 -050089 put_cred(new);
David Howellse0e81732009-09-02 09:13:40 +010090 validate_process_creds();
David Howells3b11a1d2008-11-14 10:39:26 +110091 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +110092
93oom:
94 ret = -ENOMEM;
95error:
96 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return ret;
98}
David Howellsb6dff3e2008-11-14 10:39:16 +110099