0bf1d7a71af6a2a57e5c2b2f726ec98295b674ac
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
);
242 plog("unable to start strongSwan -- errors in config");
246 /* determine if we have a native netkey IPsec stack */
247 if (!starter_netkey_init())
249 plog("no netkey IPSec stack detected");
253 last_reload
= time(NULL
);
255 if (stat(STARTER_PID_FILE
, &stb
) == 0)
257 plog("starter is already running (%s exists) -- no fork done", STARTER_PID_FILE
);
261 /* fork if we're not debugging stuff */
264 log_to_stderr
= FALSE
;
270 int fnull
= open("/dev/null", O_RDWR
);
274 dup2(fnull
, STDIN_FILENO
);
275 dup2(fnull
, STDOUT_FILENO
);
276 dup2(fnull
, STDERR_FILENO
);
282 plog("can't fork: %s", strerror(errno
));
289 /* save pid file in /var/run/starter.pid */
291 FILE *fd
= fopen(STARTER_PID_FILE
, "w");
295 fprintf(fd
, "%u\n", getpid());
303 * Stop pluto/charon (if started) and exit
305 if (_action_
& FLAG_ACTION_QUIT
)
307 if (starter_pluto_pid())
308 starter_stop_pluto();
309 if (starter_charon_pid())
310 starter_stop_charon();
311 starter_netkey_cleanup();
313 unlink(STARTER_PID_FILE
);
315 #ifdef LEAK_DETECTIVE
317 #endif /* LEAK_DETECTIVE */
319 plog("ipsec starter stopped");
324 * Delete all connections. Will be added below
326 if (_action_
& FLAG_ACTION_RELOAD
)
328 if (starter_pluto_pid() || starter_charon_pid())
330 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
332 if (conn
->state
== STATE_ADDED
)
334 if (starter_charon_pid())
336 starter_stroke_del_conn(conn
);
338 if (starter_pluto_pid())
340 starter_whack_del_conn(conn
);
342 conn
->state
= STATE_TO_ADD
;
345 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
347 if (ca
->state
== STATE_ADDED
)
349 if (starter_charon_pid())
351 starter_stroke_del_ca(ca
);
353 if (starter_pluto_pid())
355 starter_whack_del_ca(ca
);
357 ca
->state
= STATE_TO_ADD
;
361 _action_
&= ~FLAG_ACTION_RELOAD
;
365 * Update configuration
367 if (_action_
& FLAG_ACTION_UPDATE
)
371 DBG_log("Reloading config...")
373 new_cfg
= confread_load(CONFIG_FILE
);
377 /* Switch to new config. New conn will be loaded below */
378 if (!starter_cmp_defaultroute(&new_cfg
->defaultroute
379 , &cfg
->defaultroute
))
381 _action_
|= FLAG_ACTION_LISTEN
;
384 if (!starter_cmp_pluto(cfg
, new_cfg
))
386 plog("Pluto has changed");
387 if (starter_pluto_pid())
388 starter_stop_pluto();
389 _action_
&= ~FLAG_ACTION_LISTEN
;
390 _action_
|= FLAG_ACTION_START_PLUTO
;
394 /* Only reload conn and ca sections if pluto is not killed */
396 /* Look for new connections that are already loaded */
397 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
399 if (conn
->state
== STATE_ADDED
)
401 for (conn2
= new_cfg
->conn_first
; conn2
; conn2
= conn2
->next
)
403 if (conn2
->state
== STATE_TO_ADD
&& starter_cmp_conn(conn
, conn2
))
405 conn
->state
= STATE_REPLACED
;
406 conn2
->state
= STATE_ADDED
;
407 conn2
->id
= conn
->id
;
414 /* Remove conn sections that have become unused */
415 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
417 if (conn
->state
== STATE_ADDED
)
419 if (starter_charon_pid())
421 starter_stroke_del_conn(conn
);
423 if (starter_pluto_pid())
425 starter_whack_del_conn(conn
);
430 /* Look for new ca sections that are already loaded */
431 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
433 if (ca
->state
== STATE_ADDED
)
435 for (ca2
= new_cfg
->ca_first
; ca2
; ca2
= ca2
->next
)
437 if (ca2
->state
== STATE_TO_ADD
&& starter_cmp_ca(ca
, ca2
))
439 ca
->state
= STATE_REPLACED
;
440 ca2
->state
= STATE_ADDED
;
447 /* Remove ca sections that have become unused */
448 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
450 if (ca
->state
== STATE_ADDED
)
452 if (starter_charon_pid())
454 starter_stroke_del_ca(ca
);
456 if (starter_pluto_pid())
458 starter_whack_del_ca(ca
);
468 plog("can't reload config file: %s -- keeping old one");
470 _action_
&= ~FLAG_ACTION_UPDATE
;
471 last_reload
= time(NULL
);
477 if (_action_
& FLAG_ACTION_START_PLUTO
)
479 if (cfg
->setup
.plutostart
&& !starter_pluto_pid())
482 DBG_log("Attempting to start pluto...")
485 if (starter_start_pluto(cfg
, no_fork
) == 0)
487 starter_whack_listen();
491 /* schedule next try */
492 alarm(PLUTO_RESTART_DELAY
);
495 _action_
&= ~FLAG_ACTION_START_PLUTO
;
497 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
499 if (ca
->state
== STATE_ADDED
)
500 ca
->state
= STATE_TO_ADD
;
503 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
505 if (conn
->state
== STATE_ADDED
)
506 conn
->state
= STATE_TO_ADD
;
513 if (_action_
& FLAG_ACTION_START_CHARON
)
515 if (cfg
->setup
.charonstart
&& !starter_charon_pid())
518 DBG_log("Attempting to start charon...")
520 if (starter_start_charon(cfg
, no_fork
))
522 /* schedule next try */
523 alarm(PLUTO_RESTART_DELAY
);
526 _action_
&= ~FLAG_ACTION_START_CHARON
;
530 * Tell pluto to reread its interfaces
532 if (_action_
& FLAG_ACTION_LISTEN
)
534 if (starter_pluto_pid())
536 starter_whack_listen();
537 _action_
&= ~FLAG_ACTION_LISTEN
;
542 * Add stale conn and ca sections
544 if (starter_pluto_pid() || starter_charon_pid())
546 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
548 if (ca
->state
== STATE_TO_ADD
)
550 if (starter_charon_pid())
552 starter_stroke_add_ca(ca
);
554 if (starter_pluto_pid())
556 starter_whack_add_ca(ca
);
558 ca
->state
= STATE_ADDED
;
562 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
564 if (conn
->state
== STATE_TO_ADD
)
568 /* affect new unique id */
571 if (starter_charon_pid())
573 starter_stroke_add_conn(conn
);
575 if (starter_pluto_pid())
577 starter_whack_add_conn(conn
);
579 conn
->state
= STATE_ADDED
;
581 if (conn
->startup
== STARTUP_START
)
583 if (conn
->keyexchange
== KEY_EXCHANGE_IKEV2
)
585 if (starter_charon_pid())
587 starter_stroke_initiate_conn(conn
);
592 if (starter_pluto_pid())
594 starter_whack_initiate_conn(conn
);
598 else if (conn
->startup
== STARTUP_ROUTE
)
600 if (conn
->keyexchange
== KEY_EXCHANGE_IKEV2
)
602 if (starter_charon_pid())
604 starter_stroke_route_conn(conn
);
609 if (starter_pluto_pid())
611 starter_whack_route_conn(conn
);
620 * If auto_update activated, when to stop select
624 time_t now
= time(NULL
);
626 tv
.tv_sec
= (now
< last_reload
+ auto_update
)
627 ?
(last_reload
+ auto_update
-now
) : 0;
632 * Wait for something to happen
634 if (select(0, NULL
, NULL
, NULL
, auto_update ?
&tv
: NULL
) == 0)
636 /* timeout -> auto_update */
637 _action_
|= FLAG_ACTION_UPDATE
;