summaryrefslogtreecommitdiffstats
path: root/net/sunrpc/auth.c
blob: b8c0a7be877cf5023bf65c76e5c25433292bbd64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * linux/fs/nfs/rpcauth.c
 *
 * Generic RPC authentication API.
 *
 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
 */

#include <linux/types.h>
#include <linux/sched.h>
#include <linux/malloc.h>
#include <linux/errno.h>
#include <linux/socket.h>
#include <linux/sunrpc/clnt.h>
#include <linux/spinlock.h>

#ifdef RPC_DEBUG
# define RPCDBG_FACILITY	RPCDBG_AUTH
#endif

#define RPC_MAXFLAVOR	8

static struct rpc_authops *	auth_flavors[RPC_MAXFLAVOR] = {
	&authnull_ops,		/* AUTH_NULL */
	&authunix_ops,		/* AUTH_UNIX */
	NULL,			/* others can be loadable modules */
};

int
rpcauth_register(struct rpc_authops *ops)
{
	unsigned int	flavor;

	if ((flavor = ops->au_flavor) >= RPC_MAXFLAVOR)
		return -EINVAL;
	if (auth_flavors[flavor] != NULL)
		return -EPERM;		/* what else? */
	auth_flavors[flavor] = ops;
	return 0;
}

int
rpcauth_unregister(struct rpc_authops *ops)
{
	unsigned int	flavor;

	if ((flavor = ops->au_flavor) >= RPC_MAXFLAVOR)
		return -EINVAL;
	if (auth_flavors[flavor] != ops)
		return -EPERM;		/* what else? */
	auth_flavors[flavor] = NULL;
	return 0;
}

struct rpc_auth *
rpcauth_create(unsigned int flavor, struct rpc_clnt *clnt)
{
	struct rpc_authops	*ops;

	if (flavor >= RPC_MAXFLAVOR || !(ops = auth_flavors[flavor]))
		return NULL;
	clnt->cl_auth = ops->create(clnt);
	return clnt->cl_auth;
}

void
rpcauth_destroy(struct rpc_auth *auth)
{
	auth->au_ops->destroy(auth);
}

spinlock_t rpc_credcache_lock = SPIN_LOCK_UNLOCKED;

/*
 * Initialize RPC credential cache
 */
void
rpcauth_init_credcache(struct rpc_auth *auth)
{
	memset(auth->au_credcache, 0, sizeof(auth->au_credcache));
	auth->au_nextgc = jiffies + (auth->au_expire >> 1);
}

static inline void
rpcauth_crdestroy(struct rpc_auth *auth, struct rpc_cred *cred)
{
	if (auth->au_ops->crdestroy)
		auth->au_ops->crdestroy(cred);
	else
		rpc_free(cred);
}

/*
 * Clear the RPC credential cache
 */
void
rpcauth_free_credcache(struct rpc_auth *auth)
{
	struct rpc_cred	**q, *cred;
	void		(*destroy)(struct rpc_cred *);
	int		i;

	if (!(destroy = auth->au_ops->crdestroy))
		destroy = (void (*)(struct rpc_cred *)) rpc_free;

	spin_lock(&rpc_credcache_lock);
	for (i = 0; i < RPC_CREDCACHE_NR; i++) {
		q = &auth->au_credcache[i];
		while ((cred = *q) != NULL) {
			*q = cred->cr_next;
			destroy(cred);
		}
	}
	spin_unlock(&rpc_credcache_lock);
}

/*
 * Remove stale credentials. Avoid sleeping inside the loop.
 */
static void
rpcauth_gc_credcache(struct rpc_auth *auth)
{
	struct rpc_cred	**q, *cred, *free = NULL;
	int		i;

	dprintk("RPC: gc'ing RPC credentials for auth %p\n", auth);
	spin_lock(&rpc_credcache_lock);
	for (i = 0; i < RPC_CREDCACHE_NR; i++) {
		q = &auth->au_credcache[i];
		while ((cred = *q) != NULL) {
			if (!cred->cr_count &&
			    time_before(cred->cr_expire, jiffies)) {
				*q = cred->cr_next;
				cred->cr_next = free;
				free = cred;
				continue;
			}
			q = &cred->cr_next;
		}
	}
	spin_unlock(&rpc_credcache_lock);
	while ((cred = free) != NULL) {
		free = cred->cr_next;
		rpcauth_crdestroy(auth, cred);
	}
	auth->au_nextgc = jiffies + auth->au_expire;
}

/*
 * Insert credential into cache
 */
void
rpcauth_insert_credcache(struct rpc_auth *auth, struct rpc_cred *cred)
{
	int		nr;

	nr = (cred->cr_uid % RPC_CREDCACHE_NR);
	spin_lock(&rpc_credcache_lock);
	cred->cr_next = auth->au_credcache[nr];
	auth->au_credcache[nr] = cred;
	cred->cr_count++;
	cred->cr_expire = jiffies + auth->au_expire;
	spin_unlock(&rpc_credcache_lock);
}

/*
 * Look up a process' credentials in the authentication cache
 */
static struct rpc_cred *
rpcauth_lookup_credcache(struct rpc_auth *auth, int taskflags)
{
	struct rpc_cred	**q, *cred = NULL;
	int		nr = 0;

	if (!(taskflags & RPC_TASK_ROOTCREDS))
		nr = current->uid % RPC_CREDCACHE_NR;

	if (time_before(auth->au_nextgc, jiffies))
		rpcauth_gc_credcache(auth);

	spin_lock(&rpc_credcache_lock);
	q = &auth->au_credcache[nr];
	while ((cred = *q) != NULL) {
		if (!(cred->cr_flags & RPCAUTH_CRED_DEAD) &&
		    auth->au_ops->crmatch(cred, taskflags)) {
			*q = cred->cr_next;
			break;
		}
		q = &cred->cr_next;
	}
	spin_unlock(&rpc_credcache_lock);

	if (!cred)
		cred = auth->au_ops->crcreate(taskflags);

	if (cred)
		rpcauth_insert_credcache(auth, cred);

	return (struct rpc_cred *) cred;
}

/*
 * Remove cred handle from cache
 */
static void
rpcauth_remove_credcache(struct rpc_auth *auth, struct rpc_cred *cred)
{
	struct rpc_cred	**q, *cr;
	int		nr;

	nr = (cred->cr_uid % RPC_CREDCACHE_NR);
	spin_lock(&rpc_credcache_lock);
	q = &auth->au_credcache[nr];
	while ((cr = *q) != NULL) {
		if (cred == cr) {
			*q = cred->cr_next;
			cred->cr_next = NULL;
			break;
		}
		q = &cred->cr_next;
	}
	spin_unlock(&rpc_credcache_lock);
}

struct rpc_cred *
rpcauth_lookupcred(struct rpc_auth *auth, int taskflags)
{
	dprintk("RPC:     looking up %s cred\n",
		auth->au_ops->au_name);
	return rpcauth_lookup_credcache(auth, taskflags);
}

struct rpc_cred *
rpcauth_bindcred(struct rpc_task *task)
{
	struct rpc_auth *auth = task->tk_auth;

	dprintk("RPC: %4d looking up %s cred\n",
		task->tk_pid, task->tk_auth->au_ops->au_name);
	task->tk_msg.rpc_cred = rpcauth_lookup_credcache(auth, task->tk_flags);
	if (task->tk_msg.rpc_cred == 0)
		task->tk_status = -ENOMEM;
	return task->tk_msg.rpc_cred;
}

int
rpcauth_matchcred(struct rpc_auth *auth, struct rpc_cred *cred, int taskflags)
{
	dprintk("RPC:     matching %s cred %d\n",
		auth->au_ops->au_name, taskflags);
	return auth->au_ops->crmatch(cred, taskflags);
}

void
rpcauth_holdcred(struct rpc_task *task)
{
	dprintk("RPC: %4d holding %s cred %p\n",
		task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
	if (task->tk_msg.rpc_cred) {
		task->tk_msg.rpc_cred->cr_count++;
		task->tk_msg.rpc_cred->cr_expire = jiffies + task->tk_auth->au_expire;
	}
}

void
rpcauth_releasecred(struct rpc_auth *auth, struct rpc_cred *cred)
{
	if (cred != NULL && cred->cr_count > 0) {
		cred->cr_count--;
		if (cred->cr_flags & RPCAUTH_CRED_DEAD) {
			rpcauth_remove_credcache(auth, cred);
			if (!cred->cr_count)
				rpcauth_crdestroy(auth, cred);
		}
	}
}

void
rpcauth_unbindcred(struct rpc_task *task)
{
	struct rpc_auth	*auth = task->tk_auth;
	struct rpc_cred	*cred = task->tk_msg.rpc_cred;

	dprintk("RPC: %4d releasing %s cred %p\n",
		task->tk_pid, auth->au_ops->au_name, cred);

	rpcauth_releasecred(auth, cred);
	task->tk_msg.rpc_cred = NULL;
}

u32 *
rpcauth_marshcred(struct rpc_task *task, u32 *p)
{
	struct rpc_auth	*auth = task->tk_auth;

	dprintk("RPC: %4d marshaling %s cred %p\n",
		task->tk_pid, auth->au_ops->au_name, task->tk_msg.rpc_cred);
	return auth->au_ops->crmarshal(task, p,
				task->tk_flags & RPC_CALL_REALUID);
}

u32 *
rpcauth_checkverf(struct rpc_task *task, u32 *p)
{
	struct rpc_auth	*auth = task->tk_auth;

	dprintk("RPC: %4d validating %s cred %p\n",
		task->tk_pid, auth->au_ops->au_name, task->tk_msg.rpc_cred);
	return auth->au_ops->crvalidate(task, p);
}

int
rpcauth_refreshcred(struct rpc_task *task)
{
	struct rpc_auth	*auth = task->tk_auth;

	dprintk("RPC: %4d refreshing %s cred %p\n",
		task->tk_pid, auth->au_ops->au_name, task->tk_msg.rpc_cred);
	task->tk_status = auth->au_ops->crrefresh(task);
	return task->tk_status;
}

void
rpcauth_invalcred(struct rpc_task *task)
{
	dprintk("RPC: %4d invalidating %s cred %p\n",
		task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
	if (task->tk_msg.rpc_cred)
		task->tk_msg.rpc_cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
}

int
rpcauth_uptodatecred(struct rpc_task *task)
{
	return !(task->tk_msg.rpc_cred) ||
		(task->tk_msg.rpc_cred->cr_flags & RPCAUTH_CRED_UPTODATE);
}