506fe47e2305fcbb7283cb768abb74862596d62e
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
17 #include <sys/select.h>
18 #include <sys/types.h>
36 #include <utils/backtrace.h>
37 #include <threading/thread.h>
38 #include <utils/debug.h>
42 #include "starterstroke.h"
43 #include "invokecharon.h"
47 #define LOG_AUTHPRIV LOG_AUTH
50 #define CHARON_RESTART_DELAY 5
52 static const char* cmd_default
= IPSEC_DIR
"/charon";
53 static const char* pid_file_default
= IPSEC_PIDDIR
"/charon.pid";
54 static const char* starter_pid_file_default
= IPSEC_PIDDIR
"/starter.pid";
56 char *daemon_name
= NULL
;
58 char *pid_file
= NULL
;
59 char *starter_pid_file
= NULL
;
61 static char *config_file
= NULL
;
64 static bool log_to_stderr
= TRUE
;
65 static bool log_to_syslog
= TRUE
;
66 static level_t current_loglevel
= 1;
69 * logging function for scepclient
71 static void starter_dbg(debug_t group
, level_t level
, char *fmt
, ...)
74 char *current
= buffer
, *next
;
77 if (level
<= current_loglevel
)
82 vfprintf(stderr
, fmt
, args
);
84 fprintf(stderr
, "\n");
88 /* write in memory buffer first */
90 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
93 /* do a syslog with every line */
96 next
= strchr(current
, '\n');
101 syslog(LOG_INFO
, "%s\n", current
);
109 * Initialize logging to stderr/syslog
111 static void init_log(const char *program
)
117 setbuf(stderr
, NULL
);
121 openlog(program
, LOG_CONS
| LOG_NDELAY
| LOG_PID
, LOG_AUTHPRIV
);
126 * Deinitialize logging to syslog
128 static void close_log()
137 * Return codes defined by Linux Standard Base Core Specification 3.1
138 * in section 20.2. Init Script Actions
140 #define LSB_RC_SUCCESS 0 /* success */
141 #define LSB_RC_FAILURE 1 /* generic or unspecified error */
142 #define LSB_RC_INVALID_ARGUMENT 2 /* invalid or excess argument(s) */
143 #define LSB_RC_NOT_IMPLEMENTED 3 /* unimplemented feature (reload) */
144 #define LSB_RC_NOT_ALLOWED 4 /* user had insufficient privilege */
145 #define LSB_RC_NOT_INSTALLED 5 /* program is not installed */
146 #define LSB_RC_NOT_CONFIGURED 6 /* program is not configured */
147 #define LSB_RC_NOT_RUNNING 7 /* program is not running */
149 #define FLAG_ACTION_START_PLUTO 0x01
150 #define FLAG_ACTION_UPDATE 0x02
151 #define FLAG_ACTION_RELOAD 0x04
152 #define FLAG_ACTION_QUIT 0x08
153 #define FLAG_ACTION_LISTEN 0x10
154 #define FLAG_ACTION_START_CHARON 0x20
156 static unsigned int _action_
= 0;
159 * Handle signals in the main thread
161 static void signal_handler(int signal
)
167 int status
, exit_status
= 0;
171 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
173 if (pid
== starter_charon_pid())
175 if (asprintf(&name
, " (%s)", daemon_name
) < 0)
180 if (WIFSIGNALED(status
))
182 DBG2(DBG_APP
, "child %d%s has been killed by sig %d\n",
183 pid
, name?name
:"", WTERMSIG(status
));
185 else if (WIFSTOPPED(status
))
187 DBG2(DBG_APP
, "child %d%s has been stopped by sig %d\n",
188 pid
, name?name
:"", WSTOPSIG(status
));
190 else if (WIFEXITED(status
))
192 exit_status
= WEXITSTATUS(status
);
193 if (exit_status
>= SS_RC_FIRST
&& exit_status
<= SS_RC_LAST
)
195 _action_
= FLAG_ACTION_QUIT
;
197 DBG2(DBG_APP
, "child %d%s has quit (exit code %d)\n",
198 pid
, name?name
:"", exit_status
);
202 DBG2(DBG_APP
, "child %d%s has quit", pid
, name?name
:"");
204 if (pid
== starter_charon_pid())
206 starter_charon_sigchild(pid
, exit_status
);
218 _action_
|= FLAG_ACTION_START_CHARON
;
222 _action_
|= FLAG_ACTION_UPDATE
;
228 _action_
|= FLAG_ACTION_QUIT
;
232 _action_
|= FLAG_ACTION_RELOAD
;
233 _action_
|= FLAG_ACTION_UPDATE
;
237 DBG1(DBG_APP
, "fsig(): unknown signal %d -- investigate", signal
);
243 * Handle fatal signals raised by threads
245 static void fatal_signal_handler(int signal
)
247 backtrace_t
*backtrace
;
249 DBG1(DBG_APP
, "thread %u received %d", thread_current_id(), signal
);
250 backtrace
= backtrace_create(2);
251 backtrace
->log(backtrace
, stderr
, TRUE
);
252 backtrace
->destroy(backtrace
);
254 DBG1(DBG_APP
, "killing ourself, received critical signal");
258 static bool check_pid(char *pid_file
)
263 if (stat(pid_file
, &stb
) == 0)
265 pidfile
= fopen(pid_file
, "r");
270 memset(buf
, 0, sizeof(buf
));
271 if (fread(buf
, 1, sizeof(buf
), pidfile
))
273 buf
[sizeof(buf
) - 1] = '\0';
277 if (pid
&& pid
!= getpid() && kill(pid
, 0) == 0)
278 { /* such a process is running */
282 DBG1(DBG_APP
, "removing pidfile '%s', process not running", pid_file
);
288 /* Set daemon name and adjust command and pid filenames accordingly */
289 static bool set_daemon_name()
293 daemon_name
= "charon";
296 if (asprintf(&cmd
, IPSEC_DIR
"/%s", daemon_name
) < 0)
298 cmd
= (char*)cmd_default
;
301 if (asprintf(&pid_file
, IPSEC_PIDDIR
"/%s.pid", daemon_name
) < 0)
303 pid_file
= (char*)pid_file_default
;
306 if (asprintf(&starter_pid_file
, IPSEC_PIDDIR
"/starter.%s.pid",
309 starter_pid_file
= (char*)starter_pid_file_default
;
315 static void cleanup()
317 if (cmd
!= cmd_default
)
322 if (pid_file
!= pid_file_default
)
327 if (starter_pid_file
!= starter_pid_file_default
)
329 free(starter_pid_file
);
333 static void usage(char *name
)
335 fprintf(stderr
, "Usage: starter [--nofork] [--auto-update <sec>]\n"
336 " [--debug|--debug-more|--debug-all|--nolog]\n"
337 " [--attach-gdb] [--daemon <name>]\n"
338 " [--conf <path to ipsec.conf>]\n");
339 exit(LSB_RC_INVALID_ARGUMENT
);
342 int main (int argc
, char **argv
)
344 starter_config_t
*cfg
= NULL
;
345 starter_config_t
*new_cfg
;
346 starter_conn_t
*conn
, *conn2
;
347 starter_ca_t
*ca
, *ca2
;
349 struct sigaction action
;
355 unsigned long auto_update
= 0;
357 bool no_fork
= FALSE
;
358 bool attach_gdb
= FALSE
;
359 bool load_warning
= FALSE
;
360 bool conftest
= FALSE
;
362 library_init(NULL
, "starter");
363 atexit(library_deinit
);
365 /* parse command line */
366 for (i
= 1; i
< argc
; i
++)
368 if (streq(argv
[i
], "--debug"))
370 current_loglevel
= 2;
372 else if (streq(argv
[i
], "--debug-more"))
374 current_loglevel
= 3;
376 else if (streq(argv
[i
], "--debug-all"))
378 current_loglevel
= 4;
380 else if (streq(argv
[i
], "--nolog"))
382 current_loglevel
= 0;
384 else if (streq(argv
[i
], "--nofork"))
388 else if (streq(argv
[i
], "--attach-gdb"))
393 else if (streq(argv
[i
], "--auto-update") && i
+1 < argc
)
395 auto_update
= atoi(argv
[++i
]);
399 else if (streq(argv
[i
], "--daemon") && i
+1 < argc
)
401 daemon_name
= argv
[++i
];
403 else if (streq(argv
[i
], "--conf") && i
+1 < argc
)
405 config_file
= argv
[++i
];
407 else if (streq(argv
[i
], "--conftest"))
417 if (!set_daemon_name())
419 DBG1(DBG_APP
, "unable to set daemon name");
420 exit(LSB_RC_FAILURE
);
424 config_file
= lib
->settings
->get_str(lib
->settings
,
425 "starter.config_file", CONFIG_FILE
);
428 init_log("ipsec_starter");
432 int status
= LSB_RC_SUCCESS
;
434 cfg
= confread_load(config_file
);
435 if (cfg
== NULL
|| cfg
->err
> 0)
437 DBG1(DBG_APP
, "config invalid!");
438 status
= LSB_RC_INVALID_ARGUMENT
;
442 DBG1(DBG_APP
, "config OK");
452 if (stat(cmd
, &stb
) != 0)
454 DBG1(DBG_APP
, "IKE daemon '%s' not found", cmd
);
456 exit(LSB_RC_FAILURE
);
459 DBG1(DBG_APP
, "Starting %sSwan "VERSION
" IPsec [starter]...",
460 lib
->settings
->get_bool(lib
->settings
,
461 "charon.i_dont_care_about_security_and_use_aggressive_mode_psk",
462 FALSE
) ?
"weak" : "strong");
468 if (lib
->settings
->get_bool(lib
->settings
, "starter.load_warning", load_warning
))
470 if (lib
->settings
->get_str(lib
->settings
, "charon.load", NULL
))
472 DBG1(DBG_APP
, "!! Your strongswan.conf contains manual plugin load options for charon.");
473 DBG1(DBG_APP
, "!! This is recommended for experts only, see");
474 DBG1(DBG_APP
, "!! http://wiki.strongswan.org/projects/strongswan/wiki/PluginLoad");
478 #ifndef STARTER_ALLOW_NON_ROOT
479 /* verify that we can start */
482 DBG1(DBG_APP
, "permission denied (must be superuser)");
484 exit(LSB_RC_NOT_ALLOWED
);
488 if (check_pid(pid_file
))
490 DBG1(DBG_APP
, "%s is already running (%s exists) -- skipping daemon start",
491 daemon_name
, pid_file
);
495 _action_
|= FLAG_ACTION_START_CHARON
;
497 if (stat(DEV_RANDOM
, &stb
) != 0)
499 DBG1(DBG_APP
, "unable to start strongSwan IPsec -- no %s!", DEV_RANDOM
);
501 exit(LSB_RC_FAILURE
);
504 if (stat(DEV_URANDOM
, &stb
)!= 0)
506 DBG1(DBG_APP
, "unable to start strongSwan IPsec -- no %s!", DEV_URANDOM
);
508 exit(LSB_RC_FAILURE
);
511 cfg
= confread_load(config_file
);
512 if (cfg
== NULL
|| cfg
->err
> 0)
514 DBG1(DBG_APP
, "unable to start strongSwan -- fatal errors in config");
520 exit(LSB_RC_INVALID_ARGUMENT
);
523 last_reload
= time_monotonic(NULL
);
525 if (check_pid(starter_pid_file
))
527 DBG1(DBG_APP
, "starter is already running (%s exists) -- no fork done",
531 exit(LSB_RC_SUCCESS
);
534 /* fork if we're not debugging stuff */
537 log_to_stderr
= FALSE
;
547 fnull
= open("/dev/null", O_RDWR
);
550 dup2(fnull
, STDIN_FILENO
);
551 dup2(fnull
, STDOUT_FILENO
);
552 dup2(fnull
, STDERR_FILENO
);
557 init_log("ipsec_starter");
561 DBG1(DBG_APP
, "can't fork: %s", strerror(errno
));
566 exit(LSB_RC_SUCCESS
);
570 /* save pid file in /var/run/starter[.daemon_name].pid */
572 FILE *fd
= fopen(starter_pid_file
, "w");
576 fprintf(fd
, "%u\n", getpid());
581 /* we handle these signals only in pselect() */
582 memset(&action
, 0, sizeof(action
));
583 sigemptyset(&action
.sa_mask
);
584 sigaddset(&action
.sa_mask
, SIGHUP
);
585 sigaddset(&action
.sa_mask
, SIGINT
);
586 sigaddset(&action
.sa_mask
, SIGTERM
);
587 sigaddset(&action
.sa_mask
, SIGQUIT
);
588 sigaddset(&action
.sa_mask
, SIGALRM
);
589 sigaddset(&action
.sa_mask
, SIGUSR1
);
590 pthread_sigmask(SIG_SETMASK
, &action
.sa_mask
, NULL
);
592 /* install a handler for fatal signals */
593 action
.sa_handler
= fatal_signal_handler
;
594 sigaction(SIGSEGV
, &action
, NULL
);
595 sigaction(SIGILL
, &action
, NULL
);
596 sigaction(SIGBUS
, &action
, NULL
);
597 action
.sa_handler
= SIG_IGN
;
598 sigaction(SIGPIPE
, &action
, NULL
);
600 /* install main signal handler */
601 action
.sa_handler
= signal_handler
;
602 sigaction(SIGHUP
, &action
, NULL
);
603 sigaction(SIGINT
, &action
, NULL
);
604 sigaction(SIGTERM
, &action
, NULL
);
605 sigaction(SIGQUIT
, &action
, NULL
);
606 sigaction(SIGALRM
, &action
, NULL
);
607 sigaction(SIGUSR1
, &action
, NULL
);
608 /* this is not blocked above as we want to receive it asynchronously */
609 sigaction(SIGCHLD
, &action
, NULL
);
611 /* empty mask for pselect() call below */
612 sigemptyset(&action
.sa_mask
);
617 * Stop charon (if started) and exit
619 if (_action_
& FLAG_ACTION_QUIT
)
621 if (starter_charon_pid())
623 starter_stop_charon();
626 unlink(starter_pid_file
);
628 DBG1(DBG_APP
, "ipsec starter stopped");
630 exit(LSB_RC_SUCCESS
);
634 * Delete all connections. Will be added below
636 if (_action_
& FLAG_ACTION_RELOAD
)
638 _action_
&= ~FLAG_ACTION_RELOAD
;
639 if (starter_charon_pid())
641 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
643 if (conn
->state
== STATE_ADDED
)
645 if (starter_charon_pid())
647 if (conn
->startup
== STARTUP_ROUTE
)
649 starter_stroke_unroute_conn(conn
);
651 starter_stroke_del_conn(conn
);
653 conn
->state
= STATE_TO_ADD
;
656 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
658 if (ca
->state
== STATE_ADDED
)
660 if (starter_charon_pid())
662 starter_stroke_del_ca(ca
);
664 ca
->state
= STATE_TO_ADD
;
671 * Update configuration
673 if (_action_
& FLAG_ACTION_UPDATE
)
675 _action_
&= ~FLAG_ACTION_UPDATE
;
676 DBG2(DBG_APP
, "Reloading config...");
677 new_cfg
= confread_load(config_file
);
679 if (new_cfg
&& (new_cfg
->err
== 0))
681 /* Switch to new config. New conn will be loaded below */
683 /* Look for new connections that are already loaded */
684 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
686 if (conn
->state
== STATE_ADDED
)
688 for (conn2
= new_cfg
->conn_first
; conn2
; conn2
= conn2
->next
)
690 if (conn2
->state
== STATE_TO_ADD
&& starter_cmp_conn(conn
, conn2
))
692 conn
->state
= STATE_REPLACED
;
693 conn2
->state
= STATE_ADDED
;
694 conn2
->id
= conn
->id
;
701 /* Remove conn sections that have become unused */
702 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
704 if (conn
->state
== STATE_ADDED
)
706 if (starter_charon_pid())
708 if (conn
->startup
== STARTUP_ROUTE
)
710 starter_stroke_unroute_conn(conn
);
712 starter_stroke_del_conn(conn
);
717 /* Look for new ca sections that are already loaded */
718 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
720 if (ca
->state
== STATE_ADDED
)
722 for (ca2
= new_cfg
->ca_first
; ca2
; ca2
= ca2
->next
)
724 if (ca2
->state
== STATE_TO_ADD
&& starter_cmp_ca(ca
, ca2
))
726 ca
->state
= STATE_REPLACED
;
727 ca2
->state
= STATE_ADDED
;
734 /* Remove ca sections that have become unused */
735 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
737 if (ca
->state
== STATE_ADDED
)
739 if (starter_charon_pid())
741 starter_stroke_del_ca(ca
);
750 DBG1(DBG_APP
, "can't reload config file due to errors -- keeping old one");
753 confread_free(new_cfg
);
756 last_reload
= time_monotonic(NULL
);
762 if (_action_
& FLAG_ACTION_START_CHARON
)
764 _action_
&= ~FLAG_ACTION_START_CHARON
;
765 if (!starter_charon_pid())
767 DBG2(DBG_APP
, "Attempting to start %s...", daemon_name
);
768 if (starter_start_charon(cfg
, no_fork
, attach_gdb
))
770 /* schedule next try */
771 alarm(CHARON_RESTART_DELAY
);
773 starter_stroke_configure(cfg
);
776 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
778 if (ca
->state
== STATE_ADDED
)
780 ca
->state
= STATE_TO_ADD
;
784 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
786 if (conn
->state
== STATE_ADDED
)
788 conn
->state
= STATE_TO_ADD
;
794 * Add stale conn and ca sections
796 if (starter_charon_pid())
798 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
800 if (ca
->state
== STATE_TO_ADD
)
802 if (starter_charon_pid())
804 starter_stroke_add_ca(ca
);
806 ca
->state
= STATE_ADDED
;
810 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
812 if (conn
->state
== STATE_TO_ADD
)
816 /* affect new unique id */
819 if (starter_charon_pid())
821 starter_stroke_add_conn(cfg
, conn
);
823 conn
->state
= STATE_ADDED
;
825 if (conn
->startup
== STARTUP_START
)
827 if (starter_charon_pid())
829 starter_stroke_initiate_conn(conn
);
832 else if (conn
->startup
== STARTUP_ROUTE
)
834 if (starter_charon_pid())
836 starter_stroke_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
859 pselect(0, NULL
, NULL
, NULL
, auto_update ?
&ts
: NULL
,
860 &action
.sa_mask
) == 0)
862 /* timeout -> auto_update */
863 _action_
|= FLAG_ACTION_UPDATE
;
866 exit(LSB_RC_SUCCESS
);