added setsid() to properly detach from console
[strongswan.git] / src / starter / starter.c
1 /* strongSwan IPsec starter
2 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * RCSID $Id: starter.c,v 1.23 2006/02/15 18:37:46 as Exp $
15 */
16
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 #include <sys/stat.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <sys/time.h>
25 #include <time.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <fcntl.h>
29
30 #include <freeswan.h>
31
32 #include "../pluto/constants.h"
33 #include "../pluto/defs.h"
34 #include "../pluto/log.h"
35
36 #include "confread.h"
37 #include "files.h"
38 #include "starterwhack.h"
39 #include "starterstroke.h"
40 #include "invokepluto.h"
41 #include "invokecharon.h"
42 #include "netkey.h"
43 #include "cmp.h"
44 #include "interfaces.h"
45
46 #define FLAG_ACTION_START_PLUTO 0x01
47 #define FLAG_ACTION_UPDATE 0x02
48 #define FLAG_ACTION_RELOAD 0x04
49 #define FLAG_ACTION_QUIT 0x08
50 #define FLAG_ACTION_LISTEN 0x10
51 #define FLAG_ACTION_START_CHARON 0x20
52
53 static unsigned int _action_ = 0;
54
55 static void
56 fsig(int signal)
57 {
58 switch (signal)
59 {
60 case SIGCHLD:
61 {
62 int status;
63 pid_t pid;
64 char *name = NULL;
65
66 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
67 {
68 if (pid == starter_pluto_pid())
69 name = " (Pluto)";
70 if (pid == starter_charon_pid())
71 name = " (Charon)";
72 if (WIFSIGNALED(status))
73 DBG(DBG_CONTROL,
74 DBG_log("child %d%s has been killed by sig %d\n",
75 pid, name?name:"", WTERMSIG(status))
76 )
77 else if (WIFSTOPPED(status))
78 DBG(DBG_CONTROL,
79 DBG_log("child %d%s has been stopped by sig %d\n",
80 pid, name?name:"", WSTOPSIG(status))
81 )
82 else if (WIFEXITED(status))
83 DBG(DBG_CONTROL,
84 DBG_log("child %d%s has quit (exit code %d)\n",
85 pid, name?name:"", WEXITSTATUS(status))
86 )
87 else
88 DBG(DBG_CONTROL,
89 DBG_log("child %d%s has quit", pid, name?name:"")
90 )
91 if (pid == starter_pluto_pid())
92 starter_pluto_sigchild(pid);
93 if (pid == starter_charon_pid())
94 starter_charon_sigchild(pid);
95 }
96 }
97 break;
98
99 case SIGPIPE:
100 /** ignore **/
101 break;
102
103 case SIGALRM:
104 _action_ |= FLAG_ACTION_START_PLUTO;
105 _action_ |= FLAG_ACTION_START_CHARON;
106 break;
107
108 case SIGHUP:
109 _action_ |= FLAG_ACTION_UPDATE;
110 break;
111
112 case SIGTERM:
113 case SIGQUIT:
114 case SIGINT:
115 _action_ |= FLAG_ACTION_QUIT;
116 break;
117
118 case SIGUSR1:
119 _action_ |= FLAG_ACTION_RELOAD;
120 _action_ |= FLAG_ACTION_UPDATE;
121 break;
122
123 default:
124 plog("fsig(): unknown signal %d -- investigate", signal);
125 break;
126 }
127 }
128
129 static void
130 usage(char *name)
131 {
132 fprintf(stderr, "Usage: starter [--nofork] [--auto-update <sec>] "
133 "[--debug|--debug-more|--debug-all]\n");
134 exit(1);
135 }
136
137 int main (int argc, char **argv)
138 {
139 starter_config_t *cfg = NULL;
140 starter_config_t *new_cfg;
141 starter_conn_t *conn, *conn2;
142 starter_ca_t *ca, *ca2;
143
144 struct stat stb;
145
146 char *err = NULL;
147 int i;
148 int id = 1;
149 struct timeval tv;
150 unsigned long auto_update = 0;
151 time_t last_reload;
152 bool no_fork = FALSE;
153
154 /* global variables defined in log.h */
155 log_to_stderr = TRUE;
156 base_debugging = DBG_NONE;
157
158 /* parse command line */
159 for (i = 1; i < argc; i++)
160 {
161 if (streq(argv[i], "--debug"))
162 {
163 base_debugging |= DBG_CONTROL;
164 }
165 else if (streq(argv[i], "--debug-more"))
166 {
167 base_debugging |= DBG_CONTROLMORE;
168 }
169 else if (streq(argv[i], "--debug-all"))
170 {
171 base_debugging |= DBG_ALL;
172 }
173 else if (streq(argv[i], "--nofork"))
174 {
175 no_fork = TRUE;
176 }
177 else if (streq(argv[i], "--auto-update") && i+1 < argc)
178 {
179 auto_update = atoi(argv[++i]);
180 if (!auto_update)
181 usage(argv[0]);
182 }
183 else
184 {
185 usage(argv[0]);
186 }
187 }
188
189 /* Init */
190 init_log("ipsec_starter");
191 cur_debugging = base_debugging;
192
193 signal(SIGHUP, fsig);
194 signal(SIGCHLD, fsig);
195 signal(SIGPIPE, fsig);
196 signal(SIGINT, fsig);
197 signal(SIGTERM, fsig);
198 signal(SIGQUIT, fsig);
199 signal(SIGALRM, fsig);
200 signal(SIGUSR1, fsig);
201
202 plog("Starting strongSwan %s IPsec [starter]...", ipsec_version_code());
203
204 /* verify that we can start */
205 if (getuid() != 0)
206 {
207 plog("permission denied (must be superuser)");
208 exit(1);
209 }
210
211 if (stat(PLUTO_PID_FILE, &stb) == 0)
212 {
213 plog("pluto is already running (%s exists) -- skipping pluto start", PLUTO_PID_FILE);
214 }
215 else
216 {
217 _action_ |= FLAG_ACTION_START_PLUTO;
218 }
219 if (stat(CHARON_PID_FILE, &stb) == 0)
220 {
221 plog("charon is already running (%s exists) -- skipping charon start", CHARON_PID_FILE);
222 }
223 else
224 {
225 _action_ |= FLAG_ACTION_START_CHARON;
226 }
227 if (stat(DEV_RANDOM, &stb) != 0)
228 {
229 plog("unable to start strongSwan IPsec -- no %s!", DEV_RANDOM);
230 exit(1);
231 }
232
233 if (stat(DEV_URANDOM, &stb)!= 0)
234 {
235 plog("unable to start strongSwan IPsec -- no %s!", DEV_URANDOM);
236 exit(1);
237 }
238
239 cfg = confread_load(CONFIG_FILE);
240 if (!cfg)
241 {
242 plog("unable to start strongSwan -- errors in config");
243 exit(1);
244 }
245
246 /* determine if we have a native netkey IPsec stack */
247 if (!starter_netkey_init())
248 {
249 plog("no netkey IPSec stack detected");
250 exit(1);
251 }
252
253 last_reload = time(NULL);
254
255 if (stat(STARTER_PID_FILE, &stb) == 0)
256 {
257 plog("starter is already running (%s exists) -- no fork done", STARTER_PID_FILE);
258 exit(0);
259 }
260
261 /* fork if we're not debugging stuff */
262 if (!no_fork)
263 {
264 log_to_stderr = FALSE;
265
266 switch (fork())
267 {
268 case 0:
269 {
270 int fnull = open("/dev/null", O_RDWR);
271
272 if (fnull >= 0)
273 {
274 dup2(fnull, STDIN_FILENO);
275 dup2(fnull, STDOUT_FILENO);
276 dup2(fnull, STDERR_FILENO);
277 close(fnull);
278 }
279 setsid();
280 }
281 break;
282 case -1:
283 plog("can't fork: %s", strerror(errno));
284 break;
285 default:
286 exit(0);
287 }
288 }
289
290 /* save pid file in /var/run/starter.pid */
291 {
292 FILE *fd = fopen(STARTER_PID_FILE, "w");
293
294 if (fd)
295 {
296 fprintf(fd, "%u\n", getpid());
297 fclose(fd);
298 }
299 }
300
301 for (;;)
302 {
303 /*
304 * Stop pluto/charon (if started) and exit
305 */
306 if (_action_ & FLAG_ACTION_QUIT)
307 {
308 if (starter_pluto_pid())
309 starter_stop_pluto();
310 if (starter_charon_pid())
311 starter_stop_charon();
312 starter_netkey_cleanup();
313 confread_free(cfg);
314 unlink(STARTER_PID_FILE);
315 unlink(INFO_FILE);
316 #ifdef LEAK_DETECTIVE
317 report_leaks();
318 #endif /* LEAK_DETECTIVE */
319 close_log();
320 plog("ipsec starter stopped");
321 exit(0);
322 }
323
324 /*
325 * Delete all connections. Will be added below
326 */
327 if (_action_ & FLAG_ACTION_RELOAD)
328 {
329 if (starter_pluto_pid() || starter_charon_pid())
330 {
331 for (conn = cfg->conn_first; conn; conn = conn->next)
332 {
333 if (conn->state == STATE_ADDED)
334 {
335 if (starter_charon_pid())
336 {
337 starter_stroke_del_conn(conn);
338 }
339 if (starter_pluto_pid())
340 {
341 starter_whack_del_conn(conn);
342 }
343 conn->state = STATE_TO_ADD;
344 }
345 }
346 for (ca = cfg->ca_first; ca; ca = ca->next)
347 {
348 if (ca->state == STATE_ADDED)
349 {
350 if (starter_charon_pid())
351 {
352 starter_stroke_del_ca(ca);
353 }
354 if (starter_pluto_pid())
355 {
356 starter_whack_del_ca(ca);
357 }
358 ca->state = STATE_TO_ADD;
359 }
360 }
361 }
362 _action_ &= ~FLAG_ACTION_RELOAD;
363 }
364
365 /*
366 * Update configuration
367 */
368 if (_action_ & FLAG_ACTION_UPDATE)
369 {
370 err = NULL;
371 DBG(DBG_CONTROL,
372 DBG_log("Reloading config...")
373 );
374 new_cfg = confread_load(CONFIG_FILE);
375
376 if (new_cfg)
377 {
378 /* Switch to new config. New conn will be loaded below */
379 if (!starter_cmp_defaultroute(&new_cfg->defaultroute
380 , &cfg->defaultroute))
381 {
382 _action_ |= FLAG_ACTION_LISTEN;
383 }
384
385 if (!starter_cmp_pluto(cfg, new_cfg))
386 {
387 plog("Pluto has changed");
388 if (starter_pluto_pid())
389 starter_stop_pluto();
390 _action_ &= ~FLAG_ACTION_LISTEN;
391 _action_ |= FLAG_ACTION_START_PLUTO;
392 }
393 else
394 {
395 /* Only reload conn and ca sections if pluto is not killed */
396
397 /* Look for new connections that are already loaded */
398 for (conn = cfg->conn_first; conn; conn = conn->next)
399 {
400 if (conn->state == STATE_ADDED)
401 {
402 for (conn2 = new_cfg->conn_first; conn2; conn2 = conn2->next)
403 {
404 if (conn2->state == STATE_TO_ADD && starter_cmp_conn(conn, conn2))
405 {
406 conn->state = STATE_REPLACED;
407 conn2->state = STATE_ADDED;
408 conn2->id = conn->id;
409 break;
410 }
411 }
412 }
413 }
414
415 /* Remove conn sections that have become unused */
416 for (conn = cfg->conn_first; conn; conn = conn->next)
417 {
418 if (conn->state == STATE_ADDED)
419 {
420 if (starter_charon_pid())
421 {
422 starter_stroke_del_conn(conn);
423 }
424 if (starter_pluto_pid())
425 {
426 starter_whack_del_conn(conn);
427 }
428 }
429 }
430
431 /* Look for new ca sections that are already loaded */
432 for (ca = cfg->ca_first; ca; ca = ca->next)
433 {
434 if (ca->state == STATE_ADDED)
435 {
436 for (ca2 = new_cfg->ca_first; ca2; ca2 = ca2->next)
437 {
438 if (ca2->state == STATE_TO_ADD && starter_cmp_ca(ca, ca2))
439 {
440 ca->state = STATE_REPLACED;
441 ca2->state = STATE_ADDED;
442 break;
443 }
444 }
445 }
446 }
447
448 /* Remove ca sections that have become unused */
449 for (ca = cfg->ca_first; ca; ca = ca->next)
450 {
451 if (ca->state == STATE_ADDED)
452 {
453 if (starter_charon_pid())
454 {
455 starter_stroke_del_ca(ca);
456 }
457 if (starter_pluto_pid())
458 {
459 starter_whack_del_ca(ca);
460 }
461 }
462 }
463 }
464 confread_free(cfg);
465 cfg = new_cfg;
466 }
467 else
468 {
469 plog("can't reload config file: %s -- keeping old one");
470 }
471 _action_ &= ~FLAG_ACTION_UPDATE;
472 last_reload = time(NULL);
473 }
474
475 /*
476 * Start pluto
477 */
478 if (_action_ & FLAG_ACTION_START_PLUTO)
479 {
480 if (cfg->setup.plutostart && !starter_pluto_pid())
481 {
482 DBG(DBG_CONTROL,
483 DBG_log("Attempting to start pluto...")
484 );
485
486 if (starter_start_pluto(cfg, no_fork) == 0)
487 {
488 starter_whack_listen();
489 }
490 else
491 {
492 /* schedule next try */
493 alarm(PLUTO_RESTART_DELAY);
494 }
495 }
496 _action_ &= ~FLAG_ACTION_START_PLUTO;
497
498 for (ca = cfg->ca_first; ca; ca = ca->next)
499 {
500 if (ca->state == STATE_ADDED)
501 ca->state = STATE_TO_ADD;
502 }
503
504 for (conn = cfg->conn_first; conn; conn = conn->next)
505 {
506 if (conn->state == STATE_ADDED)
507 conn->state = STATE_TO_ADD;
508 }
509 }
510
511 /*
512 * Start charon
513 */
514 if (_action_ & FLAG_ACTION_START_CHARON)
515 {
516 if (cfg->setup.charonstart && !starter_charon_pid())
517 {
518 DBG(DBG_CONTROL,
519 DBG_log("Attempting to start charon...")
520 );
521 if (starter_start_charon(cfg, no_fork))
522 {
523 /* schedule next try */
524 alarm(PLUTO_RESTART_DELAY);
525 }
526 }
527 _action_ &= ~FLAG_ACTION_START_CHARON;
528 }
529
530 /*
531 * Tell pluto to reread its interfaces
532 */
533 if (_action_ & FLAG_ACTION_LISTEN)
534 {
535 if (starter_pluto_pid())
536 {
537 starter_whack_listen();
538 _action_ &= ~FLAG_ACTION_LISTEN;
539 }
540 }
541
542 /*
543 * Add stale conn and ca sections
544 */
545 if (starter_pluto_pid() || starter_charon_pid())
546 {
547 for (ca = cfg->ca_first; ca; ca = ca->next)
548 {
549 if (ca->state == STATE_TO_ADD)
550 {
551 if (starter_charon_pid())
552 {
553 starter_stroke_add_ca(ca);
554 }
555 if (starter_pluto_pid())
556 {
557 starter_whack_add_ca(ca);
558 }
559 ca->state = STATE_ADDED;
560 }
561 }
562
563 for (conn = cfg->conn_first; conn; conn = conn->next)
564 {
565 if (conn->state == STATE_TO_ADD)
566 {
567 if (conn->id == 0)
568 {
569 /* affect new unique id */
570 conn->id = id++;
571 }
572 if (starter_charon_pid())
573 {
574 starter_stroke_add_conn(conn);
575 }
576 if (starter_pluto_pid())
577 {
578 starter_whack_add_conn(conn);
579 }
580 conn->state = STATE_ADDED;
581
582 if (conn->startup == STARTUP_START)
583 {
584 if (conn->keyexchange == KEY_EXCHANGE_IKEV2)
585 {
586 if (starter_charon_pid())
587 {
588 starter_stroke_initiate_conn(conn);
589 }
590 }
591 else
592 {
593 if (starter_pluto_pid())
594 {
595 starter_whack_initiate_conn(conn);
596 }
597 }
598 }
599 else if (conn->startup == STARTUP_ROUTE)
600 {
601 if (conn->keyexchange == KEY_EXCHANGE_IKEV2)
602 {
603 if (starter_charon_pid())
604 {
605 starter_stroke_route_conn(conn);
606 }
607 }
608 else
609 {
610 if (starter_pluto_pid())
611 {
612 starter_whack_route_conn(conn);
613 }
614 }
615 }
616 }
617 }
618 }
619
620 /*
621 * If auto_update activated, when to stop select
622 */
623 if (auto_update)
624 {
625 time_t now = time(NULL);
626
627 tv.tv_sec = (now < last_reload + auto_update)
628 ? (last_reload + auto_update-now) : 0;
629 tv.tv_usec = 0;
630 }
631
632 /*
633 * Wait for something to happen
634 */
635 if (select(0, NULL, NULL, NULL, auto_update ? &tv : NULL) == 0)
636 {
637 /* timeout -> auto_update */
638 _action_ |= FLAG_ACTION_UPDATE;
639 }
640 }
641
642 return 0;
643 }
644