summaryrefslogtreecommitdiff
path: root/usr.sbin/radiusd/radiusd_module.c
blob: a7968a9337a09331590d15644485fd056f68fe5a (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*	$OpenBSD: radiusd_module.c,v 1.9 2015/12/05 13:22:32 claudio Exp $	*/

/*
 * Copyright (c) 2015 YASUOKA Masahiko <yasuoka@yasuoka.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* radiusd_module.c -- helper functions for radiusd modules */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/uio.h>

#include <err.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <imsg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <pwd.h>

#include "radiusd.h"
#include "radiusd_module.h"
#include "imsg_subr.h"

static void	(*module_config_set) (void *, const char *, int,
		    char * const *) = NULL;
static void	(*module_start_module) (void *) = NULL;
static void	(*module_stop_module) (void *) = NULL;
static void	(*module_userpass) (void *, u_int, const char *, const char *)
		    = NULL;
static void	(*module_access_request) (void *, u_int, const u_char *,
		    size_t) = NULL;

struct module_base {
	void			*ctx;
	struct imsgbuf		 ibuf;
	bool			 priv_dropped;

	/* Buffer for receiving the RADIUS packet */
	u_char			*radpkt;
	int			 radpktsiz;
	int			 radpktoff;

#ifdef USE_LIBEVENT
	struct module_imsgbuf	*module_imsgbuf;
	bool			 writeready;
	bool			 stopped;
	bool			 ev_onhandler;
	struct event		 ev;
#endif
};

static int	 module_common_radpkt(struct module_base *, uint32_t, u_int,
		    int, const u_char *, size_t);
static int	 module_recv_imsg(struct module_base *);
static int	 module_imsg_handler(struct module_base *, struct imsg *);
#ifdef USE_LIBEVENT
static void	 module_on_event(int, short, void *);
#endif
static void	 module_stop(struct module_base *);
static void	 module_reset_event(struct module_base *);

struct module_base *
module_create(int sock, void *ctx, struct module_handlers *handler)
{
	struct module_base	*base;

	if ((base = calloc(1, sizeof(struct module_base))) == NULL)
		return (NULL);

	imsg_init(&base->ibuf, sock);
	base->ctx = ctx;

	module_userpass = handler->userpass;
	module_access_request = handler->access_request;
	module_config_set = handler->config_set;
	module_start_module = handler->start;
	module_stop_module = handler->stop;

	return (base);
}

void
module_start(struct module_base *base)
{
#ifdef USE_LIBEVENT
	int	 ival;

	if ((ival = fcntl(base->ibuf.fd, F_GETFL, 0)) < 0)
		err(1, "Failed to F_GETFL");
	if (fcntl(base->ibuf.fd, F_SETFL, ival | O_NONBLOCK) < 0)
		err(1, "Failed to setup NONBLOCK");
	event_set(&base->ev, base->ibuf.fd, EV_READ, module_on_event, base);
	event_add(&base->ev, NULL);
#endif
}

int
module_run(struct module_base *base)
{
	int	 ret;

	ret = module_recv_imsg(base);
	if (ret == 0)
		imsg_flush(&base->ibuf);

	return (ret);
}

void
module_destroy(struct module_base *base)
{
	imsg_clear(&base->ibuf);
	free(base);
}

void
module_load(struct module_base *base)
{
	struct radiusd_module_load_arg	 load;

	memset(&load, 0, sizeof(load));
	if (module_userpass != NULL)
		load.cap |= RADIUSD_MODULE_CAP_USERPASS;
	if (module_access_request != NULL)
		load.cap |= RADIUSD_MODULE_CAP_ACCSREQ;
	imsg_compose(&base->ibuf, IMSG_RADIUSD_MODULE_LOAD, 0, 0, -1, &load,
	    sizeof(load));
	imsg_flush(&base->ibuf);
}

void
module_drop_privilege(struct module_base *base)
{
	struct passwd	*pw;

	tzset();

	/* Drop the privilege */
	if ((pw = getpwnam(RADIUSD_USER)) == NULL)
		goto on_fail;
	if (chroot(pw->pw_dir) == -1)
		goto on_fail;
	if (chdir("/") == -1)
		goto on_fail;
	if (setgroups(1, &pw->pw_gid) ||
	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
		goto on_fail;
	base->priv_dropped = true;

on_fail:
	return;
}

int
module_notify_secret(struct module_base *base, const char *secret)
{
	int		 ret;

	ret = imsg_compose(&base->ibuf, IMSG_RADIUSD_MODULE_NOTIFY_SECRET,
	    0, 0, -1, secret, strlen(secret) + 1);
	module_reset_event(base);

	return (ret);
}

int
module_send_message(struct module_base *base, uint32_t cmd, const char *fmt,
    ...)
{
	char	*msg;
	va_list	 ap;
	int	 ret;

	if (fmt == NULL)
		ret = imsg_compose(&base->ibuf, cmd, 0, 0, -1, NULL, 0);
	else {
		va_start(ap, fmt);
		vasprintf(&msg, fmt, ap);
		va_end(ap);
		if (msg == NULL)
			return (-1);
		ret = imsg_compose(&base->ibuf, cmd, 0, 0, -1, msg,
		    strlen(msg) + 1);
		free(msg);
	}
	module_reset_event(base);

	return (ret);
}

int
module_userpass_ok(struct module_base *base, u_int q_id, const char *msg)
{
	int		 ret;
	struct iovec	 iov[2];

	iov[0].iov_base = &q_id;
	iov[0].iov_len = sizeof(q_id);
	iov[1].iov_base = (char *)msg;
	iov[1].iov_len = strlen(msg) + 1;
	ret = imsg_composev(&base->ibuf, IMSG_RADIUSD_MODULE_USERPASS_OK,
	    0, 0, -1, iov, 2);
	module_reset_event(base);

	return (ret);
}

int
module_userpass_fail(struct module_base *base, u_int q_id, const char *msg)
{
	int		 ret;
	struct iovec	 iov[2];

	iov[0].iov_base = &q_id;
	iov[0].iov_len = sizeof(q_id);
	iov[1].iov_base = (char *)msg;
	iov[1].iov_len = strlen(msg) + 1;
	ret = imsg_composev(&base->ibuf, IMSG_RADIUSD_MODULE_USERPASS_FAIL,
	    0, 0, -1, iov, 2);
	module_reset_event(base);

	return (ret);
}

int
module_accsreq_answer(struct module_base *base, u_int q_id, int modified,
    const u_char *pkt, size_t pktlen)
{
	return (module_common_radpkt(base, IMSG_RADIUSD_MODULE_ACCSREQ_ANSWER,
	    q_id, modified, pkt, pktlen));
}

int
module_accsreq_aborted(struct module_base *base, u_int q_id)
{
	int	 ret;

	ret = imsg_compose(&base->ibuf, IMSG_RADIUSD_MODULE_ACCSREQ_ABORTED,
	    0, 0, -1, &q_id, sizeof(u_int));
	module_reset_event(base);

	return (ret);
}

static int
module_common_radpkt(struct module_base *base, uint32_t imsg_type, u_int q_id,
    int modified, const u_char *pkt, size_t pktlen)
{
	int		 ret = 0, off = 0, len, siz;
	struct iovec	 iov[2];
	struct radiusd_module_radpkt_arg	 ans;

	len = pktlen;
	ans.q_id = q_id;
	ans.modified = modified;
	while (off < len) {
		siz = MAX_IMSGSIZE - sizeof(ans);
		if (len - off > siz) {
			ans.final = true;
			ans.datalen = siz;
		} else {
			ans.final = true;
			ans.datalen = len - off;
		}
		iov[0].iov_base = &ans;
		iov[0].iov_len = sizeof(ans);
		iov[1].iov_base = (u_char *)pkt + off;
		iov[1].iov_len = ans.datalen;
		ret = imsg_composev(&base->ibuf, imsg_type, 0, 0, -1, iov, 2);
		if (ret == -1)
			break;
		off += ans.datalen;
	}
	module_reset_event(base);

	return (ret);
}

static int
module_recv_imsg(struct module_base *base)
{
	ssize_t		 n;
	struct imsg	 imsg;

	if (((n = imsg_read(&base->ibuf)) == -1 && errno != EAGAIN) || n == 0) {
		if (n != 0)
			syslog(LOG_ERR, "%s: imsg_read(): %m", __func__);
		module_stop(base);
		return (-1);
	}

	for (;;) {
		if ((n = imsg_get(&base->ibuf, &imsg)) == -1) {
			syslog(LOG_ERR, "%s: imsg_get(): %m", __func__);
			module_stop(base);
			return (-1);
		}
		if (n == 0)
			break;
		module_imsg_handler(base, &imsg);
		imsg_free(&imsg);
	}
	module_reset_event(base);

	return (0);
}

static int
module_imsg_handler(struct module_base *base, struct imsg *imsg)
{
	ssize_t	 datalen;

	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
	switch (imsg->hdr.type) {
	case IMSG_RADIUSD_MODULE_SET_CONFIG:
	    {
		struct radiusd_module_set_arg	 *arg;
		struct radiusd_module_object	 *val;
		u_int				  i;
		size_t				  off;
		char				**argv;

		arg = (struct radiusd_module_set_arg *)imsg->data;
		off = sizeof(struct radiusd_module_set_arg);

		if ((argv = calloc(sizeof(const char *), arg->nparamval))
		    == NULL) {
			module_send_message(base, IMSG_NG,
			    "Out of memory: %s", strerror(errno));
			break;
		}
		for (i = 0; i < arg->nparamval; i++) {
			if (datalen - off <
			    sizeof(struct radiusd_module_object))
				break;
			val = (struct radiusd_module_object *)
			    ((caddr_t)imsg->data + off);
			if (datalen - off < val->size)
				break;
			argv[i] = (char *)(val + 1);
			off += val->size;
		}
		if (i >= arg->nparamval)
			module_config_set(base->ctx, arg->paramname,
			    arg->nparamval, argv);
		else
			module_send_message(base, IMSG_NG,
			    "Internal protocol error");
		free(argv);

		break;
	    }
	case IMSG_RADIUSD_MODULE_START:
		if (module_start_module != NULL) {
			module_start_module(base->ctx);
			if (!base->priv_dropped) {
				syslog(LOG_ERR, "Module tried to start with "
				    "root privileges");
				abort();
			}
		} else {
			if (!base->priv_dropped) {
				syslog(LOG_ERR, "Module tried to start with "
				    "root privileges");
				abort();
			}
			module_send_message(base, IMSG_OK, NULL);
		}
		break;
	case IMSG_RADIUSD_MODULE_STOP:
		module_stop(base);
		break;
	case IMSG_RADIUSD_MODULE_USERPASS:
	    {
		struct radiusd_module_userpass_arg *userpass;

		if (module_userpass == NULL) {
			syslog(LOG_ERR, "Received USERPASS message, but "
			    "module doesn't support");
			break;
		}
		if (datalen <
		    (ssize_t)sizeof(struct radiusd_module_userpass_arg)) {
			syslog(LOG_ERR, "Received USERPASS message, but "
			    "length is wrong");
			break;
		}
		userpass = (struct radiusd_module_userpass_arg *)imsg->data;
		module_userpass(base->ctx, userpass->q_id, userpass->user,
		    (userpass->has_pass)? userpass->pass : NULL);
		explicit_bzero(userpass,
		    sizeof(struct radiusd_module_userpass_arg));
		break;
	    }
	case IMSG_RADIUSD_MODULE_ACCSREQ:
	    {
		struct radiusd_module_radpkt_arg	*accessreq;
		int					 chunklen;

		if (module_access_request == NULL) {
			syslog(LOG_ERR, "Received ACCSREQ message, but "
			    "module doesn't support");
			break;
		}
		if (datalen <
		    (ssize_t)sizeof(struct radiusd_module_radpkt_arg)) {
			syslog(LOG_ERR, "Received ACCSREQ message, but "
			    "length is wrong");
			break;
		}
		accessreq = (struct radiusd_module_radpkt_arg *)imsg->data;
		if (base->radpktsiz < accessreq->datalen) {
			u_char *nradpkt;
			if ((nradpkt = realloc(base->radpkt,
			    accessreq->datalen)) == NULL) {
				syslog(LOG_ERR, "Could not handle received "
				    "ACCSREQ message: %m");
				base->radpktoff = 0;
				goto accsreq_out;
			}
			base->radpkt = nradpkt;
			base->radpktsiz = accessreq->datalen;
		}
		chunklen = datalen - sizeof(struct radiusd_module_radpkt_arg);
		if (chunklen > base->radpktsiz - base->radpktoff){
			syslog(LOG_ERR,
			    "Could not handle received ACCSREQ message: "
			    "received length is too big");
			base->radpktoff = 0;
			goto accsreq_out;
		}
		memcpy(base->radpkt + base->radpktoff,
		    (caddr_t)(accessreq + 1), chunklen);
		base->radpktoff += chunklen;
		if (!accessreq->final)
			goto accsreq_out;
		if (base->radpktoff != accessreq->datalen) {
			syslog(LOG_ERR,
			    "Could not handle received ACCSREQ "
			    "message: length is mismatch");
			base->radpktoff = 0;
			goto accsreq_out;
		}
		module_access_request(base->ctx, accessreq->q_id,
		    base->radpkt, base->radpktoff);
		base->radpktoff = 0;
accsreq_out:
		break;
	    }
	}

	return (0);
}

static void
module_stop(struct module_base *base)
{
	if (module_stop_module != NULL)
		module_stop_module(base->ctx);
#ifdef USE_LIBEVENT
	event_del(&base->ev);
	base->stopped = true;
#endif
	close(base->ibuf.fd);
}

#ifdef USE_LIBEVENT
static void
module_on_event(int fd, short evmask, void *ctx)
{
	struct module_base	*base = ctx;
	int			 ret;

	base->ev_onhandler = true;
	if (evmask & EV_WRITE)
		base->writeready = true;
	if (evmask & EV_READ) {
		ret = module_recv_imsg(base);
		if (ret < 0)
			return;
	}
	while (base->writeready && base->ibuf.w.queued) {
		ret = msgbuf_write(&base->ibuf.w);
		if (ret > 0)
			continue;
		base->writeready = false;
		if (ret == 0 && errno == EAGAIN)
			break;
		syslog(LOG_ERR, "%s: msgbuf_write: %m", __func__);
		module_stop(base);
		return;
	}
	base->ev_onhandler = false;
	module_reset_event(base);
	return;
}
#endif

static void
module_reset_event(struct module_base *base)
{
#ifdef USE_LIBEVENT
	short		 evmask = 0;
	struct timeval	*tvp = NULL, tv = { 0, 0 };

	if (base->ev_onhandler)
		return;
	if (base->stopped)
		return;
	event_del(&base->ev);

	evmask |= EV_READ;
	if (base->ibuf.w.queued) {
		if (!base->writeready)
			evmask |= EV_WRITE;
		else
			tvp = &tv;	/* fire immediately */
	}
	event_set(&base->ev, base->ibuf.fd, evmask, module_on_event, base);
	if (event_add(&base->ev, tvp) == -1)
		syslog(LOG_ERR, "event_add() failed in %s()", __func__);
#endif
}