USB: serial: remove recourse to generic method
[wandboard.git] / net / sunrpc / auth_generic.c
blob4028502f052858b1d66af4b31c17d641c16fac0b
1 /*
2 * Generic RPC credential
4 * Copyright (C) 2008, Trond Myklebust <Trond.Myklebust@netapp.com>
5 */
7 #include <linux/err.h>
8 #include <linux/types.h>
9 #include <linux/module.h>
10 #include <linux/sched.h>
11 #include <linux/sunrpc/auth.h>
12 #include <linux/sunrpc/clnt.h>
13 #include <linux/sunrpc/debug.h>
14 #include <linux/sunrpc/sched.h>
16 #ifdef RPC_DEBUG
17 # define RPCDBG_FACILITY RPCDBG_AUTH
18 #endif
20 #define RPC_MACHINE_CRED_USERID ((uid_t)0)
21 #define RPC_MACHINE_CRED_GROUPID ((gid_t)0)
23 struct generic_cred {
24 struct rpc_cred gc_base;
25 struct auth_cred acred;
28 static struct rpc_auth generic_auth;
29 static struct rpc_cred_cache generic_cred_cache;
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(void)
46 struct auth_cred acred = {
47 .uid = RPC_MACHINE_CRED_USERID,
48 .gid = RPC_MACHINE_CRED_GROUPID,
49 .machine_cred = 1,
52 dprintk("RPC: looking up machine cred\n");
53 return generic_auth.au_ops->lookup_cred(&generic_auth, &acred, 0);
55 EXPORT_SYMBOL_GPL(rpc_lookup_machine_cred);
57 static void
58 generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred)
60 struct rpc_auth *auth = task->tk_client->cl_auth;
61 struct auth_cred *acred = &container_of(cred, struct generic_cred, gc_base)->acred;
62 struct rpc_cred *ret;
64 ret = auth->au_ops->lookup_cred(auth, acred, 0);
65 if (!IS_ERR(ret))
66 task->tk_msg.rpc_cred = ret;
67 else
68 task->tk_status = PTR_ERR(ret);
72 * Lookup generic creds for current process
74 static struct rpc_cred *
75 generic_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
77 return rpcauth_lookup_credcache(&generic_auth, acred, flags);
80 static struct rpc_cred *
81 generic_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
83 struct generic_cred *gcred;
85 gcred = kmalloc(sizeof(*gcred), GFP_KERNEL);
86 if (gcred == NULL)
87 return ERR_PTR(-ENOMEM);
89 rpcauth_init_cred(&gcred->gc_base, acred, &generic_auth, &generic_credops);
90 gcred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
92 gcred->acred.uid = acred->uid;
93 gcred->acred.gid = acred->gid;
94 gcred->acred.group_info = acred->group_info;
95 if (gcred->acred.group_info != NULL)
96 get_group_info(gcred->acred.group_info);
97 gcred->acred.machine_cred = acred->machine_cred;
99 dprintk("RPC: allocated %s cred %p for uid %d gid %d\n",
100 gcred->acred.machine_cred ? "machine" : "generic",
101 gcred, acred->uid, acred->gid);
102 return &gcred->gc_base;
105 static void
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);
113 kfree(gcred);
116 static void
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);
123 static void
124 generic_destroy_cred(struct rpc_cred *cred)
126 call_rcu(&cred->cr_rcu, generic_free_cred_callback);
130 * Match credentials against current process creds.
132 static int
133 generic_match(struct auth_cred *acred, struct rpc_cred *cred, int flags)
135 struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
136 int i;
138 if (gcred->acred.uid != acred->uid ||
139 gcred->acred.gid != acred->gid ||
140 gcred->acred.machine_cred != acred->machine_cred)
141 goto out_nomatch;
143 /* Optimisation in the case where pointers are identical... */
144 if (gcred->acred.group_info == acred->group_info)
145 goto out_match;
147 /* Slow path... */
148 if (gcred->acred.group_info->ngroups != acred->group_info->ngroups)
149 goto out_nomatch;
150 for (i = 0; i < gcred->acred.group_info->ngroups; i++) {
151 if (GROUP_AT(gcred->acred.group_info, i) !=
152 GROUP_AT(acred->group_info, i))
153 goto out_nomatch;
155 out_match:
156 return 1;
157 out_nomatch:
158 return 0;
161 void __init rpc_init_generic_auth(void)
163 spin_lock_init(&generic_cred_cache.lock);
166 void __exit rpc_destroy_generic_auth(void)
168 rpcauth_clear_credcache(&generic_cred_cache);
171 static struct rpc_cred_cache generic_cred_cache = {
172 {{ NULL, },},
175 static const struct rpc_authops generic_auth_ops = {
176 .owner = THIS_MODULE,
177 .au_name = "Generic",
178 .lookup_cred = generic_lookup_cred,
179 .crcreate = generic_create_cred,
182 static struct rpc_auth generic_auth = {
183 .au_ops = &generic_auth_ops,
184 .au_count = ATOMIC_INIT(0),
185 .au_credcache = &generic_cred_cache,
188 static const struct rpc_credops generic_credops = {
189 .cr_name = "Generic cred",
190 .crdestroy = generic_destroy_cred,
191 .crbind = generic_bind_cred,
192 .crmatch = generic_match,