2 * linux/net/sunrpc/auth.c
4 * Generic RPC client authentication API.
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/sched.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/errno.h>
14 #include <linux/socket.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/spinlock.h>
19 # define RPCDBG_FACILITY RPCDBG_AUTH
22 static struct rpc_authops
* auth_flavors
[RPC_AUTH_MAXFLAVOR
] = {
23 &authnull_ops
, /* AUTH_NULL */
24 &authunix_ops
, /* AUTH_UNIX */
25 NULL
, /* others can be loadable modules */
29 pseudoflavor_to_flavor(u32 flavor
) {
30 if (flavor
>= RPC_AUTH_MAXFLAVOR
)
36 rpcauth_register(struct rpc_authops
*ops
)
38 rpc_authflavor_t flavor
;
40 if ((flavor
= ops
->au_flavor
) >= RPC_AUTH_MAXFLAVOR
)
42 if (auth_flavors
[flavor
] != NULL
)
43 return -EPERM
; /* what else? */
44 auth_flavors
[flavor
] = ops
;
49 rpcauth_unregister(struct rpc_authops
*ops
)
51 rpc_authflavor_t flavor
;
53 if ((flavor
= ops
->au_flavor
) >= RPC_AUTH_MAXFLAVOR
)
55 if (auth_flavors
[flavor
] != ops
)
56 return -EPERM
; /* what else? */
57 auth_flavors
[flavor
] = NULL
;
62 rpcauth_create(rpc_authflavor_t pseudoflavor
, struct rpc_clnt
*clnt
)
64 struct rpc_auth
*auth
;
65 struct rpc_authops
*ops
;
66 u32 flavor
= pseudoflavor_to_flavor(pseudoflavor
);
68 if (flavor
>= RPC_AUTH_MAXFLAVOR
|| !(ops
= auth_flavors
[flavor
]))
70 auth
= ops
->create(clnt
, pseudoflavor
);
74 rpcauth_destroy(clnt
->cl_auth
);
80 rpcauth_destroy(struct rpc_auth
*auth
)
82 if (!atomic_dec_and_test(&auth
->au_count
))
84 auth
->au_ops
->destroy(auth
);
87 static DEFINE_SPINLOCK(rpc_credcache_lock
);
90 * Initialize RPC credential cache
93 rpcauth_init_credcache(struct rpc_auth
*auth
, unsigned long expire
)
95 struct rpc_cred_cache
*new;
98 new = (struct rpc_cred_cache
*)kmalloc(sizeof(*new), GFP_KERNEL
);
101 for (i
= 0; i
< RPC_CREDCACHE_NR
; i
++)
102 INIT_HLIST_HEAD(&new->hashtable
[i
]);
103 new->expire
= expire
;
104 new->nextgc
= jiffies
+ (expire
>> 1);
105 auth
->au_credcache
= new;
110 * Destroy a list of credentials
113 void rpcauth_destroy_credlist(struct hlist_head
*head
)
115 struct rpc_cred
*cred
;
117 while (!hlist_empty(head
)) {
118 cred
= hlist_entry(head
->first
, struct rpc_cred
, cr_hash
);
119 hlist_del_init(&cred
->cr_hash
);
125 * Clear the RPC credential cache, and delete those credentials
126 * that are not referenced.
129 rpcauth_free_credcache(struct rpc_auth
*auth
)
131 struct rpc_cred_cache
*cache
= auth
->au_credcache
;
133 struct hlist_node
*pos
, *next
;
134 struct rpc_cred
*cred
;
137 spin_lock(&rpc_credcache_lock
);
138 for (i
= 0; i
< RPC_CREDCACHE_NR
; i
++) {
139 hlist_for_each_safe(pos
, next
, &cache
->hashtable
[i
]) {
140 cred
= hlist_entry(pos
, struct rpc_cred
, cr_hash
);
141 __hlist_del(&cred
->cr_hash
);
142 hlist_add_head(&cred
->cr_hash
, &free
);
145 spin_unlock(&rpc_credcache_lock
);
146 rpcauth_destroy_credlist(&free
);
150 rpcauth_prune_expired(struct rpc_auth
*auth
, struct rpc_cred
*cred
, struct hlist_head
*free
)
152 if (atomic_read(&cred
->cr_count
) != 1)
154 if (time_after(jiffies
, cred
->cr_expire
+ auth
->au_credcache
->expire
))
155 cred
->cr_flags
&= ~RPCAUTH_CRED_UPTODATE
;
156 if (!(cred
->cr_flags
& RPCAUTH_CRED_UPTODATE
)) {
157 __hlist_del(&cred
->cr_hash
);
158 hlist_add_head(&cred
->cr_hash
, free
);
163 * Remove stale credentials. Avoid sleeping inside the loop.
166 rpcauth_gc_credcache(struct rpc_auth
*auth
, struct hlist_head
*free
)
168 struct rpc_cred_cache
*cache
= auth
->au_credcache
;
169 struct hlist_node
*pos
, *next
;
170 struct rpc_cred
*cred
;
173 dprintk("RPC: gc'ing RPC credentials for auth %p\n", auth
);
174 for (i
= 0; i
< RPC_CREDCACHE_NR
; i
++) {
175 hlist_for_each_safe(pos
, next
, &cache
->hashtable
[i
]) {
176 cred
= hlist_entry(pos
, struct rpc_cred
, cr_hash
);
177 rpcauth_prune_expired(auth
, cred
, free
);
180 cache
->nextgc
= jiffies
+ cache
->expire
;
184 * Look up a process' credentials in the authentication cache
187 rpcauth_lookup_credcache(struct rpc_auth
*auth
, struct auth_cred
* acred
,
190 struct rpc_cred_cache
*cache
= auth
->au_credcache
;
192 struct hlist_node
*pos
, *next
;
193 struct rpc_cred
*new = NULL
,
197 if (!(taskflags
& RPC_TASK_ROOTCREDS
))
198 nr
= acred
->uid
& RPC_CREDCACHE_MASK
;
200 spin_lock(&rpc_credcache_lock
);
201 if (time_before(cache
->nextgc
, jiffies
))
202 rpcauth_gc_credcache(auth
, &free
);
203 hlist_for_each_safe(pos
, next
, &cache
->hashtable
[nr
]) {
204 struct rpc_cred
*entry
;
205 entry
= hlist_entry(pos
, struct rpc_cred
, cr_hash
);
206 if (entry
->cr_ops
->crmatch(acred
, entry
, taskflags
)) {
207 hlist_del(&entry
->cr_hash
);
211 rpcauth_prune_expired(auth
, entry
, &free
);
215 hlist_add_head(&new->cr_hash
, &free
);
220 hlist_add_head(&cred
->cr_hash
, &cache
->hashtable
[nr
]);
223 spin_unlock(&rpc_credcache_lock
);
225 rpcauth_destroy_credlist(&free
);
228 new = auth
->au_ops
->crcreate(auth
, acred
, taskflags
);
231 new->cr_magic
= RPCAUTH_CRED_MAGIC
;
238 return (struct rpc_cred
*) cred
;
242 rpcauth_lookupcred(struct rpc_auth
*auth
, int taskflags
)
244 struct auth_cred acred
= {
245 .uid
= current
->fsuid
,
246 .gid
= current
->fsgid
,
247 .group_info
= current
->group_info
,
249 struct rpc_cred
*ret
;
251 dprintk("RPC: looking up %s cred\n",
252 auth
->au_ops
->au_name
);
253 get_group_info(acred
.group_info
);
254 ret
= auth
->au_ops
->lookup_cred(auth
, &acred
, taskflags
);
255 put_group_info(acred
.group_info
);
260 rpcauth_bindcred(struct rpc_task
*task
)
262 struct rpc_auth
*auth
= task
->tk_auth
;
263 struct auth_cred acred
= {
264 .uid
= current
->fsuid
,
265 .gid
= current
->fsgid
,
266 .group_info
= current
->group_info
,
268 struct rpc_cred
*ret
;
270 dprintk("RPC: %4d looking up %s cred\n",
271 task
->tk_pid
, task
->tk_auth
->au_ops
->au_name
);
272 get_group_info(acred
.group_info
);
273 ret
= auth
->au_ops
->lookup_cred(auth
, &acred
, task
->tk_flags
);
275 task
->tk_msg
.rpc_cred
= ret
;
277 task
->tk_status
= PTR_ERR(ret
);
278 put_group_info(acred
.group_info
);
283 rpcauth_holdcred(struct rpc_task
*task
)
285 dprintk("RPC: %4d holding %s cred %p\n",
286 task
->tk_pid
, task
->tk_auth
->au_ops
->au_name
, task
->tk_msg
.rpc_cred
);
287 if (task
->tk_msg
.rpc_cred
)
288 get_rpccred(task
->tk_msg
.rpc_cred
);
292 put_rpccred(struct rpc_cred
*cred
)
294 cred
->cr_expire
= jiffies
;
295 if (!atomic_dec_and_test(&cred
->cr_count
))
297 cred
->cr_ops
->crdestroy(cred
);
301 rpcauth_unbindcred(struct rpc_task
*task
)
303 struct rpc_auth
*auth
= task
->tk_auth
;
304 struct rpc_cred
*cred
= task
->tk_msg
.rpc_cred
;
306 dprintk("RPC: %4d releasing %s cred %p\n",
307 task
->tk_pid
, auth
->au_ops
->au_name
, cred
);
310 task
->tk_msg
.rpc_cred
= NULL
;
314 rpcauth_marshcred(struct rpc_task
*task
, u32
*p
)
316 struct rpc_auth
*auth
= task
->tk_auth
;
317 struct rpc_cred
*cred
= task
->tk_msg
.rpc_cred
;
319 dprintk("RPC: %4d marshaling %s cred %p\n",
320 task
->tk_pid
, auth
->au_ops
->au_name
, cred
);
321 return cred
->cr_ops
->crmarshal(task
, p
);
325 rpcauth_checkverf(struct rpc_task
*task
, u32
*p
)
327 struct rpc_auth
*auth
= task
->tk_auth
;
328 struct rpc_cred
*cred
= task
->tk_msg
.rpc_cred
;
330 dprintk("RPC: %4d validating %s cred %p\n",
331 task
->tk_pid
, auth
->au_ops
->au_name
, cred
);
332 return cred
->cr_ops
->crvalidate(task
, p
);
336 rpcauth_wrap_req(struct rpc_task
*task
, kxdrproc_t encode
, void *rqstp
,
337 u32
*data
, void *obj
)
339 struct rpc_cred
*cred
= task
->tk_msg
.rpc_cred
;
341 dprintk("RPC: %4d using %s cred %p to wrap rpc data\n",
342 task
->tk_pid
, cred
->cr_ops
->cr_name
, cred
);
343 if (cred
->cr_ops
->crwrap_req
)
344 return cred
->cr_ops
->crwrap_req(task
, encode
, rqstp
, data
, obj
);
345 /* By default, we encode the arguments normally. */
346 return encode(rqstp
, data
, obj
);
350 rpcauth_unwrap_resp(struct rpc_task
*task
, kxdrproc_t decode
, void *rqstp
,
351 u32
*data
, void *obj
)
353 struct rpc_cred
*cred
= task
->tk_msg
.rpc_cred
;
355 dprintk("RPC: %4d using %s cred %p to unwrap rpc data\n",
356 task
->tk_pid
, cred
->cr_ops
->cr_name
, cred
);
357 if (cred
->cr_ops
->crunwrap_resp
)
358 return cred
->cr_ops
->crunwrap_resp(task
, decode
, rqstp
,
360 /* By default, we decode the arguments normally. */
361 return decode(rqstp
, data
, obj
);
365 rpcauth_refreshcred(struct rpc_task
*task
)
367 struct rpc_auth
*auth
= task
->tk_auth
;
368 struct rpc_cred
*cred
= task
->tk_msg
.rpc_cred
;
371 dprintk("RPC: %4d refreshing %s cred %p\n",
372 task
->tk_pid
, auth
->au_ops
->au_name
, cred
);
373 err
= cred
->cr_ops
->crrefresh(task
);
375 task
->tk_status
= err
;
380 rpcauth_invalcred(struct rpc_task
*task
)
382 dprintk("RPC: %4d invalidating %s cred %p\n",
383 task
->tk_pid
, task
->tk_auth
->au_ops
->au_name
, task
->tk_msg
.rpc_cred
);
384 spin_lock(&rpc_credcache_lock
);
385 if (task
->tk_msg
.rpc_cred
)
386 task
->tk_msg
.rpc_cred
->cr_flags
&= ~RPCAUTH_CRED_UPTODATE
;
387 spin_unlock(&rpc_credcache_lock
);
391 rpcauth_uptodatecred(struct rpc_task
*task
)
393 return !(task
->tk_msg
.rpc_cred
) ||
394 (task
->tk_msg
.rpc_cred
->cr_flags
& RPCAUTH_CRED_UPTODATE
);