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>
32 #include <linux/capability.h>
33 #include <sys/prctl.h>
40 #include "constants.h"
46 #include "connections.h"
47 #include "foodgroups.h"
49 #include "demux.h" /* needs packet.h */
54 #include "adns.h" /* needs <resolv.h> */
55 #include "dnskey.h" /* needs keys.h and adns.h */
58 #include "ipsec_doi.h" /* needs demux.h and state.h */
65 #include "crypto.h" /* requires sha1.h and md5.h */
66 #include "nat_traversal.h"
69 /* on some distros, a capset() definition is missing */
70 #ifdef NO_CAPSET_DEFINED
71 extern int capset(cap_user_header_t hdrp
, const cap_user_data_t datap
);
72 #endif /* NO_CAPSET_DEFINED */
75 usage(const char *mess
)
77 if (mess
!= NULL
&& *mess
!= '\0')
78 fprintf(stderr
, "%s\n", mess
);
83 " [--optionsfrom <filename>]"
91 " [--crlcheckinterval <interval>]"
95 "[--interface <ifname>]"
96 " [--ikeport <port-number>]"
100 "[--perpeerlogbase <path>] [--perpeerlog]"
102 "[--secretsfile <secrets-file>]"
103 " [--policygroupsdir <policygroups-dir>]"
105 "[--adns <pathname>]"
106 "[--pkcs11module <path>]"
107 "[--pkcs11keepstate]"
108 "[--pkcs11initargs <string>]"
117 " [--debug-emitting]"
120 " [--debug-lifecycle]"
125 " [--debug-controlmore]"
130 "[--nat_traversal] [--keep_alive <delay_sec>]"
132 "[--force_keepalive] [--disable_port_floating]"
134 "[--virtual_private <network_list>]"
137 , ipsec_version_code());
138 exit_pluto(mess
== NULL?
0 : 1);
143 * - provides convenient way for scripts to find Pluto's pid
144 * - prevents multiple Plutos competing for the same port
145 * - same basename as unix domain control socket
146 * NOTE: will not take account of sharing LOCK_DIR with other systems.
149 static char pluto_lock
[sizeof(ctl_addr
.sun_path
)] = DEFAULT_CTLBASE LOCK_SUFFIX
;
150 static bool pluto_lock_created
= FALSE
;
152 /* create lockfile, or die in the attempt */
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
;
180 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
;
193 if (pluto_lock_created
)
196 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
;
227 main(int argc
, char **argv
)
229 bool fork_desired
= TRUE
;
230 bool log_to_stderr_desired
= FALSE
;
231 bool nat_traversal
= FALSE
;
232 bool nat_t_spf
= TRUE
; /* support port floating */
233 unsigned int keep_alive
= 0;
234 bool force_keepalive
= FALSE
;
235 char *virtual_private
= NULL
;
237 struct __user_cap_header_struct hdr
;
238 struct __user_cap_data_struct data
;
240 /* handle arguments */
243 # define DBG_OFFSET 256
244 static const struct option long_opts
[] = {
245 /* name, has_arg, flag, val */
246 { "help", no_argument
, NULL
, 'h' },
247 { "version", no_argument
, NULL
, 'v' },
248 { "optionsfrom", required_argument
, NULL
, '+' },
249 { "nofork", no_argument
, NULL
, 'd' },
250 { "stderrlog", no_argument
, NULL
, 'e' },
251 { "noklips", no_argument
, NULL
, 'n' },
252 { "nocrsend", no_argument
, NULL
, 'c' },
253 { "strictcrlpolicy", no_argument
, NULL
, 'r' },
254 { "crlcheckinterval", required_argument
, NULL
, 'x'},
255 { "cachecrls", no_argument
, NULL
, 'C' },
256 { "uniqueids", no_argument
, NULL
, 'u' },
257 { "interface", required_argument
, NULL
, 'i' },
258 { "ikeport", required_argument
, NULL
, 'p' },
259 { "ctlbase", required_argument
, NULL
, 'b' },
260 { "secretsfile", required_argument
, NULL
, 's' },
261 { "foodgroupsdir", required_argument
, NULL
, 'f' },
262 { "perpeerlogbase", required_argument
, NULL
, 'P' },
263 { "perpeerlog", no_argument
, NULL
, 'l' },
264 { "policygroupsdir", required_argument
, NULL
, 'f' },
266 { "lwdnsq", required_argument
, NULL
, 'a' },
267 #else /* !USE_LWRES */
268 { "adns", required_argument
, NULL
, 'a' },
269 #endif /* !USE_LWRES */
270 { "pkcs11module", required_argument
, NULL
, 'm' },
271 { "pkcs11keepstate", no_argument
, NULL
, 'k' },
272 { "pkcs11initargs", required_argument
, NULL
, 'z' },
273 { "pkcs11proxy", no_argument
, NULL
, 'y' },
274 { "nat_traversal", no_argument
, NULL
, '1' },
275 { "keep_alive", required_argument
, NULL
, '2' },
276 { "force_keepalive", no_argument
, NULL
, '3' },
277 { "disable_port_floating", no_argument
, NULL
, '4' },
278 { "debug-natt", no_argument
, NULL
, '5' },
279 { "virtual_private", required_argument
, NULL
, '6' },
281 { "debug-none", no_argument
, NULL
, 'N' },
282 { "debug-all", no_argument
, NULL
, 'A' },
283 { "debug-raw", no_argument
, NULL
, DBG_RAW
+ DBG_OFFSET
},
284 { "debug-crypt", no_argument
, NULL
, DBG_CRYPT
+ DBG_OFFSET
},
285 { "debug-parsing", no_argument
, NULL
, DBG_PARSING
+ DBG_OFFSET
},
286 { "debug-emitting", no_argument
, NULL
, DBG_EMITTING
+ DBG_OFFSET
},
287 { "debug-control", no_argument
, NULL
, DBG_CONTROL
+ DBG_OFFSET
},
288 { "debug-lifecycle", no_argument
, NULL
, DBG_LIFECYCLE
+ DBG_OFFSET
},
289 { "debug-klips", no_argument
, NULL
, DBG_KLIPS
+ DBG_OFFSET
},
290 { "debug-dns", no_argument
, NULL
, DBG_DNS
+ DBG_OFFSET
},
291 { "debug-oppo", no_argument
, NULL
, DBG_OPPO
+ DBG_OFFSET
},
292 { "debug-controlmore", no_argument
, NULL
, DBG_CONTROLMORE
+ DBG_OFFSET
},
293 { "debug-private", no_argument
, NULL
, DBG_PRIVATE
+ DBG_OFFSET
},
295 { "impair-delay-adns-key-answer", no_argument
, NULL
, IMPAIR_DELAY_ADNS_KEY_ANSWER
+ DBG_OFFSET
},
296 { "impair-delay-adns-txt-answer", no_argument
, NULL
, IMPAIR_DELAY_ADNS_TXT_ANSWER
+ DBG_OFFSET
},
297 { "impair-bust-mi2", no_argument
, NULL
, IMPAIR_BUST_MI2
+ DBG_OFFSET
},
298 { "impair-bust-mr2", no_argument
, NULL
, IMPAIR_BUST_MR2
+ DBG_OFFSET
},
302 /* Note: we don't like the way short options get parsed
303 * by getopt_long, so we simply pass an empty string as
304 * the list. It could be "hvdenp:l:s:" "NARXPECK".
306 int c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
308 /* Note: "breaking" from case terminates loop */
311 case EOF
: /* end of flags */
314 case 0: /* long option already handled */
317 case ':': /* diagnostic already printed by getopt_long */
318 case '?': /* diagnostic already printed by getopt_long */
320 break; /* not actually reached */
322 case 'h': /* --help */
324 break; /* not actually reached */
326 case 'v': /* --version */
328 const char **sp
= ipsec_copyright_notice();
330 printf("%s%s\n", ipsec_version_string(),
331 compile_time_interop_options
);
332 for (; *sp
!= NULL
; sp
++)
336 break; /* not actually reached */
338 case '+': /* --optionsfrom <filename> */
339 optionsfrom(optarg
, &argc
, &argv
, optind
, stderr
);
340 /* does not return on error */
343 case 'd': /* --nofork*/
344 fork_desired
= FALSE
;
347 case 'e': /* --stderrlog */
348 log_to_stderr_desired
= TRUE
;
351 case 'n': /* --noklips */
355 case 'c': /* --nocrsend */
359 case 'r': /* --strictcrlpolicy */
360 strict_crl_policy
= TRUE
;
363 case 'x': /* --crlcheckinterval <time>*/
364 if (optarg
== NULL
|| !isdigit(optarg
[0]))
365 usage("missing interval time");
369 long interval
= strtol(optarg
, &endptr
, 0);
371 if (*endptr
!= '\0' || endptr
== optarg
373 usage("<interval-time> must be a positive number");
374 crl_check_interval
= interval
;
378 case 'C': /* --cachecrls */
382 case 'u': /* --uniqueids */
386 case 'i': /* --interface <ifname> */
387 if (!use_interface(optarg
))
388 usage("too many --interface specifications");
391 case 'p': /* --port <portnumber> */
392 if (optarg
== NULL
|| !isdigit(optarg
[0]))
393 usage("missing port number");
397 long port
= strtol(optarg
, &endptr
, 0);
399 if (*endptr
!= '\0' || endptr
== optarg
400 || port
<= 0 || port
> 0x10000)
401 usage("<port-number> must be a number between 1 and 65535");
406 case 'b': /* --ctlbase <path> */
407 if (snprintf(ctl_addr
.sun_path
, sizeof(ctl_addr
.sun_path
)
408 , "%s%s", optarg
, CTL_SUFFIX
) == -1)
409 usage("<path>" CTL_SUFFIX
" too long for sun_path");
410 if (snprintf(info_addr
.sun_path
, sizeof(info_addr
.sun_path
)
411 , "%s%s", optarg
, INFO_SUFFIX
) == -1)
412 usage("<path>" INFO_SUFFIX
" too long for sun_path");
413 if (snprintf(pluto_lock
, sizeof(pluto_lock
)
414 , "%s%s", optarg
, LOCK_SUFFIX
) == -1)
415 usage("<path>" LOCK_SUFFIX
" must fit");
418 case 's': /* --secretsfile <secrets-file> */
419 shared_secrets_file
= optarg
;
422 case 'f': /* --policygroupsdir <policygroups-dir> */
423 policygroups_dir
= optarg
;
426 case 'a': /* --adns <pathname> */
427 pluto_adns_option
= optarg
;
430 case 'm': /* --pkcs11module <pathname> */
431 pkcs11_module_path
= optarg
;
434 case 'k': /* --pkcs11keepstate */
435 pkcs11_keep_state
= TRUE
;
438 case 'y': /* --pkcs11proxy */
442 case 'z': /* --pkcs11initargs */
443 pkcs11_init_args
= optarg
;
447 case 'N': /* --debug-none */
448 base_debugging
= DBG_NONE
;
451 case 'A': /* --debug-all */
452 base_debugging
= DBG_ALL
;
456 case 'P': /* --perpeerlogbase */
457 base_perpeer_logdir
= optarg
;
461 log_to_perpeer
= TRUE
;
464 case '1': /* --nat_traversal */
465 nat_traversal
= TRUE
;
467 case '2': /* --keep_alive */
468 keep_alive
= atoi(optarg
);
470 case '3': /* --force_keepalive */
471 force_keepalive
= TRUE
;
473 case '4': /* --disable_port_floating */
476 case '5': /* --debug-nat_t */
477 base_debugging
|= DBG_NATT
;
479 case '6': /* --virtual_private */
480 virtual_private
= optarg
;
487 base_debugging
|= c
- DBG_OFFSET
;
497 usage("unexpected argument");
499 lockfd
= create_lock();
501 /* select between logging methods */
503 if (log_to_stderr_desired
)
504 log_to_syslog
= FALSE
;
506 log_to_stderr
= FALSE
;
508 /* set the logging function of pfkey debugging */
510 pfkey_debug_func
= DBG_log
;
512 pfkey_debug_func
= NULL
;
515 /* create control socket.
516 * We must create it before the parent process returns so that
517 * there will be no race condition in using it. The easiest
518 * place to do this is before the daemon fork.
521 err_t ugh
= init_ctl_socket();
525 fprintf(stderr
, "pluto: %s", ugh
);
530 /* If not suppressed, do daemon fork */
541 fprintf(stderr
, "pluto: fork failed (%d %s)\n",
548 /* parent: die, after filling PID into lock file.
549 * must not use exit_pluto: lock would be removed!
551 exit(fill_lock(lockfd
, pid
)?
0 : 1);
559 fprintf(stderr
, "setsid() failed in main(). Errno %d: %s\n",
566 /* no daemon fork: we have to fill in lock file */
567 (void) fill_lock(lockfd
, getpid());
568 fprintf(stdout
, "Pluto initialized\n");
572 /* Close everything but ctl_fd and (if needed) stderr.
573 * There is some danger that a library that we don't know
574 * about is using some fd that we don't know about.
575 * I guess we'll soon find out.
580 for (i
= getdtablesize() - 1; i
>= 0; i
--) /* Bad hack */
582 if ((!log_to_stderr
|| i
!= 2) && i
!= ctl_fd
)
586 /* make sure that stdin, stdout, stderr are reserved */
587 if (open("/dev/null", O_RDONLY
) != 0)
591 if (!log_to_stderr
&& dup2(0, 2) != 2)
598 /* Note: some scripts may look for this exact message -- don't change
599 * ipsec barf was one, but it no longer does.
601 plog("Starting Pluto (strongSwan Version %s%s)"
602 , ipsec_version_code()
603 , compile_time_interop_options
);
605 init_nat_traversal(nat_traversal
, keep_alive
, force_keepalive
, nat_t_spf
);
606 init_virtual_ip(virtual_private
);
607 scx_init(pkcs11_module_path
, pkcs11_init_args
); /* load and initialize PKCS #11 module */
608 xauth_init(); /* load and initialize XAUTH module */
619 /* drop unneeded capabilities and change UID/GID */
620 hdr
.version
= _LINUX_CAPABILITY_VERSION
;
622 data
.effective
= data
.permitted
= 1<<CAP_NET_ADMIN
| 1<<CAP_NET_BIND_SERVICE
;
623 data
.inheritable
= 0;
625 prctl(PR_SET_KEEPCAPS
, 1);
633 if (capset(&hdr
, &data
))
635 plog("unable to drop root privileges");
639 /* loading X.509 CA certificates */
640 load_authcerts("CA cert", CA_CERT_PATH
, AUTH_CA
);
641 /* loading X.509 AA certificates */
642 load_authcerts("AA cert", AA_CERT_PATH
, AUTH_AA
);
643 /* loading X.509 OCSP certificates */
644 load_authcerts("OCSP cert", OCSP_CERT_PATH
, AUTH_OCSP
);
645 /* loading X.509 CRLs */
647 /* loading attribute certificates (experimental) */
652 return -1; /* Shouldn't ever reach this */
655 /* leave pluto, with status.
656 * Once child is launched, parent must not exit this way because
657 * the lock would be released.
660 * 1 general discomfort
661 * 10 lock file exists
664 exit_pluto(int status
)
666 reset_globals(); /* needed because we may be called in odd state */
667 free_preshared_secrets();
668 free_remembered_public_keys();
669 delete_every_connection();
670 free_crl_fetch(); /* free chain of crl fetch requests */
671 free_ocsp_fetch(); /* free chain of ocsp fetch requests */
672 free_authcerts(); /* free chain of X.509 authority certificates */
673 free_crls(); /* free chain of X.509 CRLs */
674 free_acerts(); /* free chain of X.509 attribute certificates */
675 free_ca_infos(); /* free chain of X.509 CA information records */
676 free_ocsp(); /* free ocsp cache */
678 scx_finalize(); /* finalize and unload PKCS #11 module */
679 xauth_finalize(); /* finalize and unload XAUTH module */
683 #ifdef LEAK_DETECTIVE
685 #endif /* LEAK_DETECTIVE */