xref: /openbsd/usr.sbin/relayd/hce.c (revision 404b540a)
1 /*	$OpenBSD: hce.c,v 1.53 2009/06/05 23:39:51 pyr Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/queue.h>
21 #include <sys/time.h>
22 #include <sys/stat.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 
26 #include <net/if.h>
27 #include <netinet/in_systm.h>
28 #include <netinet/in.h>
29 #include <netinet/ip.h>
30 
31 #include <errno.h>
32 #include <event.h>
33 #include <fcntl.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <err.h>
38 #include <pwd.h>
39 
40 #include <openssl/ssl.h>
41 
42 #include "relayd.h"
43 
44 __dead void hce_shutdown(void);
45 void	hce_sig_handler(int sig, short, void *);
46 void	hce_dispatch_imsg(int, short, void *);
47 void	hce_dispatch_parent(int, short, void *);
48 void	hce_launch_checks(int, short, void *);
49 void	hce_setup_events(void);
50 void	hce_disable_events(void);
51 
52 static struct relayd *env = NULL;
53 struct imsgev		*iev_pfe;
54 struct imsgev		*iev_main;
55 int			 running = 0;
56 
57 void
58 hce_sig_handler(int sig, short event, void *arg)
59 {
60 	switch (sig) {
61 	case SIGINT:
62 	case SIGTERM:
63 		hce_shutdown();
64 		break;
65 	default:
66 		fatalx("hce_sig_handler: unexpected signal");
67 	}
68 }
69 
70 pid_t
71 hce(struct relayd *x_env, int pipe_parent2pfe[2], int pipe_parent2hce[2],
72     int pipe_parent2relay[RELAY_MAXPROC][2], int pipe_pfe2hce[2],
73     int pipe_pfe2relay[RELAY_MAXPROC][2])
74 {
75 	pid_t		 pid;
76 	struct passwd	*pw;
77 	int		 i;
78 	struct event	 ev_sigint;
79 	struct event	 ev_sigterm;
80 
81 	switch (pid = fork()) {
82 	case -1:
83 		fatal("hce: cannot fork");
84 	case 0:
85 		break;
86 	default:
87 		return (pid);
88 	}
89 
90 	env = x_env;
91 	purge_config(env, PURGE_RDRS|PURGE_RELAYS|PURGE_PROTOS);
92 
93 	if ((pw = getpwnam(RELAYD_USER)) == NULL)
94 		fatal("hce: getpwnam");
95 
96 #ifndef DEBUG
97 	if (chroot(pw->pw_dir) == -1)
98 		fatal("hce: chroot");
99 	if (chdir("/") == -1)
100 		fatal("hce: chdir(\"/\")");
101 #else
102 #warning disabling privilege revocation and chroot in DEBUG mode
103 #endif
104 
105 	setproctitle("host check engine");
106 	relayd_process = PROC_HCE;
107 
108 	/* this is needed for icmp tests */
109 	icmp_init(env);
110 
111 #ifndef DEBUG
112 	if (setgroups(1, &pw->pw_gid) ||
113 	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
114 	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
115 		fatal("hce: can't drop privileges");
116 #endif
117 
118 	event_init();
119 
120 	if ((iev_pfe = calloc(1, sizeof(struct imsgev))) == NULL ||
121 	    (iev_main = calloc(1, sizeof(struct imsgev))) == NULL)
122 		fatal("hce");
123 	imsg_init(&iev_pfe->ibuf, pipe_pfe2hce[0]);
124 	iev_pfe->handler = hce_dispatch_imsg;
125 	imsg_init(&iev_main->ibuf, pipe_parent2hce[1]);
126 	iev_main->handler = hce_dispatch_parent;
127 
128 	iev_pfe->events = EV_READ;
129 	event_set(&iev_pfe->ev, iev_pfe->ibuf.fd, iev_pfe->events,
130 	    iev_pfe->handler, iev_pfe);
131 	event_add(&iev_pfe->ev, NULL);
132 
133 	iev_main->events = EV_READ;
134 	event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
135 	    iev_main->handler, iev_main);
136 	event_add(&iev_main->ev, NULL);
137 
138 	signal_set(&ev_sigint, SIGINT, hce_sig_handler, NULL);
139 	signal_set(&ev_sigterm, SIGTERM, hce_sig_handler, NULL);
140 	signal_add(&ev_sigint, NULL);
141 	signal_add(&ev_sigterm, NULL);
142 	signal(SIGPIPE, SIG_IGN);
143 	signal(SIGHUP, SIG_IGN);
144 
145 	/* setup pipes */
146 	close(pipe_pfe2hce[1]);
147 	close(pipe_parent2hce[0]);
148 	close(pipe_parent2pfe[0]);
149 	close(pipe_parent2pfe[1]);
150 	for (i = 0; i < env->sc_prefork_relay; i++) {
151 		close(pipe_parent2relay[i][0]);
152 		close(pipe_parent2relay[i][1]);
153 		close(pipe_pfe2relay[i][0]);
154 		close(pipe_pfe2relay[i][1]);
155 	}
156 
157 	hce_setup_events();
158 	event_dispatch();
159 	hce_shutdown();
160 
161 	return (0);
162 }
163 
164 void
165 hce_setup_events(void)
166 {
167 	struct timeval	 tv;
168 	struct table	*table;
169 
170 	snmp_init(env, iev_main);
171 
172 	if (!TAILQ_EMPTY(env->sc_tables)) {
173 		evtimer_set(&env->sc_ev, hce_launch_checks, env);
174 		bzero(&tv, sizeof(tv));
175 		evtimer_add(&env->sc_ev, &tv);
176 	}
177 
178 	if (env->sc_flags & F_SSL) {
179 		ssl_init(env);
180 		TAILQ_FOREACH(table, env->sc_tables, entry) {
181 			if (!(table->conf.flags & F_SSL))
182 				continue;
183 			table->ssl_ctx = ssl_ctx_create(env);
184 		}
185 	}
186 }
187 
188 void
189 hce_disable_events(void)
190 {
191 	struct table	*table;
192 	struct host	*host;
193 
194 	evtimer_del(&env->sc_ev);
195 	TAILQ_FOREACH(table, env->sc_tables, entry) {
196 		TAILQ_FOREACH(host, &table->hosts, entry) {
197 			host->he = HCE_ABORT;
198 			event_del(&host->cte.ev);
199 			close(host->cte.s);
200 		}
201 	}
202 	if (env->sc_has_icmp) {
203 		event_del(&env->sc_icmp_send.ev);
204 		event_del(&env->sc_icmp_recv.ev);
205 	}
206 	if (env->sc_has_icmp6) {
207 		event_del(&env->sc_icmp6_send.ev);
208 		event_del(&env->sc_icmp6_recv.ev);
209 	}
210 }
211 
212 void
213 hce_launch_checks(int fd, short event, void *arg)
214 {
215 	struct host		*host;
216 	struct table		*table;
217 	struct timeval		 tv;
218 
219 	/*
220 	 * notify pfe checks are done and schedule next check
221 	 */
222 	imsg_compose_event(iev_pfe, IMSG_SYNC, 0, 0, -1, NULL, 0);
223 	TAILQ_FOREACH(table, env->sc_tables, entry) {
224 		TAILQ_FOREACH(host, &table->hosts, entry) {
225 			if ((host->flags & F_CHECK_DONE) == 0)
226 				host->he = HCE_INTERVAL_TIMEOUT;
227 			host->flags &= ~(F_CHECK_SENT|F_CHECK_DONE);
228 			event_del(&host->cte.ev);
229 		}
230 	}
231 
232 	if (gettimeofday(&tv, NULL) == -1)
233 		fatal("hce_launch_checks: gettimeofday");
234 
235 	TAILQ_FOREACH(table, env->sc_tables, entry) {
236 		if (table->conf.flags & F_DISABLE)
237 			continue;
238 		if (table->conf.skip_cnt) {
239 			if (table->skipped++ > table->conf.skip_cnt)
240 				table->skipped = 0;
241 			if (table->skipped != 1)
242 				continue;
243 		}
244 		if (table->conf.check == CHECK_NOCHECK)
245 			fatalx("hce_launch_checks: unknown check type");
246 
247 		TAILQ_FOREACH(host, &table->hosts, entry) {
248 			if (host->flags & F_DISABLE || host->conf.parentid)
249 				continue;
250 			switch (table->conf.check) {
251 			case CHECK_ICMP:
252 				schedule_icmp(env, host);
253 				break;
254 			case CHECK_SCRIPT:
255 				check_script(host);
256 				break;
257 			default:
258 				/* Any other TCP-style checks */
259 				host->last_up = host->up;
260 				host->cte.host = host;
261 				host->cte.table = table;
262 				bcopy(&tv, &host->cte.tv_start,
263 				    sizeof(host->cte.tv_start));
264 				check_tcp(&host->cte);
265 				break;
266 			}
267 		}
268 	}
269 	check_icmp(env, &tv);
270 
271 	bcopy(&env->sc_interval, &tv, sizeof(tv));
272 	evtimer_add(&env->sc_ev, &tv);
273 }
274 
275 void
276 hce_notify_done(struct host *host, enum host_error he)
277 {
278 	struct table		*table;
279 	struct ctl_status	 st;
280 	struct timeval		 tv_now, tv_dur;
281 	u_long			 duration;
282 	u_int			 logopt;
283 	struct host		*h;
284 	int			 hostup;
285 	const char		*msg;
286 
287 	hostup = host->up;
288 	host->he = he;
289 
290 	if (host->up == HOST_DOWN && host->retry_cnt) {
291 		log_debug("hce_notify_done: host %s retry %d",
292 		    host->conf.name, host->retry_cnt);
293 		host->up = host->last_up;
294 		host->retry_cnt--;
295 	} else
296 		host->retry_cnt = host->conf.retry;
297 	if (host->up != HOST_UNKNOWN) {
298 		host->check_cnt++;
299 		if (host->up == HOST_UP)
300 			host->up_cnt++;
301 	}
302 	st.id = host->conf.id;
303 	st.up = host->up;
304 	st.check_cnt = host->check_cnt;
305 	st.retry_cnt = host->retry_cnt;
306 	st.he = he;
307 	host->flags |= (F_CHECK_SENT|F_CHECK_DONE);
308 	msg = host_error(he);
309 	if (msg)
310 		log_debug("hce_notify_done: %s (%s)", host->conf.name, msg);
311 
312 	imsg_compose_event(iev_pfe, IMSG_HOST_STATUS,
313 	    0, 0, -1, &st, sizeof(st));
314 	if (host->up != host->last_up)
315 		logopt = RELAYD_OPT_LOGUPDATE;
316 	else
317 		logopt = RELAYD_OPT_LOGNOTIFY;
318 
319 	if (gettimeofday(&tv_now, NULL) == -1)
320 		fatal("hce_notify_done: gettimeofday");
321 	timersub(&tv_now, &host->cte.tv_start, &tv_dur);
322 	if (timercmp(&host->cte.tv_start, &tv_dur, >))
323 		duration = (tv_dur.tv_sec * 1000) + (tv_dur.tv_usec / 1000.0);
324 	else
325 		duration = 0;
326 
327 	if ((table = table_find(env, host->conf.tableid)) == NULL)
328 		fatalx("hce_notify_done: invalid table id");
329 
330 	if (env->sc_opts & logopt) {
331 		log_info("host %s, check %s%s (%lums), state %s -> %s, "
332 		    "availability %s",
333 		    host->conf.name, table_check(table->conf.check),
334 		    (table->conf.flags & F_SSL) ? " use ssl" : "", duration,
335 		    host_status(host->last_up), host_status(host->up),
336 		    print_availability(host->check_cnt, host->up_cnt));
337 	}
338 
339 	if (host->last_up != host->up)
340 		snmp_hosttrap(table, host);
341 
342 	host->last_up = host->up;
343 
344 	if (SLIST_EMPTY(&host->children))
345 		return;
346 
347 	/* Notify for all other hosts that inherit the state from this one */
348 	SLIST_FOREACH(h, &host->children, child) {
349 		h->up = hostup;
350 		hce_notify_done(h, he);
351 	}
352 }
353 
354 void
355 hce_shutdown(void)
356 {
357 	log_info("host check engine exiting");
358 	_exit(0);
359 }
360 
361 void
362 hce_dispatch_imsg(int fd, short event, void *ptr)
363 {
364 	struct imsgev		*iev;
365 	struct imsgbuf		*ibuf;
366 	struct imsg		 imsg;
367 	ssize_t			 n;
368 	objid_t			 id;
369 	struct host		*host;
370 	struct table		*table;
371 
372 	iev = ptr;
373 	ibuf = &iev->ibuf;
374 
375 	if (event & EV_READ) {
376 		if ((n = imsg_read(ibuf)) == -1)
377 			fatal("hce_dispatch_imsg: imsg_read_error");
378 		if (n == 0) {
379 			/* this pipe is dead, so remove the event handler */
380 			event_del(&iev->ev);
381 			event_loopexit(NULL);
382 			return;
383 		}
384 	}
385 
386 	if (event & EV_WRITE) {
387 		if (msgbuf_write(&ibuf->w) == -1)
388 			fatal("hce_dispatch_imsg: msgbuf_write");
389 	}
390 
391 	for (;;) {
392 		if ((n = imsg_get(ibuf, &imsg)) == -1)
393 			fatal("hce_dispatch_imsg: imsg_read error");
394 		if (n == 0)
395 			break;
396 
397 		switch (imsg.hdr.type) {
398 		case IMSG_HOST_DISABLE:
399 			memcpy(&id, imsg.data, sizeof(id));
400 			if ((host = host_find(env, id)) == NULL)
401 				fatalx("hce_dispatch_imsg: desynchronized");
402 			host->flags |= F_DISABLE;
403 			host->up = HOST_UNKNOWN;
404 			host->check_cnt = 0;
405 			host->up_cnt = 0;
406 			host->he = HCE_NONE;
407 			break;
408 		case IMSG_HOST_ENABLE:
409 			memcpy(&id, imsg.data, sizeof(id));
410 			if ((host = host_find(env, id)) == NULL)
411 				fatalx("hce_dispatch_imsg: desynchronized");
412 			host->flags &= ~(F_DISABLE);
413 			host->up = HOST_UNKNOWN;
414 			host->he = HCE_NONE;
415 			break;
416 		case IMSG_TABLE_DISABLE:
417 			memcpy(&id, imsg.data, sizeof(id));
418 			if ((table = table_find(env, id)) == NULL)
419 				fatalx("hce_dispatch_imsg: desynchronized");
420 			table->conf.flags |= F_DISABLE;
421 			TAILQ_FOREACH(host, &table->hosts, entry)
422 				host->up = HOST_UNKNOWN;
423 			break;
424 		case IMSG_TABLE_ENABLE:
425 			memcpy(&id, imsg.data, sizeof(id));
426 			if ((table = table_find(env, id)) == NULL)
427 				fatalx("hce_dispatch_imsg: desynchronized");
428 			table->conf.flags &= ~(F_DISABLE);
429 			TAILQ_FOREACH(host, &table->hosts, entry)
430 				host->up = HOST_UNKNOWN;
431 			break;
432 		case IMSG_CTL_POLL:
433 			evtimer_del(&env->sc_ev);
434 			TAILQ_FOREACH(table, env->sc_tables, entry)
435 				table->skipped = 0;
436 			hce_launch_checks(-1, EV_TIMEOUT, env);
437 			break;
438 		default:
439 			log_debug("hce_dispatch_msg: unexpected imsg %d",
440 			    imsg.hdr.type);
441 			break;
442 		}
443 		imsg_free(&imsg);
444 	}
445 	imsg_event_add(iev);
446 }
447 
448 void
449 hce_dispatch_parent(int fd, short event, void * ptr)
450 {
451 	struct imsgev		*iev;
452 	struct imsgbuf		*ibuf;
453 	struct imsg		 imsg;
454 	struct ctl_script	 scr;
455 	ssize_t			 n;
456 	size_t			 len;
457 	static struct table	*table = NULL;
458 	struct host		*host, *parent;
459 
460 	iev = ptr;
461 	ibuf = &iev->ibuf;
462 
463 	if (event & EV_READ) {
464 		if ((n = imsg_read(ibuf)) == -1)
465 			fatal("hce_dispatch_parent: imsg_read error");
466 		if (n == 0) {
467 			/* this pipe is dead, so remove the event handler */
468 			event_del(&iev->ev);
469 			event_loopexit(NULL);
470 			return;
471 		}
472 	}
473 
474 	if (event & EV_WRITE) {
475 		if (msgbuf_write(&ibuf->w) == -1)
476 			fatal("hce_dispatch_parent: msgbuf_write");
477 	}
478 
479 	for (;;) {
480 		if ((n = imsg_get(ibuf, &imsg)) == -1)
481 			fatal("hce_dispatch_parent: imsg_read error");
482 		if (n == 0)
483 			break;
484 
485 		switch (imsg.hdr.type) {
486 		case IMSG_SCRIPT:
487 			if (imsg.hdr.len - IMSG_HEADER_SIZE !=
488 			    sizeof(scr))
489 				fatalx("hce_dispatch_parent: "
490 				    "invalid size of script request");
491 			bcopy(imsg.data, &scr, sizeof(scr));
492 			script_done(env, &scr);
493 			break;
494 		case IMSG_RECONF:
495 			log_debug("hce: reloading configuration");
496 			if (imsg.hdr.len !=
497 			    sizeof(struct relayd) + IMSG_HEADER_SIZE)
498 				fatalx("corrupted reload data");
499 			hce_disable_events();
500 			purge_config(env, PURGE_TABLES);
501 			merge_config(env, (struct relayd *)imsg.data);
502 
503 			env->sc_tables = calloc(1, sizeof(*env->sc_tables));
504 			if (env->sc_tables == NULL)
505 				fatal(NULL);
506 
507 			TAILQ_INIT(env->sc_tables);
508 			break;
509 		case IMSG_RECONF_TABLE:
510 			if ((table = calloc(1, sizeof(*table))) == NULL)
511 				fatal(NULL);
512 			memcpy(&table->conf, imsg.data, sizeof(table->conf));
513 			TAILQ_INIT(&table->hosts);
514 			TAILQ_INSERT_TAIL(env->sc_tables, table, entry);
515 			break;
516 		case IMSG_RECONF_SENDBUF:
517 			len = imsg.hdr.len - IMSG_HEADER_SIZE;
518 			table->sendbuf = calloc(1, len);
519 			(void)strlcpy(table->sendbuf, (char *)imsg.data, len);
520 			break;
521 		case IMSG_RECONF_HOST:
522 			if ((host = calloc(1, sizeof(*host))) == NULL)
523 				fatal(NULL);
524 			memcpy(&host->conf, imsg.data, sizeof(host->conf));
525 			host->tablename = table->conf.name;
526 			TAILQ_INSERT_TAIL(&table->hosts, host, entry);
527 			if (host->conf.parentid) {
528 				parent = host_find(env, host->conf.parentid);
529 				SLIST_INSERT_HEAD(&parent->children,
530 				    host, child);
531 			}
532 			break;
533 		case IMSG_RECONF_END:
534 			log_warnx("hce: configuration reloaded");
535 			hce_setup_events();
536 			break;
537 		default:
538 			log_debug("hce_dispatch_parent: unexpected imsg %d",
539 			    imsg.hdr.type);
540 			break;
541 		}
542 		imsg_free(&imsg);
543 	}
544 	imsg_event_add(iev);
545 }
546