Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[firefly-linux-kernel-4.4.55.git] / net / sunrpc / auth_generic.c
1 /*
2  * Generic RPC credential
3  *
4  * Copyright (C) 2008, Trond Myklebust <Trond.Myklebust@netapp.com>
5  */
6
7 #include <linux/err.h>
8 #include <linux/slab.h>
9 #include <linux/types.h>
10 #include <linux/module.h>
11 #include <linux/sched.h>
12 #include <linux/sunrpc/auth.h>
13 #include <linux/sunrpc/clnt.h>
14 #include <linux/sunrpc/debug.h>
15 #include <linux/sunrpc/sched.h>
16
17 #ifdef RPC_DEBUG
18 # define RPCDBG_FACILITY        RPCDBG_AUTH
19 #endif
20
21 #define RPC_MACHINE_CRED_USERID         ((uid_t)0)
22 #define RPC_MACHINE_CRED_GROUPID        ((gid_t)0)
23
24 struct generic_cred {
25         struct rpc_cred gc_base;
26         struct auth_cred acred;
27 };
28
29 static struct rpc_auth generic_auth;
30 static const struct rpc_credops generic_credops;
31
32 /*
33  * Public call interface
34  */
35 struct rpc_cred *rpc_lookup_cred(void)
36 {
37         return rpcauth_lookupcred(&generic_auth, 0);
38 }
39 EXPORT_SYMBOL_GPL(rpc_lookup_cred);
40
41 /*
42  * Public call interface for looking up machine creds.
43  */
44 struct rpc_cred *rpc_lookup_machine_cred(const char *service_name)
45 {
46         struct auth_cred acred = {
47                 .uid = RPC_MACHINE_CRED_USERID,
48                 .gid = RPC_MACHINE_CRED_GROUPID,
49                 .principal = service_name,
50                 .machine_cred = 1,
51         };
52
53         dprintk("RPC:       looking up machine cred for service %s\n",
54                         service_name);
55         return generic_auth.au_ops->lookup_cred(&generic_auth, &acred, 0);
56 }
57 EXPORT_SYMBOL_GPL(rpc_lookup_machine_cred);
58
59 static struct rpc_cred *generic_bind_cred(struct rpc_task *task,
60                 struct rpc_cred *cred, int lookupflags)
61 {
62         struct rpc_auth *auth = task->tk_client->cl_auth;
63         struct auth_cred *acred = &container_of(cred, struct generic_cred, gc_base)->acred;
64
65         return auth->au_ops->lookup_cred(auth, acred, lookupflags);
66 }
67
68 /*
69  * Lookup generic creds for current process
70  */
71 static struct rpc_cred *
72 generic_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
73 {
74         return rpcauth_lookup_credcache(&generic_auth, acred, flags);
75 }
76
77 static struct rpc_cred *
78 generic_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
79 {
80         struct generic_cred *gcred;
81
82         gcred = kmalloc(sizeof(*gcred), GFP_KERNEL);
83         if (gcred == NULL)
84                 return ERR_PTR(-ENOMEM);
85
86         rpcauth_init_cred(&gcred->gc_base, acred, &generic_auth, &generic_credops);
87         gcred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
88
89         gcred->acred.uid = acred->uid;
90         gcred->acred.gid = acred->gid;
91         gcred->acred.group_info = acred->group_info;
92         if (gcred->acred.group_info != NULL)
93                 get_group_info(gcred->acred.group_info);
94         gcred->acred.machine_cred = acred->machine_cred;
95
96         dprintk("RPC:       allocated %s cred %p for uid %d gid %d\n",
97                         gcred->acred.machine_cred ? "machine" : "generic",
98                         gcred, acred->uid, acred->gid);
99         return &gcred->gc_base;
100 }
101
102 static void
103 generic_free_cred(struct rpc_cred *cred)
104 {
105         struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
106
107         dprintk("RPC:       generic_free_cred %p\n", gcred);
108         if (gcred->acred.group_info != NULL)
109                 put_group_info(gcred->acred.group_info);
110         kfree(gcred);
111 }
112
113 static void
114 generic_free_cred_callback(struct rcu_head *head)
115 {
116         struct rpc_cred *cred = container_of(head, struct rpc_cred, cr_rcu);
117         generic_free_cred(cred);
118 }
119
120 static void
121 generic_destroy_cred(struct rpc_cred *cred)
122 {
123         call_rcu(&cred->cr_rcu, generic_free_cred_callback);
124 }
125
126 /*
127  * Match credentials against current process creds.
128  */
129 static int
130 generic_match(struct auth_cred *acred, struct rpc_cred *cred, int flags)
131 {
132         struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
133         int i;
134
135         if (gcred->acred.uid != acred->uid ||
136             gcred->acred.gid != acred->gid ||
137             gcred->acred.machine_cred != acred->machine_cred)
138                 goto out_nomatch;
139
140         /* Optimisation in the case where pointers are identical... */
141         if (gcred->acred.group_info == acred->group_info)
142                 goto out_match;
143
144         /* Slow path... */
145         if (gcred->acred.group_info->ngroups != acred->group_info->ngroups)
146                 goto out_nomatch;
147         for (i = 0; i < gcred->acred.group_info->ngroups; i++) {
148                 if (GROUP_AT(gcred->acred.group_info, i) !=
149                                 GROUP_AT(acred->group_info, i))
150                         goto out_nomatch;
151         }
152 out_match:
153         return 1;
154 out_nomatch:
155         return 0;
156 }
157
158 int __init rpc_init_generic_auth(void)
159 {
160         return rpcauth_init_credcache(&generic_auth);
161 }
162
163 void rpc_destroy_generic_auth(void)
164 {
165         rpcauth_destroy_credcache(&generic_auth);
166 }
167
168 static const struct rpc_authops generic_auth_ops = {
169         .owner = THIS_MODULE,
170         .au_name = "Generic",
171         .lookup_cred = generic_lookup_cred,
172         .crcreate = generic_create_cred,
173 };
174
175 static struct rpc_auth generic_auth = {
176         .au_ops = &generic_auth_ops,
177         .au_count = ATOMIC_INIT(0),
178 };
179
180 static const struct rpc_credops generic_credops = {
181         .cr_name = "Generic cred",
182         .crdestroy = generic_destroy_cred,
183         .crbind = generic_bind_cred,
184         .crmatch = generic_match,
185 };