2 * Generic RPC credential
4 * Copyright (C) 2008, Trond Myklebust <Trond.Myklebust@netapp.com>
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>
18 # define RPCDBG_FACILITY RPCDBG_AUTH
21 #define RPC_MACHINE_CRED_USERID GLOBAL_ROOT_UID
22 #define RPC_MACHINE_CRED_GROUPID GLOBAL_ROOT_GID
25 struct rpc_cred gc_base;
26 struct auth_cred acred;
29 static struct rpc_auth generic_auth;
30 static const struct rpc_credops generic_credops;
33 * Public call interface
35 struct rpc_cred *rpc_lookup_cred(void)
37 return rpcauth_lookupcred(&generic_auth, 0);
39 EXPORT_SYMBOL_GPL(rpc_lookup_cred);
42 * Public call interface for looking up machine creds.
44 struct rpc_cred *rpc_lookup_machine_cred(const char *service_name)
46 struct auth_cred acred = {
47 .uid = RPC_MACHINE_CRED_USERID,
48 .gid = RPC_MACHINE_CRED_GROUPID,
49 .principal = service_name,
53 dprintk("RPC: looking up machine cred for service %s\n",
55 return generic_auth.au_ops->lookup_cred(&generic_auth, &acred, 0);
57 EXPORT_SYMBOL_GPL(rpc_lookup_machine_cred);
59 static struct rpc_cred *generic_bind_cred(struct rpc_task *task,
60 struct rpc_cred *cred, int lookupflags)
62 struct rpc_auth *auth = task->tk_client->cl_auth;
63 struct auth_cred *acred = &container_of(cred, struct generic_cred, gc_base)->acred;
65 return auth->au_ops->lookup_cred(auth, acred, lookupflags);
69 * Lookup generic creds for current process
71 static struct rpc_cred *
72 generic_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
74 return rpcauth_lookup_credcache(&generic_auth, acred, flags);
77 static struct rpc_cred *
78 generic_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
80 struct generic_cred *gcred;
82 gcred = kmalloc(sizeof(*gcred), GFP_KERNEL);
84 return ERR_PTR(-ENOMEM);
86 rpcauth_init_cred(&gcred->gc_base, acred, &generic_auth, &generic_credops);
87 gcred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
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 gcred->acred.principal = acred->principal;
97 dprintk("RPC: allocated %s cred %p for uid %d gid %d\n",
98 gcred->acred.machine_cred ? "machine" : "generic",
100 from_kuid(&init_user_ns, acred->uid),
101 from_kgid(&init_user_ns, acred->gid));
102 return &gcred->gc_base;
106 generic_free_cred(struct rpc_cred *cred)
108 struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
110 dprintk("RPC: generic_free_cred %p\n", gcred);
111 if (gcred->acred.group_info != NULL)
112 put_group_info(gcred->acred.group_info);
117 generic_free_cred_callback(struct rcu_head *head)
119 struct rpc_cred *cred = container_of(head, struct rpc_cred, cr_rcu);
120 generic_free_cred(cred);
124 generic_destroy_cred(struct rpc_cred *cred)
126 call_rcu(&cred->cr_rcu, generic_free_cred_callback);
130 machine_cred_match(struct auth_cred *acred, struct generic_cred *gcred, int flags)
132 if (!gcred->acred.machine_cred ||
133 gcred->acred.principal != acred->principal ||
134 !uid_eq(gcred->acred.uid, acred->uid) ||
135 !gid_eq(gcred->acred.gid, acred->gid))
141 * Match credentials against current process creds.
144 generic_match(struct auth_cred *acred, struct rpc_cred *cred, int flags)
146 struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
149 if (acred->machine_cred)
150 return machine_cred_match(acred, gcred, flags);
152 if (!uid_eq(gcred->acred.uid, acred->uid) ||
153 !gid_eq(gcred->acred.gid, acred->gid) ||
154 gcred->acred.machine_cred != 0)
157 /* Optimisation in the case where pointers are identical... */
158 if (gcred->acred.group_info == acred->group_info)
162 if (gcred->acred.group_info->ngroups != acred->group_info->ngroups)
164 for (i = 0; i < gcred->acred.group_info->ngroups; i++) {
165 if (!gid_eq(GROUP_AT(gcred->acred.group_info, i),
166 GROUP_AT(acred->group_info, i)))
175 int __init rpc_init_generic_auth(void)
177 return rpcauth_init_credcache(&generic_auth);
180 void rpc_destroy_generic_auth(void)
182 rpcauth_destroy_credcache(&generic_auth);
185 static const struct rpc_authops generic_auth_ops = {
186 .owner = THIS_MODULE,
187 .au_name = "Generic",
188 .lookup_cred = generic_lookup_cred,
189 .crcreate = generic_create_cred,
192 static struct rpc_auth generic_auth = {
193 .au_ops = &generic_auth_ops,
194 .au_count = ATOMIC_INIT(0),
197 static const struct rpc_credops generic_credops = {
198 .cr_name = "Generic cred",
199 .crdestroy = generic_destroy_cred,
200 .crbind = generic_bind_cred,
201 .crmatch = generic_match,