Revert "Warn about manual plugin load directives for pluto/charon"
[strongswan.git] / src / starter / starter.c
1 /* strongSwan IPsec starter
2 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
3 *
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>.
8 *
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
12 * for more details.
13 */
14
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/stat.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <signal.h>
21 #include <unistd.h>
22 #include <sys/time.h>
23 #include <time.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <pwd.h>
28 #include <grp.h>
29
30 #include <freeswan.h>
31 #include <library.h>
32
33 #include "../pluto/constants.h"
34 #include "../pluto/defs.h"
35 #include "../pluto/log.h"
36
37 #include "confread.h"
38 #include "files.h"
39 #include "starterwhack.h"
40 #include "starterstroke.h"
41 #include "invokepluto.h"
42 #include "invokecharon.h"
43 #include "netkey.h"
44 #include "klips.h"
45 #include "cmp.h"
46 #include "interfaces.h"
47
48 /**
49 * Return codes defined by Linux Standard Base Core Specification 3.1
50 * in section 20.2. Init Script Actions
51 */
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 */
60
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
67
68 static unsigned int _action_ = 0;
69
70 static void fsig(int signal)
71 {
72 switch (signal)
73 {
74 case SIGCHLD:
75 {
76 int status, exit_status = 0;
77 pid_t pid;
78 char *name = NULL;
79
80 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
81 {
82 if (pid == starter_pluto_pid())
83 {
84 name = " (Pluto)";
85 }
86 if (pid == starter_charon_pid())
87 {
88 name = " (Charon)";
89 }
90 if (WIFSIGNALED(status))
91 {
92 DBG(DBG_CONTROL,
93 DBG_log("child %d%s has been killed by sig %d\n",
94 pid, name?name:"", WTERMSIG(status))
95 )
96 }
97 else if (WIFSTOPPED(status))
98 {
99 DBG(DBG_CONTROL,
100 DBG_log("child %d%s has been stopped by sig %d\n",
101 pid, name?name:"", WSTOPSIG(status))
102 )
103 }
104 else if (WIFEXITED(status))
105 {
106 exit_status = WEXITSTATUS(status);
107 if (exit_status >= SS_RC_FIRST && exit_status <= SS_RC_LAST)
108 {
109 _action_ = FLAG_ACTION_QUIT;
110 }
111 DBG(DBG_CONTROL,
112 DBG_log("child %d%s has quit (exit code %d)\n",
113 pid, name?name:"", exit_status)
114 )
115 }
116 else
117 {
118 DBG(DBG_CONTROL,
119 DBG_log("child %d%s has quit", pid, name?name:"")
120 )
121 }
122 if (pid == starter_pluto_pid())
123 {
124 starter_pluto_sigchild(pid, exit_status);
125 }
126 if (pid == starter_charon_pid())
127 {
128 starter_charon_sigchild(pid, exit_status);
129 }
130 }
131 }
132 break;
133
134 case SIGPIPE:
135 /** ignore **/
136 break;
137
138 case SIGALRM:
139 _action_ |= FLAG_ACTION_START_PLUTO;
140 _action_ |= FLAG_ACTION_START_CHARON;
141 break;
142
143 case SIGHUP:
144 _action_ |= FLAG_ACTION_UPDATE;
145 break;
146
147 case SIGTERM:
148 case SIGQUIT:
149 case SIGINT:
150 _action_ |= FLAG_ACTION_QUIT;
151 break;
152
153 case SIGUSR1:
154 _action_ |= FLAG_ACTION_RELOAD;
155 _action_ |= FLAG_ACTION_UPDATE;
156 break;
157
158 default:
159 plog("fsig(): unknown signal %d -- investigate", signal);
160 break;
161 }
162 }
163
164 static void generate_selfcert()
165 {
166 struct stat stb;
167
168 /* if ipsec.secrets file is missing then generate RSA default key pair */
169 if (stat(SECRETS_FILE, &stb) != 0)
170 {
171 mode_t oldmask;
172 FILE *f;
173 uid_t uid = 0;
174 gid_t gid = 0;
175
176 #ifdef IPSEC_GROUP
177 {
178 char buf[1024];
179 struct group group, *grp;
180
181 if (getgrnam_r(IPSEC_GROUP, &group, buf, sizeof(buf), &grp) == 0 && grp)
182 {
183 gid = grp->gr_gid;
184 }
185 }
186 #endif
187 #ifdef IPSEC_USER
188 {
189 char buf[1024];
190 struct passwd passwd, *pwp;
191
192 if (getpwnam_r(IPSEC_USER, &passwd, buf, sizeof(buf), &pwp) == 0 && pwp)
193 {
194 uid = pwp->pw_uid;
195 }
196 }
197 #endif
198 setegid(gid);
199 seteuid(uid);
200 ignore_result(system("ipsec scepclient --out pkcs1 --out cert-self --quiet"));
201 seteuid(0);
202 setegid(0);
203
204 /* ipsec.secrets is root readable only */
205 oldmask = umask(0066);
206
207 f = fopen(SECRETS_FILE, "w");
208 if (f)
209 {
210 fprintf(f, "# /etc/ipsec.secrets - strongSwan IPsec secrets file\n");
211 fprintf(f, "\n");
212 fprintf(f, ": RSA myKey.der\n");
213 fclose(f);
214 }
215 ignore_result(chown(SECRETS_FILE, uid, gid));
216 umask(oldmask);
217 }
218 }
219
220 static void usage(char *name)
221 {
222 fprintf(stderr, "Usage: starter [--nofork] [--auto-update <sec>] "
223 "[--debug|--debug-more|--debug-all]\n");
224 exit(LSB_RC_INVALID_ARGUMENT);
225 }
226
227 int main (int argc, char **argv)
228 {
229 starter_config_t *cfg = NULL;
230 starter_config_t *new_cfg;
231 starter_conn_t *conn, *conn2;
232 starter_ca_t *ca, *ca2;
233
234 struct stat stb;
235
236 char *err = NULL;
237 int i;
238 int id = 1;
239 struct timeval tv;
240 unsigned long auto_update = 0;
241 time_t last_reload;
242 bool no_fork = FALSE;
243 bool attach_gdb = FALSE;
244
245 /* global variables defined in log.h */
246 log_to_stderr = TRUE;
247 base_debugging = DBG_NONE;
248
249 library_init(NULL);
250 atexit(library_deinit);
251
252 /* parse command line */
253 for (i = 1; i < argc; i++)
254 {
255 if (streq(argv[i], "--debug"))
256 {
257 base_debugging |= DBG_CONTROL;
258 }
259 else if (streq(argv[i], "--debug-more"))
260 {
261 base_debugging |= DBG_CONTROLMORE;
262 }
263 else if (streq(argv[i], "--debug-all"))
264 {
265 base_debugging |= DBG_ALL;
266 }
267 else if (streq(argv[i], "--nofork"))
268 {
269 no_fork = TRUE;
270 }
271 else if (streq(argv[i], "--attach-gdb"))
272 {
273 no_fork = TRUE;
274 attach_gdb = TRUE;
275 }
276 else if (streq(argv[i], "--auto-update") && i+1 < argc)
277 {
278 auto_update = atoi(argv[++i]);
279 if (!auto_update)
280 usage(argv[0]);
281 }
282 else
283 {
284 usage(argv[0]);
285 }
286 }
287
288 /* Init */
289 init_log("ipsec_starter");
290 cur_debugging = base_debugging;
291
292 signal(SIGHUP, fsig);
293 signal(SIGCHLD, fsig);
294 signal(SIGPIPE, fsig);
295 signal(SIGINT, fsig);
296 signal(SIGTERM, fsig);
297 signal(SIGQUIT, fsig);
298 signal(SIGALRM, fsig);
299 signal(SIGUSR1, fsig);
300
301 plog("Starting strongSwan "VERSION" IPsec [starter]...");
302
303 /* verify that we can start */
304 if (getuid() != 0)
305 {
306 plog("permission denied (must be superuser)");
307 exit(LSB_RC_NOT_ALLOWED);
308 }
309
310 if (stat(PLUTO_PID_FILE, &stb) == 0)
311 {
312 plog("pluto is already running (%s exists) -- skipping pluto start", PLUTO_PID_FILE);
313 }
314 else
315 {
316 _action_ |= FLAG_ACTION_START_PLUTO;
317 }
318 if (stat(CHARON_PID_FILE, &stb) == 0)
319 {
320 plog("charon is already running (%s exists) -- skipping charon start", CHARON_PID_FILE);
321 }
322 else
323 {
324 _action_ |= FLAG_ACTION_START_CHARON;
325 }
326 if (stat(DEV_RANDOM, &stb) != 0)
327 {
328 plog("unable to start strongSwan IPsec -- no %s!", DEV_RANDOM);
329 exit(LSB_RC_FAILURE);
330 }
331
332 if (stat(DEV_URANDOM, &stb)!= 0)
333 {
334 plog("unable to start strongSwan IPsec -- no %s!", DEV_URANDOM);
335 exit(LSB_RC_FAILURE);
336 }
337
338 cfg = confread_load(CONFIG_FILE);
339 if (cfg == NULL || cfg->err > 0)
340 {
341 plog("unable to start strongSwan -- fatal errors in config");
342 if (cfg)
343 {
344 confread_free(cfg);
345 }
346 exit(LSB_RC_INVALID_ARGUMENT);
347 }
348
349 /* determine if we have a native netkey IPsec stack */
350 if (!starter_netkey_init())
351 {
352 plog("no netkey IPsec stack detected");
353 if (!starter_klips_init())
354 {
355 plog("no KLIPS IPsec stack detected");
356 plog("no known IPsec stack detected, ignoring!");
357 }
358 }
359
360 last_reload = time_monotonic(NULL);
361
362 if (stat(STARTER_PID_FILE, &stb) == 0)
363 {
364 plog("starter is already running (%s exists) -- no fork done", STARTER_PID_FILE);
365 confread_free(cfg);
366 exit(LSB_RC_SUCCESS);
367 }
368
369 generate_selfcert();
370
371 /* fork if we're not debugging stuff */
372 if (!no_fork)
373 {
374 log_to_stderr = FALSE;
375
376 switch (fork())
377 {
378 case 0:
379 {
380 int fnull = open("/dev/null", O_RDWR);
381
382 if (fnull >= 0)
383 {
384 dup2(fnull, STDIN_FILENO);
385 dup2(fnull, STDOUT_FILENO);
386 dup2(fnull, STDERR_FILENO);
387 close(fnull);
388 }
389 setsid();
390 }
391 break;
392 case -1:
393 plog("can't fork: %s", strerror(errno));
394 break;
395 default:
396 confread_free(cfg);
397 exit(LSB_RC_SUCCESS);
398 }
399 }
400
401 /* save pid file in /var/run/starter.pid */
402 {
403 FILE *fd = fopen(STARTER_PID_FILE, "w");
404
405 if (fd)
406 {
407 fprintf(fd, "%u\n", getpid());
408 fclose(fd);
409 }
410 }
411
412 for (;;)
413 {
414 /*
415 * Stop pluto/charon (if started) and exit
416 */
417 if (_action_ & FLAG_ACTION_QUIT)
418 {
419 if (starter_pluto_pid())
420 {
421 starter_stop_pluto();
422 }
423 if (starter_charon_pid())
424 {
425 starter_stop_charon();
426 }
427 starter_netkey_cleanup();
428 confread_free(cfg);
429 unlink(STARTER_PID_FILE);
430 unlink(INFO_FILE);
431 plog("ipsec starter stopped");
432 close_log();
433 exit(LSB_RC_SUCCESS);
434 }
435
436 /*
437 * Delete all connections. Will be added below
438 */
439 if (_action_ & FLAG_ACTION_RELOAD)
440 {
441 if (starter_pluto_pid() || starter_charon_pid())
442 {
443 for (conn = cfg->conn_first; conn; conn = conn->next)
444 {
445 if (conn->state == STATE_ADDED)
446 {
447 if (starter_charon_pid())
448 {
449 starter_stroke_del_conn(conn);
450 }
451 if (starter_pluto_pid())
452 {
453 starter_whack_del_conn(conn);
454 }
455 conn->state = STATE_TO_ADD;
456 }
457 }
458 for (ca = cfg->ca_first; ca; ca = ca->next)
459 {
460 if (ca->state == STATE_ADDED)
461 {
462 if (starter_charon_pid())
463 {
464 starter_stroke_del_ca(ca);
465 }
466 if (starter_pluto_pid())
467 {
468 starter_whack_del_ca(ca);
469 }
470 ca->state = STATE_TO_ADD;
471 }
472 }
473 }
474 _action_ &= ~FLAG_ACTION_RELOAD;
475 }
476
477 /*
478 * Update configuration
479 */
480 if (_action_ & FLAG_ACTION_UPDATE)
481 {
482 err = NULL;
483 DBG(DBG_CONTROL,
484 DBG_log("Reloading config...")
485 );
486 new_cfg = confread_load(CONFIG_FILE);
487
488 if (new_cfg && (new_cfg->err + new_cfg->non_fatal_err == 0))
489 {
490 /* Switch to new config. New conn will be loaded below */
491 if (!starter_cmp_defaultroute(&new_cfg->defaultroute
492 , &cfg->defaultroute))
493 {
494 _action_ |= FLAG_ACTION_LISTEN;
495 }
496
497 if (!starter_cmp_pluto(cfg, new_cfg))
498 {
499 plog("Pluto has changed");
500 if (starter_pluto_pid())
501 starter_stop_pluto();
502 _action_ &= ~FLAG_ACTION_LISTEN;
503 _action_ |= FLAG_ACTION_START_PLUTO;
504 }
505 else
506 {
507 /* Only reload conn and ca sections if pluto is not killed */
508
509 /* Look for new connections that are already loaded */
510 for (conn = cfg->conn_first; conn; conn = conn->next)
511 {
512 if (conn->state == STATE_ADDED)
513 {
514 for (conn2 = new_cfg->conn_first; conn2; conn2 = conn2->next)
515 {
516 if (conn2->state == STATE_TO_ADD && starter_cmp_conn(conn, conn2))
517 {
518 conn->state = STATE_REPLACED;
519 conn2->state = STATE_ADDED;
520 conn2->id = conn->id;
521 break;
522 }
523 }
524 }
525 }
526
527 /* Remove conn sections that have become unused */
528 for (conn = cfg->conn_first; conn; conn = conn->next)
529 {
530 if (conn->state == STATE_ADDED)
531 {
532 if (starter_charon_pid())
533 {
534 starter_stroke_del_conn(conn);
535 }
536 if (starter_pluto_pid())
537 {
538 starter_whack_del_conn(conn);
539 }
540 }
541 }
542
543 /* Look for new ca sections that are already loaded */
544 for (ca = cfg->ca_first; ca; ca = ca->next)
545 {
546 if (ca->state == STATE_ADDED)
547 {
548 for (ca2 = new_cfg->ca_first; ca2; ca2 = ca2->next)
549 {
550 if (ca2->state == STATE_TO_ADD && starter_cmp_ca(ca, ca2))
551 {
552 ca->state = STATE_REPLACED;
553 ca2->state = STATE_ADDED;
554 break;
555 }
556 }
557 }
558 }
559
560 /* Remove ca sections that have become unused */
561 for (ca = cfg->ca_first; ca; ca = ca->next)
562 {
563 if (ca->state == STATE_ADDED)
564 {
565 if (starter_charon_pid())
566 {
567 starter_stroke_del_ca(ca);
568 }
569 if (starter_pluto_pid())
570 {
571 starter_whack_del_ca(ca);
572 }
573 }
574 }
575 }
576 confread_free(cfg);
577 cfg = new_cfg;
578 }
579 else
580 {
581 plog("can't reload config file due to errors -- keeping old one");
582 if (new_cfg)
583 {
584 confread_free(new_cfg);
585 }
586 }
587 _action_ &= ~FLAG_ACTION_UPDATE;
588 last_reload = time_monotonic(NULL);
589 }
590
591 /*
592 * Start pluto
593 */
594 if (_action_ & FLAG_ACTION_START_PLUTO)
595 {
596 if (cfg->setup.plutostart && !starter_pluto_pid())
597 {
598 DBG(DBG_CONTROL,
599 DBG_log("Attempting to start pluto...")
600 );
601
602 if (starter_start_pluto(cfg, no_fork, attach_gdb) == 0)
603 {
604 starter_whack_listen();
605 }
606 else
607 {
608 /* schedule next try */
609 alarm(PLUTO_RESTART_DELAY);
610 }
611 }
612 _action_ &= ~FLAG_ACTION_START_PLUTO;
613
614 for (ca = cfg->ca_first; ca; ca = ca->next)
615 {
616 if (ca->state == STATE_ADDED)
617 ca->state = STATE_TO_ADD;
618 }
619
620 for (conn = cfg->conn_first; conn; conn = conn->next)
621 {
622 if (conn->state == STATE_ADDED)
623 conn->state = STATE_TO_ADD;
624 }
625 }
626
627 /*
628 * Start charon
629 */
630 if (_action_ & FLAG_ACTION_START_CHARON)
631 {
632 if (cfg->setup.charonstart && !starter_charon_pid())
633 {
634 DBG(DBG_CONTROL,
635 DBG_log("Attempting to start charon...")
636 );
637 if (starter_start_charon(cfg, no_fork, attach_gdb))
638 {
639 /* schedule next try */
640 alarm(PLUTO_RESTART_DELAY);
641 }
642 starter_stroke_configure(cfg);
643 }
644 _action_ &= ~FLAG_ACTION_START_CHARON;
645 }
646
647 /*
648 * Tell pluto to reread its interfaces
649 */
650 if (_action_ & FLAG_ACTION_LISTEN)
651 {
652 if (starter_pluto_pid())
653 {
654 starter_whack_listen();
655 _action_ &= ~FLAG_ACTION_LISTEN;
656 }
657 }
658
659 /*
660 * Add stale conn and ca sections
661 */
662 if (starter_pluto_pid() || starter_charon_pid())
663 {
664 for (ca = cfg->ca_first; ca; ca = ca->next)
665 {
666 if (ca->state == STATE_TO_ADD)
667 {
668 if (starter_charon_pid())
669 {
670 starter_stroke_add_ca(ca);
671 }
672 if (starter_pluto_pid())
673 {
674 starter_whack_add_ca(ca);
675 }
676 ca->state = STATE_ADDED;
677 }
678 }
679
680 for (conn = cfg->conn_first; conn; conn = conn->next)
681 {
682 if (conn->state == STATE_TO_ADD)
683 {
684 if (conn->id == 0)
685 {
686 /* affect new unique id */
687 conn->id = id++;
688 }
689 if (starter_charon_pid())
690 {
691 starter_stroke_add_conn(cfg, conn);
692 }
693 if (starter_pluto_pid())
694 {
695 starter_whack_add_conn(conn);
696 }
697 conn->state = STATE_ADDED;
698
699 if (conn->startup == STARTUP_START)
700 {
701 if (conn->keyexchange == KEY_EXCHANGE_IKEV2)
702 {
703 if (starter_charon_pid())
704 {
705 starter_stroke_initiate_conn(conn);
706 }
707 }
708 else
709 {
710 if (starter_pluto_pid())
711 {
712 starter_whack_initiate_conn(conn);
713 }
714 }
715 }
716 else if (conn->startup == STARTUP_ROUTE)
717 {
718 if (conn->keyexchange == KEY_EXCHANGE_IKEV2)
719 {
720 if (starter_charon_pid())
721 {
722 starter_stroke_route_conn(conn);
723 }
724 }
725 else
726 {
727 if (starter_pluto_pid())
728 {
729 starter_whack_route_conn(conn);
730 }
731 }
732 }
733 }
734 }
735 }
736
737 /*
738 * If auto_update activated, when to stop select
739 */
740 if (auto_update)
741 {
742 time_t now = time_monotonic(NULL);
743
744 tv.tv_sec = (now < last_reload + auto_update)
745 ? (last_reload + auto_update-now) : 0;
746 tv.tv_usec = 0;
747 }
748
749 /*
750 * Wait for something to happen
751 */
752 if (select(0, NULL, NULL, NULL, auto_update ? &tv : NULL) == 0)
753 {
754 /* timeout -> auto_update */
755 _action_ |= FLAG_ACTION_UPDATE;
756 }
757 }
758 exit(LSB_RC_SUCCESS);
759 }
760