1 /* strongSwan IPsec starter
2 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
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>.
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
14 * RCSID $Id: starter.c,v 1.23 2006/02/15 18:37:46 as Exp $
17 #include <sys/types.h>
32 #include "../pluto/constants.h"
33 #include "../pluto/defs.h"
34 #include "../pluto/log.h"
38 #include "starterwhack.h"
39 #include "starterstroke.h"
40 #include "invokepluto.h"
41 #include "invokecharon.h"
44 #include "interfaces.h"
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
53 static unsigned int _action_
= 0;
66 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
68 if (pid
== starter_pluto_pid())
70 if (pid
== starter_charon_pid())
72 if (WIFSIGNALED(status
))
74 DBG_log("child %d%s has been killed by sig %d\n",
75 pid
, name?name
:"", WTERMSIG(status
))
77 else if (WIFSTOPPED(status
))
79 DBG_log("child %d%s has been stopped by sig %d\n",
80 pid
, name?name
:"", WSTOPSIG(status
))
82 else if (WIFEXITED(status
))
84 DBG_log("child %d%s has quit (exit code %d)\n",
85 pid
, name?name
:"", WEXITSTATUS(status
))
89 DBG_log("child %d%s has quit", pid
, name?name
:"")
91 if (pid
== starter_pluto_pid())
92 starter_pluto_sigchild(pid
);
93 if (pid
== starter_charon_pid())
94 starter_charon_sigchild(pid
);
104 _action_
|= FLAG_ACTION_START_PLUTO
;
105 _action_
|= FLAG_ACTION_START_CHARON
;
109 _action_
|= FLAG_ACTION_UPDATE
;
115 _action_
|= FLAG_ACTION_QUIT
;
119 _action_
|= FLAG_ACTION_RELOAD
;
120 _action_
|= FLAG_ACTION_UPDATE
;
124 plog("fsig(): unknown signal %d -- investigate", signal
);
132 fprintf(stderr
, "Usage: starter [--nofork] [--auto-update <sec>] "
133 "[--debug|--debug-more|--debug-all]\n");
137 int main (int argc
, char **argv
)
139 starter_config_t
*cfg
= NULL
;
140 starter_config_t
*new_cfg
;
141 starter_conn_t
*conn
, *conn2
;
142 starter_ca_t
*ca
, *ca2
;
150 unsigned long auto_update
= 0;
152 bool no_fork
= FALSE
;
154 /* global variables defined in log.h */
155 log_to_stderr
= TRUE
;
156 base_debugging
= DBG_NONE
;
158 /* parse command line */
159 for (i
= 1; i
< argc
; i
++)
161 if (streq(argv
[i
], "--debug"))
163 base_debugging
|= DBG_CONTROL
;
165 else if (streq(argv
[i
], "--debug-more"))
167 base_debugging
|= DBG_CONTROLMORE
;
169 else if (streq(argv
[i
], "--debug-all"))
171 base_debugging
|= DBG_ALL
;
173 else if (streq(argv
[i
], "--nofork"))
177 else if (streq(argv
[i
], "--auto-update") && i
+1 < argc
)
179 auto_update
= atoi(argv
[++i
]);
190 init_log("ipsec_starter");
191 cur_debugging
= base_debugging
;
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
);
202 plog("Starting strongSwan %s IPsec [starter]...", ipsec_version_code());
204 /* verify that we can start */
207 plog("permission denied (must be superuser)");
211 if (stat(PLUTO_PID_FILE
, &stb
) == 0)
213 plog("pluto is already running (%s exists) -- skipping pluto start", PLUTO_PID_FILE
);
217 _action_
|= FLAG_ACTION_START_PLUTO
;
219 if (stat(CHARON_PID_FILE
, &stb
) == 0)
221 plog("charon is already running (%s exists) -- skipping charon start", CHARON_PID_FILE
);
225 _action_
|= FLAG_ACTION_START_CHARON
;
227 if (stat(DEV_RANDOM
, &stb
) != 0)
229 plog("unable to start strongSwan IPsec -- no %s!", DEV_RANDOM
);
233 if (stat(DEV_URANDOM
, &stb
)!= 0)
235 plog("unable to start strongSwan IPsec -- no %s!", DEV_URANDOM
);
239 cfg
= confread_load(CONFIG_FILE
);
240 if (cfg
== NULL
|| cfg
->err
> 0)
242 plog("unable to start strongSwan -- fatal errors in config");
247 /* determine if we have a native netkey IPsec stack */
248 if (!starter_netkey_init())
250 plog("no netkey IPSec stack detected");
254 last_reload
= time(NULL
);
256 if (stat(STARTER_PID_FILE
, &stb
) == 0)
258 plog("starter is already running (%s exists) -- no fork done", STARTER_PID_FILE
);
262 /* fork if we're not debugging stuff */
265 log_to_stderr
= FALSE
;
271 int fnull
= open("/dev/null", O_RDWR
);
275 dup2(fnull
, STDIN_FILENO
);
276 dup2(fnull
, STDOUT_FILENO
);
277 dup2(fnull
, STDERR_FILENO
);
284 plog("can't fork: %s", strerror(errno
));
291 /* save pid file in /var/run/starter.pid */
293 FILE *fd
= fopen(STARTER_PID_FILE
, "w");
297 fprintf(fd
, "%u\n", getpid());
305 * Stop pluto/charon (if started) and exit
307 if (_action_
& FLAG_ACTION_QUIT
)
309 if (starter_pluto_pid())
310 starter_stop_pluto();
311 if (starter_charon_pid())
312 starter_stop_charon();
313 starter_netkey_cleanup();
315 unlink(STARTER_PID_FILE
);
317 #ifdef LEAK_DETECTIVE
319 #endif /* LEAK_DETECTIVE */
321 plog("ipsec starter stopped");
326 * Delete all connections. Will be added below
328 if (_action_
& FLAG_ACTION_RELOAD
)
330 if (starter_pluto_pid() || starter_charon_pid())
332 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
334 if (conn
->state
== STATE_ADDED
)
336 if (starter_charon_pid())
338 starter_stroke_del_conn(conn
);
340 if (starter_pluto_pid())
342 starter_whack_del_conn(conn
);
344 conn
->state
= STATE_TO_ADD
;
347 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
349 if (ca
->state
== STATE_ADDED
)
351 if (starter_charon_pid())
353 starter_stroke_del_ca(ca
);
355 if (starter_pluto_pid())
357 starter_whack_del_ca(ca
);
359 ca
->state
= STATE_TO_ADD
;
363 _action_
&= ~FLAG_ACTION_RELOAD
;
367 * Update configuration
369 if (_action_
& FLAG_ACTION_UPDATE
)
373 DBG_log("Reloading config...")
375 new_cfg
= confread_load(CONFIG_FILE
);
377 if (new_cfg
->err
+ new_cfg
->non_fatal_err
== 0)
379 /* Switch to new config. New conn will be loaded below */
380 if (!starter_cmp_defaultroute(&new_cfg
->defaultroute
381 , &cfg
->defaultroute
))
383 _action_
|= FLAG_ACTION_LISTEN
;
386 if (!starter_cmp_pluto(cfg
, new_cfg
))
388 plog("Pluto has changed");
389 if (starter_pluto_pid())
390 starter_stop_pluto();
391 _action_
&= ~FLAG_ACTION_LISTEN
;
392 _action_
|= FLAG_ACTION_START_PLUTO
;
396 /* Only reload conn and ca sections if pluto is not killed */
398 /* Look for new connections that are already loaded */
399 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
401 if (conn
->state
== STATE_ADDED
)
403 for (conn2
= new_cfg
->conn_first
; conn2
; conn2
= conn2
->next
)
405 if (conn2
->state
== STATE_TO_ADD
&& starter_cmp_conn(conn
, conn2
))
407 conn
->state
= STATE_REPLACED
;
408 conn2
->state
= STATE_ADDED
;
409 conn2
->id
= conn
->id
;
416 /* Remove conn sections that have become unused */
417 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
419 if (conn
->state
== STATE_ADDED
)
421 if (starter_charon_pid())
423 starter_stroke_del_conn(conn
);
425 if (starter_pluto_pid())
427 starter_whack_del_conn(conn
);
432 /* Look for new ca sections that are already loaded */
433 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
435 if (ca
->state
== STATE_ADDED
)
437 for (ca2
= new_cfg
->ca_first
; ca2
; ca2
= ca2
->next
)
439 if (ca2
->state
== STATE_TO_ADD
&& starter_cmp_ca(ca
, ca2
))
441 ca
->state
= STATE_REPLACED
;
442 ca2
->state
= STATE_ADDED
;
449 /* Remove ca sections that have become unused */
450 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
452 if (ca
->state
== STATE_ADDED
)
454 if (starter_charon_pid())
456 starter_stroke_del_ca(ca
);
458 if (starter_pluto_pid())
460 starter_whack_del_ca(ca
);
470 plog("can't reload config file due to errors -- keeping old one");
471 confread_free(new_cfg
);
473 _action_
&= ~FLAG_ACTION_UPDATE
;
474 last_reload
= time(NULL
);
480 if (_action_
& FLAG_ACTION_START_PLUTO
)
482 if (cfg
->setup
.plutostart
&& !starter_pluto_pid())
485 DBG_log("Attempting to start pluto...")
488 if (starter_start_pluto(cfg
, no_fork
) == 0)
490 starter_whack_listen();
494 /* schedule next try */
495 alarm(PLUTO_RESTART_DELAY
);
498 _action_
&= ~FLAG_ACTION_START_PLUTO
;
500 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
502 if (ca
->state
== STATE_ADDED
)
503 ca
->state
= STATE_TO_ADD
;
506 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
508 if (conn
->state
== STATE_ADDED
)
509 conn
->state
= STATE_TO_ADD
;
516 if (_action_
& FLAG_ACTION_START_CHARON
)
518 if (cfg
->setup
.charonstart
&& !starter_charon_pid())
521 DBG_log("Attempting to start charon...")
523 if (starter_start_charon(cfg
, no_fork
))
525 /* schedule next try */
526 alarm(PLUTO_RESTART_DELAY
);
529 _action_
&= ~FLAG_ACTION_START_CHARON
;
533 * Tell pluto to reread its interfaces
535 if (_action_
& FLAG_ACTION_LISTEN
)
537 if (starter_pluto_pid())
539 starter_whack_listen();
540 _action_
&= ~FLAG_ACTION_LISTEN
;
545 * Add stale conn and ca sections
547 if (starter_pluto_pid() || starter_charon_pid())
549 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
551 if (ca
->state
== STATE_TO_ADD
)
553 if (starter_charon_pid())
555 starter_stroke_add_ca(ca
);
557 if (starter_pluto_pid())
559 starter_whack_add_ca(ca
);
561 ca
->state
= STATE_ADDED
;
565 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
567 if (conn
->state
== STATE_TO_ADD
)
571 /* affect new unique id */
574 if (starter_charon_pid())
576 starter_stroke_add_conn(conn
);
578 if (starter_pluto_pid())
580 starter_whack_add_conn(conn
);
582 conn
->state
= STATE_ADDED
;
584 if (conn
->startup
== STARTUP_START
)
586 if (conn
->keyexchange
== KEY_EXCHANGE_IKEV2
)
588 if (starter_charon_pid())
590 starter_stroke_initiate_conn(conn
);
595 if (starter_pluto_pid())
597 starter_whack_initiate_conn(conn
);
601 else if (conn
->startup
== STARTUP_ROUTE
)
603 if (conn
->keyexchange
== KEY_EXCHANGE_IKEV2
)
605 if (starter_charon_pid())
607 starter_stroke_route_conn(conn
);
612 if (starter_pluto_pid())
614 starter_whack_route_conn(conn
);
623 * If auto_update activated, when to stop select
627 time_t now
= time(NULL
);
629 tv
.tv_sec
= (now
< last_reload
+ auto_update
)
630 ?
(last_reload
+ auto_update
-now
) : 0;
635 * Wait for something to happen
637 if (select(0, NULL
, NULL
, NULL
, auto_update ?
&tv
: NULL
) == 0)
639 /* timeout -> auto_update */
640 _action_
|= FLAG_ACTION_UPDATE
;