25ee9c7851eec1c7897209d3ec6240236c5258c7
[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 }
280 break;
281 case -1:
282 plog("can't fork: %s", strerror(errno));
283 break;
284 default:
285 exit(0);
286 }
287 }
288
289 /* save pid file in /var/run/starter.pid */
290 {
291 FILE *fd = fopen(STARTER_PID_FILE, "w");
292
293 if (fd)
294 {
295 fprintf(fd, "%u\n", getpid());
296 fclose(fd);
297 }
298 }
299
300 for (;;)
301 {
302 /*
303 * Stop pluto/charon (if started) and exit
304 */
305 if (_action_ & FLAG_ACTION_QUIT)
306 {
307 if (starter_pluto_pid())
308 starter_stop_pluto();
309 if (starter_charon_pid())
310 starter_stop_charon();
311 starter_netkey_cleanup();
312 confread_free(cfg);
313 unlink(STARTER_PID_FILE);
314 unlink(INFO_FILE);
315 #ifdef LEAK_DETECTIVE
316 report_leaks();
317 #endif /* LEAK_DETECTIVE */
318 close_log();
319 plog("ipsec starter stopped");
320 exit(0);
321 }
322
323 /*
324 * Delete all connections. Will be added below
325 */
326 if (_action_ & FLAG_ACTION_RELOAD)
327 {
328 if (starter_pluto_pid() || starter_charon_pid())
329 {
330 for (conn = cfg->conn_first; conn; conn = conn->next)
331 {
332 if (conn->state == STATE_ADDED)
333 {
334 if (starter_charon_pid())
335 {
336 starter_stroke_del_conn(conn);
337 }
338 if (starter_pluto_pid())
339 {
340 starter_whack_del_conn(conn);
341 }
342 conn->state = STATE_TO_ADD;
343 }
344 }
345 for (ca = cfg->ca_first; ca; ca = ca->next)
346 {
347 if (ca->state == STATE_ADDED)
348 {
349 if (starter_pluto_pid())
350 {
351 starter_whack_del_ca(ca);
352 ca->state = STATE_TO_ADD;
353 }
354 }
355 }
356 }
357 _action_ &= ~FLAG_ACTION_RELOAD;
358 }
359
360 /*
361 * Update configuration
362 */
363 if (_action_ & FLAG_ACTION_UPDATE)
364 {
365 err = NULL;
366 DBG(DBG_CONTROL,
367 DBG_log("Reloading config...")
368 );
369 new_cfg = confread_load(CONFIG_FILE);
370
371 if (new_cfg)
372 {
373 /* Switch to new config. New conn will be loaded below */
374 if (!starter_cmp_defaultroute(&new_cfg->defaultroute
375 , &cfg->defaultroute))
376 {
377 _action_ |= FLAG_ACTION_LISTEN;
378 }
379
380 if (!starter_cmp_pluto(cfg, new_cfg))
381 {
382 plog("Pluto has changed");
383 if (starter_pluto_pid())
384 starter_stop_pluto();
385 _action_ &= ~FLAG_ACTION_LISTEN;
386 _action_ |= FLAG_ACTION_START_PLUTO;
387 }
388 else
389 {
390 /* Only reload conn and ca sections if pluto is not killed */
391
392 /* Look for new connections that are already loaded */
393 for (conn = cfg->conn_first; conn; conn = conn->next)
394 {
395 if (conn->state == STATE_ADDED)
396 {
397 for (conn2 = new_cfg->conn_first; conn2; conn2 = conn2->next)
398 {
399 if (conn2->state == STATE_TO_ADD && starter_cmp_conn(conn, conn2))
400 {
401 conn->state = STATE_REPLACED;
402 conn2->state = STATE_ADDED;
403 conn2->id = conn->id;
404 break;
405 }
406 }
407 }
408 }
409
410 /* Remove conn sections that have become unused */
411 for (conn = cfg->conn_first; conn; conn = conn->next)
412 {
413 if (conn->state == STATE_ADDED)
414 {
415 if (starter_charon_pid())
416 {
417 starter_stroke_del_conn(conn);
418 }
419 if (starter_pluto_pid())
420 {
421 starter_whack_del_conn(conn);
422 }
423 }
424 }
425
426 /* Look for new ca sections that are already loaded */
427 for (ca = cfg->ca_first; ca; ca = ca->next)
428 {
429 if (ca->state == STATE_ADDED)
430 {
431 for (ca2 = new_cfg->ca_first; ca2; ca2 = ca2->next)
432 {
433 if (ca2->state == STATE_TO_ADD && starter_cmp_ca(ca, ca2))
434 {
435 ca->state = STATE_REPLACED;
436 ca2->state = STATE_ADDED;
437 break;
438 }
439 }
440 }
441 }
442
443 /* Remove ca sections that have become unused */
444 for (ca = cfg->ca_first; ca; ca = ca->next)
445 {
446 if (ca->state == STATE_ADDED)
447 {
448 if (starter_pluto_pid())
449 {
450 starter_whack_del_ca(ca);
451 }
452 }
453 }
454 }
455 confread_free(cfg);
456 cfg = new_cfg;
457 }
458 else
459 {
460 plog("can't reload config file: %s -- keeping old one");
461 }
462 _action_ &= ~FLAG_ACTION_UPDATE;
463 last_reload = time(NULL);
464 }
465
466 /*
467 * Start pluto
468 */
469 if (_action_ & FLAG_ACTION_START_PLUTO)
470 {
471 if (cfg->setup.plutostart && !starter_pluto_pid())
472 {
473 DBG(DBG_CONTROL,
474 DBG_log("Attempting to start pluto...")
475 );
476
477 if (starter_start_pluto(cfg, no_fork) == 0)
478 {
479 starter_whack_listen();
480 }
481 else
482 {
483 /* schedule next try */
484 alarm(PLUTO_RESTART_DELAY);
485 }
486 }
487 _action_ &= ~FLAG_ACTION_START_PLUTO;
488
489 for (ca = cfg->ca_first; ca; ca = ca->next)
490 {
491 if (ca->state == STATE_ADDED)
492 ca->state = STATE_TO_ADD;
493 }
494
495 for (conn = cfg->conn_first; conn; conn = conn->next)
496 {
497 if (conn->state == STATE_ADDED)
498 conn->state = STATE_TO_ADD;
499 }
500 }
501
502 /*
503 * Start charon
504 */
505 if (_action_ & FLAG_ACTION_START_CHARON)
506 {
507 if (cfg->setup.charonstart && !starter_charon_pid())
508 {
509 DBG(DBG_CONTROL,
510 DBG_log("Attempting to start charon...")
511 );
512 if (starter_start_charon(cfg, no_fork))
513 {
514 /* schedule next try */
515 alarm(PLUTO_RESTART_DELAY);
516 }
517 }
518 _action_ &= ~FLAG_ACTION_START_CHARON;
519 }
520
521 /*
522 * Tell pluto to reread its interfaces
523 */
524 if (_action_ & FLAG_ACTION_LISTEN)
525 {
526 if (starter_pluto_pid())
527 {
528 starter_whack_listen();
529 _action_ &= ~FLAG_ACTION_LISTEN;
530 }
531 }
532
533 /*
534 * Add stale conn and ca sections
535 */
536 if (starter_pluto_pid() || starter_charon_pid())
537 {
538 for (ca = cfg->ca_first; ca; ca = ca->next)
539 {
540 if (ca->state == STATE_TO_ADD)
541 {
542 if (starter_pluto_pid())
543 {
544 starter_whack_add_ca(ca);
545 ca->state = STATE_ADDED;
546 }
547 }
548 }
549
550 for (conn = cfg->conn_first; conn; conn = conn->next)
551 {
552 if (conn->state == STATE_TO_ADD)
553 {
554 if (conn->id == 0)
555 {
556 /* affect new unique id */
557 conn->id = id++;
558 }
559 if (starter_charon_pid())
560 {
561 starter_stroke_add_conn(conn);
562 }
563 if (starter_pluto_pid())
564 {
565 starter_whack_add_conn(conn);
566 }
567 conn->state = STATE_ADDED;
568
569 if (conn->startup == STARTUP_START)
570 {
571 if (conn->keyexchange == KEY_EXCHANGE_IKEV2)
572 {
573 if (starter_charon_pid())
574 {
575 starter_stroke_initiate_conn(conn);
576 }
577 }
578 else
579 {
580 if (starter_pluto_pid())
581 {
582 starter_whack_initiate_conn(conn);
583 }
584 }
585 }
586 else if (conn->startup == STARTUP_ROUTE)
587 {
588 if (conn->keyexchange == KEY_EXCHANGE_IKEV2)
589 {
590 if (starter_charon_pid())
591 {
592 starter_stroke_route_conn(conn);
593 }
594 }
595 else
596 {
597 if (starter_pluto_pid())
598 {
599 starter_whack_route_conn(conn);
600 }
601 }
602 }
603 }
604 }
605 }
606
607 /*
608 * If auto_update activated, when to stop select
609 */
610 if (auto_update)
611 {
612 time_t now = time(NULL);
613
614 tv.tv_sec = (now < last_reload + auto_update)
615 ? (last_reload + auto_update-now) : 0;
616 tv.tv_usec = 0;
617 }
618
619 /*
620 * Wait for something to happen
621 */
622 if (select(0, NULL, NULL, NULL, auto_update ? &tv : NULL) == 0)
623 {
624 /* timeout -> auto_update */
625 _action_ |= FLAG_ACTION_UPDATE;
626 }
627 }
628
629 return 0;
630 }
631