chiark / gitweb /
server/tripe.c: Formalize the main loop machinery.
[tripe] / server / tripe.c
CommitLineData
410c8acf 1/* -*-c-*-
410c8acf 2 *
3 * Main program
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
410c8acf 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
11ad66c2
MW
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
e04c2d50 16 *
11ad66c2
MW
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
e04c2d50 21 *
410c8acf 22 * You should have received a copy of the GNU General Public License
11ad66c2 23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
410c8acf 24 */
25
410c8acf 26/*----- Header files ------------------------------------------------------*/
27
28#include "tripe.h"
29
30/*----- Global variables --------------------------------------------------*/
31
32sel_state sel;
410c8acf 33
34/*----- Static variables --------------------------------------------------*/
35
36static sel_timer it;
37#define T_INTERVAL MIN(1)
38
a7abb429
MW
39static unsigned iv_nreasons = 0;
40static struct timeval iv_next = { 0, 0 };
41
42/*----- The interval timer ------------------------------------------------*/
410c8acf 43
44/* --- @interval@ --- *
45 *
46 * Arguments: @struct timeval *tv@ = time when called
47 * @void *v@ = boring pointer
48 *
49 * Returns: ---
50 *
51 * Use: Called periodically to do housekeeping tasks.
52 */
53
42da2a58 54static void interval(struct timeval *tv, void *v)
410c8acf 55{
410c8acf 56 T( trace(T_PEER, "peer: interval timer"); )
a7abb429 57 iv_next = *tv;
b5c45da1 58 rand_seed(RAND_GLOBAL, MAXHASHSZ);
410c8acf 59 p_interval();
a7abb429
MW
60 iv_next.tv_sec += T_INTERVAL;
61 sel_addtimer(&sel, &it, &iv_next, interval, v);
62}
63
64/* --- @iv_addreason@ --- *
65 *
66 * Arguments: ---
67 *
68 * Returns: ---
69 *
70 * Use: Adds an `interval timer reason'; if there are no others, the
71 * interval timer is engaged.
72 */
73
74void iv_addreason(void)
75{
76 struct timeval tv;
77
78 if (!iv_nreasons) {
79 gettimeofday(&tv, 0);
80 if (TV_CMP(&tv, >=, &iv_next)) interval(&tv, 0);
81 else sel_addtimer(&sel, &it, &iv_next, interval, 0);
82 }
83 iv_nreasons++;
410c8acf 84}
85
a7abb429
MW
86/* --- @iv_rmreason@ --- *
87 *
88 * Arguments: ---
89 *
90 * Returns: ---
91 *
92 * Use: Removes an interval timer reason; if there are none left, the
93 * interval timer is disengaged.
94 */
95
96void iv_rmreason(void)
97{
98 assert(iv_nreasons); iv_nreasons--;
99 if (!iv_nreasons) sel_rmtimer(&it);
100}
101
c9aded9f
MW
102/*----- The main loop -----------------------------------------------------*/
103
104/* --- @lp_init@ --- *
105 *
106 * Arguments: ---
107 *
108 * Returns: ---
109 *
110 * Use: Initializes the main loop. Most importantly, this sets up
111 * the select multiplexor that everything else hooks onto.
112 */
113
114void lp_init(void)
115{
116 rand_noisesrc(RAND_GLOBAL, &noise_source);
117 rand_seed(RAND_GLOBAL, MAXHASHSZ);
118 gettimeofday(&iv_next, 0); iv_next.tv_sec += T_INTERVAL;
119 signal(SIGPIPE, SIG_IGN);
120 sel_init(&sel);
121 sig_init(&sel);
122}
123
124/* --- @lp_run@ --- *
125 *
126 * Arguments: ---
127 *
128 * Returns: Zero on successful termination; @-1@ if things went wrong.
129 *
130 * Use: Cranks the main loop until it should be cranked no more.
131 */
132
133int lp_run(void)
134{
135 int nerr = 0;
136
137 for (;;) {
138 a_preselect();
139 if (!sel_select(&sel)) nerr = 0;
140 else if (errno != EINTR && errno != EAGAIN) {
141 a_warn("SERVER", "select-error", "?ERRNO", A_END);
142 nerr++;
143 if (nerr > 8) {
144 a_warn("ABORT", "repeated-select-errors", A_END);
145 abort();
146 }
147 }
148 }
149 return (0);
150}
151
a7abb429
MW
152/*----- Main code ---------------------------------------------------------*/
153
410c8acf 154/* --- @main@ --- *
155 *
156 * Arguments: @int argc@ = number of command line arguments
157 * @char *argv[]@ = vector of arguments
158 *
159 * Returns: Zero if OK, nonzero on error.
160 *
161 * Use: Main program. Provides a simple VPN.
162 */
163
164static void usage(FILE *fp)
165{
46dde080 166 pquis(fp, "Usage: $ [-DF] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\
a9279e37 167 [-U USER] [-G GROUP] [-a SOCKET] [-m MODE] [-T TRACE-OPTS]\n\
2d752320 168 [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n");
410c8acf 169}
170
f98df549 171static void version(FILE *fp) { pquis(fp, "$, version " VERSION "\n"); }
410c8acf 172
173static void help(FILE *fp)
174{
175 version(fp);
176 fputc('\n', fp);
177 usage(fp);
178 fputs("\n\
179Options:\n\
180\n\
181-h, --help Display this help text.\n\
182-v, --version Display version number.\n\
183-u, --usage Display pointless usage message.\n\
42da2a58 184 --tunnels Display IP tunnel drivers and exit.\n\
410c8acf 185\n\
47828bd9
MW
186-4, --ipv4 Transport over IPv4 only.\n\
187-6, --ipv6 Transport over IPv6 only.\n\
410c8acf 188-D, --daemon Run in the background.\n\
46dde080 189-F, --foreground Quit when stdin reports end-of-file.\n\
ef4a1ab7 190-d, --directory=DIR Switch to directory DIR [default " CONFIGDIR "].\n\
d13e5724 191-b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
165efde7
MW
192-p, --port=PORT Select UDP port to listen to "
193 "[default " STR(TRIPE_PORT) "].\n\
42da2a58 194-n, --tunnel=TUNNEL Seelect default tunnel driver.\n\
d13e5724 195-U, --setuid=USER Set uid to USER after initialization.\n\
196-G, --setgid=GROUP Set gid to GROUP after initialization.\n\
410c8acf 197-k, --priv-keyring=FILE Get private key from FILE.\n\
198-K, --pub-keyring=FILE Get public keys from FILE.\n\
199-t, --tag=KEYTAG Use private key labelled TAG.\n\
200-a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
a9279e37 201-m, --admin-perms=MODE Permissions to set on admin socket [default 600].\n\
c5a2ea9b 202" T( "\
410c8acf 203-T, --trace=OPTIONS Turn on tracing options.\n\
c5a2ea9b 204" ) "\
410c8acf 205", fp);
206}
207
208int main(int argc, char *argv[])
209{
210 const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
fc5f4823 211 const char *tag_priv = 0;
ef4a1ab7 212 const char *csock = SOCKETDIR "/tripesock";
a9279e37 213 int csockmode = 0600;
ef4a1ab7 214 const char *dir = CONFIGDIR;
410c8acf 215 const char *p;
89640f3f
MW
216 const char *bindhost = 0, *bindsvc = STR(TRIPE_PORT);
217 struct addrinfo aihint = { 0 }, *ailist;
410c8acf 218 unsigned f = 0;
42da2a58 219 int i;
c9aded9f 220 int err;
46dde080 221 unsigned af;
410c8acf 222 uid_t u = -1;
223 gid_t g = -1;
224
225#define f_bogus 1u
226#define f_daemon 2u
46dde080 227#define f_foreground 4u
410c8acf 228
229 ego(argv[0]);
d360f042 230 T( trace_on(stderr, 0); )
410c8acf 231
232 if ((p = getenv("TRIPEDIR")) != 0)
233 dir = p;
797cf76b
MW
234 if ((p = getenv("TRIPESOCK")) != 0)
235 csock = p;
42da2a58 236 tun_default = tunnels[0];
47828bd9 237 aihint.ai_family = AF_UNSPEC;
410c8acf 238
239 for (;;) {
240 static const struct option opts[] = {
241 { "help", 0, 0, 'h' },
242 { "version", 0, 0, 'v' },
243 { "usage", 0, 0, 'u' },
42da2a58 244 { "tunnels", 0, 0, '0' },
410c8acf 245
47828bd9
MW
246 { "ipv4", 0, 0, '4' },
247 { "ipv6", 0, 0, '6' },
410c8acf 248 { "daemon", 0, 0, 'D' },
46dde080 249 { "foreground", 0, 0, 'F' },
410c8acf 250 { "uid", OPTF_ARGREQ, 0, 'U' },
251 { "setuid", OPTF_ARGREQ, 0, 'U' },
252 { "gid", OPTF_ARGREQ, 0, 'G' },
253 { "setgid", OPTF_ARGREQ, 0, 'G' },
767b36e2 254 { "bind-address", OPTF_ARGREQ, 0, 'b' },
42da2a58 255 { "tunnel", OPTF_ARGREQ, 0, 'n' },
410c8acf 256 { "port", OPTF_ARGREQ, 0, 'p' },
257 { "directory", OPTF_ARGREQ, 0, 'd' },
258 { "priv-keyring", OPTF_ARGREQ, 0, 'k' },
259 { "pub-keyring", OPTF_ARGREQ, 0, 'K' },
260 { "tag", OPTF_ARGREQ, 0, 't' },
261 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
a9279e37 262 { "admin-perms", OPTF_ARGREQ, 0, 'm' },
410c8acf 263#ifndef NTRACE
264 { "trace", OPTF_ARGREQ, 0, 'T' },
265#endif
266
267 { 0, 0, 0, 0 }
268 };
269
47828bd9 270 i = mdwopt(argc, argv, "hvu46DFU:G:b:n:p:d:k:K:t:a:m:" T("T:"),
42da2a58 271 opts, 0, 0, 0);
410c8acf 272 if (i < 0)
273 break;
274 switch (i) {
275 case 'h':
276 help(stdout);
277 exit(0);
278 case 'v':
279 version(stdout);
280 exit(0);
281 case 'u':
282 usage(stdout);
283 exit(0);
284
47828bd9
MW
285 case '4':
286 aihint.ai_family = AF_INET;
287 break;
288 case '6':
289 aihint.ai_family = AF_INET6;
290 break;
410c8acf 291 case 'D':
292 f |= f_daemon;
293 break;
52b86648
MW
294 case 'U':
295 u = u_getuser(optarg, &g);
296 break;
297 case 'G':
298 g = u_getgroup(optarg);
299 break;
46dde080
MW
300 case 'F':
301 f |= f_foreground;
302 break;
767b36e2 303
89640f3f
MW
304 case 'b':
305 bindhost = optarg;
306 break;
307 case 'p':
308 bindsvc = optarg;
309 break;
42da2a58 310 case 'n': {
311 int i;
312 for (i = 0;; i++) {
313 if (!tunnels[i])
314 die(EXIT_FAILURE, "unknown tunnel `%s'", optarg);
315 if (mystrieq(optarg, tunnels[i]->name))
316 break;
317 }
318 tun_default = tunnels[i];
319 } break;
410c8acf 320 case 'd':
321 dir = optarg;
322 break;
323 case 'k':
324 kr_priv = optarg;
325 break;
326 case 'K':
327 kr_pub = optarg;
328 break;
329 case 'a':
330 csock = optarg;
331 break;
a9279e37
MW
332 case 'm': {
333 char *p;
334 csockmode = strtol(optarg, &p, 8);
335 if (*p) die(EXIT_FAILURE, "bad permissions: `%s'", optarg);
336 } break;
410c8acf 337 case 't':
338 tag_priv = optarg;
339 break;
340#ifndef NTRACE
341 case 'T':
342 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
343 trace_level(tr_flags);
344 break;
345#endif
ef4a1ab7 346 case '0': {
42da2a58 347 int i;
348 for (i = 0; tunnels[i]; i++)
349 puts(tunnels[i]->name);
ef4a1ab7 350 exit(0);
351 } break;
410c8acf 352 default:
353 f |= f_bogus;
354 break;
355 }
356 }
357
358 if (optind < argc || (f & f_bogus)) {
359 usage(stderr);
360 exit(EXIT_FAILURE);
361 }
46dde080
MW
362 if (!(~f & (f_daemon | f_foreground)))
363 die(EXIT_FAILURE, "foreground operation for a daemon is silly");
410c8acf 364
89640f3f
MW
365 aihint.ai_protocol = IPPROTO_UDP;
366 aihint.ai_socktype = SOCK_DGRAM;
367 aihint.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
368 if ((err = getaddrinfo(bindhost, bindsvc, &aihint, &ailist)) != 0) {
369 die(EXIT_FAILURE, "couldn't resolve hostname %c%s%c, port `%s': %s",
370 bindhost ? '`' : '<',
371 bindhost ? bindhost : "nil",
372 bindhost ? '\'' : '>',
373 bindsvc, gai_strerror(err));
374 }
375
410c8acf 376 if (chdir(dir)) {
377 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
378 dir, strerror(errno));
379 }
380
c9aded9f 381 lp_init();
acd781a8 382
3cdc3f3a 383 if (!(f & f_daemon)) {
46dde080 384 af = AF_WARN;
3cdc3f3a 385#ifndef NTRACE
46dde080 386 af |= AF_TRACE;
3cdc3f3a 387#endif
46dde080
MW
388 if (f & f_foreground)
389 af |= AF_FOREGROUND;
390 a_create(STDIN_FILENO, STDOUT_FILENO, af);
a47e5f92 391 a_switcherr();
3cdc3f3a 392 }
97d410cd 393
acd781a8
MW
394 p_init();
395 for (i = 0; tunnels[i]; i++)
396 tunnels[i]->init();
397 p_bind(ailist); freeaddrinfo(ailist);
398
97d410cd
MW
399 for (i = 0; tunnels[i]; i++) {
400 if (tunnels[i]->flags&TUNF_PRIVOPEN) {
401 ps_split(f & f_daemon);
402 break;
403 }
404 }
405
7737eb87
MW
406 a_init();
407 a_signals();
408 a_listen(csock, u, g, csockmode);
52b86648 409 u_setugid(u, g);
410c8acf 410 km_init(kr_priv, kr_pub, tag_priv);
8362ac1c 411 kx_init();
410c8acf 412 if (f & f_daemon) {
5ae728a6
MW
413 if (daemonize()) {
414 a_warn("SERVER", "daemon-error", "?ERRNO", A_END);
415 exit(EXIT_FAILURE);
416 }
410c8acf 417 a_daemon();
a47e5f92 418 a_switcherr();
410c8acf 419 }
420
a7abb429 421 iv_addreason();
410c8acf 422
c9aded9f 423 lp_run();
410c8acf 424 return (0);
425}
426
427/*----- That's all, folks -------------------------------------------------*/