GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/sasyncd/sasyncd.c Lines: 0 110 0.0 %
Date: 2017-11-07 Branches: 0 57 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: sasyncd.c,v 1.27 2017/04/10 09:27:08 reyk Exp $	*/
2
3
/*
4
 * Copyright (c) 2005 Håkan Olsson.  All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
28
/*
29
 * This code was written under funding by Multicom Security AB.
30
 */
31
32
33
#include <sys/types.h>
34
#include <sys/time.h>
35
#include <errno.h>
36
#include <fcntl.h>
37
#include <pwd.h>
38
#include <signal.h>
39
#include <stdio.h>
40
#include <string.h>
41
#include <stdlib.h>
42
#include <unistd.h>
43
44
#include "sasyncd.h"
45
46
volatile sig_atomic_t	daemon_shutdown = 0;
47
48
/* Called by signal handler for controlled daemon shutdown. */
49
static void
50
sasyncd_stop(int s)
51
{
52
	daemon_shutdown++;
53
}
54
55
static int
56
sasyncd_run(pid_t ppid)
57
{
58
	struct timeval	*timeout, tv;
59
	fd_set		*rfds, *wfds;
60
	size_t		 fdsetsize;
61
	int		 maxfd, n;
62
63
	n = getdtablesize();
64
	fdsetsize = howmany(n, NFDBITS) * sizeof(fd_mask);
65
66
	rfds = malloc(fdsetsize);
67
	if (!rfds) {
68
		log_err("malloc(%lu) failed", (unsigned long)fdsetsize);
69
		return -1;
70
	}
71
72
	wfds = malloc(fdsetsize);
73
	if (!wfds) {
74
		log_err("malloc(%lu) failed", (unsigned long)fdsetsize);
75
		free(rfds);
76
		return -1;
77
	}
78
79
	control_setrun();
80
81
	signal(SIGINT, sasyncd_stop);
82
	signal(SIGTERM, sasyncd_stop);
83
84
	timer_add("carp_undemote", CARP_DEMOTE_MAXTIME,
85
	    monitor_carpundemote, NULL);
86
87
	while (!daemon_shutdown) {
88
		memset(rfds, 0, fdsetsize);
89
		memset(wfds, 0, fdsetsize);
90
		maxfd = net_set_rfds(rfds);
91
		n = net_set_pending_wfds(wfds);
92
		if (n > maxfd)
93
			maxfd = n;
94
95
		pfkey_set_rfd(rfds);
96
		pfkey_set_pending_wfd(wfds);
97
		if (cfgstate.pfkey_socket + 1 > maxfd)
98
			maxfd = cfgstate.pfkey_socket + 1;
99
100
		carp_set_rfd(rfds);
101
		if (cfgstate.route_socket + 1 > maxfd)
102
			maxfd = cfgstate.route_socket + 1;
103
104
		timeout = &tv;
105
		timer_next_event(&tv);
106
107
		n = select(maxfd, rfds, wfds, 0, timeout);
108
		if (n == -1) {
109
			if (errno != EINTR) {
110
				log_err("select()");
111
				sleep(1);
112
			}
113
		} else if (n) {
114
			net_handle_messages(rfds);
115
			net_send_messages(wfds);
116
			pfkey_read_message(rfds);
117
			pfkey_send_message(wfds);
118
			carp_read_message(rfds);
119
		}
120
		timer_run();
121
122
		/* Mostly for debugging. */
123
		if (getppid() != ppid) {
124
			log_msg(0, "sasyncd: parent died");
125
			daemon_shutdown++;
126
		}
127
	}
128
	free(rfds);
129
	free(wfds);
130
	return 0;
131
}
132
133
__dead static void
134
usage(void)
135
{
136
	extern char *__progname;
137
138
	fprintf(stderr, "usage: %s [-dnv] [-c config-file]\n", __progname);
139
	exit(1);
140
}
141
142
int
143
main(int argc, char **argv)
144
{
145
	extern char	*__progname;
146
	char		*cfgfile = 0;
147
	int		 ch, noaction = 0;
148
149
	if (geteuid() != 0) {
150
		/* No point in continuing. */
151
		fprintf(stderr, "%s: This daemon needs to be run as root.\n",
152
		    __progname);
153
		return 1;
154
	}
155
156
	while ((ch = getopt(argc, argv, "c:dnv")) != -1) {
157
		switch (ch) {
158
		case 'c':
159
			if (cfgfile)
160
				usage();
161
			cfgfile = optarg;
162
			break;
163
		case 'd':
164
			cfgstate.debug++;
165
			break;
166
		case 'n':
167
			noaction = 1;
168
			break;
169
		case 'v':
170
			cfgstate.verboselevel++;
171
			break;
172
		default:
173
			usage();
174
		}
175
	}
176
	argc -= optind;
177
	argv += optind;
178
179
	if (argc > 0)
180
		usage();
181
182
	log_init(__progname);
183
	timer_init();
184
185
	cfgstate.runstate = INIT;
186
	LIST_INIT(&cfgstate.peerlist);
187
188
	cfgstate.listen_port = SASYNCD_DEFAULT_PORT;
189
	cfgstate.flags |= CTL_DEFAULT;
190
191
	if (!cfgfile)
192
		cfgfile = SASYNCD_CFGFILE;
193
194
	if (conf_parse_file(cfgfile) == 0 ) {
195
		if (!cfgstate.sharedkey) {
196
			fprintf(stderr, "config: "
197
			    "no shared key specified, cannot continue\n");
198
			exit(1);
199
		}
200
		if (!cfgstate.carp_ifname || !*cfgstate.carp_ifname) {
201
			fprintf(stderr, "config: "
202
			    "no carp interface specified, cannot continue\n");
203
			exit(1);
204
		}
205
	} else {
206
		exit(1);
207
	}
208
209
	if (noaction) {
210
		fprintf(stderr, "configuration OK\n");
211
		exit(0);
212
	}
213
214
	carp_demote(CARP_INC, 0);
215
216
	if (carp_init())
217
		return 1;
218
	if (pfkey_init(0))
219
		return 1;
220
	if (net_init())
221
		return 1;
222
223
	if (!cfgstate.debug)
224
		if (daemon(1, 0)) {
225
			perror("daemon()");
226
			exit(1);
227
		}
228
229
	if (monitor_init()) {
230
		/* Parent, with privileges. */
231
		monitor_loop();
232
		exit(0);
233
	}
234
235
	/* Child, no privileges left. Run main loop. */
236
	sasyncd_run(getppid());
237
238
	/* Shutdown. */
239
	log_msg(0, "shutting down...");
240
241
	net_shutdown();
242
	pfkey_shutdown();
243
	return 0;
244
}