2018-02-27 19:20:58 +00:00
|
|
|
/*-
|
2015-02-24 18:19:20 +00:00
|
|
|
* SSLsplit - transparent SSL/TLS interception
|
2018-02-27 19:20:58 +00:00
|
|
|
* https://www.roe.ch/SSLsplit
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009-2018, Daniel Roethlisberger <daniel@roe.ch>.
|
2012-04-13 12:47:30 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2018-02-27 19:20:58 +00:00
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
2012-04-13 12:47:30 +00:00
|
|
|
*
|
2018-02-27 19:20:58 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2012-04-13 12:47:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* silence daemon(3) deprecation warning on Mac OS X */
|
|
|
|
#if __APPLE__
|
|
|
|
#define daemon xdaemon
|
|
|
|
#endif /* __APPLE__ */
|
|
|
|
|
|
|
|
#include "opts.h"
|
|
|
|
#include "proxy.h"
|
2014-11-24 21:01:52 +00:00
|
|
|
#include "privsep.h"
|
2012-04-13 12:47:30 +00:00
|
|
|
#include "ssl.h"
|
|
|
|
#include "nat.h"
|
2014-11-14 15:20:07 +00:00
|
|
|
#include "proc.h"
|
2012-04-13 12:47:30 +00:00
|
|
|
#include "cachemgr.h"
|
|
|
|
#include "sys.h"
|
|
|
|
#include "log.h"
|
2018-03-26 15:14:54 +00:00
|
|
|
#include "build.h"
|
2014-11-25 22:45:40 +00:00
|
|
|
#include "defaults.h"
|
2012-04-13 12:47:30 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifndef __BSD__
|
|
|
|
#include <getopt.h>
|
|
|
|
#endif /* !__BSD__ */
|
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/x509.h>
|
2018-11-03 15:23:31 +00:00
|
|
|
#include <event2/event.h>
|
|
|
|
#ifndef WITHOUT_MIRROR
|
|
|
|
#include <libnet.h>
|
|
|
|
#include <pcap.h>
|
|
|
|
#endif /* !WITHOUT_MIRROR */
|
2012-04-13 12:47:30 +00:00
|
|
|
|
|
|
|
#if __APPLE__
|
|
|
|
#undef daemon
|
|
|
|
extern int daemon(int, int);
|
|
|
|
#endif /* __APPLE__ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print version information to stderr.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
main_version(void)
|
|
|
|
{
|
2018-03-26 15:14:54 +00:00
|
|
|
fprintf(stderr, "%s %s (built %s)\n",
|
|
|
|
PKGLABEL, build_version, build_date);
|
|
|
|
if (strlen(build_version) < 5) {
|
2016-03-25 11:34:52 +00:00
|
|
|
/*
|
|
|
|
* Note to package maintainers: If you break the version
|
|
|
|
* string in your build, it will be impossible to provide
|
|
|
|
* proper upstream support to the users of the package,
|
|
|
|
* because it will be difficult or impossible to identify
|
|
|
|
* the exact codebase that is being used by the user
|
|
|
|
* reporting a bug. The version string is provided through
|
|
|
|
* different means depending on whether the code is a git
|
|
|
|
* checkout, a tarball downloaded from GitHub or a release.
|
|
|
|
* See GNUmakefile for the gory details.
|
|
|
|
*/
|
|
|
|
fprintf(stderr, "---------------------------------------"
|
|
|
|
"---------------------------------------\n");
|
|
|
|
fprintf(stderr, "WARNING: Something is wrong with the "
|
2017-08-11 12:01:51 +00:00
|
|
|
"version compiled into sslproxy!\n");
|
2016-03-25 11:34:52 +00:00
|
|
|
fprintf(stderr, "The version should contain a release "
|
|
|
|
"number and/or a git commit reference.\n");
|
|
|
|
fprintf(stderr, "If using a package, please report a bug "
|
|
|
|
"to the distro package maintainer.\n");
|
|
|
|
fprintf(stderr, "---------------------------------------"
|
|
|
|
"---------------------------------------\n");
|
|
|
|
}
|
2018-01-18 00:18:53 +00:00
|
|
|
fprintf(stderr, "Copyright (c) 2017-2018, Soner Tari <sonertari@gmail.com>\n");
|
2017-10-26 18:41:30 +00:00
|
|
|
fprintf(stderr, "https://github.com/sonertari/SSLproxy\n");
|
2018-01-18 00:18:53 +00:00
|
|
|
fprintf(stderr, "Copyright (c) 2009-2018, "
|
2012-04-13 12:47:30 +00:00
|
|
|
"Daniel Roethlisberger <daniel@roe.ch>\n");
|
2018-02-27 19:20:58 +00:00
|
|
|
fprintf(stderr, "https://www.roe.ch/SSLsplit\n");
|
2014-10-30 22:01:55 +00:00
|
|
|
if (build_info[0]) {
|
|
|
|
fprintf(stderr, "Build info: %s\n", build_info);
|
|
|
|
}
|
2018-03-26 15:14:54 +00:00
|
|
|
if (build_features[0]) {
|
|
|
|
fprintf(stderr, "Features: %s\n", build_features);
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
nat_version();
|
2014-11-14 15:30:07 +00:00
|
|
|
fprintf(stderr, "Local process info support: ");
|
|
|
|
#ifdef HAVE_LOCAL_PROCINFO
|
2014-11-25 22:38:37 +00:00
|
|
|
fprintf(stderr, "yes (" LOCAL_PROCINFO_STR ")\n");
|
2014-11-14 15:30:07 +00:00
|
|
|
#else /* !HAVE_LOCAL_PROCINFO */
|
|
|
|
fprintf(stderr, "no\n");
|
|
|
|
#endif /* !HAVE_LOCAL_PROCINFO */
|
2012-04-13 12:47:30 +00:00
|
|
|
ssl_openssl_version();
|
|
|
|
fprintf(stderr, "compiled against libevent %s\n", LIBEVENT_VERSION);
|
|
|
|
fprintf(stderr, "rtlinked against libevent %s\n", event_get_version());
|
2018-11-03 15:23:31 +00:00
|
|
|
#ifndef WITHOUT_MIRROR
|
|
|
|
fprintf(stderr, "compiled against libnet %s\n", LIBNET_VERSION);
|
|
|
|
#ifndef __OpenBSD__
|
|
|
|
const char *lnv = libnet_version();
|
|
|
|
if (!strncmp(lnv, "libnet version ", 15))
|
|
|
|
lnv += 15;
|
|
|
|
fprintf(stderr, "rtlinked against libnet %s\n", lnv);
|
|
|
|
#else /* __OpenBSD__ */
|
|
|
|
fprintf(stderr, "rtlinked against libnet n/a\n");
|
|
|
|
#endif /* __OpenBSD__ */
|
|
|
|
fprintf(stderr, "compiled against libpcap n/a\n");
|
|
|
|
const char *lpv = pcap_lib_version();
|
|
|
|
if (!strncmp(lpv, "libpcap version ", 16))
|
|
|
|
lpv += 16;
|
|
|
|
fprintf(stderr, "rtlinked against libpcap %s\n", lpv);
|
|
|
|
#endif /* !WITHOUT_MIRROR */
|
2012-04-13 12:47:30 +00:00
|
|
|
fprintf(stderr, "%d CPU cores detected\n", sys_get_cpu_cores());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print usage to stderr.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
main_usage(void)
|
|
|
|
{
|
|
|
|
const char *dflt, *warn;
|
2018-11-03 15:23:31 +00:00
|
|
|
const char *usagefmt1 =
|
2018-09-14 23:51:26 +00:00
|
|
|
"Usage: %s [-D] [-f conffile] [-o opt=val] [options...] [proxyspecs...]\n"
|
2018-05-09 17:05:29 +00:00
|
|
|
" -f conffile use conffile to load configuration from\n"
|
2018-09-14 23:51:26 +00:00
|
|
|
" -o opt=val override conffile option opt with value val\n"
|
2012-04-22 20:59:00 +00:00
|
|
|
" -c pemfile use CA cert (and key) from pemfile to sign forged certs\n"
|
|
|
|
" -k pemfile use CA key (and cert) from pemfile to sign forged certs\n"
|
2012-04-13 12:47:30 +00:00
|
|
|
" -C pemfile use CA chain from pemfile (intermediate and root CA certs)\n"
|
|
|
|
" -K pemfile use key from pemfile for leaf certs (default: generate)\n"
|
2018-01-18 00:18:53 +00:00
|
|
|
" -q crlurl use URL as CRL distribution point for all forged certs\n"
|
2012-04-13 12:47:30 +00:00
|
|
|
" -t certdir use cert+chain+key PEM files from certdir to target all sites\n"
|
|
|
|
" matching the common names (non-matching: generate if CA)\n"
|
2014-12-13 01:36:45 +00:00
|
|
|
" -w gendir write leaf key and only generated certificates to gendir\n"
|
|
|
|
" -W gendir write leaf key and all certificates to gendir\n"
|
2012-04-22 17:12:38 +00:00
|
|
|
" -O deny all OCSP requests on all proxyspecs\n"
|
2012-04-13 12:47:30 +00:00
|
|
|
" -P passthrough SSL connections if they cannot be split because of\n"
|
|
|
|
" client cert auth or no matching cert and no CA (default: drop)\n"
|
2018-08-03 20:36:51 +00:00
|
|
|
" -a pemfile use cert from pemfile when destination requests client certs\n"
|
|
|
|
" -b pemfile use key from pemfile when destination requests client certs\n"
|
2012-04-13 12:47:30 +00:00
|
|
|
#ifndef OPENSSL_NO_DH
|
|
|
|
" -g pemfile use DH group params from pemfile (default: keyfiles or auto)\n"
|
|
|
|
#define OPT_g "g:"
|
|
|
|
#else /* OPENSSL_NO_DH */
|
|
|
|
#define OPT_g
|
|
|
|
#endif /* !OPENSSL_NO_DH */
|
|
|
|
#ifndef OPENSSL_NO_ECDH
|
2016-03-25 15:28:30 +00:00
|
|
|
" -G curve use ECDH named curve (default: " DFLT_CURVE ")\n"
|
2012-04-13 12:47:30 +00:00
|
|
|
#define OPT_G "G:"
|
|
|
|
#else /* OPENSSL_NO_ECDH */
|
|
|
|
#define OPT_G
|
|
|
|
#endif /* OPENSSL_NO_ECDH */
|
|
|
|
#ifdef SSL_OP_NO_COMPRESSION
|
|
|
|
" -Z disable SSL/TLS compression on all connections\n"
|
|
|
|
#define OPT_Z "Z"
|
|
|
|
#else /* !SSL_OP_NO_COMPRESSION */
|
|
|
|
#define OPT_Z
|
|
|
|
#endif /* !SSL_OP_NO_COMPRESSION */
|
2014-11-05 19:06:11 +00:00
|
|
|
" -r proto only support one of " SSL_PROTO_SUPPORT_S "(default: all)\n"
|
|
|
|
" -R proto disable one of " SSL_PROTO_SUPPORT_S "(default: none)\n"
|
2014-11-30 21:29:40 +00:00
|
|
|
" -s ciphers use the given OpenSSL cipher suite spec (default: " DFLT_CIPHERS ")\n"
|
2018-09-14 23:51:26 +00:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
|
|
|
" -x engine load OpenSSL engine with the given identifier\n"
|
|
|
|
#define OPT_x "x:"
|
|
|
|
#else /* OPENSSL_NO_ENGINE */
|
|
|
|
#define OPT_x
|
|
|
|
#endif /* OPENSSL_NO_ENGINE */
|
2012-04-13 12:47:30 +00:00
|
|
|
" -e engine specify default NAT engine to use (default: %s)\n"
|
|
|
|
" -E list available NAT engines and exit\n"
|
2014-11-25 22:45:40 +00:00
|
|
|
" -u user drop privileges to user (default if run as root: " DFLT_DROPUSER ")\n"
|
2014-10-23 11:23:57 +00:00
|
|
|
" -m group when using -u, override group (default: primary group of user)\n"
|
2014-11-24 21:01:52 +00:00
|
|
|
" -j jaildir chroot() to jaildir (impacts sni proxyspecs, see manual page)\n"
|
2012-04-13 12:47:30 +00:00
|
|
|
" -p pidfile write pid to pidfile (default: no pid file)\n"
|
|
|
|
" -l logfile connect log: log one line summary per connection to logfile\n"
|
2018-11-03 15:23:31 +00:00
|
|
|
" -J enable connection statistics logging\n"
|
2014-11-13 22:38:46 +00:00
|
|
|
" -L logfile content log: full data to file or named pipe (excludes -S/-F)\n"
|
2014-10-18 22:40:22 +00:00
|
|
|
" -S logdir content log: full data to separate files in dir (excludes -L/-F)\n"
|
2014-11-13 22:38:46 +00:00
|
|
|
" -F pathspec content log: full data to sep files with %% subst (excl. -L/-S):\n"
|
|
|
|
" %%T - initial connection time as an ISO 8601 UTC timestamp\n"
|
2015-03-15 16:10:25 +00:00
|
|
|
" %%d - destination host and port\n"
|
2015-03-15 17:39:36 +00:00
|
|
|
" %%D - destination host\n"
|
|
|
|
" %%p - destination port\n"
|
2015-03-15 16:10:25 +00:00
|
|
|
" %%s - source host and port\n"
|
2015-03-15 17:39:36 +00:00
|
|
|
" %%S - source host\n"
|
|
|
|
" %%q - source port\n"
|
2014-11-14 15:20:07 +00:00
|
|
|
#ifdef HAVE_LOCAL_PROCINFO
|
|
|
|
" %%x - base name of local process (requires -i)\n"
|
|
|
|
" %%X - full path to local process (requires -i)\n"
|
|
|
|
" %%u - user name or id of local process (requires -i)\n"
|
|
|
|
" %%g - group name or id of local process (requires -i)\n"
|
|
|
|
#endif /* HAVE_LOCAL_PROCINFO */
|
2014-10-18 22:40:22 +00:00
|
|
|
" %%%% - literal '%%'\n"
|
2014-11-14 15:20:07 +00:00
|
|
|
#ifdef HAVE_LOCAL_PROCINFO
|
2017-08-11 12:01:51 +00:00
|
|
|
" e.g. \"/var/log/sslproxy/%%X/%%u-%%s-%%d-%%T.log\"\n"
|
2018-11-03 15:23:31 +00:00
|
|
|
#else /* !HAVE_LOCAL_PROCINFO */
|
|
|
|
" e.g. \"/var/log/sslproxy/%%T-%%s-%%d.log\"\n"
|
|
|
|
#endif /* HAVE_LOCAL_PROCINFO */
|
|
|
|
" -X pcapfile pcap log: packets to pcapfile (excludes -Y/-y)\n"
|
|
|
|
" -Y pcapdir pcap log: packets to separate files in dir (excludes -X/-y)\n"
|
|
|
|
" -y pathspec pcap log: packets to sep files with %% subst (excl. -X/-Y):\n"
|
|
|
|
" see option -F for pathspec format\n"
|
|
|
|
#ifndef WITHOUT_MIRROR
|
|
|
|
" -I if mirror packets to interface\n"
|
|
|
|
" -T addr mirror packets to target address (used with -I)\n"
|
|
|
|
#define OPT_I "I:"
|
|
|
|
#define OPT_T "T:"
|
|
|
|
#else /* WITHOUT_MIRROR */
|
|
|
|
#define OPT_I
|
|
|
|
#define OPT_T
|
|
|
|
#endif /* WITHOUT_MIRROR */
|
|
|
|
" -M logfile log master keys to logfile in SSLKEYLOGFILE format\n"
|
|
|
|
#ifdef HAVE_LOCAL_PROCINFO
|
2014-11-14 15:20:07 +00:00
|
|
|
" -i look up local process owning each connection for logging\n"
|
|
|
|
#define OPT_i "i"
|
|
|
|
#else /* !HAVE_LOCAL_PROCINFO */
|
|
|
|
#define OPT_i
|
|
|
|
#endif /* HAVE_LOCAL_PROCINFO */
|
2012-04-13 12:47:30 +00:00
|
|
|
" -d daemon mode: run in background, log error messages to syslog\n"
|
|
|
|
" -D debug mode: run in foreground, log debug messages on stderr\n"
|
|
|
|
" -V print version information and exit\n"
|
2018-11-03 15:23:31 +00:00
|
|
|
" -h print usage information and exit\n";
|
|
|
|
const char *usagefmt2 =
|
2018-05-09 19:05:03 +00:00
|
|
|
" proxyspec = type listenaddr+port \"up:\"port\n"
|
|
|
|
" [natengine|targetaddr+port|\"sni\"+port]\n"
|
|
|
|
" e.g. http 0.0.0.0 8080 up:8080 roe.ch 80 # http/4; static\n"
|
|
|
|
" # hostname dst\n"
|
|
|
|
" https ::1 8443 up:8080 2001:db8::1 443 # https/6; static\n"
|
|
|
|
" # address dst\n"
|
|
|
|
" https 127.0.0.1 9443 up:8080 sni 443 # https/4; SNI DNS\n"
|
|
|
|
" # lookups\n"
|
|
|
|
" tcp 127.0.0.1 10025 up:9199 # tcp/4; default NAT\n"
|
|
|
|
" # engine\n"
|
|
|
|
" ssl 2001:db8::2 9999 up:19999 pf # ssl/6; NAT engine 'pf'\n"
|
|
|
|
" autossl ::1 10025 up:9199 # autossl/6; STARTTLS\n"
|
|
|
|
" # et al\n"
|
2012-04-13 12:47:30 +00:00
|
|
|
"Example:\n"
|
2018-05-09 19:05:03 +00:00
|
|
|
" %s -k ca.key -c ca.pem -P https 127.0.0.1 8443 up:8080 https ::1 8443\n"
|
2018-01-18 00:18:53 +00:00
|
|
|
"%s";
|
|
|
|
|
|
|
|
if (!(dflt = nat_getdefaultname())) {
|
|
|
|
dflt = "n/a";
|
|
|
|
warn = "\nWarning: no supported NAT engine on this platform!\n"
|
|
|
|
"Only static and SNI proxyspecs are supported.\n";
|
|
|
|
} else {
|
|
|
|
warn = "";
|
|
|
|
}
|
|
|
|
|
2018-11-03 15:23:31 +00:00
|
|
|
fprintf(stderr, usagefmt1, build_pkgname, dflt);
|
|
|
|
fprintf(stderr, usagefmt2, build_pkgname, warn);
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Callback to load a cert/chain/key combo from a single PEM file.
|
2017-07-25 13:07:39 +00:00
|
|
|
* A return value of -1 indicates a fatal error to the file walker.
|
2012-04-13 12:47:30 +00:00
|
|
|
*/
|
2017-07-25 13:07:39 +00:00
|
|
|
static int
|
2012-04-13 12:47:30 +00:00
|
|
|
main_loadtgcrt(const char *filename, void *arg)
|
|
|
|
{
|
2012-10-16 22:10:47 +00:00
|
|
|
opts_t *opts = arg;
|
2012-04-13 12:47:30 +00:00
|
|
|
cert_t *cert;
|
|
|
|
char **names;
|
|
|
|
|
|
|
|
cert = cert_new_load(filename);
|
|
|
|
if (!cert) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to load cert and key from PEM file "
|
2012-10-16 22:10:47 +00:00
|
|
|
"'%s'\n", filename);
|
2017-07-25 13:07:39 +00:00
|
|
|
return -1;
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
if (X509_check_private_key(cert->crt, cert->key) != 1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Cert does not match key in PEM file "
|
2012-10-16 22:10:47 +00:00
|
|
|
"'%s':\n", filename);
|
2012-04-13 12:47:30 +00:00
|
|
|
ERR_print_errors_fp(stderr);
|
2017-07-25 13:07:39 +00:00
|
|
|
return -1;
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_CERTIFICATE
|
|
|
|
log_dbg_printf("Loaded '%s':\n", filename);
|
|
|
|
log_dbg_print_free(ssl_x509_to_str(cert->crt));
|
|
|
|
log_dbg_print_free(ssl_x509_to_pem(cert->crt));
|
|
|
|
#endif /* DEBUG_CERTIFICATE */
|
|
|
|
|
2012-05-13 13:24:50 +00:00
|
|
|
if (OPTS_DEBUG(opts)) {
|
2012-04-13 12:47:30 +00:00
|
|
|
log_dbg_printf("Targets for '%s':", filename);
|
|
|
|
}
|
|
|
|
names = ssl_x509_names(cert->crt);
|
|
|
|
for (char **p = names; *p; p++) {
|
|
|
|
/* be deliberately vulnerable to NULL prefix attacks */
|
|
|
|
char *sep;
|
|
|
|
if ((sep = strchr(*p, '!'))) {
|
|
|
|
*sep = '\0';
|
|
|
|
}
|
2012-05-13 13:24:50 +00:00
|
|
|
if (OPTS_DEBUG(opts)) {
|
2012-04-13 12:47:30 +00:00
|
|
|
log_dbg_printf(" '%s'", *p);
|
|
|
|
}
|
|
|
|
cachemgr_tgcrt_set(*p, cert);
|
|
|
|
free(*p);
|
|
|
|
}
|
2012-05-13 13:24:50 +00:00
|
|
|
if (OPTS_DEBUG(opts)) {
|
2012-04-13 12:47:30 +00:00
|
|
|
log_dbg_printf("\n");
|
|
|
|
}
|
|
|
|
free(names);
|
|
|
|
cert_free(cert);
|
2017-07-25 13:07:39 +00:00
|
|
|
return 0;
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main entry point.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
const char *argv0;
|
|
|
|
int ch;
|
|
|
|
opts_t *opts;
|
|
|
|
char *natengine;
|
|
|
|
int pidfd = -1;
|
2012-10-23 19:30:11 +00:00
|
|
|
int rv = EXIT_FAILURE;
|
2012-04-13 12:47:30 +00:00
|
|
|
|
|
|
|
argv0 = argv[0];
|
|
|
|
opts = opts_new();
|
2014-10-18 01:35:44 +00:00
|
|
|
if (nat_getdefaultname()) {
|
|
|
|
natengine = strdup(nat_getdefaultname());
|
2014-10-23 11:14:06 +00:00
|
|
|
if (!natengine)
|
|
|
|
oom_die(argv0);
|
2014-10-18 01:35:44 +00:00
|
|
|
} else {
|
|
|
|
natengine = NULL;
|
|
|
|
}
|
2012-04-13 12:47:30 +00:00
|
|
|
|
2018-11-03 15:23:31 +00:00
|
|
|
while ((ch = getopt(argc, argv,
|
|
|
|
OPT_g OPT_G OPT_Z OPT_i OPT_x OPT_T OPT_I
|
2018-09-14 23:51:26 +00:00
|
|
|
"k:c:C:K:t:OPa:b:s:r:R:e:Eu:m:j:p:l:L:S:F:M:"
|
2018-11-03 15:23:31 +00:00
|
|
|
"dD::VhW:w:q:f:o:X:Y:y:J")) != -1) {
|
2012-04-13 12:47:30 +00:00
|
|
|
switch (ch) {
|
2017-08-16 13:01:52 +00:00
|
|
|
case 'f':
|
|
|
|
if (opts->conffile)
|
|
|
|
free(opts->conffile);
|
|
|
|
opts->conffile = strdup(optarg);
|
|
|
|
if (!opts->conffile)
|
|
|
|
oom_die(argv0);
|
2018-11-29 23:49:37 +00:00
|
|
|
if (opts_load_conffile(opts, argv0, &natengine) == -1) {
|
2018-09-14 23:51:26 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-11-03 15:23:31 +00:00
|
|
|
#ifdef DEBUG_OPTS
|
2017-08-16 13:01:52 +00:00
|
|
|
fprintf(stderr, "Conf file: %s\n", opts->conffile);
|
2018-11-03 15:23:31 +00:00
|
|
|
#endif /* DEBUG_OPTS */
|
2017-08-16 13:01:52 +00:00
|
|
|
break;
|
2018-09-14 23:51:26 +00:00
|
|
|
case 'o':
|
|
|
|
if (opts_set_option(opts, argv0, optarg, &natengine) == -1) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
2012-04-22 20:59:00 +00:00
|
|
|
case 'c':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_cacrt(opts, argv0, optarg);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
2012-04-22 20:59:00 +00:00
|
|
|
case 'k':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_cakey(opts, argv0, optarg);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
|
|
|
case 'C':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_chain(opts, argv0, optarg);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
|
|
|
case 'K':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_key(opts, argv0, optarg);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_tgcrtdir(opts, argv0, optarg);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
2018-01-18 00:18:53 +00:00
|
|
|
case 'q':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_crl(opts, optarg);
|
2018-01-18 00:18:53 +00:00
|
|
|
break;
|
2012-04-22 17:12:38 +00:00
|
|
|
case 'O':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_deny_ocsp(opts);
|
2012-04-22 17:12:38 +00:00
|
|
|
break;
|
2012-04-13 12:47:30 +00:00
|
|
|
case 'P':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_passthrough(opts);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
2018-08-03 20:36:51 +00:00
|
|
|
case 'a':
|
|
|
|
opts_set_clientcrt(opts, argv0, optarg);
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
opts_set_clientkey(opts, argv0, optarg);
|
|
|
|
break;
|
2012-04-13 12:47:30 +00:00
|
|
|
#ifndef OPENSSL_NO_DH
|
|
|
|
case 'g':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_dh(opts, argv0, optarg);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
|
|
|
#endif /* !OPENSSL_NO_DH */
|
|
|
|
#ifndef OPENSSL_NO_ECDH
|
|
|
|
case 'G':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_ecdhcurve(opts, argv0, optarg);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
|
|
|
#endif /* !OPENSSL_NO_ECDH */
|
|
|
|
#ifdef SSL_OP_NO_COMPRESSION
|
|
|
|
case 'Z':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_unset_sslcomp(opts);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
|
|
|
#endif /* SSL_OP_NO_COMPRESSION */
|
|
|
|
case 's':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_ciphers(opts, argv0, optarg);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
2014-11-05 19:06:11 +00:00
|
|
|
case 'r':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_force_proto(opts, argv0, optarg);
|
2014-11-05 19:06:11 +00:00
|
|
|
break;
|
|
|
|
case 'R':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_disable_proto(opts, argv0, optarg);
|
2014-11-05 19:06:11 +00:00
|
|
|
break;
|
2018-09-14 23:51:26 +00:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
|
|
|
case 'x':
|
|
|
|
opts_set_openssl_engine(opts, argv0, optarg);
|
|
|
|
break;
|
|
|
|
#endif /* !OPENSSL_NO_ENGINE */
|
2012-04-13 12:47:30 +00:00
|
|
|
case 'e':
|
2018-05-09 17:05:29 +00:00
|
|
|
if (natengine)
|
|
|
|
free(natengine);
|
2012-04-13 12:47:30 +00:00
|
|
|
natengine = strdup(optarg);
|
2014-10-23 11:14:06 +00:00
|
|
|
if (!natengine)
|
|
|
|
oom_die(argv0);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
nat_list_engines();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
break;
|
|
|
|
case 'u':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_user(opts, argv0, optarg);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
2014-10-18 06:34:51 +00:00
|
|
|
case 'm':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_group(opts, argv0, optarg);
|
2014-10-18 06:34:51 +00:00
|
|
|
break;
|
2012-04-13 12:47:30 +00:00
|
|
|
case 'p':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_pidfile(opts, argv0, optarg);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
|
|
|
case 'j':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_jaildir(opts, argv0, optarg);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
|
|
|
case 'l':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_connectlog(opts, argv0, optarg);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
2018-11-03 15:23:31 +00:00
|
|
|
case 'J':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_statslog(opts);
|
2017-07-25 13:07:39 +00:00
|
|
|
break;
|
2012-04-13 12:47:30 +00:00
|
|
|
case 'L':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_contentlog(opts, argv0, optarg);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
|
|
|
case 'S':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_contentlogdir(opts, argv0, optarg);
|
2014-10-18 22:40:22 +00:00
|
|
|
break;
|
2018-09-14 23:51:26 +00:00
|
|
|
case 'F':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_contentlogpathspec(opts, argv0, optarg);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
2018-11-03 15:23:31 +00:00
|
|
|
case 'X':
|
|
|
|
opts_set_pcaplog(opts, argv0, optarg);
|
|
|
|
break;
|
|
|
|
case 'Y':
|
|
|
|
opts_set_pcaplogdir(opts, argv0, optarg);
|
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
opts_set_pcaplogpathspec(opts, argv0, optarg);
|
|
|
|
break;
|
|
|
|
#ifndef WITHOUT_MIRROR
|
|
|
|
case 'I':
|
|
|
|
opts_set_mirrorif(opts, argv0, optarg);
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
opts_set_mirrortarget(opts, argv0, optarg);
|
|
|
|
break;
|
|
|
|
#endif /* !WITHOUT_MIRROR */
|
2014-12-09 21:43:05 +00:00
|
|
|
case 'W':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_certgendir_writeall(opts, argv0, optarg);
|
2014-12-09 21:43:05 +00:00
|
|
|
break;
|
2014-12-09 20:02:25 +00:00
|
|
|
case 'w':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_certgendir_writegencerts(opts, argv0, optarg);
|
2014-12-09 19:08:11 +00:00
|
|
|
break;
|
2014-11-14 15:20:07 +00:00
|
|
|
#ifdef HAVE_LOCAL_PROCINFO
|
|
|
|
case 'i':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_lprocinfo(opts);
|
2014-11-14 15:20:07 +00:00
|
|
|
break;
|
|
|
|
#endif /* HAVE_LOCAL_PROCINFO */
|
2018-02-27 19:20:58 +00:00
|
|
|
case 'M':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_masterkeylog(opts, argv0, optarg);
|
2018-02-27 19:20:58 +00:00
|
|
|
break;
|
2012-04-13 12:47:30 +00:00
|
|
|
case 'd':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_daemon(opts);
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
|
|
|
case 'D':
|
2018-05-09 17:05:29 +00:00
|
|
|
opts_set_debug(opts);
|
|
|
|
if (optarg) {
|
|
|
|
opts_set_debug_level(optarg);
|
2017-06-15 16:07:37 +00:00
|
|
|
}
|
2012-04-13 12:47:30 +00:00
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
main_version();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case 'h':
|
|
|
|
main_usage();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case '?':
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
default:
|
|
|
|
main_usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2018-05-09 17:05:29 +00:00
|
|
|
proxyspec_parse(&argc, &argv, natengine, &opts->spec);
|
2018-11-03 15:23:31 +00:00
|
|
|
|
2014-10-23 11:23:57 +00:00
|
|
|
/* usage checks before defaults */
|
2012-05-13 13:24:50 +00:00
|
|
|
if (opts->detach && OPTS_DEBUG(opts)) {
|
2012-04-13 12:47:30 +00:00
|
|
|
fprintf(stderr, "%s: -d and -D are mutually exclusive.\n",
|
|
|
|
argv0);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-11-03 15:23:31 +00:00
|
|
|
#ifndef WITHOUT_MIRROR
|
|
|
|
if (opts->mirrortarget && !opts->mirrorif) {
|
|
|
|
fprintf(stderr, "%s: -T depends on -I.\n", argv0);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (opts->mirrorif && !opts->mirrortarget) {
|
|
|
|
fprintf(stderr, "%s: -I depends on -T.\n", argv0);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
#endif /* !WITHOUT_MIRROR */
|
2012-04-13 12:47:30 +00:00
|
|
|
if (!opts->spec) {
|
|
|
|
fprintf(stderr, "%s: no proxyspec specified.\n", argv0);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
for (proxyspec_t *spec = opts->spec; spec; spec = spec->next) {
|
|
|
|
if (spec->connect_addrlen || spec->sni_port)
|
|
|
|
continue;
|
|
|
|
if (!spec->natengine) {
|
|
|
|
fprintf(stderr, "%s: no supported NAT engines "
|
|
|
|
"on this platform.\n"
|
|
|
|
"Only static addr and SNI proxyspecs "
|
|
|
|
"supported.\n", argv0);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (spec->listen_addr.ss_family == AF_INET6 &&
|
|
|
|
!nat_ipv6ready(spec->natengine)) {
|
|
|
|
fprintf(stderr, "%s: IPv6 not supported by '%s'\n",
|
|
|
|
argv0, spec->natengine);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
spec->natlookup = nat_getlookupcb(spec->natengine);
|
|
|
|
spec->natsocket = nat_getsocketcb(spec->natengine);
|
|
|
|
}
|
|
|
|
if (opts_has_ssl_spec(opts)) {
|
2015-04-30 14:54:09 +00:00
|
|
|
if (ssl_init() == -1) {
|
|
|
|
fprintf(stderr, "%s: failed to initialize OpenSSL.\n",
|
|
|
|
argv0);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-09-14 23:51:26 +00:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
|
|
|
if (opts->openssl_engine &&
|
|
|
|
ssl_engine(opts->openssl_engine) == -1) {
|
|
|
|
fprintf(stderr, "%s: failed to enable OpenSSL engine"
|
|
|
|
" %s.\n", argv0, opts->openssl_engine);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
#endif /* !OPENSSL_NO_ENGINE */
|
2012-04-13 12:47:30 +00:00
|
|
|
if ((opts->cacrt || !opts->tgcrtdir) && !opts->cakey) {
|
|
|
|
fprintf(stderr, "%s: no CA key specified (-k).\n",
|
|
|
|
argv0);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (opts->cakey && !opts->cacrt) {
|
|
|
|
fprintf(stderr, "%s: no CA cert specified (-c).\n",
|
|
|
|
argv0);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (opts->cakey && opts->cacrt &&
|
|
|
|
(X509_check_private_key(opts->cacrt, opts->cakey) != 1)) {
|
|
|
|
fprintf(stderr, "%s: CA cert does not match key.\n",
|
|
|
|
argv0);
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 23:04:20 +00:00
|
|
|
#ifdef __APPLE__
|
2014-11-28 09:03:29 +00:00
|
|
|
if (opts->dropuser && !!strcmp(opts->dropuser, "root") &&
|
|
|
|
nat_used("pf")) {
|
2014-11-27 23:04:20 +00:00
|
|
|
fprintf(stderr, "%s: cannot use 'pf' proxyspec with -u due "
|
|
|
|
"to Apple bug\n", argv0);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
#endif /* __APPLE__ */
|
2012-04-13 12:47:30 +00:00
|
|
|
|
2012-10-16 20:52:54 +00:00
|
|
|
/* prevent multiple instances running */
|
|
|
|
if (opts->pidfile) {
|
|
|
|
pidfd = sys_pidf_open(opts->pidfile);
|
|
|
|
if (pidfd == -1) {
|
|
|
|
fprintf(stderr, "%s: cannot open PID file '%s' "
|
|
|
|
"- process already running?\n",
|
|
|
|
argv0, opts->pidfile);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
/* dynamic defaults */
|
|
|
|
if (!opts->ciphers) {
|
2014-11-30 21:29:40 +00:00
|
|
|
opts->ciphers = strdup(DFLT_CIPHERS);
|
2014-10-23 11:14:06 +00:00
|
|
|
if (!opts->ciphers)
|
|
|
|
oom_die(argv0);
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
if (!opts->dropuser && !geteuid() && !getuid() &&
|
2014-11-25 22:45:40 +00:00
|
|
|
sys_isuser(DFLT_DROPUSER)) {
|
2014-11-27 23:04:20 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
/* Apple broke ioctl(/dev/pf) for EUID != 0 so we do not
|
|
|
|
* want to automatically drop privileges to nobody there
|
|
|
|
* if pf has been used in any proxyspec */
|
|
|
|
if (!nat_used("pf")) {
|
|
|
|
#endif /* __APPLE__ */
|
2014-11-25 22:45:40 +00:00
|
|
|
opts->dropuser = strdup(DFLT_DROPUSER);
|
2014-10-23 11:14:06 +00:00
|
|
|
if (!opts->dropuser)
|
|
|
|
oom_die(argv0);
|
2014-11-27 23:04:20 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
}
|
|
|
|
#endif /* __APPLE__ */
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
2018-11-03 15:23:31 +00:00
|
|
|
if (opts->dropuser && sys_isgeteuid(opts->dropuser)) {
|
|
|
|
if (opts->dropgroup) {
|
|
|
|
fprintf(stderr, "%s: cannot use -m when -u is "
|
|
|
|
"current user\n", argv0);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
free(opts->dropuser);
|
|
|
|
opts->dropuser = NULL;
|
|
|
|
}
|
2018-09-14 23:51:26 +00:00
|
|
|
|
|
|
|
/* usage checks after defaults */
|
|
|
|
if (opts->dropgroup && !opts->dropuser) {
|
2018-11-03 15:23:31 +00:00
|
|
|
fprintf(stderr, "%s: -m depends on -u\n", argv0);
|
2018-09-14 23:51:26 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2018-11-03 15:23:31 +00:00
|
|
|
/* Warn about options that require per-connection privileged operations
|
|
|
|
* to be executed through privsep, but only if dropuser is set and is
|
|
|
|
* not root, because privsep will fastpath in that situation, skipping
|
|
|
|
* the latency-incurring overhead. */
|
|
|
|
int privsep_warn = 0;
|
|
|
|
if (opts->dropuser) {
|
|
|
|
if (opts->contentlog_isdir) {
|
|
|
|
log_dbg_printf("| Warning: -F requires a privileged "
|
|
|
|
"operation for each connection!\n");
|
|
|
|
privsep_warn = 1;
|
|
|
|
}
|
|
|
|
if (opts->contentlog_isspec) {
|
|
|
|
log_dbg_printf("| Warning: -S requires a privileged "
|
|
|
|
"operation for each connection!\n");
|
|
|
|
privsep_warn = 1;
|
|
|
|
}
|
|
|
|
if (opts->pcaplog_isdir) {
|
|
|
|
log_dbg_printf("| Warning: -Y requires a privileged "
|
|
|
|
"operation for each connection!\n");
|
|
|
|
privsep_warn = 1;
|
|
|
|
}
|
|
|
|
if (opts->pcaplog_isspec) {
|
|
|
|
log_dbg_printf("| Warning: -y requires a privileged "
|
|
|
|
"operation for each connection!\n");
|
|
|
|
privsep_warn = 1;
|
|
|
|
}
|
|
|
|
if (opts->certgendir) {
|
|
|
|
log_dbg_printf("| Warning: -w/-W require a privileged "
|
|
|
|
"op for each connection!\n");
|
|
|
|
privsep_warn = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (privsep_warn) {
|
|
|
|
log_dbg_printf("| Privileged operations require communication "
|
|
|
|
"between parent and child process\n"
|
|
|
|
"| and will negatively impact latency and "
|
|
|
|
"performance on each connection.\n");
|
|
|
|
}
|
|
|
|
|
2018-09-14 23:51:26 +00:00
|
|
|
/* debug log, part 1 */
|
|
|
|
if (OPTS_DEBUG(opts)) {
|
|
|
|
main_version();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate leaf key */
|
2013-05-26 22:17:36 +00:00
|
|
|
if (opts_has_ssl_spec(opts) && opts->cakey && !opts->key) {
|
2018-11-03 15:23:31 +00:00
|
|
|
opts->key = ssl_key_genrsa(DFLT_LEAFKEY_RSABITS);
|
2012-10-16 20:52:54 +00:00
|
|
|
if (!opts->key) {
|
|
|
|
fprintf(stderr, "%s: error generating RSA key:\n",
|
|
|
|
argv0);
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (OPTS_DEBUG(opts)) {
|
|
|
|
log_dbg_printf("Generated RSA key for leaf certs.\n");
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 17:28:06 +00:00
|
|
|
if (opts->certgendir) {
|
2014-12-13 01:36:45 +00:00
|
|
|
char *keyid, *keyfn;
|
|
|
|
int prv;
|
|
|
|
FILE *keyf;
|
|
|
|
|
|
|
|
keyid = ssl_key_identifier(opts->key, 0);
|
|
|
|
if (!keyid) {
|
|
|
|
fprintf(stderr, "%s: error generating key id\n", argv0);
|
2014-12-12 17:28:06 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-12-13 01:36:45 +00:00
|
|
|
|
|
|
|
prv = asprintf(&keyfn, "%s/%s.key", opts->certgendir, keyid);
|
|
|
|
if (prv == -1) {
|
|
|
|
fprintf(stderr, "%s: %s (%i)\n", argv0,
|
|
|
|
strerror(errno), errno);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(keyf = fopen(keyfn, "w"))) {
|
|
|
|
fprintf(stderr, "%s: Failed to open '%s' for writing: "
|
|
|
|
"%s (%i)\n", argv0, keyfn,
|
|
|
|
strerror(errno), errno);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (!PEM_write_PrivateKey(keyf, opts->key, NULL, 0, 0,
|
|
|
|
NULL, NULL)) {
|
|
|
|
fprintf(stderr, "%s: Failed to write key to '%s': "
|
|
|
|
"%s (%i)\n", argv0, keyfn,
|
|
|
|
strerror(errno), errno);
|
|
|
|
exit(EXIT_FAILURE);
|
2014-12-12 17:28:06 +00:00
|
|
|
}
|
2014-12-13 01:36:45 +00:00
|
|
|
fclose(keyf);
|
2014-12-12 17:28:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 23:51:26 +00:00
|
|
|
/* debug log, part 2 */
|
2012-05-13 13:24:50 +00:00
|
|
|
if (OPTS_DEBUG(opts)) {
|
2014-11-05 20:18:53 +00:00
|
|
|
opts_proto_dbg_dump(opts);
|
2012-04-13 12:47:30 +00:00
|
|
|
log_dbg_printf("proxyspecs:\n");
|
|
|
|
for (proxyspec_t *spec = opts->spec; spec; spec = spec->next) {
|
2015-03-15 21:55:34 +00:00
|
|
|
char *specstr = proxyspec_str(spec);
|
|
|
|
if (!specstr) {
|
2015-03-15 16:10:25 +00:00
|
|
|
fprintf(stderr, "%s: out of memory\n", argv0);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2015-03-15 21:55:34 +00:00
|
|
|
log_dbg_printf("- %s\n", specstr);
|
|
|
|
free(specstr);
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
2018-09-14 23:51:26 +00:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
|
|
|
if (opts->openssl_engine) {
|
|
|
|
log_dbg_printf("Loaded OpenSSL engine %s\n",
|
|
|
|
opts->openssl_engine);
|
|
|
|
}
|
|
|
|
#endif /* !OPENSSL_NO_ENGINE */
|
2012-04-13 12:47:30 +00:00
|
|
|
if (opts->cacrt) {
|
|
|
|
char *subj = ssl_x509_subject(opts->cacrt);
|
|
|
|
log_dbg_printf("Loaded CA: '%s'\n", subj);
|
|
|
|
free(subj);
|
|
|
|
#ifdef DEBUG_CERTIFICATE
|
|
|
|
log_dbg_print_free(ssl_x509_to_str(opts->cacrt));
|
|
|
|
log_dbg_print_free(ssl_x509_to_pem(opts->cacrt));
|
|
|
|
#endif /* DEBUG_CERTIFICATE */
|
|
|
|
} else {
|
|
|
|
log_dbg_printf("No CA loaded.\n");
|
|
|
|
}
|
|
|
|
}
|
2012-10-16 20:52:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize as much as possible before daemon() in order to be
|
|
|
|
* able to provide direct feedback to the user when failing.
|
|
|
|
*/
|
2012-10-16 21:05:37 +00:00
|
|
|
if (cachemgr_preinit() == -1) {
|
|
|
|
fprintf(stderr, "%s: failed to preinit cachemgr.\n", argv0);
|
2012-10-16 20:52:54 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
if (log_preinit(opts) == -1) {
|
|
|
|
fprintf(stderr, "%s: failed to preinit logging.\n", argv0);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (nat_preinit() == -1) {
|
|
|
|
fprintf(stderr, "%s: failed to preinit NAT lookup.\n", argv0);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-01-30 21:33:57 +00:00
|
|
|
/* Load certs before dropping privs but after cachemgr_preinit() */
|
|
|
|
if (opts->tgcrtdir) {
|
2017-07-25 13:07:39 +00:00
|
|
|
if (sys_dir_eachfile(opts->tgcrtdir,
|
|
|
|
main_loadtgcrt, opts) == -1) {
|
|
|
|
fprintf(stderr, "%s: failed to load certs from %s\n",
|
|
|
|
argv0, opts->tgcrtdir);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-01-30 21:33:57 +00:00
|
|
|
}
|
2013-04-03 16:02:45 +00:00
|
|
|
|
2014-11-24 21:01:52 +00:00
|
|
|
/* Detach from tty; from this point on, only canonicalized absolute
|
|
|
|
* paths should be used (-j, -F, -S). */
|
2012-04-13 12:47:30 +00:00
|
|
|
if (opts->detach) {
|
2012-10-23 19:30:11 +00:00
|
|
|
if (OPTS_DEBUG(opts)) {
|
|
|
|
log_dbg_printf("Detaching from TTY, see syslog for "
|
|
|
|
"errors after this point\n");
|
|
|
|
}
|
2014-11-24 21:01:52 +00:00
|
|
|
if (daemon(0, 0) == -1) {
|
2012-04-13 12:47:30 +00:00
|
|
|
fprintf(stderr, "%s: failed to detach from TTY: %s\n",
|
|
|
|
argv0, strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
log_err_mode(LOG_ERR_MODE_SYSLOG);
|
|
|
|
}
|
2012-10-16 22:24:26 +00:00
|
|
|
|
2014-11-24 21:01:52 +00:00
|
|
|
if (opts->pidfile && (sys_pidf_write(pidfd) == -1)) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to write PID to PID file '%s': %s (%i)"
|
2014-11-24 21:01:52 +00:00
|
|
|
"\n", opts->pidfile, strerror(errno), errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fork into parent monitor process and (potentially unprivileged)
|
2018-11-03 15:23:31 +00:00
|
|
|
* child process doing the actual work. We request 6 privsep client
|
|
|
|
* sockets: five logger threads, and the child process main thread,
|
|
|
|
* which will become the main proxy thread. First slot is main thread,
|
|
|
|
* remaining slots are passed down to log subsystem. */
|
|
|
|
int clisock[6];
|
|
|
|
if (privsep_fork(opts, clisock,
|
|
|
|
sizeof(clisock)/sizeof(clisock[0])) != 0) {
|
2014-11-24 21:01:52 +00:00
|
|
|
/* parent has exited the monitor loop after waiting for child,
|
2018-11-03 15:23:31 +00:00
|
|
|
* or an error occurred */
|
2014-11-24 21:01:52 +00:00
|
|
|
if (opts->pidfile) {
|
|
|
|
sys_pidf_close(pidfd, opts->pidfile);
|
|
|
|
}
|
|
|
|
goto out_parent;
|
|
|
|
}
|
|
|
|
/* child */
|
|
|
|
|
|
|
|
/* close pidfile in child */
|
|
|
|
if (opts->pidfile)
|
|
|
|
close(pidfd);
|
|
|
|
|
|
|
|
/* Initialize proxy before dropping privs */
|
|
|
|
proxy_ctx_t *proxy = proxy_new(opts, clisock[0]);
|
|
|
|
if (!proxy) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to initialize proxy.\n");
|
2014-11-24 21:01:52 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Drop privs, chroot */
|
|
|
|
if (sys_privdrop(opts->dropuser, opts->dropgroup,
|
|
|
|
opts->jaildir) == -1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to drop privileges: %s (%i)\n",
|
2014-11-24 21:01:52 +00:00
|
|
|
strerror(errno), errno);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-08-03 20:36:51 +00:00
|
|
|
log_dbg_printf("Dropped privs to user %s group %s chroot %s\n",
|
|
|
|
opts->dropuser ? opts->dropuser : "-",
|
|
|
|
opts->dropgroup ? opts->dropgroup : "-",
|
|
|
|
opts->jaildir ? opts->jaildir : "-");
|
2017-07-25 13:07:39 +00:00
|
|
|
if (ssl_reinit() == -1) {
|
|
|
|
fprintf(stderr, "%s: failed to reinit SSL\n", argv0);
|
|
|
|
goto out_sslreinit_failed;
|
|
|
|
}
|
2014-11-24 21:01:52 +00:00
|
|
|
|
2012-10-16 22:24:26 +00:00
|
|
|
/* Post-privdrop/chroot/detach initialization, thread spawning */
|
2018-11-03 15:23:31 +00:00
|
|
|
if (log_init(opts, proxy, &clisock[1]) == -1) {
|
2014-01-06 18:03:29 +00:00
|
|
|
fprintf(stderr, "%s: failed to init log facility: %s\n",
|
|
|
|
argv0, strerror(errno));
|
2012-10-23 19:30:11 +00:00
|
|
|
goto out_log_failed;
|
2012-10-16 22:24:26 +00:00
|
|
|
}
|
2012-10-16 21:05:37 +00:00
|
|
|
if (cachemgr_init() == -1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to init cache manager.\n");
|
2012-10-23 19:30:11 +00:00
|
|
|
goto out_cachemgr_failed;
|
2012-10-16 21:05:37 +00:00
|
|
|
}
|
2012-04-13 12:47:30 +00:00
|
|
|
if (nat_init() == -1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to init NAT state table lookup.\n");
|
2012-10-23 19:30:11 +00:00
|
|
|
goto out_nat_failed;
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
2012-10-23 19:30:11 +00:00
|
|
|
rv = EXIT_SUCCESS;
|
2012-10-16 22:10:47 +00:00
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
proxy_run(proxy);
|
2017-05-29 09:22:23 +00:00
|
|
|
|
2017-08-13 01:36:33 +00:00
|
|
|
#ifdef DEBUG_PROXY
|
|
|
|
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "main: EXIT closing privsep clisock=%d\n", clisock[0]);
|
|
|
|
#endif /* DEBUG_PROXY */
|
2017-05-29 09:22:23 +00:00
|
|
|
privsep_client_close(clisock[0]);
|
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
proxy_free(proxy);
|
|
|
|
nat_fini();
|
2012-10-23 19:30:11 +00:00
|
|
|
out_nat_failed:
|
|
|
|
cachemgr_fini();
|
|
|
|
out_cachemgr_failed:
|
|
|
|
log_fini();
|
2017-07-25 13:07:39 +00:00
|
|
|
out_sslreinit_failed:
|
2012-10-23 19:30:11 +00:00
|
|
|
out_log_failed:
|
2014-11-24 21:01:52 +00:00
|
|
|
out_parent:
|
2012-04-13 12:47:30 +00:00
|
|
|
opts_free(opts);
|
|
|
|
ssl_fini();
|
2012-10-23 19:30:11 +00:00
|
|
|
return rv;
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set noet ft=c: */
|