2 * Copyright (C) 1997 Angelos D. Keromytis.
3 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
4 * Copyright (C) 2009 Andreas Steffen - Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 #include <sys/types.h>
29 #include <arpa/nameser.h> /* missing from <resolv.h> on old systems */
30 #include <sys/queue.h>
31 #include <sys/prctl.h>
36 #include <sys/capability.h>
37 #endif /* CAPABILITIES */
44 #include <utils/enumerator.h>
45 #include <utils/optionsfrom.h>
50 #include "constants.h"
56 #include "connections.h"
57 #include "foodgroups.h"
59 #include "demux.h" /* needs packet.h */
64 #include "adns.h" /* needs <resolv.h> */
65 #include "dnskey.h" /* needs keys.h and adns.h */
67 #include "ipsec_doi.h" /* needs demux.h and state.h */
73 #include "nat_traversal.h"
78 #include "whack_attribute.h"
80 static void usage(const char *mess
)
82 if (mess
!= NULL
&& *mess
!= '\0')
83 fprintf(stderr
, "%s\n", mess
);
88 " [--optionsfrom <filename>]"
96 " [--crlcheckinterval <interval>]"
100 "[--interface <ifname>]"
101 " [--ikeport <port-number>]"
105 "[--perpeerlogbase <path>] [--perpeerlog]"
107 "[--secretsfile <secrets-file>]"
108 " [--policygroupsdir <policygroups-dir>]"
110 "[--adns <pathname>]"
111 "[--pkcs11module <path>]"
112 "[--pkcs11keepstate]"
113 "[--pkcs11initargs <string>]"
122 " [--debug-emitting]"
125 " [--debug-lifecycle]"
130 " [--debug-controlmore]"
135 "[--nat_traversal] [--keep_alive <delay_sec>]"
137 "[--force_keepalive] [--disable_port_floating]"
139 "[--virtual_private <network_list>]"
141 "strongSwan "VERSION
"\n");
142 exit_pluto(mess
== NULL?
0 : 1);
147 * - provides convenient way for scripts to find Pluto's pid
148 * - prevents multiple Plutos competing for the same port
149 * - same basename as unix domain control socket
150 * NOTE: will not take account of sharing LOCK_DIR with other systems.
153 static char pluto_lock
[sizeof(ctl_addr
.sun_path
)] = DEFAULT_CTLBASE LOCK_SUFFIX
;
154 static bool pluto_lock_created
= FALSE
;
156 /* create lockfile, or die in the attempt */
157 static int create_lock(void)
159 int fd
= open(pluto_lock
, O_WRONLY
| O_CREAT
| O_EXCL
| O_TRUNC
160 , S_IRUSR
| S_IRGRP
| S_IROTH
);
166 fprintf(stderr
, "pluto: lock file \"%s\" already exists\n"
173 , "pluto: unable to create lock file \"%s\" (%d %s)\n"
174 , pluto_lock
, errno
, strerror(errno
));
178 pluto_lock_created
= TRUE
;
182 static bool fill_lock(int lockfd
, pid_t pid
)
184 char buf
[30]; /* holds "<pid>\n" */
185 int len
= snprintf(buf
, sizeof(buf
), "%u\n", (unsigned int) pid
);
186 bool ok
= len
> 0 && write(lockfd
, buf
, len
) == len
;
192 static void delete_lock(void)
194 if (pluto_lock_created
)
197 unlink(pluto_lock
); /* is noting failure useful? */
202 /* by default pluto sends certificate requests to its peers */
203 bool no_cr_send
= FALSE
;
205 /* by default the CRL policy is lenient */
206 bool strict_crl_policy
= FALSE
;
208 /* by default CRLs are cached locally as files */
209 bool cache_crls
= FALSE
;
211 /* by default pluto does not check crls dynamically */
212 long crl_check_interval
= 0;
214 /* path to the PKCS#11 module */
215 char *pkcs11_module_path
= NULL
;
217 /* by default pluto logs out after every smartcard use */
218 bool pkcs11_keep_state
= FALSE
;
220 /* by default pluto does not allow pkcs11 proxy access via whack */
221 bool pkcs11_proxy
= FALSE
;
223 /* argument string to pass to PKCS#11 module.
224 * Not used for compliant modules, just for NSS softoken
226 static const char *pkcs11_init_args
= NULL
;
228 /* options read by optionsfrom */
234 static void print_plugins()
236 char buf
[BUF_LEN
], *plugin
;
238 enumerator_t
*enumerator
;
241 enumerator
= lib
->plugins
->create_plugin_enumerator(lib
->plugins
);
242 while (len
< BUF_LEN
&& enumerator
->enumerate(enumerator
, &plugin
))
244 len
+= snprintf(&buf
[len
], BUF_LEN
-len
, "%s ", plugin
);
246 enumerator
->destroy(enumerator
);
247 DBG1(DBG_DMN
, "loaded plugins: %s", buf
);
250 int main(int argc
, char **argv
)
252 bool fork_desired
= TRUE
;
253 bool log_to_stderr_desired
= FALSE
;
254 bool nat_traversal
= FALSE
;
255 bool nat_t_spf
= TRUE
; /* support port floating */
256 unsigned int keep_alive
= 0;
257 bool force_keepalive
= FALSE
;
258 char *virtual_private
= NULL
;
262 int keep
[] = { CAP_NET_ADMIN
, CAP_NET_BIND_SERVICE
};
263 #endif /* CAPABILITIES */
265 /* initialize library and optionsfrom */
266 if (!library_init(NULL
))
269 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY
);
271 if (lib
->integrity
&&
272 !lib
->integrity
->check_file(lib
->integrity
, "pluto", argv
[0]))
274 fprintf(stderr
, "integrity check of pluto failed\n");
276 exit(SS_RC_DAEMON_INTEGRITY
);
278 if (!libhydra_init("pluto"))
282 exit(SS_RC_INITIALIZATION_FAILED
);
284 options
= options_create();
286 /* handle arguments */
289 # define DBG_OFFSET 256
290 static const struct option long_opts
[] = {
291 /* name, has_arg, flag, val */
292 { "help", no_argument
, NULL
, 'h' },
293 { "version", no_argument
, NULL
, 'v' },
294 { "optionsfrom", required_argument
, NULL
, '+' },
295 { "nofork", no_argument
, NULL
, 'd' },
296 { "stderrlog", no_argument
, NULL
, 'e' },
297 { "noklips", no_argument
, NULL
, 'n' },
298 { "nocrsend", no_argument
, NULL
, 'c' },
299 { "strictcrlpolicy", no_argument
, NULL
, 'r' },
300 { "crlcheckinterval", required_argument
, NULL
, 'x'},
301 { "cachecrls", no_argument
, NULL
, 'C' },
302 { "uniqueids", no_argument
, NULL
, 'u' },
303 { "interface", required_argument
, NULL
, 'i' },
304 { "ikeport", required_argument
, NULL
, 'p' },
305 { "ctlbase", required_argument
, NULL
, 'b' },
306 { "secretsfile", required_argument
, NULL
, 's' },
307 { "foodgroupsdir", required_argument
, NULL
, 'f' },
308 { "perpeerlogbase", required_argument
, NULL
, 'P' },
309 { "perpeerlog", no_argument
, NULL
, 'l' },
310 { "policygroupsdir", required_argument
, NULL
, 'f' },
312 { "lwdnsq", required_argument
, NULL
, 'a' },
313 #else /* !USE_LWRES */
314 { "adns", required_argument
, NULL
, 'a' },
315 #endif /* !USE_LWRES */
316 { "pkcs11module", required_argument
, NULL
, 'm' },
317 { "pkcs11keepstate", no_argument
, NULL
, 'k' },
318 { "pkcs11initargs", required_argument
, NULL
, 'z' },
319 { "pkcs11proxy", no_argument
, NULL
, 'y' },
320 { "nat_traversal", no_argument
, NULL
, '1' },
321 { "keep_alive", required_argument
, NULL
, '2' },
322 { "force_keepalive", no_argument
, NULL
, '3' },
323 { "disable_port_floating", no_argument
, NULL
, '4' },
324 { "debug-natt", no_argument
, NULL
, '5' },
325 { "virtual_private", required_argument
, NULL
, '6' },
327 { "debug-none", no_argument
, NULL
, 'N' },
328 { "debug-all", no_argument
, NULL
, 'A' },
329 { "debug-raw", no_argument
, NULL
, DBG_RAW
+ DBG_OFFSET
},
330 { "debug-crypt", no_argument
, NULL
, DBG_CRYPT
+ DBG_OFFSET
},
331 { "debug-parsing", no_argument
, NULL
, DBG_PARSING
+ DBG_OFFSET
},
332 { "debug-emitting", no_argument
, NULL
, DBG_EMITTING
+ DBG_OFFSET
},
333 { "debug-control", no_argument
, NULL
, DBG_CONTROL
+ DBG_OFFSET
},
334 { "debug-lifecycle", no_argument
, NULL
, DBG_LIFECYCLE
+ DBG_OFFSET
},
335 { "debug-klips", no_argument
, NULL
, DBG_KLIPS
+ DBG_OFFSET
},
336 { "debug-dns", no_argument
, NULL
, DBG_DNS
+ DBG_OFFSET
},
337 { "debug-oppo", no_argument
, NULL
, DBG_OPPO
+ DBG_OFFSET
},
338 { "debug-controlmore", no_argument
, NULL
, DBG_CONTROLMORE
+ DBG_OFFSET
},
339 { "debug-private", no_argument
, NULL
, DBG_PRIVATE
+ DBG_OFFSET
},
341 { "impair-delay-adns-key-answer", no_argument
, NULL
, IMPAIR_DELAY_ADNS_KEY_ANSWER
+ DBG_OFFSET
},
342 { "impair-delay-adns-txt-answer", no_argument
, NULL
, IMPAIR_DELAY_ADNS_TXT_ANSWER
+ DBG_OFFSET
},
343 { "impair-bust-mi2", no_argument
, NULL
, IMPAIR_BUST_MI2
+ DBG_OFFSET
},
344 { "impair-bust-mr2", no_argument
, NULL
, IMPAIR_BUST_MR2
+ DBG_OFFSET
},
348 /* Note: we don't like the way short options get parsed
349 * by getopt_long, so we simply pass an empty string as
350 * the list. It could be "hvdenp:l:s:" "NARXPECK".
352 int c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
354 /* Note: "breaking" from case terminates loop */
357 case EOF
: /* end of flags */
360 case 0: /* long option already handled */
363 case ':': /* diagnostic already printed by getopt_long */
364 case '?': /* diagnostic already printed by getopt_long */
366 break; /* not actually reached */
368 case 'h': /* --help */
370 break; /* not actually reached */
372 case 'v': /* --version */
374 const char **sp
= ipsec_copyright_notice();
376 printf("strongSwan "VERSION
"%s\n", compile_time_interop_options
);
377 for (; *sp
!= NULL
; sp
++)
381 break; /* not actually reached */
383 case '+': /* --optionsfrom <filename> */
384 if (!options
->from(options
, optarg
, &argc
, &argv
, optind
))
390 case 'd': /* --nofork*/
391 fork_desired
= FALSE
;
394 case 'e': /* --stderrlog */
395 log_to_stderr_desired
= TRUE
;
398 case 'n': /* --noklips */
402 case 'c': /* --nocrsend */
406 case 'r': /* --strictcrlpolicy */
407 strict_crl_policy
= TRUE
;
410 case 'x': /* --crlcheckinterval <time>*/
411 if (optarg
== NULL
|| !isdigit(optarg
[0]))
412 usage("missing interval time");
416 long interval
= strtol(optarg
, &endptr
, 0);
418 if (*endptr
!= '\0' || endptr
== optarg
420 usage("<interval-time> must be a positive number");
421 crl_check_interval
= interval
;
425 case 'C': /* --cachecrls */
429 case 'u': /* --uniqueids */
433 case 'i': /* --interface <ifname> */
434 if (!use_interface(optarg
))
435 usage("too many --interface specifications");
438 case 'p': /* --port <portnumber> */
439 if (optarg
== NULL
|| !isdigit(optarg
[0]))
440 usage("missing port number");
444 long port
= strtol(optarg
, &endptr
, 0);
446 if (*endptr
!= '\0' || endptr
== optarg
447 || port
<= 0 || port
> 0x10000)
448 usage("<port-number> must be a number between 1 and 65535");
453 case 'b': /* --ctlbase <path> */
454 if (snprintf(ctl_addr
.sun_path
, sizeof(ctl_addr
.sun_path
)
455 , "%s%s", optarg
, CTL_SUFFIX
) == -1)
456 usage("<path>" CTL_SUFFIX
" too long for sun_path");
457 if (snprintf(info_addr
.sun_path
, sizeof(info_addr
.sun_path
)
458 , "%s%s", optarg
, INFO_SUFFIX
) == -1)
459 usage("<path>" INFO_SUFFIX
" too long for sun_path");
460 if (snprintf(pluto_lock
, sizeof(pluto_lock
)
461 , "%s%s", optarg
, LOCK_SUFFIX
) == -1)
462 usage("<path>" LOCK_SUFFIX
" must fit");
465 case 's': /* --secretsfile <secrets-file> */
466 shared_secrets_file
= optarg
;
469 case 'f': /* --policygroupsdir <policygroups-dir> */
470 policygroups_dir
= optarg
;
473 case 'a': /* --adns <pathname> */
474 pluto_adns_option
= optarg
;
477 case 'm': /* --pkcs11module <pathname> */
478 pkcs11_module_path
= optarg
;
481 case 'k': /* --pkcs11keepstate */
482 pkcs11_keep_state
= TRUE
;
485 case 'y': /* --pkcs11proxy */
489 case 'z': /* --pkcs11initargs */
490 pkcs11_init_args
= optarg
;
494 case 'N': /* --debug-none */
495 base_debugging
= DBG_NONE
;
498 case 'A': /* --debug-all */
499 base_debugging
= DBG_ALL
;
503 case 'P': /* --perpeerlogbase */
504 base_perpeer_logdir
= optarg
;
508 log_to_perpeer
= TRUE
;
511 case '1': /* --nat_traversal */
512 nat_traversal
= TRUE
;
514 case '2': /* --keep_alive */
515 keep_alive
= atoi(optarg
);
517 case '3': /* --force_keepalive */
518 force_keepalive
= TRUE
;
520 case '4': /* --disable_port_floating */
523 case '5': /* --debug-nat_t */
524 base_debugging
|= DBG_NATT
;
526 case '6': /* --virtual_private */
527 virtual_private
= optarg
;
534 base_debugging
|= c
- DBG_OFFSET
;
544 usage("unexpected argument");
546 lockfd
= create_lock();
548 /* select between logging methods */
550 if (log_to_stderr_desired
)
552 log_to_syslog
= FALSE
;
556 log_to_stderr
= FALSE
;
559 /* set the logging function of pfkey debugging */
561 pfkey_debug_func
= DBG_log
;
563 pfkey_debug_func
= NULL
;
566 /* create control socket.
567 * We must create it before the parent process returns so that
568 * there will be no race condition in using it. The easiest
569 * place to do this is before the daemon fork.
572 err_t ugh
= init_ctl_socket();
576 fprintf(stderr
, "pluto: %s", ugh
);
581 /* If not suppressed, do daemon fork */
592 fprintf(stderr
, "pluto: fork failed (%d %s)\n",
599 /* parent: die, after filling PID into lock file.
600 * must not use exit_pluto: lock would be removed!
602 exit(fill_lock(lockfd
, pid
)?
0 : 1);
610 fprintf(stderr
, "setsid() failed in main(). Errno %d: %s\n",
617 /* no daemon fork: we have to fill in lock file */
618 (void) fill_lock(lockfd
, getpid());
619 fprintf(stdout
, "Pluto initialized\n");
623 /* Close everything but ctl_fd and (if needed) stderr.
624 * There is some danger that a library that we don't know
625 * about is using some fd that we don't know about.
626 * I guess we'll soon find out.
631 for (i
= getdtablesize() - 1; i
>= 0; i
--) /* Bad hack */
633 if ((!log_to_stderr
|| i
!= 2) && i
!= ctl_fd
)
637 /* make sure that stdin, stdout, stderr are reserved */
638 if (open("/dev/null", O_RDONLY
) != 0)
642 if (!log_to_stderr
&& dup2(0, 2) != 2)
649 /* Note: some scripts may look for this exact message -- don't change
650 * ipsec barf was one, but it no longer does.
652 plog("Starting IKEv1 pluto daemon (strongSwan "VERSION
")%s",
653 compile_time_interop_options
);
657 plog("integrity tests enabled:");
658 plog("lib 'libstrongswan': passed file and segment integrity tests");
659 plog("lib 'libhydra': passed file and segment integrity tests");
660 plog("daemon 'pluto': passed file integrity test");
663 /* load plugins, further infrastructure may need it */
664 if (!lib
->plugins
->load(lib
->plugins
, NULL
,
665 lib
->settings
->get_str(lib
->settings
, "pluto.load", PLUGINS
)))
667 exit(SS_RC_INITIALIZATION_FAILED
);
672 if (!init_secret() || !init_crypto())
674 plog("initialization failed - aborting pluto");
675 exit_pluto(SS_RC_INITIALIZATION_FAILED
);
677 init_nat_traversal(nat_traversal
, keep_alive
, force_keepalive
, nat_t_spf
);
678 init_virtual_ip(virtual_private
);
679 scx_init(pkcs11_module_path
, pkcs11_init_args
);
688 whack_attribute_initialize();
690 /* drop unneeded capabilities and change UID/GID */
691 prctl(PR_SET_KEEPCAPS
, 1);
695 struct group group
, *grp
;
698 if (getgrnam_r(IPSEC_GROUP
, &group
, buf
, sizeof(buf
), &grp
) != 0 ||
699 grp
== NULL
|| setgid(grp
->gr_gid
) != 0)
701 plog("unable to change daemon group");
708 struct passwd passwd
, *pwp
;
711 if (getpwnam_r(IPSEC_USER
, &passwd
, buf
, sizeof(buf
), &pwp
) != 0 ||
712 pwp
== NULL
|| setuid(pwp
->pw_uid
) != 0)
714 plog("unable to change daemon user");
722 cap_set_flag(caps
, CAP_EFFECTIVE
, 2, keep
, CAP_SET
);
723 cap_set_flag(caps
, CAP_INHERITABLE
, 2, keep
, CAP_SET
);
724 cap_set_flag(caps
, CAP_PERMITTED
, 2, keep
, CAP_SET
);
725 if (cap_set_proc(caps
) != 0)
727 plog("unable to drop daemon capabilities");
731 #endif /* CAPABILITIES */
733 /* loading X.509 CA certificates */
734 load_authcerts("ca", CA_CERT_PATH
, X509_CA
);
735 /* loading X.509 AA certificates */
736 load_authcerts("aa", AA_CERT_PATH
, X509_AA
);
737 /* loading X.509 OCSP certificates */
738 load_authcerts("ocsp", OCSP_CERT_PATH
, X509_OCSP_SIGNER
);
739 /* loading X.509 CRLs */
741 /* loading attribute certificates (experimental) */
746 return -1; /* Shouldn't ever reach this */
749 /* leave pluto, with status.
750 * Once child is launched, parent must not exit this way because
751 * the lock would be released.
754 * 1 general discomfort
755 * 10 lock file exists
757 void exit_pluto(int status
)
759 reset_globals(); /* needed because we may be called in odd state */
760 free_preshared_secrets();
761 free_remembered_public_keys();
762 delete_every_connection();
763 whack_attribute_finalize(); /* free in-memory pools */
764 fetch_finalize(); /* stop fetching thread */
765 free_crl_fetch(); /* free chain of crl fetch requests */
766 free_ocsp_fetch(); /* free chain of ocsp fetch requests */
767 free_authcerts(); /* free chain of X.509 authority certificates */
768 free_crls(); /* free chain of X.509 CRLs */
769 free_ca_infos(); /* free chain of X.509 CA information records */
770 free_ocsp(); /* free ocsp cache */
772 ac_finalize(); /* free X.509 attribute certificates */
773 scx_finalize(); /* finalize and unload PKCS #11 module */
774 xauth_finalize(); /* finalize and unload XAUTH module */
778 free_myid(); /* free myids */
779 free_events(); /* free remaining events */
780 free_vendorid(); /* free all vendor id records */
783 options
->destroy(options
);