blob: 5573508f707fa5cd652da4503049fa6e89c0f9f8 [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
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 Fieldsc7d51402007-07-19 01:49:20 -070012#include <linux/nfsd/export.h>
Harvey Harrisona254b242008-02-20 12:49:00 -080013#include "auth.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -070015int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fields1269bc62007-07-17 04:04:52 -070016{
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 Torvalds1da177e2005-04-16 15:20:36 -070028int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
29{
David Howellsd84f4f92008-11-14 10:39:23 +110030 struct group_info *rqgi;
31 struct group_info *gi;
32 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 int i;
J. Bruce Fields1269bc62007-07-17 04:04:52 -070034 int flags = nfsexp_flags(rqstp, exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 int ret;
36
David Howells3b11a1d2008-11-14 10:39:26 +110037 /* discard any old override before preparing the new set */
38 revert_creds(get_cred(current->real_cred));
David Howellsd84f4f92008-11-14 10:39:23 +110039 new = prepare_creds();
40 if (!new)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +110042
43 new->fsuid = rqstp->rq_cred.cr_uid;
44 new->fsgid = rqstp->rq_cred.cr_gid;
45
46 rqgi = rqstp->rq_cred.cr_group_info;
47
48 if (flags & NFSEXP_ALLSQUASH) {
49 new->fsuid = exp->ex_anon_uid;
50 new->fsgid = exp->ex_anon_gid;
51 gi = groups_alloc(0);
J. Bruce Fieldsbf935a72009-01-20 19:32:59 -050052 if (!gi)
53 goto oom;
David Howellsd84f4f92008-11-14 10:39:23 +110054 } else if (flags & NFSEXP_ROOTSQUASH) {
55 if (!new->fsuid)
56 new->fsuid = exp->ex_anon_uid;
57 if (!new->fsgid)
58 new->fsgid = exp->ex_anon_gid;
59
60 gi = groups_alloc(rqgi->ngroups);
61 if (!gi)
62 goto oom;
63
64 for (i = 0; i < rqgi->ngroups; i++) {
65 if (!GROUP_AT(rqgi, i))
66 GROUP_AT(gi, i) = exp->ex_anon_gid;
67 else
68 GROUP_AT(gi, i) = GROUP_AT(rqgi, i);
69 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 } else {
David Howellsd84f4f92008-11-14 10:39:23 +110071 gi = get_group_info(rqgi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
David Howellsd84f4f92008-11-14 10:39:23 +110073
74 if (new->fsuid == (uid_t) -1)
75 new->fsuid = exp->ex_anon_uid;
76 if (new->fsgid == (gid_t) -1)
77 new->fsgid = exp->ex_anon_gid;
78
79 ret = set_groups(new, gi);
80 put_group_info(gi);
David Howellsf05ef8d2009-01-05 17:19:37 +000081 if (ret < 0)
David Howellsd84f4f92008-11-14 10:39:23 +110082 goto error;
83
David Howellsf05ef8d2009-01-05 17:19:37 +000084 if (new->fsuid)
David Howellsd84f4f92008-11-14 10:39:23 +110085 new->cap_effective = cap_drop_nfsd_set(new->cap_effective);
86 else
87 new->cap_effective = cap_raise_nfsd_set(new->cap_effective,
88 new->cap_permitted);
David Howells3b11a1d2008-11-14 10:39:26 +110089 put_cred(override_creds(new));
J. Bruce Fieldsb9141522009-01-20 19:34:22 -050090 put_cred(new);
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