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 */
43 #include <utils/enumerator.h>
44 #include <utils/optionsfrom.h>
49 #include "constants.h"
55 #include "connections.h"
56 #include "foodgroups.h"
58 #include "demux.h" /* needs packet.h */
63 #include "adns.h" /* needs <resolv.h> */
64 #include "dnskey.h" /* needs keys.h and adns.h */
66 #include "ipsec_doi.h" /* needs demux.h and state.h */
72 #include "nat_traversal.h"
78 static void usage(const char *mess
)
80 if (mess
!= NULL
&& *mess
!= '\0')
81 fprintf(stderr
, "%s\n", mess
);
86 " [--optionsfrom <filename>]"
94 " [--crlcheckinterval <interval>]"
98 "[--interface <ifname>]"
99 " [--ikeport <port-number>]"
103 "[--perpeerlogbase <path>] [--perpeerlog]"
105 "[--secretsfile <secrets-file>]"
106 " [--policygroupsdir <policygroups-dir>]"
108 "[--adns <pathname>]"
109 "[--pkcs11module <path>]"
110 "[--pkcs11keepstate]"
111 "[--pkcs11initargs <string>]"
120 " [--debug-emitting]"
123 " [--debug-lifecycle]"
128 " [--debug-controlmore]"
133 "[--nat_traversal] [--keep_alive <delay_sec>]"
135 "[--force_keepalive] [--disable_port_floating]"
137 "[--virtual_private <network_list>]"
139 "strongSwan "VERSION
"\n");
140 exit_pluto(mess
== NULL?
0 : 1);
145 * - provides convenient way for scripts to find Pluto's pid
146 * - prevents multiple Plutos competing for the same port
147 * - same basename as unix domain control socket
148 * NOTE: will not take account of sharing LOCK_DIR with other systems.
151 static char pluto_lock
[sizeof(ctl_addr
.sun_path
)] = DEFAULT_CTLBASE LOCK_SUFFIX
;
152 static bool pluto_lock_created
= FALSE
;
154 /* create lockfile, or die in the attempt */
155 static int create_lock(void)
157 int fd
= open(pluto_lock
, O_WRONLY
| O_CREAT
| O_EXCL
| O_TRUNC
158 , S_IRUSR
| S_IRGRP
| S_IROTH
);
164 fprintf(stderr
, "pluto: lock file \"%s\" already exists\n"
171 , "pluto: unable to create lock file \"%s\" (%d %s)\n"
172 , pluto_lock
, errno
, strerror(errno
));
176 pluto_lock_created
= TRUE
;
180 static bool fill_lock(int lockfd
, pid_t pid
)
182 char buf
[30]; /* holds "<pid>\n" */
183 int len
= snprintf(buf
, sizeof(buf
), "%u\n", (unsigned int) pid
);
184 bool ok
= len
> 0 && write(lockfd
, buf
, len
) == len
;
190 static void delete_lock(void)
192 if (pluto_lock_created
)
195 unlink(pluto_lock
); /* is noting failure useful? */
200 /* by default pluto sends certificate requests to its peers */
201 bool no_cr_send
= FALSE
;
203 /* by default the CRL policy is lenient */
204 bool strict_crl_policy
= FALSE
;
206 /* by default CRLs are cached locally as files */
207 bool cache_crls
= FALSE
;
209 /* by default pluto does not check crls dynamically */
210 long crl_check_interval
= 0;
212 /* path to the PKCS#11 module */
213 char *pkcs11_module_path
= NULL
;
215 /* by default pluto logs out after every smartcard use */
216 bool pkcs11_keep_state
= FALSE
;
218 /* by default pluto does not allow pkcs11 proxy access via whack */
219 bool pkcs11_proxy
= FALSE
;
221 /* argument string to pass to PKCS#11 module.
222 * Not used for compliant modules, just for NSS softoken
224 static const char *pkcs11_init_args
= NULL
;
226 /* options read by optionsfrom */
232 static void print_plugins()
234 char buf
[BUF_LEN
], *plugin
;
236 enumerator_t
*enumerator
;
239 enumerator
= lib
->plugins
->create_plugin_enumerator(lib
->plugins
);
240 while (len
< BUF_LEN
&& enumerator
->enumerate(enumerator
, &plugin
))
242 len
+= snprintf(&buf
[len
], BUF_LEN
-len
, "%s ", plugin
);
244 enumerator
->destroy(enumerator
);
245 DBG1("loaded plugins: %s", buf
);
248 int main(int argc
, char **argv
)
250 bool fork_desired
= TRUE
;
251 bool log_to_stderr_desired
= FALSE
;
252 bool nat_traversal
= FALSE
;
253 bool nat_t_spf
= TRUE
; /* support port floating */
254 unsigned int keep_alive
= 0;
255 bool force_keepalive
= FALSE
;
256 char *virtual_private
= NULL
;
260 int keep
[] = { CAP_NET_ADMIN
, CAP_NET_BIND_SERVICE
};
261 #endif /* CAPABILITIES */
263 /* initialize library and optionsfrom */
264 if (!library_init(NULL
))
267 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY
);
269 if (lib
->integrity
&&
270 !lib
->integrity
->check_file(lib
->integrity
, "pluto", argv
[0]))
272 fprintf(stderr
, "integrity check of pluto failed\n");
274 exit(SS_RC_DAEMON_INTEGRITY
);
276 options
= options_create();
278 /* handle arguments */
281 # define DBG_OFFSET 256
282 static const struct option long_opts
[] = {
283 /* name, has_arg, flag, val */
284 { "help", no_argument
, NULL
, 'h' },
285 { "version", no_argument
, NULL
, 'v' },
286 { "optionsfrom", required_argument
, NULL
, '+' },
287 { "nofork", no_argument
, NULL
, 'd' },
288 { "stderrlog", no_argument
, NULL
, 'e' },
289 { "noklips", no_argument
, NULL
, 'n' },
290 { "nocrsend", no_argument
, NULL
, 'c' },
291 { "strictcrlpolicy", no_argument
, NULL
, 'r' },
292 { "crlcheckinterval", required_argument
, NULL
, 'x'},
293 { "cachecrls", no_argument
, NULL
, 'C' },
294 { "uniqueids", no_argument
, NULL
, 'u' },
295 { "interface", required_argument
, NULL
, 'i' },
296 { "ikeport", required_argument
, NULL
, 'p' },
297 { "ctlbase", required_argument
, NULL
, 'b' },
298 { "secretsfile", required_argument
, NULL
, 's' },
299 { "foodgroupsdir", required_argument
, NULL
, 'f' },
300 { "perpeerlogbase", required_argument
, NULL
, 'P' },
301 { "perpeerlog", no_argument
, NULL
, 'l' },
302 { "policygroupsdir", required_argument
, NULL
, 'f' },
304 { "lwdnsq", required_argument
, NULL
, 'a' },
305 #else /* !USE_LWRES */
306 { "adns", required_argument
, NULL
, 'a' },
307 #endif /* !USE_LWRES */
308 { "pkcs11module", required_argument
, NULL
, 'm' },
309 { "pkcs11keepstate", no_argument
, NULL
, 'k' },
310 { "pkcs11initargs", required_argument
, NULL
, 'z' },
311 { "pkcs11proxy", no_argument
, NULL
, 'y' },
312 { "nat_traversal", no_argument
, NULL
, '1' },
313 { "keep_alive", required_argument
, NULL
, '2' },
314 { "force_keepalive", no_argument
, NULL
, '3' },
315 { "disable_port_floating", no_argument
, NULL
, '4' },
316 { "debug-natt", no_argument
, NULL
, '5' },
317 { "virtual_private", required_argument
, NULL
, '6' },
319 { "debug-none", no_argument
, NULL
, 'N' },
320 { "debug-all", no_argument
, NULL
, 'A' },
321 { "debug-raw", no_argument
, NULL
, DBG_RAW
+ DBG_OFFSET
},
322 { "debug-crypt", no_argument
, NULL
, DBG_CRYPT
+ DBG_OFFSET
},
323 { "debug-parsing", no_argument
, NULL
, DBG_PARSING
+ DBG_OFFSET
},
324 { "debug-emitting", no_argument
, NULL
, DBG_EMITTING
+ DBG_OFFSET
},
325 { "debug-control", no_argument
, NULL
, DBG_CONTROL
+ DBG_OFFSET
},
326 { "debug-lifecycle", no_argument
, NULL
, DBG_LIFECYCLE
+ DBG_OFFSET
},
327 { "debug-klips", no_argument
, NULL
, DBG_KLIPS
+ DBG_OFFSET
},
328 { "debug-dns", no_argument
, NULL
, DBG_DNS
+ DBG_OFFSET
},
329 { "debug-oppo", no_argument
, NULL
, DBG_OPPO
+ DBG_OFFSET
},
330 { "debug-controlmore", no_argument
, NULL
, DBG_CONTROLMORE
+ DBG_OFFSET
},
331 { "debug-private", no_argument
, NULL
, DBG_PRIVATE
+ DBG_OFFSET
},
333 { "impair-delay-adns-key-answer", no_argument
, NULL
, IMPAIR_DELAY_ADNS_KEY_ANSWER
+ DBG_OFFSET
},
334 { "impair-delay-adns-txt-answer", no_argument
, NULL
, IMPAIR_DELAY_ADNS_TXT_ANSWER
+ DBG_OFFSET
},
335 { "impair-bust-mi2", no_argument
, NULL
, IMPAIR_BUST_MI2
+ DBG_OFFSET
},
336 { "impair-bust-mr2", no_argument
, NULL
, IMPAIR_BUST_MR2
+ DBG_OFFSET
},
340 /* Note: we don't like the way short options get parsed
341 * by getopt_long, so we simply pass an empty string as
342 * the list. It could be "hvdenp:l:s:" "NARXPECK".
344 int c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
346 /* Note: "breaking" from case terminates loop */
349 case EOF
: /* end of flags */
352 case 0: /* long option already handled */
355 case ':': /* diagnostic already printed by getopt_long */
356 case '?': /* diagnostic already printed by getopt_long */
358 break; /* not actually reached */
360 case 'h': /* --help */
362 break; /* not actually reached */
364 case 'v': /* --version */
366 const char **sp
= ipsec_copyright_notice();
368 printf("strongSwan "VERSION
"%s\n", compile_time_interop_options
);
369 for (; *sp
!= NULL
; sp
++)
373 break; /* not actually reached */
375 case '+': /* --optionsfrom <filename> */
376 if (!options
->from(options
, optarg
, &argc
, &argv
, optind
))
382 case 'd': /* --nofork*/
383 fork_desired
= FALSE
;
386 case 'e': /* --stderrlog */
387 log_to_stderr_desired
= TRUE
;
390 case 'n': /* --noklips */
394 case 'c': /* --nocrsend */
398 case 'r': /* --strictcrlpolicy */
399 strict_crl_policy
= TRUE
;
402 case 'x': /* --crlcheckinterval <time>*/
403 if (optarg
== NULL
|| !isdigit(optarg
[0]))
404 usage("missing interval time");
408 long interval
= strtol(optarg
, &endptr
, 0);
410 if (*endptr
!= '\0' || endptr
== optarg
412 usage("<interval-time> must be a positive number");
413 crl_check_interval
= interval
;
417 case 'C': /* --cachecrls */
421 case 'u': /* --uniqueids */
425 case 'i': /* --interface <ifname> */
426 if (!use_interface(optarg
))
427 usage("too many --interface specifications");
430 case 'p': /* --port <portnumber> */
431 if (optarg
== NULL
|| !isdigit(optarg
[0]))
432 usage("missing port number");
436 long port
= strtol(optarg
, &endptr
, 0);
438 if (*endptr
!= '\0' || endptr
== optarg
439 || port
<= 0 || port
> 0x10000)
440 usage("<port-number> must be a number between 1 and 65535");
445 case 'b': /* --ctlbase <path> */
446 if (snprintf(ctl_addr
.sun_path
, sizeof(ctl_addr
.sun_path
)
447 , "%s%s", optarg
, CTL_SUFFIX
) == -1)
448 usage("<path>" CTL_SUFFIX
" too long for sun_path");
449 if (snprintf(info_addr
.sun_path
, sizeof(info_addr
.sun_path
)
450 , "%s%s", optarg
, INFO_SUFFIX
) == -1)
451 usage("<path>" INFO_SUFFIX
" too long for sun_path");
452 if (snprintf(pluto_lock
, sizeof(pluto_lock
)
453 , "%s%s", optarg
, LOCK_SUFFIX
) == -1)
454 usage("<path>" LOCK_SUFFIX
" must fit");
457 case 's': /* --secretsfile <secrets-file> */
458 shared_secrets_file
= optarg
;
461 case 'f': /* --policygroupsdir <policygroups-dir> */
462 policygroups_dir
= optarg
;
465 case 'a': /* --adns <pathname> */
466 pluto_adns_option
= optarg
;
469 case 'm': /* --pkcs11module <pathname> */
470 pkcs11_module_path
= optarg
;
473 case 'k': /* --pkcs11keepstate */
474 pkcs11_keep_state
= TRUE
;
477 case 'y': /* --pkcs11proxy */
481 case 'z': /* --pkcs11initargs */
482 pkcs11_init_args
= optarg
;
486 case 'N': /* --debug-none */
487 base_debugging
= DBG_NONE
;
490 case 'A': /* --debug-all */
491 base_debugging
= DBG_ALL
;
495 case 'P': /* --perpeerlogbase */
496 base_perpeer_logdir
= optarg
;
500 log_to_perpeer
= TRUE
;
503 case '1': /* --nat_traversal */
504 nat_traversal
= TRUE
;
506 case '2': /* --keep_alive */
507 keep_alive
= atoi(optarg
);
509 case '3': /* --force_keepalive */
510 force_keepalive
= TRUE
;
512 case '4': /* --disable_port_floating */
515 case '5': /* --debug-nat_t */
516 base_debugging
|= DBG_NATT
;
518 case '6': /* --virtual_private */
519 virtual_private
= optarg
;
526 base_debugging
|= c
- DBG_OFFSET
;
536 usage("unexpected argument");
538 lockfd
= create_lock();
540 /* select between logging methods */
542 if (log_to_stderr_desired
)
544 log_to_syslog
= FALSE
;
548 log_to_stderr
= FALSE
;
551 /* set the logging function of pfkey debugging */
553 pfkey_debug_func
= DBG_log
;
555 pfkey_debug_func
= NULL
;
558 /* create control socket.
559 * We must create it before the parent process returns so that
560 * there will be no race condition in using it. The easiest
561 * place to do this is before the daemon fork.
564 err_t ugh
= init_ctl_socket();
568 fprintf(stderr
, "pluto: %s", ugh
);
573 /* If not suppressed, do daemon fork */
584 fprintf(stderr
, "pluto: fork failed (%d %s)\n",
591 /* parent: die, after filling PID into lock file.
592 * must not use exit_pluto: lock would be removed!
594 exit(fill_lock(lockfd
, pid
)?
0 : 1);
602 fprintf(stderr
, "setsid() failed in main(). Errno %d: %s\n",
609 /* no daemon fork: we have to fill in lock file */
610 (void) fill_lock(lockfd
, getpid());
611 fprintf(stdout
, "Pluto initialized\n");
615 /* Close everything but ctl_fd and (if needed) stderr.
616 * There is some danger that a library that we don't know
617 * about is using some fd that we don't know about.
618 * I guess we'll soon find out.
623 for (i
= getdtablesize() - 1; i
>= 0; i
--) /* Bad hack */
625 if ((!log_to_stderr
|| i
!= 2) && i
!= ctl_fd
)
629 /* make sure that stdin, stdout, stderr are reserved */
630 if (open("/dev/null", O_RDONLY
) != 0)
634 if (!log_to_stderr
&& dup2(0, 2) != 2)
641 /* Note: some scripts may look for this exact message -- don't change
642 * ipsec barf was one, but it no longer does.
644 plog("Starting IKEv1 pluto daemon (strongSwan "VERSION
")%s",
645 compile_time_interop_options
);
649 plog("integrity tests enabled:");
650 plog("lib 'libstrongswan': passed file and segment integrity tests");
651 plog("daemon 'pluto': passed file integrity test");
654 /* load plugins, further infrastructure may need it */
655 if (!lib
->plugins
->load(lib
->plugins
, NULL
,
656 lib
->settings
->get_str(lib
->settings
, "pluto.load", PLUGINS
)))
658 exit(SS_RC_INITIALIZATION_FAILED
);
663 if (!init_secret() || !init_crypto())
665 plog("initialization failed - aborting pluto");
666 exit_pluto(SS_RC_INITIALIZATION_FAILED
);
668 init_nat_traversal(nat_traversal
, keep_alive
, force_keepalive
, nat_t_spf
);
669 init_virtual_ip(virtual_private
);
670 scx_init(pkcs11_module_path
, pkcs11_init_args
);
680 /* drop unneeded capabilities and change UID/GID */
681 prctl(PR_SET_KEEPCAPS
, 1);
685 struct group group
, *grp
;
688 if (getgrnam_r(IPSEC_GROUP
, &group
, buf
, sizeof(buf
), &grp
) != 0 ||
689 grp
== NULL
|| setgid(grp
->gr_gid
) != 0)
691 plog("unable to change daemon group");
698 struct passwd passwd
, *pwp
;
701 if (getpwnam_r(IPSEC_USER
, &passwd
, buf
, sizeof(buf
), &pwp
) != 0 ||
702 pwp
== NULL
|| setuid(pwp
->pw_uid
) != 0)
704 plog("unable to change daemon user");
712 cap_set_flag(caps
, CAP_EFFECTIVE
, 2, keep
, CAP_SET
);
713 cap_set_flag(caps
, CAP_INHERITABLE
, 2, keep
, CAP_SET
);
714 cap_set_flag(caps
, CAP_PERMITTED
, 2, keep
, CAP_SET
);
715 if (cap_set_proc(caps
) != 0)
717 plog("unable to drop daemon capabilities");
721 #endif /* CAPABILITIES */
723 /* loading X.509 CA certificates */
724 load_authcerts("ca", CA_CERT_PATH
, X509_CA
);
725 /* loading X.509 AA certificates */
726 load_authcerts("aa", AA_CERT_PATH
, X509_AA
);
727 /* loading X.509 OCSP certificates */
728 load_authcerts("ocsp", OCSP_CERT_PATH
, X509_OCSP_SIGNER
);
729 /* loading X.509 CRLs */
731 /* loading attribute certificates (experimental) */
736 return -1; /* Shouldn't ever reach this */
739 /* leave pluto, with status.
740 * Once child is launched, parent must not exit this way because
741 * the lock would be released.
744 * 1 general discomfort
745 * 10 lock file exists
747 void exit_pluto(int status
)
749 reset_globals(); /* needed because we may be called in odd state */
750 free_preshared_secrets();
751 free_remembered_public_keys();
752 delete_every_connection();
753 fetch_finalize(); /* stop fetching thread */
754 free_crl_fetch(); /* free chain of crl fetch requests */
755 free_ocsp_fetch(); /* free chain of ocsp fetch requests */
756 free_authcerts(); /* free chain of X.509 authority certificates */
757 free_crls(); /* free chain of X.509 CRLs */
758 free_ca_infos(); /* free chain of X.509 CA information records */
759 free_ocsp(); /* free ocsp cache */
761 ac_finalize(); /* free X.509 attribute certificates */
762 scx_finalize(); /* finalize and unload PKCS #11 module */
763 xauth_finalize(); /* finalize and unload XAUTH module */
767 free_myid(); /* free myids */
768 free_events(); /* free remaining events */
769 free_vendorid(); /* free all vendor id records */
772 options
->destroy(options
);