2 * Copyright (C) 1997 Angelos D. Keromytis.
3 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
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>.
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
15 * RCSID $Id: plutomain.c,v 1.16 2005/09/25 21:30:52 as Exp $
24 #include <sys/types.h>
30 #include <arpa/nameser.h> /* missing from <resolv.h> on old systems */
31 #include <sys/queue.h>
38 #include "constants.h"
44 #include "connections.h"
45 #include "foodgroups.h"
47 #include "demux.h" /* needs packet.h */
52 #include "adns.h" /* needs <resolv.h> */
53 #include "dnskey.h" /* needs keys.h and adns.h */
56 #include "ipsec_doi.h" /* needs demux.h and state.h */
63 #include "crypto.h" /* requires sha1.h and md5.h */
64 #include "nat_traversal.h"
68 usage(const char *mess
)
70 if (mess
!= NULL
&& *mess
!= '\0')
71 fprintf(stderr
, "%s\n", mess
);
76 " [--optionsfrom <filename>]"
84 " [--crlcheckinterval]"
88 "[--interface <ifname>]"
89 " [--ikeport <port-number>]"
93 "[--perpeerlogbase <path>] [--perpeerlog]"
95 "[--secretsfile <secrets-file>]"
96 " [--policygroupsdir <policygroups-dir>]"
99 "[--pkcs11module <path>]"
109 " [--debug-emitting]"
112 " [--debug-lifecycle]"
117 " [--debug-controlmore]"
122 "[--nat_traversal] [--keep_alive <delay_sec>]"
124 "[--force_keepalive] [--disable_port_floating]"
126 "[--virtual_private <network_list>]"
129 , ipsec_version_code());
130 exit_pluto(mess
== NULL?
0 : 1);
135 * - provides convenient way for scripts to find Pluto's pid
136 * - prevents multiple Plutos competing for the same port
137 * - same basename as unix domain control socket
138 * NOTE: will not take account of sharing LOCK_DIR with other systems.
141 static char pluto_lock
[sizeof(ctl_addr
.sun_path
)] = DEFAULT_CTLBASE LOCK_SUFFIX
;
142 static bool pluto_lock_created
= FALSE
;
144 /* create lockfile, or die in the attempt */
148 int fd
= open(pluto_lock
, O_WRONLY
| O_CREAT
| O_EXCL
| O_TRUNC
149 , S_IRUSR
| S_IRGRP
| S_IROTH
);
155 fprintf(stderr
, "pluto: lock file \"%s\" already exists\n"
162 , "pluto: unable to create lock file \"%s\" (%d %s)\n"
163 , pluto_lock
, errno
, strerror(errno
));
167 pluto_lock_created
= TRUE
;
172 fill_lock(int lockfd
, pid_t pid
)
174 char buf
[30]; /* holds "<pid>\n" */
175 int len
= snprintf(buf
, sizeof(buf
), "%u\n", (unsigned int) pid
);
176 bool ok
= len
> 0 && write(lockfd
, buf
, len
) == len
;
185 if (pluto_lock_created
)
188 unlink(pluto_lock
); /* is noting failure useful? */
192 /* by default pluto sends certificate requests to its peers */
193 bool no_cr_send
= FALSE
;
195 /* by default the CRL policy is lenient */
196 bool strict_crl_policy
= FALSE
;
198 /* by default CRLs are cached locally as files */
199 bool cache_crls
= FALSE
;
201 /* by default pluto does not check crls dynamically */
202 long crl_check_interval
= 0;
204 /* path to the PKCS#11 module */
205 char *pkcs11_module_path
= NULL
;
207 /* by default pluto logs out after every smartcard use */
208 bool pkcs11_keep_state
= FALSE
;
210 /* by default pluto does not allow pkcs11 proxy access via whack */
211 bool pkcs11_proxy
= FALSE
;
214 main(int argc
, char **argv
)
216 bool fork_desired
= TRUE
;
217 bool log_to_stderr_desired
= FALSE
;
218 bool nat_traversal
= FALSE
;
219 bool nat_t_spf
= TRUE
; /* support port floating */
220 unsigned int keep_alive
= 0;
221 bool force_keepalive
= FALSE
;
222 char *virtual_private
= NULL
;
225 /* handle arguments */
228 # define DBG_OFFSET 256
229 static const struct option long_opts
[] = {
230 /* name, has_arg, flag, val */
231 { "help", no_argument
, NULL
, 'h' },
232 { "version", no_argument
, NULL
, 'v' },
233 { "optionsfrom", required_argument
, NULL
, '+' },
234 { "nofork", no_argument
, NULL
, 'd' },
235 { "stderrlog", no_argument
, NULL
, 'e' },
236 { "noklips", no_argument
, NULL
, 'n' },
237 { "nocrsend", no_argument
, NULL
, 'c' },
238 { "strictcrlpolicy", no_argument
, NULL
, 'r' },
239 { "crlcheckinterval", required_argument
, NULL
, 'x'},
240 { "cachecrls", no_argument
, NULL
, 'C' },
241 { "uniqueids", no_argument
, NULL
, 'u' },
242 { "interface", required_argument
, NULL
, 'i' },
243 { "ikeport", required_argument
, NULL
, 'p' },
244 { "ctlbase", required_argument
, NULL
, 'b' },
245 { "secretsfile", required_argument
, NULL
, 's' },
246 { "foodgroupsdir", required_argument
, NULL
, 'f' },
247 { "perpeerlogbase", required_argument
, NULL
, 'P' },
248 { "perpeerlog", no_argument
, NULL
, 'l' },
249 { "policygroupsdir", required_argument
, NULL
, 'f' },
251 { "lwdnsq", required_argument
, NULL
, 'a' },
252 #else /* !USE_LWRES */
253 { "adns", required_argument
, NULL
, 'a' },
254 #endif /* !USE_LWRES */
255 { "pkcs11module", required_argument
, NULL
, 'm' },
256 { "pkcs11keepstate", no_argument
, NULL
, 'k' },
257 { "pkcs11proxy", no_argument
, NULL
, 'y' },
258 { "nat_traversal", no_argument
, NULL
, '1' },
259 { "keep_alive", required_argument
, NULL
, '2' },
260 { "force_keepalive", no_argument
, NULL
, '3' },
261 { "disable_port_floating", no_argument
, NULL
, '4' },
262 { "debug-natt", no_argument
, NULL
, '5' },
263 { "virtual_private", required_argument
, NULL
, '6' },
265 { "debug-none", no_argument
, NULL
, 'N' },
266 { "debug-all", no_argument
, NULL
, 'A' },
267 { "debug-raw", no_argument
, NULL
, DBG_RAW
+ DBG_OFFSET
},
268 { "debug-crypt", no_argument
, NULL
, DBG_CRYPT
+ DBG_OFFSET
},
269 { "debug-parsing", no_argument
, NULL
, DBG_PARSING
+ DBG_OFFSET
},
270 { "debug-emitting", no_argument
, NULL
, DBG_EMITTING
+ DBG_OFFSET
},
271 { "debug-control", no_argument
, NULL
, DBG_CONTROL
+ DBG_OFFSET
},
272 { "debug-lifecycle", no_argument
, NULL
, DBG_LIFECYCLE
+ DBG_OFFSET
},
273 { "debug-klips", no_argument
, NULL
, DBG_KLIPS
+ DBG_OFFSET
},
274 { "debug-dns", no_argument
, NULL
, DBG_DNS
+ DBG_OFFSET
},
275 { "debug-oppo", no_argument
, NULL
, DBG_OPPO
+ DBG_OFFSET
},
276 { "debug-controlmore", no_argument
, NULL
, DBG_CONTROLMORE
+ DBG_OFFSET
},
277 { "debug-private", no_argument
, NULL
, DBG_PRIVATE
+ DBG_OFFSET
},
279 { "impair-delay-adns-key-answer", no_argument
, NULL
, IMPAIR_DELAY_ADNS_KEY_ANSWER
+ DBG_OFFSET
},
280 { "impair-delay-adns-txt-answer", no_argument
, NULL
, IMPAIR_DELAY_ADNS_TXT_ANSWER
+ DBG_OFFSET
},
281 { "impair-bust-mi2", no_argument
, NULL
, IMPAIR_BUST_MI2
+ DBG_OFFSET
},
282 { "impair-bust-mr2", no_argument
, NULL
, IMPAIR_BUST_MR2
+ DBG_OFFSET
},
286 /* Note: we don't like the way short options get parsed
287 * by getopt_long, so we simply pass an empty string as
288 * the list. It could be "hvdenp:l:s:" "NARXPECK".
290 int c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
292 /* Note: "breaking" from case terminates loop */
295 case EOF
: /* end of flags */
298 case 0: /* long option already handled */
301 case ':': /* diagnostic already printed by getopt_long */
302 case '?': /* diagnostic already printed by getopt_long */
304 break; /* not actually reached */
306 case 'h': /* --help */
308 break; /* not actually reached */
310 case 'v': /* --version */
312 const char **sp
= ipsec_copyright_notice();
314 printf("%s%s\n", ipsec_version_string(),
315 compile_time_interop_options
);
316 for (; *sp
!= NULL
; sp
++)
320 break; /* not actually reached */
322 case '+': /* --optionsfrom <filename> */
323 optionsfrom(optarg
, &argc
, &argv
, optind
, stderr
);
324 /* does not return on error */
327 case 'd': /* --nofork*/
328 fork_desired
= FALSE
;
331 case 'e': /* --stderrlog */
332 log_to_stderr_desired
= TRUE
;
335 case 'n': /* --noklips */
339 case 'c': /* --nocrsend */
343 case 'r': /* --strictcrlpolicy */
344 strict_crl_policy
= TRUE
;
347 case 'x': /* --crlcheckinterval <time>*/
348 if (optarg
== NULL
|| !isdigit(optarg
[0]))
349 usage("missing interval time");
353 long interval
= strtol(optarg
, &endptr
, 0);
355 if (*endptr
!= '\0' || endptr
== optarg
357 usage("<interval-time> must be a positive number");
358 crl_check_interval
= interval
;
362 case 'C': /* --cachecrls */
366 case 'u': /* --uniqueids */
370 case 'i': /* --interface <ifname> */
371 if (!use_interface(optarg
))
372 usage("too many --interface specifications");
375 case 'p': /* --port <portnumber> */
376 if (optarg
== NULL
|| !isdigit(optarg
[0]))
377 usage("missing port number");
381 long port
= strtol(optarg
, &endptr
, 0);
383 if (*endptr
!= '\0' || endptr
== optarg
384 || port
<= 0 || port
> 0x10000)
385 usage("<port-number> must be a number between 1 and 65535");
390 case 'b': /* --ctlbase <path> */
391 if (snprintf(ctl_addr
.sun_path
, sizeof(ctl_addr
.sun_path
)
392 , "%s%s", optarg
, CTL_SUFFIX
) == -1)
393 usage("<path>" CTL_SUFFIX
" too long for sun_path");
394 if (snprintf(info_addr
.sun_path
, sizeof(info_addr
.sun_path
)
395 , "%s%s", optarg
, INFO_SUFFIX
) == -1)
396 usage("<path>" INFO_SUFFIX
" too long for sun_path");
397 if (snprintf(pluto_lock
, sizeof(pluto_lock
)
398 , "%s%s", optarg
, LOCK_SUFFIX
) == -1)
399 usage("<path>" LOCK_SUFFIX
" must fit");
402 case 's': /* --secretsfile <secrets-file> */
403 shared_secrets_file
= optarg
;
406 case 'f': /* --policygroupsdir <policygroups-dir> */
407 policygroups_dir
= optarg
;
410 case 'a': /* --adns <pathname> */
411 pluto_adns_option
= optarg
;
414 case 'm': /* --pkcs11module <pathname> */
415 pkcs11_module_path
= optarg
;
418 case 'k': /* --pkcs11keepstate */
419 pkcs11_keep_state
= TRUE
;
422 case 'y': /* --pkcs11proxy */
427 case 'N': /* --debug-none */
428 base_debugging
= DBG_NONE
;
431 case 'A': /* --debug-all */
432 base_debugging
= DBG_ALL
;
436 case 'P': /* --perpeerlogbase */
437 base_perpeer_logdir
= optarg
;
441 log_to_perpeer
= TRUE
;
444 case '1': /* --nat_traversal */
445 nat_traversal
= TRUE
;
447 case '2': /* --keep_alive */
448 keep_alive
= atoi(optarg
);
450 case '3': /* --force_keepalive */
451 force_keepalive
= TRUE
;
453 case '4': /* --disable_port_floating */
456 case '5': /* --debug-nat_t */
457 base_debugging
|= DBG_NATT
;
459 case '6': /* --virtual_private */
460 virtual_private
= optarg
;
467 base_debugging
|= c
- DBG_OFFSET
;
477 usage("unexpected argument");
479 lockfd
= create_lock();
481 /* select between logging methods */
483 if (log_to_stderr_desired
)
484 log_to_syslog
= FALSE
;
486 log_to_stderr
= FALSE
;
488 /* set the logging function of pfkey debugging */
490 pfkey_debug_func
= DBG_log
;
492 pfkey_debug_func
= NULL
;
495 /* create control socket.
496 * We must create it before the parent process returns so that
497 * there will be no race condition in using it. The easiest
498 * place to do this is before the daemon fork.
501 err_t ugh
= init_ctl_socket();
505 fprintf(stderr
, "pluto: %s", ugh
);
511 /* create info socket. */
513 err_t ugh
= init_info_socket();
517 fprintf(stderr
, "pluto: %s", ugh
);
523 /* If not suppressed, do daemon fork */
534 fprintf(stderr
, "pluto: fork failed (%d %s)\n",
541 /* parent: die, after filling PID into lock file.
542 * must not use exit_pluto: lock would be removed!
544 exit(fill_lock(lockfd
, pid
)?
0 : 1);
552 fprintf(stderr
, "setsid() failed in main(). Errno %d: %s\n",
559 /* no daemon fork: we have to fill in lock file */
560 (void) fill_lock(lockfd
, getpid());
561 fprintf(stdout
, "Pluto initialized\n");
565 /* Close everything but ctl_fd and (if needed) stderr.
566 * There is some danger that a library that we don't know
567 * about is using some fd that we don't know about.
568 * I guess we'll soon find out.
573 for (i
= getdtablesize() - 1; i
>= 0; i
--) /* Bad hack */
574 if ((!log_to_stderr
|| i
!= 2)
581 /* make sure that stdin, stdout, stderr are reserved */
582 if (open("/dev/null", O_RDONLY
) != 0)
586 if (!log_to_stderr
&& dup2(0, 2) != 2)
593 /* Note: some scripts may look for this exact message -- don't change
594 * ipsec barf was one, but it no longer does.
596 plog("Starting Pluto (strongSwan Version %s%s)"
597 , ipsec_version_code()
598 , compile_time_interop_options
);
600 init_nat_traversal(nat_traversal
, keep_alive
, force_keepalive
, nat_t_spf
);
601 init_virtual_ip(virtual_private
);
602 scx_init(pkcs11_module_path
); /* load and initialize PKCS #11 module */
603 xauth_init(); /* load and initialize XAUTH module */
614 /* loading X.509 CA certificates */
615 load_authcerts("CA cert", CA_CERT_PATH
, AUTH_CA
);
616 /* loading X.509 AA certificates */
617 load_authcerts("AA cert", AA_CERT_PATH
, AUTH_AA
);
618 /* loading X.509 OCSP certificates */
619 load_authcerts("OCSP cert", OCSP_CERT_PATH
, AUTH_OCSP
);
620 /* loading X.509 CRLs */
622 /* loading attribute certificates (experimental) */
627 return -1; /* Shouldn't ever reach this */
630 /* leave pluto, with status.
631 * Once child is launched, parent must not exit this way because
632 * the lock would be released.
635 * 1 general discomfort
636 * 10 lock file exists
639 exit_pluto(int status
)
641 reset_globals(); /* needed because we may be called in odd state */
642 free_preshared_secrets();
643 free_remembered_public_keys();
644 delete_every_connection();
645 free_crl_fetch(); /* free chain of crl fetch requests */
646 free_ocsp_fetch(); /* free chain of ocsp fetch requests */
647 free_authcerts(); /* free chain of X.509 authority certificates */
648 free_crls(); /* free chain of X.509 CRLs */
649 free_acerts(); /* free chain of X.509 attribute certificates */
650 free_ca_infos(); /* free chain of X.509 CA information records */
651 free_ocsp(); /* free ocsp cache */
653 scx_finalize(); /* finalize and unload PKCS #11 module */
654 xauth_finalize(); /* finalize and unload XAUTH module */
658 #ifdef LEAK_DETECTIVE
660 #endif /* LEAK_DETECTIVE */