f62d55c7a9e5073eead77d3a4289967815091e20
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
15 #include <sys/select.h>
16 #include <sys/types.h>
35 #include <utils/backtrace.h>
36 #include <threading/thread.h>
38 #include "../pluto/constants.h"
39 #include "../pluto/defs.h"
40 #include "../pluto/log.h"
44 #include "starterwhack.h"
45 #include "starterstroke.h"
46 #include "invokepluto.h"
47 #include "invokecharon.h"
51 #include "interfaces.h"
54 * Return codes defined by Linux Standard Base Core Specification 3.1
55 * in section 20.2. Init Script Actions
57 #define LSB_RC_SUCCESS 0 /* success */
58 #define LSB_RC_FAILURE 1 /* generic or unspecified error */
59 #define LSB_RC_INVALID_ARGUMENT 2 /* invalid or excess argument(s) */
60 #define LSB_RC_NOT_IMPLEMENTED 3 /* unimplemented feature (reload) */
61 #define LSB_RC_NOT_ALLOWED 4 /* user had insufficient privilege */
62 #define LSB_RC_NOT_INSTALLED 5 /* program is not installed */
63 #define LSB_RC_NOT_CONFIGURED 6 /* program is not configured */
64 #define LSB_RC_NOT_RUNNING 7 /* program is not running */
66 #define FLAG_ACTION_START_PLUTO 0x01
67 #define FLAG_ACTION_UPDATE 0x02
68 #define FLAG_ACTION_RELOAD 0x04
69 #define FLAG_ACTION_QUIT 0x08
70 #define FLAG_ACTION_LISTEN 0x10
71 #define FLAG_ACTION_START_CHARON 0x20
73 static unsigned int _action_
= 0;
76 * Handle signals in the main thread
78 static void signal_handler(int signal
)
84 int status
, exit_status
= 0;
88 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
90 if (pid
== starter_pluto_pid())
94 if (pid
== starter_charon_pid())
98 if (WIFSIGNALED(status
))
101 DBG_log("child %d%s has been killed by sig %d\n",
102 pid
, name?name
:"", WTERMSIG(status
))
105 else if (WIFSTOPPED(status
))
108 DBG_log("child %d%s has been stopped by sig %d\n",
109 pid
, name?name
:"", WSTOPSIG(status
))
112 else if (WIFEXITED(status
))
114 exit_status
= WEXITSTATUS(status
);
115 if (exit_status
>= SS_RC_FIRST
&& exit_status
<= SS_RC_LAST
)
117 _action_
= FLAG_ACTION_QUIT
;
120 DBG_log("child %d%s has quit (exit code %d)\n",
121 pid
, name?name
:"", exit_status
)
127 DBG_log("child %d%s has quit", pid
, name?name
:"")
130 if (pid
== starter_pluto_pid())
132 starter_pluto_sigchild(pid
, exit_status
);
134 if (pid
== starter_charon_pid())
136 starter_charon_sigchild(pid
, exit_status
);
143 _action_
|= FLAG_ACTION_START_PLUTO
;
144 _action_
|= FLAG_ACTION_START_CHARON
;
148 _action_
|= FLAG_ACTION_UPDATE
;
154 _action_
|= FLAG_ACTION_QUIT
;
158 _action_
|= FLAG_ACTION_RELOAD
;
159 _action_
|= FLAG_ACTION_UPDATE
;
163 plog("fsig(): unknown signal %d -- investigate", signal
);
169 * Handle fatal signals raised by threads
171 static void fatal_signal_handler(int signal
)
173 backtrace_t
*backtrace
;
175 plog("thread %u received %d", thread_current_id(), signal
);
176 backtrace
= backtrace_create(2);
177 backtrace
->log(backtrace
, stderr
, TRUE
);
178 backtrace
->destroy(backtrace
);
180 plog("killing ourself, received critical signal");
184 #ifdef GENERATE_SELFCERT
185 static void generate_selfcert()
189 /* if ipsec.secrets file is missing then generate RSA default key pair */
190 if (stat(SECRETS_FILE
, &stb
) != 0)
200 struct group group
, *grp
;
202 if (getgrnam_r(IPSEC_GROUP
, &group
, buf
, sizeof(buf
), &grp
) == 0 && grp
)
211 struct passwd passwd
, *pwp
;
213 if (getpwnam_r(IPSEC_USER
, &passwd
, buf
, sizeof(buf
), &pwp
) == 0 && pwp
)
221 ignore_result(system("ipsec scepclient --out pkcs1 --out cert-self --quiet"));
225 /* ipsec.secrets is root readable only */
226 oldmask
= umask(0066);
228 f
= fopen(SECRETS_FILE
, "w");
231 fprintf(f
, "# /etc/ipsec.secrets - strongSwan IPsec secrets file\n");
233 fprintf(f
, ": RSA myKey.der\n");
236 ignore_result(chown(SECRETS_FILE
, uid
, gid
));
240 #endif /* GENERATE_SELFCERT */
242 static bool check_pid(char *pid_file
)
247 if (stat(pid_file
, &stb
) == 0)
249 pidfile
= fopen(pid_file
, "r");
254 memset(buf
, 0, sizeof(buf
));
255 if (fread(buf
, 1, sizeof(buf
), pidfile
))
257 buf
[sizeof(buf
) - 1] = '\0';
261 if (pid
&& kill(pid
, 0) == 0)
262 { /* such a process is running */
266 plog("removing pidfile '%s', process not running", pid_file
);
272 static void usage(char *name
)
274 fprintf(stderr
, "Usage: starter [--nofork] [--auto-update <sec>] "
275 "[--debug|--debug-more|--debug-all]\n");
276 exit(LSB_RC_INVALID_ARGUMENT
);
279 int main (int argc
, char **argv
)
281 starter_config_t
*cfg
= NULL
;
282 starter_config_t
*new_cfg
;
283 starter_conn_t
*conn
, *conn2
;
284 starter_ca_t
*ca
, *ca2
;
286 struct sigaction action
;
292 unsigned long auto_update
= 0;
294 bool no_fork
= FALSE
;
295 bool attach_gdb
= FALSE
;
296 bool load_warning
= FALSE
;
298 /* global variables defined in log.h */
299 log_to_stderr
= TRUE
;
300 base_debugging
= DBG_NONE
;
303 atexit(library_deinit
);
305 libhydra_init("starter");
306 atexit(libhydra_deinit
);
308 /* parse command line */
309 for (i
= 1; i
< argc
; i
++)
311 if (streq(argv
[i
], "--debug"))
313 base_debugging
|= DBG_CONTROL
;
315 else if (streq(argv
[i
], "--debug-more"))
317 base_debugging
|= DBG_CONTROLMORE
;
319 else if (streq(argv
[i
], "--debug-all"))
321 base_debugging
|= DBG_ALL
;
323 else if (streq(argv
[i
], "--nofork"))
327 else if (streq(argv
[i
], "--attach-gdb"))
332 else if (streq(argv
[i
], "--auto-update") && i
+1 < argc
)
334 auto_update
= atoi(argv
[++i
]);
345 init_log("ipsec_starter");
346 cur_debugging
= base_debugging
;
348 plog("Starting strongSwan "VERSION
" IPsec [starter]...");
354 if (lib
->settings
->get_bool(lib
->settings
, "starter.load_warning", load_warning
))
356 if (lib
->settings
->get_str(lib
->settings
, "charon.load", NULL
) ||
357 lib
->settings
->get_str(lib
->settings
, "pluto.load", NULL
))
359 plog("!! Your strongswan.conf contains manual plugin load options for");
360 plog("!! pluto and/or charon. This is recommended for experts only, see");
361 plog("!! http://wiki.strongswan.org/projects/strongswan/wiki/PluginLoad");
365 /* verify that we can start */
368 plog("permission denied (must be superuser)");
369 exit(LSB_RC_NOT_ALLOWED
);
372 if (check_pid(PLUTO_PID_FILE
))
374 plog("pluto is already running (%s exists) -- skipping pluto start",
379 _action_
|= FLAG_ACTION_START_PLUTO
;
381 if (check_pid(CHARON_PID_FILE
))
383 plog("charon is already running (%s exists) -- skipping charon start",
388 _action_
|= FLAG_ACTION_START_CHARON
;
390 if (stat(DEV_RANDOM
, &stb
) != 0)
392 plog("unable to start strongSwan IPsec -- no %s!", DEV_RANDOM
);
393 exit(LSB_RC_FAILURE
);
396 if (stat(DEV_URANDOM
, &stb
)!= 0)
398 plog("unable to start strongSwan IPsec -- no %s!", DEV_URANDOM
);
399 exit(LSB_RC_FAILURE
);
402 cfg
= confread_load(CONFIG_FILE
);
403 if (cfg
== NULL
|| cfg
->err
> 0)
405 plog("unable to start strongSwan -- fatal errors in config");
410 exit(LSB_RC_INVALID_ARGUMENT
);
413 /* determine if we have a native netkey IPsec stack */
414 if (!starter_netkey_init())
416 plog("no netkey IPsec stack detected");
417 if (!starter_klips_init())
419 plog("no KLIPS IPsec stack detected");
420 plog("no known IPsec stack detected, ignoring!");
424 last_reload
= time_monotonic(NULL
);
426 if (check_pid(STARTER_PID_FILE
))
428 plog("starter is already running (%s exists) -- no fork done",
431 exit(LSB_RC_SUCCESS
);
434 #ifdef GENERATE_SELFCERT
438 /* fork if we're not debugging stuff */
441 log_to_stderr
= FALSE
;
452 fnull
= open("/dev/null", O_RDWR
);
455 dup2(fnull
, STDIN_FILENO
);
456 dup2(fnull
, STDOUT_FILENO
);
457 dup2(fnull
, STDERR_FILENO
);
462 init_log("ipsec_starter");
466 plog("can't fork: %s", strerror(errno
));
470 exit(LSB_RC_SUCCESS
);
474 /* save pid file in /var/run/starter.pid */
476 FILE *fd
= fopen(STARTER_PID_FILE
, "w");
480 fprintf(fd
, "%u\n", getpid());
486 if (!lib
->plugins
->load(lib
->plugins
, NULL
,
487 lib
->settings
->get_str(lib
->settings
, "starter.load", PLUGINS
)))
489 exit(LSB_RC_FAILURE
);
492 /* we handle these signals only in pselect() */
493 memset(&action
, 0, sizeof(action
));
494 sigemptyset(&action
.sa_mask
);
495 sigaddset(&action
.sa_mask
, SIGHUP
);
496 sigaddset(&action
.sa_mask
, SIGINT
);
497 sigaddset(&action
.sa_mask
, SIGTERM
);
498 sigaddset(&action
.sa_mask
, SIGQUIT
);
499 sigaddset(&action
.sa_mask
, SIGALRM
);
500 sigaddset(&action
.sa_mask
, SIGUSR1
);
501 pthread_sigmask(SIG_SETMASK
, &action
.sa_mask
, NULL
);
503 /* install a handler for fatal signals */
504 action
.sa_handler
= fatal_signal_handler
;
505 sigaction(SIGSEGV
, &action
, NULL
);
506 sigaction(SIGILL
, &action
, NULL
);
507 sigaction(SIGBUS
, &action
, NULL
);
508 action
.sa_handler
= SIG_IGN
;
509 sigaction(SIGPIPE
, &action
, NULL
);
511 /* install main signal handler */
512 action
.sa_handler
= signal_handler
;
513 sigaction(SIGHUP
, &action
, NULL
);
514 sigaction(SIGINT
, &action
, NULL
);
515 sigaction(SIGTERM
, &action
, NULL
);
516 sigaction(SIGQUIT
, &action
, NULL
);
517 sigaction(SIGALRM
, &action
, NULL
);
518 sigaction(SIGUSR1
, &action
, NULL
);
519 /* this is not blocked above as we want to receive it asynchronously */
520 sigaction(SIGCHLD
, &action
, NULL
);
522 /* empty mask for pselect() call below */
523 sigemptyset(&action
.sa_mask
);
528 * Stop pluto/charon (if started) and exit
530 if (_action_
& FLAG_ACTION_QUIT
)
532 if (starter_pluto_pid())
534 starter_stop_pluto();
536 if (starter_charon_pid())
538 starter_stop_charon();
540 starter_netkey_cleanup();
542 unlink(STARTER_PID_FILE
);
543 plog("ipsec starter stopped");
544 lib
->plugins
->unload(lib
->plugins
);
546 exit(LSB_RC_SUCCESS
);
550 * Delete all connections. Will be added below
552 if (_action_
& FLAG_ACTION_RELOAD
)
554 if (starter_pluto_pid() || starter_charon_pid())
556 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
558 if (conn
->state
== STATE_ADDED
)
560 if (starter_charon_pid())
562 starter_stroke_del_conn(conn
);
564 if (starter_pluto_pid())
566 starter_whack_del_conn(conn
);
568 conn
->state
= STATE_TO_ADD
;
571 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
573 if (ca
->state
== STATE_ADDED
)
575 if (starter_charon_pid())
577 starter_stroke_del_ca(ca
);
579 if (starter_pluto_pid())
581 starter_whack_del_ca(ca
);
583 ca
->state
= STATE_TO_ADD
;
587 _action_
&= ~FLAG_ACTION_RELOAD
;
591 * Update configuration
593 if (_action_
& FLAG_ACTION_UPDATE
)
596 DBG_log("Reloading config...")
598 new_cfg
= confread_load(CONFIG_FILE
);
600 if (new_cfg
&& (new_cfg
->err
+ new_cfg
->non_fatal_err
== 0))
602 /* Switch to new config. New conn will be loaded below */
603 if (!starter_cmp_defaultroute(&new_cfg
->defaultroute
604 , &cfg
->defaultroute
))
606 _action_
|= FLAG_ACTION_LISTEN
;
609 if (!starter_cmp_pluto(cfg
, new_cfg
))
611 plog("Pluto has changed");
612 if (starter_pluto_pid())
613 starter_stop_pluto();
614 _action_
&= ~FLAG_ACTION_LISTEN
;
615 _action_
|= FLAG_ACTION_START_PLUTO
;
619 /* Only reload conn and ca sections if pluto is not killed */
621 /* Look for new connections that are already loaded */
622 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
624 if (conn
->state
== STATE_ADDED
)
626 for (conn2
= new_cfg
->conn_first
; conn2
; conn2
= conn2
->next
)
628 if (conn2
->state
== STATE_TO_ADD
&& starter_cmp_conn(conn
, conn2
))
630 conn
->state
= STATE_REPLACED
;
631 conn2
->state
= STATE_ADDED
;
632 conn2
->id
= conn
->id
;
639 /* Remove conn sections that have become unused */
640 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
642 if (conn
->state
== STATE_ADDED
)
644 if (starter_charon_pid())
646 starter_stroke_del_conn(conn
);
648 if (starter_pluto_pid())
650 starter_whack_del_conn(conn
);
655 /* Look for new ca sections that are already loaded */
656 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
658 if (ca
->state
== STATE_ADDED
)
660 for (ca2
= new_cfg
->ca_first
; ca2
; ca2
= ca2
->next
)
662 if (ca2
->state
== STATE_TO_ADD
&& starter_cmp_ca(ca
, ca2
))
664 ca
->state
= STATE_REPLACED
;
665 ca2
->state
= STATE_ADDED
;
672 /* Remove ca sections that have become unused */
673 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
675 if (ca
->state
== STATE_ADDED
)
677 if (starter_charon_pid())
679 starter_stroke_del_ca(ca
);
681 if (starter_pluto_pid())
683 starter_whack_del_ca(ca
);
693 plog("can't reload config file due to errors -- keeping old one");
696 confread_free(new_cfg
);
699 _action_
&= ~FLAG_ACTION_UPDATE
;
700 last_reload
= time_monotonic(NULL
);
706 if (_action_
& FLAG_ACTION_START_PLUTO
)
708 if (cfg
->setup
.plutostart
&& !starter_pluto_pid())
711 DBG_log("Attempting to start pluto...")
714 if (starter_start_pluto(cfg
, no_fork
, attach_gdb
) == 0)
716 starter_whack_listen();
720 /* schedule next try */
721 alarm(PLUTO_RESTART_DELAY
);
724 _action_
&= ~FLAG_ACTION_START_PLUTO
;
726 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
728 if (ca
->state
== STATE_ADDED
)
729 ca
->state
= STATE_TO_ADD
;
732 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
734 if (conn
->state
== STATE_ADDED
)
735 conn
->state
= STATE_TO_ADD
;
742 if (_action_
& FLAG_ACTION_START_CHARON
)
744 if (cfg
->setup
.charonstart
&& !starter_charon_pid())
747 DBG_log("Attempting to start charon...")
749 if (starter_start_charon(cfg
, no_fork
, attach_gdb
))
751 /* schedule next try */
752 alarm(PLUTO_RESTART_DELAY
);
754 starter_stroke_configure(cfg
);
756 _action_
&= ~FLAG_ACTION_START_CHARON
;
760 * Tell pluto to reread its interfaces
762 if (_action_
& FLAG_ACTION_LISTEN
)
764 if (starter_pluto_pid())
766 starter_whack_listen();
767 _action_
&= ~FLAG_ACTION_LISTEN
;
772 * Add stale conn and ca sections
774 if (starter_pluto_pid() || starter_charon_pid())
776 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
778 if (ca
->state
== STATE_TO_ADD
)
780 if (starter_charon_pid())
782 starter_stroke_add_ca(ca
);
784 if (starter_pluto_pid())
786 starter_whack_add_ca(ca
);
788 ca
->state
= STATE_ADDED
;
792 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
794 if (conn
->state
== STATE_TO_ADD
)
798 /* affect new unique id */
801 if (starter_charon_pid())
803 starter_stroke_add_conn(cfg
, conn
);
805 if (starter_pluto_pid())
807 starter_whack_add_conn(conn
);
809 conn
->state
= STATE_ADDED
;
811 if (conn
->startup
== STARTUP_START
)
813 if (starter_charon_pid())
815 starter_stroke_initiate_conn(conn
);
817 if (conn
->keyexchange
== KEY_EXCHANGE_IKEV1
)
819 if (starter_pluto_pid())
821 starter_whack_initiate_conn(conn
);
825 else if (conn
->startup
== STARTUP_ROUTE
)
827 if (starter_charon_pid())
829 starter_stroke_route_conn(conn
);
831 if (conn
->keyexchange
== KEY_EXCHANGE_IKEV1
)
833 if (starter_pluto_pid())
835 starter_whack_route_conn(conn
);
844 * If auto_update activated, when to stop select
848 time_t now
= time_monotonic(NULL
);
850 ts
.tv_sec
= (now
< last_reload
+ auto_update
) ?
851 (last_reload
+ auto_update
- now
) : 0;
856 * Wait for something to happen
858 if (pselect(0, NULL
, NULL
, NULL
, auto_update ?
&ts
: NULL
,
859 &action
.sa_mask
) == 0)
861 /* timeout -> auto_update */
862 _action_
|= FLAG_ACTION_UPDATE
;
865 exit(LSB_RC_SUCCESS
);