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/types.h>
34 #include "../pluto/constants.h"
35 #include "../pluto/defs.h"
36 #include "../pluto/log.h"
40 #include "starterwhack.h"
41 #include "starterstroke.h"
42 #include "invokepluto.h"
43 #include "invokecharon.h"
46 #include "interfaces.h"
49 * Return codes defined by Linux Standard Base Core Specification 3.1
50 * in section 20.2. Init Script Actions
52 #define LSB_RC_SUCCESS 0 /* success */
53 #define LSB_RC_FAILURE 1 /* generic or unspecified error */
54 #define LSB_RC_INVALID_ARGUMENT 2 /* invalid or excess argument(s) */
55 #define LSB_RC_NOT_IMPLEMENTED 3 /* unimplemented feature (reload) */
56 #define LSB_RC_NOT_ALLOWED 4 /* user had insufficient privilege */
57 #define LSB_RC_NOT_INSTALLED 5 /* program is not installed */
58 #define LSB_RC_NOT_CONFIGURED 6 /* program is not configured */
59 #define LSB_RC_NOT_RUNNING 7 /* program is not running */
61 #define FLAG_ACTION_START_PLUTO 0x01
62 #define FLAG_ACTION_UPDATE 0x02
63 #define FLAG_ACTION_RELOAD 0x04
64 #define FLAG_ACTION_QUIT 0x08
65 #define FLAG_ACTION_LISTEN 0x10
66 #define FLAG_ACTION_START_CHARON 0x20
68 static unsigned int _action_
= 0;
81 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
83 if (pid
== starter_pluto_pid())
85 if (pid
== starter_charon_pid())
87 if (WIFSIGNALED(status
))
89 DBG_log("child %d%s has been killed by sig %d\n",
90 pid
, name?name
:"", WTERMSIG(status
))
92 else if (WIFSTOPPED(status
))
94 DBG_log("child %d%s has been stopped by sig %d\n",
95 pid
, name?name
:"", WSTOPSIG(status
))
97 else if (WIFEXITED(status
))
99 DBG_log("child %d%s has quit (exit code %d)\n",
100 pid
, name?name
:"", WEXITSTATUS(status
))
104 DBG_log("child %d%s has quit", pid
, name?name
:"")
106 if (pid
== starter_pluto_pid())
107 starter_pluto_sigchild(pid
);
108 if (pid
== starter_charon_pid())
109 starter_charon_sigchild(pid
);
119 _action_
|= FLAG_ACTION_START_PLUTO
;
120 _action_
|= FLAG_ACTION_START_CHARON
;
124 _action_
|= FLAG_ACTION_UPDATE
;
130 _action_
|= FLAG_ACTION_QUIT
;
134 _action_
|= FLAG_ACTION_RELOAD
;
135 _action_
|= FLAG_ACTION_UPDATE
;
139 plog("fsig(): unknown signal %d -- investigate", signal
);
144 static void generate_selfcert()
148 /* if ipsec.secrets file is missing then generate RSA default key pair */
149 if (stat(SECRETS_FILE
, &stb
) != 0)
159 struct group group
, *grp
;
161 if (getgrnam_r(IPSEC_GROUP
, &group
, buf
, sizeof(buf
), &grp
) == 0 &&
171 struct passwd passwd
, *pwp
;
173 if (getpwnam_r(IPSEC_USER
, &passwd
, buf
, sizeof(buf
), &pwp
) == 0 &&
182 system("ipsec scepclient --out pkcs1 --out cert-self --quiet");
186 /* ipsec.secrets is root readable only */
187 oldmask
= umask(0066);
189 f
= fopen(SECRETS_FILE
, "w");
192 fprintf(f
, "# /etc/ipsec.secrets - strongSwan IPsec secrets file\n");
194 fprintf(f
, ": RSA myKey.der\n");
197 chown(SECRETS_FILE
, uid
, gid
);
205 fprintf(stderr
, "Usage: starter [--nofork] [--auto-update <sec>] "
206 "[--debug|--debug-more|--debug-all]\n");
207 exit(LSB_RC_INVALID_ARGUMENT
);
210 int main (int argc
, char **argv
)
212 starter_config_t
*cfg
= NULL
;
213 starter_config_t
*new_cfg
;
214 starter_conn_t
*conn
, *conn2
;
215 starter_ca_t
*ca
, *ca2
;
223 unsigned long auto_update
= 0;
225 bool no_fork
= FALSE
;
227 /* global variables defined in log.h */
228 log_to_stderr
= TRUE
;
229 base_debugging
= DBG_NONE
;
231 /* parse command line */
232 for (i
= 1; i
< argc
; i
++)
234 if (streq(argv
[i
], "--debug"))
236 base_debugging
|= DBG_CONTROL
;
238 else if (streq(argv
[i
], "--debug-more"))
240 base_debugging
|= DBG_CONTROLMORE
;
242 else if (streq(argv
[i
], "--debug-all"))
244 base_debugging
|= DBG_ALL
;
246 else if (streq(argv
[i
], "--nofork"))
250 else if (streq(argv
[i
], "--auto-update") && i
+1 < argc
)
252 auto_update
= atoi(argv
[++i
]);
263 init_log("ipsec_starter");
264 cur_debugging
= base_debugging
;
266 signal(SIGHUP
, fsig
);
267 signal(SIGCHLD
, fsig
);
268 signal(SIGPIPE
, fsig
);
269 signal(SIGINT
, fsig
);
270 signal(SIGTERM
, fsig
);
271 signal(SIGQUIT
, fsig
);
272 signal(SIGALRM
, fsig
);
273 signal(SIGUSR1
, fsig
);
275 plog("Starting strongSwan %s IPsec [starter]...", ipsec_version_code());
277 /* verify that we can start */
280 plog("permission denied (must be superuser)");
281 exit(LSB_RC_NOT_ALLOWED
);
284 if (stat(PLUTO_PID_FILE
, &stb
) == 0)
286 plog("pluto is already running (%s exists) -- skipping pluto start", PLUTO_PID_FILE
);
290 _action_
|= FLAG_ACTION_START_PLUTO
;
292 if (stat(CHARON_PID_FILE
, &stb
) == 0)
294 plog("charon is already running (%s exists) -- skipping charon start", CHARON_PID_FILE
);
298 _action_
|= FLAG_ACTION_START_CHARON
;
300 if (stat(DEV_RANDOM
, &stb
) != 0)
302 plog("unable to start strongSwan IPsec -- no %s!", DEV_RANDOM
);
303 exit(LSB_RC_FAILURE
);
306 if (stat(DEV_URANDOM
, &stb
)!= 0)
308 plog("unable to start strongSwan IPsec -- no %s!", DEV_URANDOM
);
309 exit(LSB_RC_FAILURE
);
312 cfg
= confread_load(CONFIG_FILE
);
313 if (cfg
== NULL
|| cfg
->err
> 0)
315 plog("unable to start strongSwan -- fatal errors in config");
320 exit(LSB_RC_INVALID_ARGUMENT
);
323 /* determine if we have a native netkey IPsec stack */
324 if (!starter_netkey_init())
326 plog("no netkey IPSec stack detected");
327 exit(LSB_RC_FAILURE
);
330 last_reload
= time(NULL
);
332 if (stat(STARTER_PID_FILE
, &stb
) == 0)
334 plog("starter is already running (%s exists) -- no fork done", STARTER_PID_FILE
);
335 exit(LSB_RC_SUCCESS
);
340 /* fork if we're not debugging stuff */
343 log_to_stderr
= FALSE
;
349 int fnull
= open("/dev/null", O_RDWR
);
353 dup2(fnull
, STDIN_FILENO
);
354 dup2(fnull
, STDOUT_FILENO
);
355 dup2(fnull
, STDERR_FILENO
);
362 plog("can't fork: %s", strerror(errno
));
365 exit(LSB_RC_SUCCESS
);
369 /* save pid file in /var/run/starter.pid */
371 FILE *fd
= fopen(STARTER_PID_FILE
, "w");
375 fprintf(fd
, "%u\n", getpid());
383 * Stop pluto/charon (if started) and exit
385 if (_action_
& FLAG_ACTION_QUIT
)
387 if (starter_pluto_pid())
388 starter_stop_pluto();
389 if (starter_charon_pid())
390 starter_stop_charon();
391 starter_netkey_cleanup();
393 unlink(STARTER_PID_FILE
);
395 #ifdef LEAK_DETECTIVE
397 #endif /* LEAK_DETECTIVE */
399 plog("ipsec starter stopped");
400 exit(LSB_RC_SUCCESS
);
404 * Delete all connections. Will be added below
406 if (_action_
& FLAG_ACTION_RELOAD
)
408 if (starter_pluto_pid() || starter_charon_pid())
410 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
412 if (conn
->state
== STATE_ADDED
)
414 if (starter_charon_pid())
416 starter_stroke_del_conn(conn
);
418 if (starter_pluto_pid())
420 starter_whack_del_conn(conn
);
422 conn
->state
= STATE_TO_ADD
;
425 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
427 if (ca
->state
== STATE_ADDED
)
429 if (starter_charon_pid())
431 starter_stroke_del_ca(ca
);
433 if (starter_pluto_pid())
435 starter_whack_del_ca(ca
);
437 ca
->state
= STATE_TO_ADD
;
441 _action_
&= ~FLAG_ACTION_RELOAD
;
445 * Update configuration
447 if (_action_
& FLAG_ACTION_UPDATE
)
451 DBG_log("Reloading config...")
453 new_cfg
= confread_load(CONFIG_FILE
);
455 if (new_cfg
->err
+ new_cfg
->non_fatal_err
== 0)
457 /* Switch to new config. New conn will be loaded below */
458 if (!starter_cmp_defaultroute(&new_cfg
->defaultroute
459 , &cfg
->defaultroute
))
461 _action_
|= FLAG_ACTION_LISTEN
;
464 if (!starter_cmp_pluto(cfg
, new_cfg
))
466 plog("Pluto has changed");
467 if (starter_pluto_pid())
468 starter_stop_pluto();
469 _action_
&= ~FLAG_ACTION_LISTEN
;
470 _action_
|= FLAG_ACTION_START_PLUTO
;
474 /* Only reload conn and ca sections if pluto is not killed */
476 /* Look for new connections that are already loaded */
477 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
479 if (conn
->state
== STATE_ADDED
)
481 for (conn2
= new_cfg
->conn_first
; conn2
; conn2
= conn2
->next
)
483 if (conn2
->state
== STATE_TO_ADD
&& starter_cmp_conn(conn
, conn2
))
485 conn
->state
= STATE_REPLACED
;
486 conn2
->state
= STATE_ADDED
;
487 conn2
->id
= conn
->id
;
494 /* Remove conn sections that have become unused */
495 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
497 if (conn
->state
== STATE_ADDED
)
499 if (starter_charon_pid())
501 starter_stroke_del_conn(conn
);
503 if (starter_pluto_pid())
505 starter_whack_del_conn(conn
);
510 /* Look for new ca sections that are already loaded */
511 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
513 if (ca
->state
== STATE_ADDED
)
515 for (ca2
= new_cfg
->ca_first
; ca2
; ca2
= ca2
->next
)
517 if (ca2
->state
== STATE_TO_ADD
&& starter_cmp_ca(ca
, ca2
))
519 ca
->state
= STATE_REPLACED
;
520 ca2
->state
= STATE_ADDED
;
527 /* Remove ca sections that have become unused */
528 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
530 if (ca
->state
== STATE_ADDED
)
532 if (starter_charon_pid())
534 starter_stroke_del_ca(ca
);
536 if (starter_pluto_pid())
538 starter_whack_del_ca(ca
);
548 plog("can't reload config file due to errors -- keeping old one");
549 confread_free(new_cfg
);
551 _action_
&= ~FLAG_ACTION_UPDATE
;
552 last_reload
= time(NULL
);
558 if (_action_
& FLAG_ACTION_START_PLUTO
)
560 if (cfg
->setup
.plutostart
&& !starter_pluto_pid())
563 DBG_log("Attempting to start pluto...")
566 if (starter_start_pluto(cfg
, no_fork
) == 0)
568 starter_whack_listen();
572 /* schedule next try */
573 alarm(PLUTO_RESTART_DELAY
);
576 _action_
&= ~FLAG_ACTION_START_PLUTO
;
578 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
580 if (ca
->state
== STATE_ADDED
)
581 ca
->state
= STATE_TO_ADD
;
584 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
586 if (conn
->state
== STATE_ADDED
)
587 conn
->state
= STATE_TO_ADD
;
594 if (_action_
& FLAG_ACTION_START_CHARON
)
596 if (cfg
->setup
.charonstart
&& !starter_charon_pid())
599 DBG_log("Attempting to start charon...")
601 if (starter_start_charon(cfg
, no_fork
))
603 /* schedule next try */
604 alarm(PLUTO_RESTART_DELAY
);
606 starter_stroke_configure(cfg
);
608 _action_
&= ~FLAG_ACTION_START_CHARON
;
612 * Tell pluto to reread its interfaces
614 if (_action_
& FLAG_ACTION_LISTEN
)
616 if (starter_pluto_pid())
618 starter_whack_listen();
619 _action_
&= ~FLAG_ACTION_LISTEN
;
624 * Add stale conn and ca sections
626 if (starter_pluto_pid() || starter_charon_pid())
628 for (ca
= cfg
->ca_first
; ca
; ca
= ca
->next
)
630 if (ca
->state
== STATE_TO_ADD
)
632 if (starter_charon_pid())
634 starter_stroke_add_ca(ca
);
636 if (starter_pluto_pid())
638 starter_whack_add_ca(ca
);
640 ca
->state
= STATE_ADDED
;
644 for (conn
= cfg
->conn_first
; conn
; conn
= conn
->next
)
646 if (conn
->state
== STATE_TO_ADD
)
650 /* affect new unique id */
653 if (starter_charon_pid())
655 starter_stroke_add_conn(cfg
, conn
);
657 if (starter_pluto_pid())
659 starter_whack_add_conn(conn
);
661 conn
->state
= STATE_ADDED
;
663 if (conn
->startup
== STARTUP_START
)
665 if (conn
->keyexchange
== KEY_EXCHANGE_IKEV2
)
667 if (starter_charon_pid())
669 starter_stroke_initiate_conn(conn
);
674 if (starter_pluto_pid())
676 starter_whack_initiate_conn(conn
);
680 else if (conn
->startup
== STARTUP_ROUTE
)
682 if (conn
->keyexchange
== KEY_EXCHANGE_IKEV2
)
684 if (starter_charon_pid())
686 starter_stroke_route_conn(conn
);
691 if (starter_pluto_pid())
693 starter_whack_route_conn(conn
);
702 * If auto_update activated, when to stop select
706 time_t now
= time(NULL
);
708 tv
.tv_sec
= (now
< last_reload
+ auto_update
)
709 ?
(last_reload
+ auto_update
-now
) : 0;
714 * Wait for something to happen
716 if (select(0, NULL
, NULL
, NULL
, auto_update ?
&tv
: NULL
) == 0)
718 /* timeout -> auto_update */
719 _action_
|= FLAG_ACTION_UPDATE
;
722 exit(LSB_RC_SUCCESS
);