blob: 75762f346975ed6e2be6d639ed7a3e1d83ede077 [file] [log] [blame]
Trond Myklebust9a559ef2008-03-12 12:24:49 -04001/*
2 * Generic RPC credential
3 *
4 * Copyright (C) 2008, Trond Myklebust <Trond.Myklebust@netapp.com>
5 */
6
7#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Trond Myklebust9a559ef2008-03-12 12:24:49 -04009#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
Trond Myklebustb4528762008-05-11 12:18:51 -070021#define RPC_MACHINE_CRED_USERID ((uid_t)0)
22#define RPC_MACHINE_CRED_GROUPID ((gid_t)0)
Trond Myklebust7c67db32008-04-07 20:50:11 -040023
Trond Myklebust9a559ef2008-03-12 12:24:49 -040024struct generic_cred {
25 struct rpc_cred gc_base;
26 struct auth_cred acred;
27};
28
29static struct rpc_auth generic_auth;
Trond Myklebust9a559ef2008-03-12 12:24:49 -040030static const struct rpc_credops generic_credops;
31
32/*
33 * Public call interface
34 */
35struct rpc_cred *rpc_lookup_cred(void)
36{
37 return rpcauth_lookupcred(&generic_auth, 0);
38}
39EXPORT_SYMBOL_GPL(rpc_lookup_cred);
40
Trond Myklebust7c67db32008-04-07 20:50:11 -040041/*
42 * Public call interface for looking up machine creds.
43 */
Trond Myklebust68c97152012-01-03 13:22:46 -050044struct rpc_cred *rpc_lookup_machine_cred(const char *service_name)
Trond Myklebust7c67db32008-04-07 20:50:11 -040045{
46 struct auth_cred acred = {
Trond Myklebustb4528762008-05-11 12:18:51 -070047 .uid = RPC_MACHINE_CRED_USERID,
48 .gid = RPC_MACHINE_CRED_GROUPID,
Trond Myklebust68c97152012-01-03 13:22:46 -050049 .principal = service_name,
Trond Myklebust7c67db32008-04-07 20:50:11 -040050 .machine_cred = 1,
51 };
52
Trond Myklebust68c97152012-01-03 13:22:46 -050053 dprintk("RPC: looking up machine cred for service %s\n",
54 service_name);
Trond Myklebust7c67db32008-04-07 20:50:11 -040055 return generic_auth.au_ops->lookup_cred(&generic_auth, &acred, 0);
56}
57EXPORT_SYMBOL_GPL(rpc_lookup_machine_cred);
58
Trond Myklebust8572b8e2010-07-31 14:29:08 -040059static struct rpc_cred *generic_bind_cred(struct rpc_task *task,
60 struct rpc_cred *cred, int lookupflags)
Trond Myklebust5c691042008-03-12 16:21:07 -040061{
62 struct rpc_auth *auth = task->tk_client->cl_auth;
63 struct auth_cred *acred = &container_of(cred, struct generic_cred, gc_base)->acred;
Trond Myklebust5c691042008-03-12 16:21:07 -040064
Trond Myklebust8572b8e2010-07-31 14:29:08 -040065 return auth->au_ops->lookup_cred(auth, acred, lookupflags);
Trond Myklebust5c691042008-03-12 16:21:07 -040066}
67
Trond Myklebust9a559ef2008-03-12 12:24:49 -040068/*
69 * Lookup generic creds for current process
70 */
71static struct rpc_cred *
72generic_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
73{
74 return rpcauth_lookup_credcache(&generic_auth, acred, flags);
75}
76
77static struct rpc_cred *
78generic_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);
Trond Myklebust7c67db32008-04-07 20:50:11 -040094 gcred->acred.machine_cred = acred->machine_cred;
Trond Myklebust875ad3f2012-01-23 12:49:36 -050095 gcred->acred.principal = acred->principal;
Trond Myklebust9a559ef2008-03-12 12:24:49 -040096
Trond Myklebust7c67db32008-04-07 20:50:11 -040097 dprintk("RPC: allocated %s cred %p for uid %d gid %d\n",
98 gcred->acred.machine_cred ? "machine" : "generic",
Trond Myklebust9a559ef2008-03-12 12:24:49 -040099 gcred, acred->uid, acred->gid);
100 return &gcred->gc_base;
101}
102
103static void
104generic_free_cred(struct rpc_cred *cred)
105{
106 struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
107
108 dprintk("RPC: generic_free_cred %p\n", gcred);
109 if (gcred->acred.group_info != NULL)
110 put_group_info(gcred->acred.group_info);
111 kfree(gcred);
112}
113
114static void
115generic_free_cred_callback(struct rcu_head *head)
116{
117 struct rpc_cred *cred = container_of(head, struct rpc_cred, cr_rcu);
118 generic_free_cred(cred);
119}
120
121static void
122generic_destroy_cred(struct rpc_cred *cred)
123{
124 call_rcu(&cred->cr_rcu, generic_free_cred_callback);
125}
126
Trond Myklebust875ad3f2012-01-23 12:49:36 -0500127static int
128machine_cred_match(struct auth_cred *acred, struct generic_cred *gcred, int flags)
129{
130 if (!gcred->acred.machine_cred ||
131 gcred->acred.principal != acred->principal ||
132 gcred->acred.uid != acred->uid ||
133 gcred->acred.gid != acred->gid)
134 return 0;
135 return 1;
136}
137
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400138/*
139 * Match credentials against current process creds.
140 */
141static int
142generic_match(struct auth_cred *acred, struct rpc_cred *cred, int flags)
143{
144 struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
Trond Myklebust23918b02008-11-20 16:06:21 -0500145 int i;
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400146
Trond Myklebust875ad3f2012-01-23 12:49:36 -0500147 if (acred->machine_cred)
148 return machine_cred_match(acred, gcred, flags);
149
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400150 if (gcred->acred.uid != acred->uid ||
151 gcred->acred.gid != acred->gid ||
Trond Myklebust875ad3f2012-01-23 12:49:36 -0500152 gcred->acred.machine_cred != 0)
Trond Myklebust23918b02008-11-20 16:06:21 -0500153 goto out_nomatch;
154
155 /* Optimisation in the case where pointers are identical... */
156 if (gcred->acred.group_info == acred->group_info)
157 goto out_match;
158
159 /* Slow path... */
160 if (gcred->acred.group_info->ngroups != acred->group_info->ngroups)
161 goto out_nomatch;
162 for (i = 0; i < gcred->acred.group_info->ngroups; i++) {
163 if (GROUP_AT(gcred->acred.group_info, i) !=
164 GROUP_AT(acred->group_info, i))
165 goto out_nomatch;
166 }
167out_match:
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400168 return 1;
Trond Myklebust23918b02008-11-20 16:06:21 -0500169out_nomatch:
170 return 0;
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400171}
172
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400173int __init rpc_init_generic_auth(void)
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400174{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400175 return rpcauth_init_credcache(&generic_auth);
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400176}
177
Stephen Rothwellc135e842010-09-29 14:16:57 +1000178void rpc_destroy_generic_auth(void)
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400179{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400180 rpcauth_destroy_credcache(&generic_auth);
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400181}
182
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400183static const struct rpc_authops generic_auth_ops = {
184 .owner = THIS_MODULE,
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400185 .au_name = "Generic",
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400186 .lookup_cred = generic_lookup_cred,
187 .crcreate = generic_create_cred,
188};
189
190static struct rpc_auth generic_auth = {
191 .au_ops = &generic_auth_ops,
192 .au_count = ATOMIC_INIT(0),
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400193};
194
195static const struct rpc_credops generic_credops = {
196 .cr_name = "Generic cred",
197 .crdestroy = generic_destroy_cred,
Trond Myklebust5c691042008-03-12 16:21:07 -0400198 .crbind = generic_bind_cred,
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400199 .crmatch = generic_match,
200};