summaryrefslogtreecommitdiff
path: root/usr.sbin/radiusd/radiusd_module.c
blob: a666051e80cc532963b99adf98ae6ede06ff9e47 (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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*	$OpenBSD: radiusd_module.c,v 1.20 2024/09/15 05:14:32 yasuoka 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;
static void	(*module_next_response) (void *, u_int, const u_char *,
		    size_t) = NULL;
static void	(*module_request_decoration) (void *, u_int, const u_char *,
		    size_t) = NULL;
static void	(*module_response_decoration) (void *, u_int, const u_char *,
		    size_t, const u_char *, size_t) = NULL;
static void	(*module_accounting_request) (void *, u_int, const u_char *,
		    size_t) = NULL;
static void	(*module_dispatch_control) (void *, struct imsg *) = 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;
	u_char			*radpkt2;
	int			 radpkt2siz;	/* allocated size */
	int			 radpkt2len;	/* actual size */

#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,
		    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_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_next_response = handler->next_response;
	module_config_set = handler->config_set;
	module_request_decoration = handler->request_decoration;
	module_response_decoration = handler->response_decoration;
	module_accounting_request = handler->accounting_request;
	module_start_module = handler->start;
	module_stop_module = handler->stop;
	module_dispatch_control = handler->dispatch_control;

	return (base);
}

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

	if ((ival = fcntl(base->ibuf.fd, F_GETFL)) == -1)
		err(1, "Failed to F_GETFL");
	if (fcntl(base->ibuf.fd, F_SETFL, ival | O_NONBLOCK) == -1)
		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)
{
	if (base != NULL) {
		free(base->radpkt);
		free(base->radpkt2);
		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;
	if (module_next_response != NULL)
		load.cap |= RADIUSD_MODULE_CAP_NEXTRES;
	if (module_request_decoration != NULL)
		load.cap |= RADIUSD_MODULE_CAP_REQDECO;
	if (module_response_decoration != NULL)
		load.cap |= RADIUSD_MODULE_CAP_RESDECO;
	if (module_accounting_request != NULL)
		load.cap |= RADIUSD_MODULE_CAP_ACCTREQ;
	if (module_dispatch_control != NULL)
		load.cap |= RADIUSD_MODULE_CAP_CONTROL;
	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, int nochroot)
{
	struct passwd	*pw;

	tzset();

	/* Drop the privilege */
	if ((pw = getpwnam(RADIUSD_USER)) == NULL)
		goto on_fail;
	if (nochroot == 0 && 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, const u_char *pkt,
    size_t pktlen)
{
	return (module_common_radpkt(base, IMSG_RADIUSD_MODULE_ACCSREQ_ANSWER,
	    q_id, pkt, pktlen));
}

int
module_accsreq_next(struct module_base *base, u_int q_id, const u_char *pkt,
    size_t pktlen)
{
	return (module_common_radpkt(base, IMSG_RADIUSD_MODULE_ACCSREQ_NEXT,
	    q_id, 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);
}

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

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

static int
module_common_radpkt(struct module_base *base, uint32_t imsg_type, u_int q_id,
    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.pktlen = pktlen;
	ans.final = false;

	while (!ans.final) {
		siz = MAX_IMSGSIZE - sizeof(ans);
		if (len - off <= siz) {
			ans.final = true;
			siz = len - off;
		}
		iov[0].iov_base = &ans;
		iov[0].iov_len = sizeof(ans);
		if (siz > 0) {
			iov[1].iov_base = (u_char *)pkt + off;
			iov[1].iov_len = siz;
		}
		ret = imsg_composev(&base->ibuf, imsg_type, 0, 0, -1, iov,
		    (siz > 0)? 2 : 1);
		if (ret == -1)
			break;
		off += siz;
	}
	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:
	case IMSG_RADIUSD_MODULE_NEXTRES:
	case IMSG_RADIUSD_MODULE_REQDECO:
	case IMSG_RADIUSD_MODULE_RESDECO0_REQ:
	case IMSG_RADIUSD_MODULE_RESDECO:
	case IMSG_RADIUSD_MODULE_ACCTREQ:
	    {
		struct radiusd_module_radpkt_arg	*accessreq;
		int					 chunklen;
		const char				*typestr;

		if (imsg->hdr.type == IMSG_RADIUSD_MODULE_ACCSREQ) {
			if (module_access_request == NULL) {
				syslog(LOG_ERR, "Received ACCSREQ message, but "
				    "module doesn't support");
				break;
			}
			typestr = "ACCSREQ";
		} else if (imsg->hdr.type == IMSG_RADIUSD_MODULE_NEXTRES) {
			if (module_next_response == NULL) {
				syslog(LOG_ERR, "Received NEXTRES message, but "
				    "module doesn't support");
				break;
			}
			typestr = "NEXTRES";
		} else if (imsg->hdr.type == IMSG_RADIUSD_MODULE_ACCTREQ) {
			if (module_accounting_request == NULL) {
				syslog(LOG_ERR, "Received ACCTREQ message, but "
				    "module doesn't support");
				break;
			}
			typestr = "ACCTREQ";
		} else if (imsg->hdr.type == IMSG_RADIUSD_MODULE_REQDECO) {
			if (module_request_decoration == NULL) {
				syslog(LOG_ERR, "Received REQDECO message, but "
				    "module doesn't support");
				break;
			}
			typestr = "REQDECO";
		} else {
			if (module_response_decoration == NULL) {
				syslog(LOG_ERR, "Received RESDECO message, but "
				    "module doesn't support");
				break;
			}
			if (imsg->hdr.type == IMSG_RADIUSD_MODULE_RESDECO0_REQ)
				typestr = "RESDECO0_REQ";
			else
				typestr = "RESDECO";
		}

		if (datalen <
		    (ssize_t)sizeof(struct radiusd_module_radpkt_arg)) {
			syslog(LOG_ERR, "Received %s message, but "
			    "length is wrong", typestr);
			break;
		}
		accessreq = (struct radiusd_module_radpkt_arg *)imsg->data;
		if (base->radpktsiz < accessreq->pktlen) {
			u_char *nradpkt;
			if ((nradpkt = realloc(base->radpkt,
			    accessreq->pktlen)) == NULL) {
				syslog(LOG_ERR, "Could not handle received "
				    "%s message: %m", typestr);
				base->radpktoff = 0;
				goto accsreq_out;
			}
			base->radpkt = nradpkt;
			base->radpktsiz = accessreq->pktlen;
		}
		chunklen = datalen - sizeof(struct radiusd_module_radpkt_arg);
		if (chunklen > base->radpktsiz - base->radpktoff){
			syslog(LOG_ERR,
			    "Could not handle received %s message: "
			    "received length is too big", typestr);
			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->pktlen) {
			syslog(LOG_ERR,
			    "Could not handle received %s "
			    "message: length is mismatch", typestr);
			base->radpktoff = 0;
			goto accsreq_out;
		}
		if (imsg->hdr.type == IMSG_RADIUSD_MODULE_ACCSREQ)
			module_access_request(base->ctx, accessreq->q_id,
			    base->radpkt, base->radpktoff);
		else if (imsg->hdr.type == IMSG_RADIUSD_MODULE_NEXTRES)
			module_next_response(base->ctx, accessreq->q_id,
			    base->radpkt, base->radpktoff);
		else if (imsg->hdr.type == IMSG_RADIUSD_MODULE_REQDECO)
			module_request_decoration(base->ctx, accessreq->q_id,
			    base->radpkt, base->radpktoff);
		else if (imsg->hdr.type == IMSG_RADIUSD_MODULE_RESDECO0_REQ) {
			/* preserve request */
			if (base->radpktoff > base->radpkt2siz) {
				u_char *nradpkt;
				if ((nradpkt = realloc(base->radpkt2,
				    base->radpktoff)) == NULL) {
					syslog(LOG_ERR, "Could not handle "
					    "received %s message: %m", typestr);
					base->radpktoff = 0;
					goto accsreq_out;
				}
				base->radpkt2 = nradpkt;
				base->radpkt2siz = base->radpktoff;
			}
			memcpy(base->radpkt2, base->radpkt, base->radpktoff);
			base->radpkt2len = base->radpktoff;
		} else if (imsg->hdr.type == IMSG_RADIUSD_MODULE_RESDECO) {
			module_response_decoration(base->ctx, accessreq->q_id,
			    base->radpkt2, base->radpkt2len, base->radpkt,
			    base->radpktoff);
			base->radpkt2len = 0;
		} else
			module_accounting_request(base->ctx, accessreq->q_id,
			    base->radpkt, base->radpktoff);
		base->radpktoff = 0;
 accsreq_out:
		break;
	    }
	case IMSG_RADIUSD_MODULE_CTRL_UNBIND:
		goto forward_msg;
		break;
	default:
		if (imsg->hdr.type >= IMSG_RADIUSD_MODULE_MIN) {
 forward_msg:
			if (module_dispatch_control == NULL) {
				const char msg[] =
				    "the module doesn't handle any controls";
				imsg_compose(&base->ibuf, IMSG_NG,
				    imsg->hdr.peerid, 0, -1, msg, sizeof(msg));
			} else
				module_dispatch_control(base->ctx, imsg);
		}
	}

	return (0);
}

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 == -1 && errno == EAGAIN)
			break;
		if (ret == 0)
			syslog(LOG_ERR, "%s: connection is closed", __func__);
		else
			syslog(LOG_ERR, "%s: msgbuf_write: %d %m", __func__,
			    ret);
		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
}

int
module_imsg_compose(struct module_base *base, uint32_t type, uint32_t id,
    pid_t pid, int fd, const void *data, size_t datalen)
{
	int	 ret;

	if ((ret = imsg_compose(&base->ibuf, type, id, pid, fd, data, datalen))
	    != -1)
		module_reset_event(base);

	return (ret);
}

int
module_imsg_composev(struct module_base *base, uint32_t type, uint32_t id,
    pid_t pid, int fd, const struct iovec *iov, int iovcnt)
{
	int	 ret;

	if ((ret = imsg_composev(&base->ibuf, type, id, pid, fd, iov, iovcnt))
	    != -1)
		module_reset_event(base);

	return (ret);
}