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"
79 static void usage(const char *mess
)
81 if (mess
!= NULL
&& *mess
!= '\0')
82 fprintf(stderr
, "%s\n", mess
);
87 " [--optionsfrom <filename>]"
95 " [--crlcheckinterval <interval>]"
99 "[--interface <ifname>]"
100 " [--ikeport <port-number>]"
104 "[--perpeerlogbase <path>] [--perpeerlog]"
106 "[--secretsfile <secrets-file>]"
107 " [--policygroupsdir <policygroups-dir>]"
109 "[--adns <pathname>]"
110 "[--pkcs11module <path>]"
111 "[--pkcs11keepstate]"
112 "[--pkcs11initargs <string>]"
121 " [--debug-emitting]"
124 " [--debug-lifecycle]"
129 " [--debug-controlmore]"
134 "[--nat_traversal] [--keep_alive <delay_sec>]"
136 "[--force_keepalive] [--disable_port_floating]"
138 "[--virtual_private <network_list>]"
140 "strongSwan "VERSION
"\n");
141 exit_pluto(mess
== NULL?
0 : 1);
146 * - provides convenient way for scripts to find Pluto's pid
147 * - prevents multiple Plutos competing for the same port
148 * - same basename as unix domain control socket
149 * NOTE: will not take account of sharing LOCK_DIR with other systems.
152 static char pluto_lock
[sizeof(ctl_addr
.sun_path
)] = DEFAULT_CTLBASE LOCK_SUFFIX
;
153 static bool pluto_lock_created
= FALSE
;
155 /* create lockfile, or die in the attempt */
156 static int create_lock(void)
158 int fd
= open(pluto_lock
, O_WRONLY
| O_CREAT
| O_EXCL
| O_TRUNC
159 , S_IRUSR
| S_IRGRP
| S_IROTH
);
165 fprintf(stderr
, "pluto: lock file \"%s\" already exists\n"
172 , "pluto: unable to create lock file \"%s\" (%d %s)\n"
173 , pluto_lock
, errno
, strerror(errno
));
177 pluto_lock_created
= TRUE
;
181 static bool fill_lock(int lockfd
, pid_t pid
)
183 char buf
[30]; /* holds "<pid>\n" */
184 int len
= snprintf(buf
, sizeof(buf
), "%u\n", (unsigned int) pid
);
185 bool ok
= len
> 0 && write(lockfd
, buf
, len
) == len
;
191 static void delete_lock(void)
193 if (pluto_lock_created
)
196 unlink(pluto_lock
); /* is noting failure useful? */
201 /* by default pluto sends certificate requests to its peers */
202 bool no_cr_send
= FALSE
;
204 /* by default the CRL policy is lenient */
205 bool strict_crl_policy
= FALSE
;
207 /* by default CRLs are cached locally as files */
208 bool cache_crls
= FALSE
;
210 /* by default pluto does not check crls dynamically */
211 long crl_check_interval
= 0;
213 /* path to the PKCS#11 module */
214 char *pkcs11_module_path
= NULL
;
216 /* by default pluto logs out after every smartcard use */
217 bool pkcs11_keep_state
= FALSE
;
219 /* by default pluto does not allow pkcs11 proxy access via whack */
220 bool pkcs11_proxy
= FALSE
;
222 /* argument string to pass to PKCS#11 module.
223 * Not used for compliant modules, just for NSS softoken
225 static const char *pkcs11_init_args
= NULL
;
227 /* options read by optionsfrom */
233 static void print_plugins()
235 char buf
[BUF_LEN
], *plugin
;
237 enumerator_t
*enumerator
;
240 enumerator
= lib
->plugins
->create_plugin_enumerator(lib
->plugins
);
241 while (len
< BUF_LEN
&& enumerator
->enumerate(enumerator
, &plugin
))
243 len
+= snprintf(&buf
[len
], BUF_LEN
-len
, "%s ", plugin
);
245 enumerator
->destroy(enumerator
);
246 DBG1("loaded plugins: %s", buf
);
249 int main(int argc
, char **argv
)
251 bool fork_desired
= TRUE
;
252 bool log_to_stderr_desired
= FALSE
;
253 bool nat_traversal
= FALSE
;
254 bool nat_t_spf
= TRUE
; /* support port floating */
255 unsigned int keep_alive
= 0;
256 bool force_keepalive
= FALSE
;
257 char *virtual_private
= NULL
;
261 int keep
[] = { CAP_NET_ADMIN
, CAP_NET_BIND_SERVICE
};
262 #endif /* CAPABILITIES */
264 /* initialize library and optionsfrom */
265 if (!library_init(NULL
))
268 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY
);
270 if (lib
->integrity
&&
271 !lib
->integrity
->check_file(lib
->integrity
, "pluto", argv
[0]))
273 fprintf(stderr
, "integrity check of pluto failed\n");
275 exit(SS_RC_DAEMON_INTEGRITY
);
277 if (!libhydra_init())
281 exit(SS_RC_INITIALIZATION_FAILED
);
283 options
= options_create();
285 /* handle arguments */
288 # define DBG_OFFSET 256
289 static const struct option long_opts
[] = {
290 /* name, has_arg, flag, val */
291 { "help", no_argument
, NULL
, 'h' },
292 { "version", no_argument
, NULL
, 'v' },
293 { "optionsfrom", required_argument
, NULL
, '+' },
294 { "nofork", no_argument
, NULL
, 'd' },
295 { "stderrlog", no_argument
, NULL
, 'e' },
296 { "noklips", no_argument
, NULL
, 'n' },
297 { "nocrsend", no_argument
, NULL
, 'c' },
298 { "strictcrlpolicy", no_argument
, NULL
, 'r' },
299 { "crlcheckinterval", required_argument
, NULL
, 'x'},
300 { "cachecrls", no_argument
, NULL
, 'C' },
301 { "uniqueids", no_argument
, NULL
, 'u' },
302 { "interface", required_argument
, NULL
, 'i' },
303 { "ikeport", required_argument
, NULL
, 'p' },
304 { "ctlbase", required_argument
, NULL
, 'b' },
305 { "secretsfile", required_argument
, NULL
, 's' },
306 { "foodgroupsdir", required_argument
, NULL
, 'f' },
307 { "perpeerlogbase", required_argument
, NULL
, 'P' },
308 { "perpeerlog", no_argument
, NULL
, 'l' },
309 { "policygroupsdir", required_argument
, NULL
, 'f' },
311 { "lwdnsq", required_argument
, NULL
, 'a' },
312 #else /* !USE_LWRES */
313 { "adns", required_argument
, NULL
, 'a' },
314 #endif /* !USE_LWRES */
315 { "pkcs11module", required_argument
, NULL
, 'm' },
316 { "pkcs11keepstate", no_argument
, NULL
, 'k' },
317 { "pkcs11initargs", required_argument
, NULL
, 'z' },
318 { "pkcs11proxy", no_argument
, NULL
, 'y' },
319 { "nat_traversal", no_argument
, NULL
, '1' },
320 { "keep_alive", required_argument
, NULL
, '2' },
321 { "force_keepalive", no_argument
, NULL
, '3' },
322 { "disable_port_floating", no_argument
, NULL
, '4' },
323 { "debug-natt", no_argument
, NULL
, '5' },
324 { "virtual_private", required_argument
, NULL
, '6' },
326 { "debug-none", no_argument
, NULL
, 'N' },
327 { "debug-all", no_argument
, NULL
, 'A' },
328 { "debug-raw", no_argument
, NULL
, DBG_RAW
+ DBG_OFFSET
},
329 { "debug-crypt", no_argument
, NULL
, DBG_CRYPT
+ DBG_OFFSET
},
330 { "debug-parsing", no_argument
, NULL
, DBG_PARSING
+ DBG_OFFSET
},
331 { "debug-emitting", no_argument
, NULL
, DBG_EMITTING
+ DBG_OFFSET
},
332 { "debug-control", no_argument
, NULL
, DBG_CONTROL
+ DBG_OFFSET
},
333 { "debug-lifecycle", no_argument
, NULL
, DBG_LIFECYCLE
+ DBG_OFFSET
},
334 { "debug-klips", no_argument
, NULL
, DBG_KLIPS
+ DBG_OFFSET
},
335 { "debug-dns", no_argument
, NULL
, DBG_DNS
+ DBG_OFFSET
},
336 { "debug-oppo", no_argument
, NULL
, DBG_OPPO
+ DBG_OFFSET
},
337 { "debug-controlmore", no_argument
, NULL
, DBG_CONTROLMORE
+ DBG_OFFSET
},
338 { "debug-private", no_argument
, NULL
, DBG_PRIVATE
+ DBG_OFFSET
},
340 { "impair-delay-adns-key-answer", no_argument
, NULL
, IMPAIR_DELAY_ADNS_KEY_ANSWER
+ DBG_OFFSET
},
341 { "impair-delay-adns-txt-answer", no_argument
, NULL
, IMPAIR_DELAY_ADNS_TXT_ANSWER
+ DBG_OFFSET
},
342 { "impair-bust-mi2", no_argument
, NULL
, IMPAIR_BUST_MI2
+ DBG_OFFSET
},
343 { "impair-bust-mr2", no_argument
, NULL
, IMPAIR_BUST_MR2
+ DBG_OFFSET
},
347 /* Note: we don't like the way short options get parsed
348 * by getopt_long, so we simply pass an empty string as
349 * the list. It could be "hvdenp:l:s:" "NARXPECK".
351 int c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
353 /* Note: "breaking" from case terminates loop */
356 case EOF
: /* end of flags */
359 case 0: /* long option already handled */
362 case ':': /* diagnostic already printed by getopt_long */
363 case '?': /* diagnostic already printed by getopt_long */
365 break; /* not actually reached */
367 case 'h': /* --help */
369 break; /* not actually reached */
371 case 'v': /* --version */
373 const char **sp
= ipsec_copyright_notice();
375 printf("strongSwan "VERSION
"%s\n", compile_time_interop_options
);
376 for (; *sp
!= NULL
; sp
++)
380 break; /* not actually reached */
382 case '+': /* --optionsfrom <filename> */
383 if (!options
->from(options
, optarg
, &argc
, &argv
, optind
))
389 case 'd': /* --nofork*/
390 fork_desired
= FALSE
;
393 case 'e': /* --stderrlog */
394 log_to_stderr_desired
= TRUE
;
397 case 'n': /* --noklips */
401 case 'c': /* --nocrsend */
405 case 'r': /* --strictcrlpolicy */
406 strict_crl_policy
= TRUE
;
409 case 'x': /* --crlcheckinterval <time>*/
410 if (optarg
== NULL
|| !isdigit(optarg
[0]))
411 usage("missing interval time");
415 long interval
= strtol(optarg
, &endptr
, 0);
417 if (*endptr
!= '\0' || endptr
== optarg
419 usage("<interval-time> must be a positive number");
420 crl_check_interval
= interval
;
424 case 'C': /* --cachecrls */
428 case 'u': /* --uniqueids */
432 case 'i': /* --interface <ifname> */
433 if (!use_interface(optarg
))
434 usage("too many --interface specifications");
437 case 'p': /* --port <portnumber> */
438 if (optarg
== NULL
|| !isdigit(optarg
[0]))
439 usage("missing port number");
443 long port
= strtol(optarg
, &endptr
, 0);
445 if (*endptr
!= '\0' || endptr
== optarg
446 || port
<= 0 || port
> 0x10000)
447 usage("<port-number> must be a number between 1 and 65535");
452 case 'b': /* --ctlbase <path> */
453 if (snprintf(ctl_addr
.sun_path
, sizeof(ctl_addr
.sun_path
)
454 , "%s%s", optarg
, CTL_SUFFIX
) == -1)
455 usage("<path>" CTL_SUFFIX
" too long for sun_path");
456 if (snprintf(info_addr
.sun_path
, sizeof(info_addr
.sun_path
)
457 , "%s%s", optarg
, INFO_SUFFIX
) == -1)
458 usage("<path>" INFO_SUFFIX
" too long for sun_path");
459 if (snprintf(pluto_lock
, sizeof(pluto_lock
)
460 , "%s%s", optarg
, LOCK_SUFFIX
) == -1)
461 usage("<path>" LOCK_SUFFIX
" must fit");
464 case 's': /* --secretsfile <secrets-file> */
465 shared_secrets_file
= optarg
;
468 case 'f': /* --policygroupsdir <policygroups-dir> */
469 policygroups_dir
= optarg
;
472 case 'a': /* --adns <pathname> */
473 pluto_adns_option
= optarg
;
476 case 'm': /* --pkcs11module <pathname> */
477 pkcs11_module_path
= optarg
;
480 case 'k': /* --pkcs11keepstate */
481 pkcs11_keep_state
= TRUE
;
484 case 'y': /* --pkcs11proxy */
488 case 'z': /* --pkcs11initargs */
489 pkcs11_init_args
= optarg
;
493 case 'N': /* --debug-none */
494 base_debugging
= DBG_NONE
;
497 case 'A': /* --debug-all */
498 base_debugging
= DBG_ALL
;
502 case 'P': /* --perpeerlogbase */
503 base_perpeer_logdir
= optarg
;
507 log_to_perpeer
= TRUE
;
510 case '1': /* --nat_traversal */
511 nat_traversal
= TRUE
;
513 case '2': /* --keep_alive */
514 keep_alive
= atoi(optarg
);
516 case '3': /* --force_keepalive */
517 force_keepalive
= TRUE
;
519 case '4': /* --disable_port_floating */
522 case '5': /* --debug-nat_t */
523 base_debugging
|= DBG_NATT
;
525 case '6': /* --virtual_private */
526 virtual_private
= optarg
;
533 base_debugging
|= c
- DBG_OFFSET
;
543 usage("unexpected argument");
545 lockfd
= create_lock();
547 /* select between logging methods */
549 if (log_to_stderr_desired
)
551 log_to_syslog
= FALSE
;
555 log_to_stderr
= FALSE
;
558 /* set the logging function of pfkey debugging */
560 pfkey_debug_func
= DBG_log
;
562 pfkey_debug_func
= NULL
;
565 /* create control socket.
566 * We must create it before the parent process returns so that
567 * there will be no race condition in using it. The easiest
568 * place to do this is before the daemon fork.
571 err_t ugh
= init_ctl_socket();
575 fprintf(stderr
, "pluto: %s", ugh
);
580 /* If not suppressed, do daemon fork */
591 fprintf(stderr
, "pluto: fork failed (%d %s)\n",
598 /* parent: die, after filling PID into lock file.
599 * must not use exit_pluto: lock would be removed!
601 exit(fill_lock(lockfd
, pid
)?
0 : 1);
609 fprintf(stderr
, "setsid() failed in main(). Errno %d: %s\n",
616 /* no daemon fork: we have to fill in lock file */
617 (void) fill_lock(lockfd
, getpid());
618 fprintf(stdout
, "Pluto initialized\n");
622 /* Close everything but ctl_fd and (if needed) stderr.
623 * There is some danger that a library that we don't know
624 * about is using some fd that we don't know about.
625 * I guess we'll soon find out.
630 for (i
= getdtablesize() - 1; i
>= 0; i
--) /* Bad hack */
632 if ((!log_to_stderr
|| i
!= 2) && i
!= ctl_fd
)
636 /* make sure that stdin, stdout, stderr are reserved */
637 if (open("/dev/null", O_RDONLY
) != 0)
641 if (!log_to_stderr
&& dup2(0, 2) != 2)
648 /* Note: some scripts may look for this exact message -- don't change
649 * ipsec barf was one, but it no longer does.
651 plog("Starting IKEv1 pluto daemon (strongSwan "VERSION
")%s",
652 compile_time_interop_options
);
656 plog("integrity tests enabled:");
657 plog("lib 'libstrongswan': passed file and segment integrity tests");
658 plog("lib 'libhydra': passed file and segment integrity tests");
659 plog("daemon 'pluto': passed file integrity test");
662 /* load plugins, further infrastructure may need it */
663 if (!lib
->plugins
->load(lib
->plugins
, NULL
,
664 lib
->settings
->get_str(lib
->settings
, "pluto.load", PLUGINS
)))
666 exit(SS_RC_INITIALIZATION_FAILED
);
671 if (!init_secret() || !init_crypto())
673 plog("initialization failed - aborting pluto");
674 exit_pluto(SS_RC_INITIALIZATION_FAILED
);
676 init_nat_traversal(nat_traversal
, keep_alive
, force_keepalive
, nat_t_spf
);
677 init_virtual_ip(virtual_private
);
678 scx_init(pkcs11_module_path
, pkcs11_init_args
);
688 /* drop unneeded capabilities and change UID/GID */
689 prctl(PR_SET_KEEPCAPS
, 1);
693 struct group group
, *grp
;
696 if (getgrnam_r(IPSEC_GROUP
, &group
, buf
, sizeof(buf
), &grp
) != 0 ||
697 grp
== NULL
|| setgid(grp
->gr_gid
) != 0)
699 plog("unable to change daemon group");
706 struct passwd passwd
, *pwp
;
709 if (getpwnam_r(IPSEC_USER
, &passwd
, buf
, sizeof(buf
), &pwp
) != 0 ||
710 pwp
== NULL
|| setuid(pwp
->pw_uid
) != 0)
712 plog("unable to change daemon user");
720 cap_set_flag(caps
, CAP_EFFECTIVE
, 2, keep
, CAP_SET
);
721 cap_set_flag(caps
, CAP_INHERITABLE
, 2, keep
, CAP_SET
);
722 cap_set_flag(caps
, CAP_PERMITTED
, 2, keep
, CAP_SET
);
723 if (cap_set_proc(caps
) != 0)
725 plog("unable to drop daemon capabilities");
729 #endif /* CAPABILITIES */
731 /* loading X.509 CA certificates */
732 load_authcerts("ca", CA_CERT_PATH
, X509_CA
);
733 /* loading X.509 AA certificates */
734 load_authcerts("aa", AA_CERT_PATH
, X509_AA
);
735 /* loading X.509 OCSP certificates */
736 load_authcerts("ocsp", OCSP_CERT_PATH
, X509_OCSP_SIGNER
);
737 /* loading X.509 CRLs */
739 /* loading attribute certificates (experimental) */
744 return -1; /* Shouldn't ever reach this */
747 /* leave pluto, with status.
748 * Once child is launched, parent must not exit this way because
749 * the lock would be released.
752 * 1 general discomfort
753 * 10 lock file exists
755 void exit_pluto(int status
)
757 reset_globals(); /* needed because we may be called in odd state */
758 free_preshared_secrets();
759 free_remembered_public_keys();
760 delete_every_connection();
761 fetch_finalize(); /* stop fetching thread */
762 free_crl_fetch(); /* free chain of crl fetch requests */
763 free_ocsp_fetch(); /* free chain of ocsp fetch requests */
764 free_authcerts(); /* free chain of X.509 authority certificates */
765 free_crls(); /* free chain of X.509 CRLs */
766 free_ca_infos(); /* free chain of X.509 CA information records */
767 free_ocsp(); /* free ocsp cache */
769 ac_finalize(); /* free X.509 attribute certificates */
770 scx_finalize(); /* finalize and unload PKCS #11 module */
771 xauth_finalize(); /* finalize and unload XAUTH module */
775 free_myid(); /* free myids */
776 free_events(); /* free remaining events */
777 free_vendorid(); /* free all vendor id records */
780 options
->destroy(options
);