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"
77 static void usage(const char *mess
)
79 if (mess
!= NULL
&& *mess
!= '\0')
80 fprintf(stderr
, "%s\n", mess
);
85 " [--optionsfrom <filename>]"
93 " [--crlcheckinterval <interval>]"
97 "[--interface <ifname>]"
98 " [--ikeport <port-number>]"
102 "[--perpeerlogbase <path>] [--perpeerlog]"
104 "[--secretsfile <secrets-file>]"
105 " [--policygroupsdir <policygroups-dir>]"
107 "[--adns <pathname>]"
108 "[--pkcs11module <path>]"
109 "[--pkcs11keepstate]"
110 "[--pkcs11initargs <string>]"
119 " [--debug-emitting]"
122 " [--debug-lifecycle]"
127 " [--debug-controlmore]"
132 "[--nat_traversal] [--keep_alive <delay_sec>]"
134 "[--force_keepalive] [--disable_port_floating]"
136 "[--virtual_private <network_list>]"
138 "strongSwan "VERSION
"\n");
139 exit_pluto(mess
== NULL?
0 : 1);
144 * - provides convenient way for scripts to find Pluto's pid
145 * - prevents multiple Plutos competing for the same port
146 * - same basename as unix domain control socket
147 * NOTE: will not take account of sharing LOCK_DIR with other systems.
150 static char pluto_lock
[sizeof(ctl_addr
.sun_path
)] = DEFAULT_CTLBASE LOCK_SUFFIX
;
151 static bool pluto_lock_created
= FALSE
;
153 /* create lockfile, or die in the attempt */
154 static int create_lock(void)
156 int fd
= open(pluto_lock
, O_WRONLY
| O_CREAT
| O_EXCL
| O_TRUNC
157 , S_IRUSR
| S_IRGRP
| S_IROTH
);
163 fprintf(stderr
, "pluto: lock file \"%s\" already exists\n"
170 , "pluto: unable to create lock file \"%s\" (%d %s)\n"
171 , pluto_lock
, errno
, strerror(errno
));
175 pluto_lock_created
= TRUE
;
179 static bool fill_lock(int lockfd
, pid_t pid
)
181 char buf
[30]; /* holds "<pid>\n" */
182 int len
= snprintf(buf
, sizeof(buf
), "%u\n", (unsigned int) pid
);
183 bool ok
= len
> 0 && write(lockfd
, buf
, len
) == len
;
189 static void delete_lock(void)
191 if (pluto_lock_created
)
194 unlink(pluto_lock
); /* is noting failure useful? */
199 /* by default pluto sends certificate requests to its peers */
200 bool no_cr_send
= FALSE
;
202 /* by default the CRL policy is lenient */
203 bool strict_crl_policy
= FALSE
;
205 /* by default CRLs are cached locally as files */
206 bool cache_crls
= FALSE
;
208 /* by default pluto does not check crls dynamically */
209 long crl_check_interval
= 0;
211 /* path to the PKCS#11 module */
212 char *pkcs11_module_path
= NULL
;
214 /* by default pluto logs out after every smartcard use */
215 bool pkcs11_keep_state
= FALSE
;
217 /* by default pluto does not allow pkcs11 proxy access via whack */
218 bool pkcs11_proxy
= FALSE
;
220 /* argument string to pass to PKCS#11 module.
221 * Not used for compliant modules, just for NSS softoken
223 static const char *pkcs11_init_args
= NULL
;
225 /* options read by optionsfrom */
231 static void print_plugins()
233 char buf
[BUF_LEN
], *plugin
;
235 enumerator_t
*enumerator
;
238 enumerator
= lib
->plugins
->create_plugin_enumerator(lib
->plugins
);
239 while (len
< BUF_LEN
&& enumerator
->enumerate(enumerator
, &plugin
))
241 len
+= snprintf(&buf
[len
], BUF_LEN
-len
, "%s ", plugin
);
243 enumerator
->destroy(enumerator
);
244 DBG1("loaded plugins: %s", buf
);
247 int main(int argc
, char **argv
)
249 bool fork_desired
= TRUE
;
250 bool log_to_stderr_desired
= FALSE
;
251 bool nat_traversal
= FALSE
;
252 bool nat_t_spf
= TRUE
; /* support port floating */
253 unsigned int keep_alive
= 0;
254 bool force_keepalive
= FALSE
;
255 char *virtual_private
= NULL
;
259 int keep
[] = { CAP_NET_ADMIN
, CAP_NET_BIND_SERVICE
};
260 #endif /* CAPABILITIES */
262 /* initialize library and optionsfrom */
263 if (!library_init(STRONGSWAN_CONF
))
267 if (lib
->integrity
&&
268 !lib
->integrity
->check_file(lib
->integrity
, "pluto", argv
[0]))
270 fprintf(stderr
, "integrity check of pluto failed\n");
274 options
= options_create();
276 /* handle arguments */
279 # define DBG_OFFSET 256
280 static const struct option long_opts
[] = {
281 /* name, has_arg, flag, val */
282 { "help", no_argument
, NULL
, 'h' },
283 { "version", no_argument
, NULL
, 'v' },
284 { "optionsfrom", required_argument
, NULL
, '+' },
285 { "nofork", no_argument
, NULL
, 'd' },
286 { "stderrlog", no_argument
, NULL
, 'e' },
287 { "noklips", no_argument
, NULL
, 'n' },
288 { "nocrsend", no_argument
, NULL
, 'c' },
289 { "strictcrlpolicy", no_argument
, NULL
, 'r' },
290 { "crlcheckinterval", required_argument
, NULL
, 'x'},
291 { "cachecrls", no_argument
, NULL
, 'C' },
292 { "uniqueids", no_argument
, NULL
, 'u' },
293 { "interface", required_argument
, NULL
, 'i' },
294 { "ikeport", required_argument
, NULL
, 'p' },
295 { "ctlbase", required_argument
, NULL
, 'b' },
296 { "secretsfile", required_argument
, NULL
, 's' },
297 { "foodgroupsdir", required_argument
, NULL
, 'f' },
298 { "perpeerlogbase", required_argument
, NULL
, 'P' },
299 { "perpeerlog", no_argument
, NULL
, 'l' },
300 { "policygroupsdir", required_argument
, NULL
, 'f' },
302 { "lwdnsq", required_argument
, NULL
, 'a' },
303 #else /* !USE_LWRES */
304 { "adns", required_argument
, NULL
, 'a' },
305 #endif /* !USE_LWRES */
306 { "pkcs11module", required_argument
, NULL
, 'm' },
307 { "pkcs11keepstate", no_argument
, NULL
, 'k' },
308 { "pkcs11initargs", required_argument
, NULL
, 'z' },
309 { "pkcs11proxy", no_argument
, NULL
, 'y' },
310 { "nat_traversal", no_argument
, NULL
, '1' },
311 { "keep_alive", required_argument
, NULL
, '2' },
312 { "force_keepalive", no_argument
, NULL
, '3' },
313 { "disable_port_floating", no_argument
, NULL
, '4' },
314 { "debug-natt", no_argument
, NULL
, '5' },
315 { "virtual_private", required_argument
, NULL
, '6' },
317 { "debug-none", no_argument
, NULL
, 'N' },
318 { "debug-all", no_argument
, NULL
, 'A' },
319 { "debug-raw", no_argument
, NULL
, DBG_RAW
+ DBG_OFFSET
},
320 { "debug-crypt", no_argument
, NULL
, DBG_CRYPT
+ DBG_OFFSET
},
321 { "debug-parsing", no_argument
, NULL
, DBG_PARSING
+ DBG_OFFSET
},
322 { "debug-emitting", no_argument
, NULL
, DBG_EMITTING
+ DBG_OFFSET
},
323 { "debug-control", no_argument
, NULL
, DBG_CONTROL
+ DBG_OFFSET
},
324 { "debug-lifecycle", no_argument
, NULL
, DBG_LIFECYCLE
+ DBG_OFFSET
},
325 { "debug-klips", no_argument
, NULL
, DBG_KLIPS
+ DBG_OFFSET
},
326 { "debug-dns", no_argument
, NULL
, DBG_DNS
+ DBG_OFFSET
},
327 { "debug-oppo", no_argument
, NULL
, DBG_OPPO
+ DBG_OFFSET
},
328 { "debug-controlmore", no_argument
, NULL
, DBG_CONTROLMORE
+ DBG_OFFSET
},
329 { "debug-private", no_argument
, NULL
, DBG_PRIVATE
+ DBG_OFFSET
},
331 { "impair-delay-adns-key-answer", no_argument
, NULL
, IMPAIR_DELAY_ADNS_KEY_ANSWER
+ DBG_OFFSET
},
332 { "impair-delay-adns-txt-answer", no_argument
, NULL
, IMPAIR_DELAY_ADNS_TXT_ANSWER
+ DBG_OFFSET
},
333 { "impair-bust-mi2", no_argument
, NULL
, IMPAIR_BUST_MI2
+ DBG_OFFSET
},
334 { "impair-bust-mr2", no_argument
, NULL
, IMPAIR_BUST_MR2
+ DBG_OFFSET
},
338 /* Note: we don't like the way short options get parsed
339 * by getopt_long, so we simply pass an empty string as
340 * the list. It could be "hvdenp:l:s:" "NARXPECK".
342 int c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
344 /* Note: "breaking" from case terminates loop */
347 case EOF
: /* end of flags */
350 case 0: /* long option already handled */
353 case ':': /* diagnostic already printed by getopt_long */
354 case '?': /* diagnostic already printed by getopt_long */
356 break; /* not actually reached */
358 case 'h': /* --help */
360 break; /* not actually reached */
362 case 'v': /* --version */
364 const char **sp
= ipsec_copyright_notice();
366 printf("strongSwan "VERSION
"%s\n", compile_time_interop_options
);
367 for (; *sp
!= NULL
; sp
++)
371 break; /* not actually reached */
373 case '+': /* --optionsfrom <filename> */
374 if (!options
->from(options
, optarg
, &argc
, &argv
, optind
))
380 case 'd': /* --nofork*/
381 fork_desired
= FALSE
;
384 case 'e': /* --stderrlog */
385 log_to_stderr_desired
= TRUE
;
388 case 'n': /* --noklips */
392 case 'c': /* --nocrsend */
396 case 'r': /* --strictcrlpolicy */
397 strict_crl_policy
= TRUE
;
400 case 'x': /* --crlcheckinterval <time>*/
401 if (optarg
== NULL
|| !isdigit(optarg
[0]))
402 usage("missing interval time");
406 long interval
= strtol(optarg
, &endptr
, 0);
408 if (*endptr
!= '\0' || endptr
== optarg
410 usage("<interval-time> must be a positive number");
411 crl_check_interval
= interval
;
415 case 'C': /* --cachecrls */
419 case 'u': /* --uniqueids */
423 case 'i': /* --interface <ifname> */
424 if (!use_interface(optarg
))
425 usage("too many --interface specifications");
428 case 'p': /* --port <portnumber> */
429 if (optarg
== NULL
|| !isdigit(optarg
[0]))
430 usage("missing port number");
434 long port
= strtol(optarg
, &endptr
, 0);
436 if (*endptr
!= '\0' || endptr
== optarg
437 || port
<= 0 || port
> 0x10000)
438 usage("<port-number> must be a number between 1 and 65535");
443 case 'b': /* --ctlbase <path> */
444 if (snprintf(ctl_addr
.sun_path
, sizeof(ctl_addr
.sun_path
)
445 , "%s%s", optarg
, CTL_SUFFIX
) == -1)
446 usage("<path>" CTL_SUFFIX
" too long for sun_path");
447 if (snprintf(info_addr
.sun_path
, sizeof(info_addr
.sun_path
)
448 , "%s%s", optarg
, INFO_SUFFIX
) == -1)
449 usage("<path>" INFO_SUFFIX
" too long for sun_path");
450 if (snprintf(pluto_lock
, sizeof(pluto_lock
)
451 , "%s%s", optarg
, LOCK_SUFFIX
) == -1)
452 usage("<path>" LOCK_SUFFIX
" must fit");
455 case 's': /* --secretsfile <secrets-file> */
456 shared_secrets_file
= optarg
;
459 case 'f': /* --policygroupsdir <policygroups-dir> */
460 policygroups_dir
= optarg
;
463 case 'a': /* --adns <pathname> */
464 pluto_adns_option
= optarg
;
467 case 'm': /* --pkcs11module <pathname> */
468 pkcs11_module_path
= optarg
;
471 case 'k': /* --pkcs11keepstate */
472 pkcs11_keep_state
= TRUE
;
475 case 'y': /* --pkcs11proxy */
479 case 'z': /* --pkcs11initargs */
480 pkcs11_init_args
= optarg
;
484 case 'N': /* --debug-none */
485 base_debugging
= DBG_NONE
;
488 case 'A': /* --debug-all */
489 base_debugging
= DBG_ALL
;
493 case 'P': /* --perpeerlogbase */
494 base_perpeer_logdir
= optarg
;
498 log_to_perpeer
= TRUE
;
501 case '1': /* --nat_traversal */
502 nat_traversal
= TRUE
;
504 case '2': /* --keep_alive */
505 keep_alive
= atoi(optarg
);
507 case '3': /* --force_keepalive */
508 force_keepalive
= TRUE
;
510 case '4': /* --disable_port_floating */
513 case '5': /* --debug-nat_t */
514 base_debugging
|= DBG_NATT
;
516 case '6': /* --virtual_private */
517 virtual_private
= optarg
;
524 base_debugging
|= c
- DBG_OFFSET
;
534 usage("unexpected argument");
536 lockfd
= create_lock();
538 /* select between logging methods */
540 if (log_to_stderr_desired
)
542 log_to_syslog
= FALSE
;
546 log_to_stderr
= FALSE
;
549 /* set the logging function of pfkey debugging */
551 pfkey_debug_func
= DBG_log
;
553 pfkey_debug_func
= NULL
;
556 /* create control socket.
557 * We must create it before the parent process returns so that
558 * there will be no race condition in using it. The easiest
559 * place to do this is before the daemon fork.
562 err_t ugh
= init_ctl_socket();
566 fprintf(stderr
, "pluto: %s", ugh
);
571 /* If not suppressed, do daemon fork */
582 fprintf(stderr
, "pluto: fork failed (%d %s)\n",
589 /* parent: die, after filling PID into lock file.
590 * must not use exit_pluto: lock would be removed!
592 exit(fill_lock(lockfd
, pid
)?
0 : 1);
600 fprintf(stderr
, "setsid() failed in main(). Errno %d: %s\n",
607 /* no daemon fork: we have to fill in lock file */
608 (void) fill_lock(lockfd
, getpid());
609 fprintf(stdout
, "Pluto initialized\n");
613 /* Close everything but ctl_fd and (if needed) stderr.
614 * There is some danger that a library that we don't know
615 * about is using some fd that we don't know about.
616 * I guess we'll soon find out.
621 for (i
= getdtablesize() - 1; i
>= 0; i
--) /* Bad hack */
623 if ((!log_to_stderr
|| i
!= 2) && i
!= ctl_fd
)
627 /* make sure that stdin, stdout, stderr are reserved */
628 if (open("/dev/null", O_RDONLY
) != 0)
632 if (!log_to_stderr
&& dup2(0, 2) != 2)
639 /* Note: some scripts may look for this exact message -- don't change
640 * ipsec barf was one, but it no longer does.
642 plog("Starting IKEv1 pluto daemon (strongSwan "VERSION
")%s",
643 compile_time_interop_options
);
645 /* load plugins, further infrastructure may need it */
646 lib
->plugins
->load(lib
->plugins
, IPSEC_PLUGINDIR
,
647 lib
->settings
->get_str(lib
->settings
, "pluto.load", PLUGINS
));
650 init_nat_traversal(nat_traversal
, keep_alive
, force_keepalive
, nat_t_spf
);
651 init_virtual_ip(virtual_private
);
652 scx_init(pkcs11_module_path
, pkcs11_init_args
);
663 /* drop unneeded capabilities and change UID/GID */
664 prctl(PR_SET_KEEPCAPS
, 1);
668 struct group group
, *grp
;
671 if (getgrnam_r(IPSEC_GROUP
, &group
, buf
, sizeof(buf
), &grp
) != 0 ||
672 grp
== NULL
|| setgid(grp
->gr_gid
) != 0)
674 plog("unable to change daemon group");
681 struct passwd passwd
, *pwp
;
684 if (getpwnam_r(IPSEC_USER
, &passwd
, buf
, sizeof(buf
), &pwp
) != 0 ||
685 pwp
== NULL
|| setuid(pwp
->pw_uid
) != 0)
687 plog("unable to change daemon user");
695 cap_set_flag(caps
, CAP_EFFECTIVE
, 2, keep
, CAP_SET
);
696 cap_set_flag(caps
, CAP_INHERITABLE
, 2, keep
, CAP_SET
);
697 cap_set_flag(caps
, CAP_PERMITTED
, 2, keep
, CAP_SET
);
698 if (cap_set_proc(caps
) != 0)
700 plog("unable to drop daemon capabilities");
704 #endif /* CAPABILITIES */
706 /* loading X.509 CA certificates */
707 load_authcerts("CA cert", CA_CERT_PATH
, AUTH_CA
);
708 /* loading X.509 AA certificates */
709 load_authcerts("AA cert", AA_CERT_PATH
, AUTH_AA
);
710 /* loading X.509 OCSP certificates */
711 load_authcerts("OCSP cert", OCSP_CERT_PATH
, AUTH_OCSP
);
712 /* loading X.509 CRLs */
714 /* loading attribute certificates (experimental) */
719 return -1; /* Shouldn't ever reach this */
722 /* leave pluto, with status.
723 * Once child is launched, parent must not exit this way because
724 * the lock would be released.
727 * 1 general discomfort
728 * 10 lock file exists
730 void exit_pluto(int status
)
732 reset_globals(); /* needed because we may be called in odd state */
733 free_preshared_secrets();
734 free_remembered_public_keys();
735 delete_every_connection();
736 free_crl_fetch(); /* free chain of crl fetch requests */
737 free_ocsp_fetch(); /* free chain of ocsp fetch requests */
738 free_authcerts(); /* free chain of X.509 authority certificates */
739 free_crls(); /* free chain of X.509 CRLs */
740 free_acerts(); /* free chain of X.509 attribute certificates */
741 free_ca_infos(); /* free chain of X.509 CA information records */
742 free_ocsp(); /* free ocsp cache */
744 scx_finalize(); /* finalize and unload PKCS #11 module */
745 xauth_finalize(); /* finalize and unload XAUTH module */
749 free_id(); /* free myids */
750 free_events(); /* free remaining events */
751 free_vendorid(); /* free all vendor id records */
753 options
->destroy(options
);