Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux...
[linux-2.6] / fs / nfsd / auth.c
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 #include <linux/nfsd/export.h>
13 #include "auth.h"
14
15 int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
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
28 int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
29 {
30         struct group_info *rqgi;
31         struct group_info *gi;
32         struct cred *new;
33         int i;
34         int flags = nfsexp_flags(rqstp, exp);
35         int ret;
36
37         /* discard any old override before preparing the new set */
38         revert_creds(get_cred(current->real_cred));
39         new = prepare_creds();
40         if (!new)
41                 return -ENOMEM;
42
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);
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                 }
68         } else {
69                 gi = get_group_info(rqgi);
70         }
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);
79         if (ret < 0)
80                 goto error;
81
82         if (new->fsuid)
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);
87         put_cred(override_creds(new));
88         return 0;
89
90 oom:
91         ret = -ENOMEM;
92 error:
93         abort_creds(new);
94         return ret;
95 }
96