load and unload XAUTH module
[strongswan.git] / src / pluto / plutomain.c
1 /* Pluto main program
2 * Copyright (C) 1997 Angelos D. Keromytis.
3 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * RCSID $Id: plutomain.c,v 1.16 2005/09/25 21:30:52 as Exp $
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/un.h>
27 #include <fcntl.h>
28 #include <getopt.h>
29 #include <resolv.h>
30 #include <arpa/nameser.h> /* missing from <resolv.h> on old systems */
31 #include <sys/queue.h>
32
33 #include <freeswan.h>
34
35 #include <pfkeyv2.h>
36 #include <pfkey.h>
37
38 #include "constants.h"
39 #include "defs.h"
40 #include "id.h"
41 #include "ca.h"
42 #include "certs.h"
43 #include "ac.h"
44 #include "connections.h"
45 #include "foodgroups.h"
46 #include "packet.h"
47 #include "demux.h" /* needs packet.h */
48 #include "server.h"
49 #include "kernel.h"
50 #include "log.h"
51 #include "keys.h"
52 #include "adns.h" /* needs <resolv.h> */
53 #include "dnskey.h" /* needs keys.h and adns.h */
54 #include "rnd.h"
55 #include "state.h"
56 #include "ipsec_doi.h" /* needs demux.h and state.h */
57 #include "ocsp.h"
58 #include "crl.h"
59 #include "fetch.h"
60 #include "sha1.h"
61 #include "md5.h"
62 #include "crypto.h" /* requires sha1.h and md5.h */
63 #include "nat_traversal.h"
64 #include "virtual.h"
65
66 static void
67 usage(const char *mess)
68 {
69 if (mess != NULL && *mess != '\0')
70 fprintf(stderr, "%s\n", mess);
71 fprintf(stderr
72 , "Usage: pluto"
73 " [--help]"
74 " [--version]"
75 " [--optionsfrom <filename>]"
76 " \\\n\t"
77 "[--nofork]"
78 " [--stderrlog]"
79 " [--noklips]"
80 " [--nocrsend]"
81 " \\\n\t"
82 "[--strictcrlpolicy]"
83 " [--crlcheckinterval]"
84 " [--cachecrls]"
85 " [--uniqueids]"
86 " \\\n\t"
87 "[--interface <ifname>]"
88 " [--ikeport <port-number>]"
89 " \\\n\t"
90 "[--ctlbase <path>]"
91 " \\\n\t"
92 "[--perpeerlogbase <path>] [--perpeerlog]"
93 " \\\n\t"
94 "[--secretsfile <secrets-file>]"
95 " [--policygroupsdir <policygroups-dir>]"
96 " \\\n\t"
97 "[--adns <pathname>]"
98 "[--pkcs11module <path>]"
99 "[--pkcs11keepstate"
100 #ifdef DEBUG
101 " \\\n\t"
102 "[--debug-none]"
103 " [--debug-all]"
104 " \\\n\t"
105 "[--debug-raw]"
106 " [--debug-crypt]"
107 " [--debug-parsing]"
108 " [--debug-emitting]"
109 " \\\n\t"
110 "[--debug-control]"
111 " [--debug-lifecycle]"
112 " [--debug-klips]"
113 " [--debug-dns]"
114 " \\\n\t"
115 "[--debug-oppo]"
116 " [--debug-controlmore]"
117 " [--debug-private]"
118 #endif
119 " [ --debug-natt]"
120 " \\\n\t"
121 "[--nat_traversal] [--keep_alive <delay_sec>]"
122 " \\\n\t"
123 "[--force_keepalive] [--disable_port_floating]"
124 " \\\n\t"
125 "[--virtual_private <network_list>]"
126 "\n"
127 "strongSwan %s\n"
128 , ipsec_version_code());
129 exit_pluto(mess == NULL? 0 : 1);
130 }
131
132
133 /* lock file support
134 * - provides convenient way for scripts to find Pluto's pid
135 * - prevents multiple Plutos competing for the same port
136 * - same basename as unix domain control socket
137 * NOTE: will not take account of sharing LOCK_DIR with other systems.
138 */
139
140 static char pluto_lock[sizeof(ctl_addr.sun_path)] = DEFAULT_CTLBASE LOCK_SUFFIX;
141 static bool pluto_lock_created = FALSE;
142
143 /* create lockfile, or die in the attempt */
144 static int
145 create_lock(void)
146 {
147 int fd = open(pluto_lock, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC
148 , S_IRUSR | S_IRGRP | S_IROTH);
149
150 if (fd < 0)
151 {
152 if (errno == EEXIST)
153 {
154 fprintf(stderr, "pluto: lock file \"%s\" already exists\n"
155 , pluto_lock);
156 exit_pluto(10);
157 }
158 else
159 {
160 fprintf(stderr
161 , "pluto: unable to create lock file \"%s\" (%d %s)\n"
162 , pluto_lock, errno, strerror(errno));
163 exit_pluto(1);
164 }
165 }
166 pluto_lock_created = TRUE;
167 return fd;
168 }
169
170 static bool
171 fill_lock(int lockfd, pid_t pid)
172 {
173 char buf[30]; /* holds "<pid>\n" */
174 int len = snprintf(buf, sizeof(buf), "%u\n", (unsigned int) pid);
175 bool ok = len > 0 && write(lockfd, buf, len) == len;
176
177 close(lockfd);
178 return ok;
179 }
180
181 static void
182 delete_lock(void)
183 {
184 if (pluto_lock_created)
185 {
186 delete_ctl_socket();
187 unlink(pluto_lock); /* is noting failure useful? */
188 }
189 }
190
191 /* by default pluto sends certificate requests to its peers */
192 bool no_cr_send = FALSE;
193
194 /* by default the CRL policy is lenient */
195 bool strict_crl_policy = FALSE;
196
197 /* by default CRLs are cached locally as files */
198 bool cache_crls = FALSE;
199
200 /* by default pluto does not check crls dynamically */
201 long crl_check_interval = 0;
202
203 /* path to the PKCS#11 module */
204 char *pkcs11_module_path = NULL;
205
206 /* by default pluto logs out after every smartcard use */
207 bool pkcs11_keep_state = FALSE;
208
209 /* by default pluto does not allow pkcs11 proxy access via whack */
210 bool pkcs11_proxy = FALSE;
211
212 int
213 main(int argc, char **argv)
214 {
215 bool fork_desired = TRUE;
216 bool log_to_stderr_desired = FALSE;
217 bool nat_traversal = FALSE;
218 bool nat_t_spf = TRUE; /* support port floating */
219 unsigned int keep_alive = 0;
220 bool force_keepalive = FALSE;
221 char *virtual_private = NULL;
222 int lockfd;
223
224 /* handle arguments */
225 for (;;)
226 {
227 # define DBG_OFFSET 256
228 static const struct option long_opts[] = {
229 /* name, has_arg, flag, val */
230 { "help", no_argument, NULL, 'h' },
231 { "version", no_argument, NULL, 'v' },
232 { "optionsfrom", required_argument, NULL, '+' },
233 { "nofork", no_argument, NULL, 'd' },
234 { "stderrlog", no_argument, NULL, 'e' },
235 { "noklips", no_argument, NULL, 'n' },
236 { "nocrsend", no_argument, NULL, 'c' },
237 { "strictcrlpolicy", no_argument, NULL, 'r' },
238 { "crlcheckinterval", required_argument, NULL, 'x'},
239 { "cachecrls", no_argument, NULL, 'C' },
240 { "uniqueids", no_argument, NULL, 'u' },
241 { "interface", required_argument, NULL, 'i' },
242 { "ikeport", required_argument, NULL, 'p' },
243 { "ctlbase", required_argument, NULL, 'b' },
244 { "secretsfile", required_argument, NULL, 's' },
245 { "foodgroupsdir", required_argument, NULL, 'f' },
246 { "perpeerlogbase", required_argument, NULL, 'P' },
247 { "perpeerlog", no_argument, NULL, 'l' },
248 { "policygroupsdir", required_argument, NULL, 'f' },
249 #ifdef USE_LWRES
250 { "lwdnsq", required_argument, NULL, 'a' },
251 #else /* !USE_LWRES */
252 { "adns", required_argument, NULL, 'a' },
253 #endif /* !USE_LWRES */
254 { "pkcs11module", required_argument, NULL, 'm' },
255 { "pkcs11keepstate", no_argument, NULL, 'k' },
256 { "pkcs11proxy", no_argument, NULL, 'y' },
257 { "nat_traversal", no_argument, NULL, '1' },
258 { "keep_alive", required_argument, NULL, '2' },
259 { "force_keepalive", no_argument, NULL, '3' },
260 { "disable_port_floating", no_argument, NULL, '4' },
261 { "debug-natt", no_argument, NULL, '5' },
262 { "virtual_private", required_argument, NULL, '6' },
263 #ifdef DEBUG
264 { "debug-none", no_argument, NULL, 'N' },
265 { "debug-all", no_argument, NULL, 'A' },
266 { "debug-raw", no_argument, NULL, DBG_RAW + DBG_OFFSET },
267 { "debug-crypt", no_argument, NULL, DBG_CRYPT + DBG_OFFSET },
268 { "debug-parsing", no_argument, NULL, DBG_PARSING + DBG_OFFSET },
269 { "debug-emitting", no_argument, NULL, DBG_EMITTING + DBG_OFFSET },
270 { "debug-control", no_argument, NULL, DBG_CONTROL + DBG_OFFSET },
271 { "debug-lifecycle", no_argument, NULL, DBG_LIFECYCLE + DBG_OFFSET },
272 { "debug-klips", no_argument, NULL, DBG_KLIPS + DBG_OFFSET },
273 { "debug-dns", no_argument, NULL, DBG_DNS + DBG_OFFSET },
274 { "debug-oppo", no_argument, NULL, DBG_OPPO + DBG_OFFSET },
275 { "debug-controlmore", no_argument, NULL, DBG_CONTROLMORE + DBG_OFFSET },
276 { "debug-private", no_argument, NULL, DBG_PRIVATE + DBG_OFFSET },
277
278 { "impair-delay-adns-key-answer", no_argument, NULL, IMPAIR_DELAY_ADNS_KEY_ANSWER + DBG_OFFSET },
279 { "impair-delay-adns-txt-answer", no_argument, NULL, IMPAIR_DELAY_ADNS_TXT_ANSWER + DBG_OFFSET },
280 { "impair-bust-mi2", no_argument, NULL, IMPAIR_BUST_MI2 + DBG_OFFSET },
281 { "impair-bust-mr2", no_argument, NULL, IMPAIR_BUST_MR2 + DBG_OFFSET },
282 #endif
283 { 0,0,0,0 }
284 };
285 /* Note: we don't like the way short options get parsed
286 * by getopt_long, so we simply pass an empty string as
287 * the list. It could be "hvdenp:l:s:" "NARXPECK".
288 */
289 int c = getopt_long(argc, argv, "", long_opts, NULL);
290
291 /* Note: "breaking" from case terminates loop */
292 switch (c)
293 {
294 case EOF: /* end of flags */
295 break;
296
297 case 0: /* long option already handled */
298 continue;
299
300 case ':': /* diagnostic already printed by getopt_long */
301 case '?': /* diagnostic already printed by getopt_long */
302 usage("");
303 break; /* not actually reached */
304
305 case 'h': /* --help */
306 usage(NULL);
307 break; /* not actually reached */
308
309 case 'v': /* --version */
310 {
311 const char **sp = ipsec_copyright_notice();
312
313 printf("%s%s\n", ipsec_version_string(),
314 compile_time_interop_options);
315 for (; *sp != NULL; sp++)
316 puts(*sp);
317 }
318 exit_pluto(0);
319 break; /* not actually reached */
320
321 case '+': /* --optionsfrom <filename> */
322 optionsfrom(optarg, &argc, &argv, optind, stderr);
323 /* does not return on error */
324 continue;
325
326 case 'd': /* --nofork*/
327 fork_desired = FALSE;
328 continue;
329
330 case 'e': /* --stderrlog */
331 log_to_stderr_desired = TRUE;
332 continue;
333
334 case 'n': /* --noklips */
335 no_klips = TRUE;
336 continue;
337
338 case 'c': /* --nocrsend */
339 no_cr_send = TRUE;
340 continue;
341
342 case 'r': /* --strictcrlpolicy */
343 strict_crl_policy = TRUE;
344 continue;
345
346 case 'x': /* --crlcheckinterval <time>*/
347 if (optarg == NULL || !isdigit(optarg[0]))
348 usage("missing interval time");
349
350 {
351 char *endptr;
352 long interval = strtol(optarg, &endptr, 0);
353
354 if (*endptr != '\0' || endptr == optarg
355 || interval <= 0)
356 usage("<interval-time> must be a positive number");
357 crl_check_interval = interval;
358 }
359 continue;
360
361 case 'C': /* --cachecrls */
362 cache_crls = TRUE;
363 continue;
364
365 case 'u': /* --uniqueids */
366 uniqueIDs = TRUE;
367 continue;
368
369 case 'i': /* --interface <ifname> */
370 if (!use_interface(optarg))
371 usage("too many --interface specifications");
372 continue;
373
374 case 'p': /* --port <portnumber> */
375 if (optarg == NULL || !isdigit(optarg[0]))
376 usage("missing port number");
377
378 {
379 char *endptr;
380 long port = strtol(optarg, &endptr, 0);
381
382 if (*endptr != '\0' || endptr == optarg
383 || port <= 0 || port > 0x10000)
384 usage("<port-number> must be a number between 1 and 65535");
385 pluto_port = port;
386 }
387 continue;
388
389 case 'b': /* --ctlbase <path> */
390 if (snprintf(ctl_addr.sun_path, sizeof(ctl_addr.sun_path)
391 , "%s%s", optarg, CTL_SUFFIX) == -1)
392 usage("<path>" CTL_SUFFIX " too long for sun_path");
393 if (snprintf(info_addr.sun_path, sizeof(info_addr.sun_path)
394 , "%s%s", optarg, INFO_SUFFIX) == -1)
395 usage("<path>" INFO_SUFFIX " too long for sun_path");
396 if (snprintf(pluto_lock, sizeof(pluto_lock)
397 , "%s%s", optarg, LOCK_SUFFIX) == -1)
398 usage("<path>" LOCK_SUFFIX " must fit");
399 continue;
400
401 case 's': /* --secretsfile <secrets-file> */
402 shared_secrets_file = optarg;
403 continue;
404
405 case 'f': /* --policygroupsdir <policygroups-dir> */
406 policygroups_dir = optarg;
407 continue;
408
409 case 'a': /* --adns <pathname> */
410 pluto_adns_option = optarg;
411 continue;
412
413 case 'm': /* --pkcs11module <pathname> */
414 pkcs11_module_path = optarg;
415 continue;
416
417 case 'k': /* --pkcs11keepstate */
418 pkcs11_keep_state = TRUE;
419 continue;
420
421 case 'y': /* --pkcs11proxy */
422 pkcs11_proxy = TRUE;
423 continue;
424
425 #ifdef DEBUG
426 case 'N': /* --debug-none */
427 base_debugging = DBG_NONE;
428 continue;
429
430 case 'A': /* --debug-all */
431 base_debugging = DBG_ALL;
432 continue;
433 #endif
434
435 case 'P': /* --perpeerlogbase */
436 base_perpeer_logdir = optarg;
437 continue;
438
439 case 'l':
440 log_to_perpeer = TRUE;
441 continue;
442
443 case '1': /* --nat_traversal */
444 nat_traversal = TRUE;
445 continue;
446 case '2': /* --keep_alive */
447 keep_alive = atoi(optarg);
448 continue;
449 case '3': /* --force_keepalive */
450 force_keepalive = TRUE;
451 continue;
452 case '4': /* --disable_port_floating */
453 nat_t_spf = FALSE;
454 continue;
455 case '5': /* --debug-nat_t */
456 base_debugging |= DBG_NATT;
457 continue;
458 case '6': /* --virtual_private */
459 virtual_private = optarg;
460 continue;
461
462 default:
463 #ifdef DEBUG
464 if (c >= DBG_OFFSET)
465 {
466 base_debugging |= c - DBG_OFFSET;
467 continue;
468 }
469 # undef DBG_OFFSET
470 #endif
471 bad_case(c);
472 }
473 break;
474 }
475 if (optind != argc)
476 usage("unexpected argument");
477 reset_debugging();
478 lockfd = create_lock();
479
480 /* select between logging methods */
481
482 if (log_to_stderr_desired)
483 log_to_syslog = FALSE;
484 else
485 log_to_stderr = FALSE;
486
487 /* set the logging function of pfkey debugging */
488 #ifdef DEBUG
489 pfkey_debug_func = DBG_log;
490 #else
491 pfkey_debug_func = NULL;
492 #endif
493
494 /* create control socket.
495 * We must create it before the parent process returns so that
496 * there will be no race condition in using it. The easiest
497 * place to do this is before the daemon fork.
498 */
499 {
500 err_t ugh = init_ctl_socket();
501
502 if (ugh != NULL)
503 {
504 fprintf(stderr, "pluto: %s", ugh);
505 exit_pluto(1);
506 }
507 }
508
509 #ifdef IPSECPOLICY
510 /* create info socket. */
511 {
512 err_t ugh = init_info_socket();
513
514 if (ugh != NULL)
515 {
516 fprintf(stderr, "pluto: %s", ugh);
517 exit_pluto(1);
518 }
519 }
520 #endif
521
522 /* If not suppressed, do daemon fork */
523
524 if (fork_desired)
525 {
526 {
527 pid_t pid = fork();
528
529 if (pid < 0)
530 {
531 int e = errno;
532
533 fprintf(stderr, "pluto: fork failed (%d %s)\n",
534 errno, strerror(e));
535 exit_pluto(1);
536 }
537
538 if (pid != 0)
539 {
540 /* parent: die, after filling PID into lock file.
541 * must not use exit_pluto: lock would be removed!
542 */
543 exit(fill_lock(lockfd, pid)? 0 : 1);
544 }
545 }
546
547 if (setsid() < 0)
548 {
549 int e = errno;
550
551 fprintf(stderr, "setsid() failed in main(). Errno %d: %s\n",
552 errno, strerror(e));
553 exit_pluto(1);
554 }
555 }
556 else
557 {
558 /* no daemon fork: we have to fill in lock file */
559 (void) fill_lock(lockfd, getpid());
560 fprintf(stdout, "Pluto initialized\n");
561 fflush(stdout);
562 }
563
564 /* Close everything but ctl_fd and (if needed) stderr.
565 * There is some danger that a library that we don't know
566 * about is using some fd that we don't know about.
567 * I guess we'll soon find out.
568 */
569 {
570 int i;
571
572 for (i = getdtablesize() - 1; i >= 0; i--) /* Bad hack */
573 if ((!log_to_stderr || i != 2)
574 #ifdef IPSECPOLICY
575 && i != info_fd
576 #endif
577 && i != ctl_fd)
578 close(i);
579
580 /* make sure that stdin, stdout, stderr are reserved */
581 if (open("/dev/null", O_RDONLY) != 0)
582 abort();
583 if (dup2(0, 1) != 1)
584 abort();
585 if (!log_to_stderr && dup2(0, 2) != 2)
586 abort();
587 }
588
589 init_constants();
590 init_log("pluto");
591
592 /* Note: some scripts may look for this exact message -- don't change
593 * ipsec barf was one, but it no longer does.
594 */
595 plog("Starting Pluto (strongSwan Version %s%s)"
596 , ipsec_version_code()
597 , compile_time_interop_options);
598
599 init_nat_traversal(nat_traversal, keep_alive, force_keepalive, nat_t_spf);
600 init_virtual_ip(virtual_private);
601 scx_init(pkcs11_module_path); /* load and initialize PKCS #11 module */
602 xauth_init(); /* load and initialize XAUTH module */
603 init_rnd_pool();
604 init_secret();
605 init_states();
606 init_crypto();
607 init_demux();
608 init_kernel();
609 init_adns();
610 init_id();
611 init_fetch();
612
613 /* loading X.509 CA certificates */
614 load_authcerts("CA cert", CA_CERT_PATH, AUTH_CA);
615 /* loading X.509 AA certificates */
616 load_authcerts("AA cert", AA_CERT_PATH, AUTH_AA);
617 /* loading X.509 OCSP certificates */
618 load_authcerts("OCSP cert", OCSP_CERT_PATH, AUTH_OCSP);
619 /* loading X.509 CRLs */
620 load_crls();
621 /* loading attribute certificates (experimental) */
622 load_acerts();
623
624 daily_log_event();
625 call_server();
626 return -1; /* Shouldn't ever reach this */
627 }
628
629 /* leave pluto, with status.
630 * Once child is launched, parent must not exit this way because
631 * the lock would be released.
632 *
633 * 0 OK
634 * 1 general discomfort
635 * 10 lock file exists
636 */
637 void
638 exit_pluto(int status)
639 {
640 reset_globals(); /* needed because we may be called in odd state */
641 free_preshared_secrets();
642 free_remembered_public_keys();
643 delete_every_connection();
644 free_crl_fetch(); /* free chain of crl fetch requests */
645 free_ocsp_fetch(); /* free chain of ocsp fetch requests */
646 free_authcerts(); /* free chain of X.509 authority certificates */
647 free_crls(); /* free chain of X.509 CRLs */
648 free_acerts(); /* free chain of X.509 attribute certificates */
649 free_ca_infos(); /* free chain of X.509 CA information records */
650 free_ocsp(); /* free ocsp cache */
651 free_ifaces();
652 scx_finalize(); /* finalize and unload PKCS #11 module */
653 xauth_finalize(); /* finalize and unload XAUTH module */
654 stop_adns();
655 free_md_pool();
656 delete_lock();
657 #ifdef LEAK_DETECTIVE
658 report_leaks();
659 #endif /* LEAK_DETECTIVE */
660 close_log();
661 exit(status);
662 }
663
664 /*
665 * Local Variables:
666 * c-basic-offset:4
667 * c-style: pluto
668 * End:
669 */