main.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
/*
* Copyright (C) 1999-2016 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*! \file */
#include <config.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <isc/backtrace.h>
#include <isc/commandline.h>
#include <isc/platform.h>
#include <isc/resource.h>
#include <dns/dispatch.h>
#include <dns/resolver.h>
#ifdef PKCS11CRYPTO
#endif
#include <dlz/dlz_dlopen_driver.h>
#ifdef HAVE_GPERFTOOLS_PROFILER
#include <gperftools/profiler.h>
#endif
/*
* Defining NS_MAIN provides storage declarations (rather than extern)
*/
#define NS_MAIN 1
#include <named/interfacemgr.h>
#ifdef HAVE_LIBSCF
#include <named/ns_smf_globals.h>
#endif
#ifdef OPENSSL
#include <openssl/opensslv.h>
#endif
#ifdef HAVE_LIBXML2
#include <libxml/xmlversion.h>
#endif
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif
/*
* Include header files for database drivers here.
*/
/* #include "xxdb.h" */
#ifdef CONTRIB_DLZ
/*
* Include contributed DLZ drivers if appropriate.
*/
#include <dlz/dlz_drivers.h>
#endif
/*
* The maximum number of stack frames to dump on assertion failure.
*/
#ifndef BACKTRACE_MAXFRAME
#define BACKTRACE_MAXFRAME 128
#endif
extern int isc_dscp_check_value;
extern unsigned int dns_zone_mkey_hour;
extern unsigned int dns_zone_mkey_day;
extern unsigned int dns_zone_mkey_month;
static char absolute_conffile[ISC_DIR_PATHMAX];
static char saved_command_line[512];
static char version[512];
static unsigned int maxsocks = 0;
static int maxudp = 0;
void
ns_main_earlywarning(const char *format, ...) {
} else {
}
}
void
ns_main_earlyfatal(const char *format, ...) {
"exiting (due to early fatal error)");
} else {
}
exit(1);
}
ISC_PLATFORM_NORETURN_PRE static void
const char *cond) ISC_PLATFORM_NORETURN_POST;
static void
const char *cond)
{
void *tracebuf[BACKTRACE_MAXFRAME];
int i, nframes;
const char *logsuffix = "";
const char *fname;
/*
* Handle assertion failures.
*/
/*
* Reset the assertion callback in case it is the log
* routines causing the assertion.
*/
&nframes);
logsuffix = ", back trace";
if (result == ISC_R_SUCCESS) {
for (i = 0; i < nframes; i++) {
unsigned long offset;
&fname,
&offset);
if (result == ISC_R_SUCCESS) {
"#%d %p in %s()+0x%lx", i,
offset);
} else {
"#%d %p in ??", i,
tracebuf[i]);
}
}
}
"exiting (due to assertion failure)");
} else {
}
if (ns_g_coreok)
abort();
exit(1);
}
ISC_PLATFORM_NORETURN_PRE static void
static void
{
/*
* Handle isc_error_fatal() calls from our libraries.
*/
/*
* Reset the error callback in case it is the log
* routines causing the assertion.
*/
"exiting (due to fatal error in library)");
} else {
}
if (ns_g_coreok)
abort();
exit(1);
}
static void
static void
{
/*
* Handle isc_error_unexpected() calls from our libraries.
*/
} else {
}
}
static void
lwresd_usage(void) {
"usage: lwresd [-4|-6] [-c conffile | -C resolvconffile] "
"[-d debuglevel] [-f|-g]\n"
" [-i pidfile] [-n number_of_cpus] "
"[-p port] [-P listen-port]\n"
" [-s] [-S sockets] [-t chrootdir] [-u username] "
"[-U listeners]\n"
" [-m {usage|trace|record|size|mctx}]\n"
"usage: lwresd [-v|-V]\n");
}
static void
usage(void) {
if (ns_g_lwresdonly) {
lwresd_usage();
return;
}
"usage: named [-4|-6] [-c conffile] [-d debuglevel] "
"[-E engine] [-f|-g]\n"
" [-n number_of_cpus] [-p port] [-s] "
"[-S sockets] [-t chrootdir]\n"
" [-u username] [-U listeners] "
"[-m {usage|trace|record|size|mctx}]\n"
"usage: named [-v|-V]\n");
}
static void
int i;
char *src;
char *dst;
char *eob;
const char truncated[] = "...";
*dst++ = ' ';
/*
* This won't perfectly produce a shell-independent
* pastable command line in all circumstances, but
* comes close, and for practical purposes will
* nearly always be fine.
*/
} else {
*dst++ = '\\';
}
}
}
else
*dst = '\0';
}
static int
char *endp;
int tmp;
long int ltmp;
if (*endp != '\0')
return (tmp);
}
static struct flag_def {
const char *name;
unsigned int value;
} mem_debug_flags[] = {
{ "none", 0},
{ "trace", ISC_MEM_DEBUGTRACE },
{ "record", ISC_MEM_DEBUGRECORD },
{ "usage", ISC_MEM_DEBUGUSAGE },
{ "size", ISC_MEM_DEBUGSIZE },
{ "mctx", ISC_MEM_DEBUGCTX },
{ NULL, 0 }
};
static void
for (;;) {
int arglen;
goto found;
}
}
break;
}
if (clear)
*ret = 0;
}
static void
parse_fuzz_arg(void) {
} else {
ns_main_earlyfatal("unknown fuzzing type '%s'",
}
}
static void
int ch;
int port;
const char *p;
/*
* NS_MAIN_ARGS is defined in main.h, so that it can be used
* both by named and by ntservice hooks.
*/
switch (ch) {
case '4':
if (ns_g_disable4)
ns_main_earlyfatal("cannot specify -4 and -6");
if (isc_net_probeipv4() != ISC_R_SUCCESS)
ns_main_earlyfatal("IPv4 not supported by OS");
break;
case '6':
if (ns_g_disable6)
ns_main_earlyfatal("cannot specify -4 and -6");
if (isc_net_probeipv6() != ISC_R_SUCCESS)
ns_main_earlyfatal("IPv6 not supported by OS");
break;
case 'A':
break;
case 'c':
ns_main_earlyfatal("cannot specify -c and -C");
break;
case 'C':
if (ns_g_conffileset)
ns_main_earlyfatal("cannot specify -c and -C");
break;
case 'd':
"debug level");
break;
case 'D':
/* Descriptive comment for 'ps'. */
break;
case 'E':
break;
case 'f':
break;
case 'g':
break;
/* XXXBEW -i should be removed */
case 'i':
break;
case 'l':
break;
case 'L':
break;
case 'M':
isc_mem_defaultflags = 0;
break;
case 'm':
break;
case 'N': /* Deprecated. */
case 'n':
"number of cpus");
if (ns_g_cpus == 0)
ns_g_cpus = 1;
break;
case 'p':
ns_main_earlyfatal("port '%s' out of range",
break;
/* XXXBEW Should -P be removed? */
case 'P':
ns_main_earlyfatal("port '%s' out of range",
break;
case 's':
/* XXXRTH temporary syntax */
break;
case 'S':
"max number of sockets");
break;
case 't':
/* XXXJAB should we make a copy? */
break;
case 'T': /* NOT DOCUMENTED */
/*
* force the server to behave (or misbehave) in
* specified ways for testing purposes.
*
* clienttest: make clients single shot with their
* own memory context.
* delay=xxxx: delay client responses by xxxx ms to
* simulate remote servers.
* dscp=x: check that dscp values are as
* expected and assert otherwise.
*/
maxudp = 512;
maxudp = 1460;
else if (!strncmp(isc_commandline_argument,
"maxudp=", 7))
else if (!strncmp(isc_commandline_argument,
"delay=", 6))
else if (!strncmp(isc_commandline_argument,
"mkeytimers=", 11))
{
if (p == NULL)
ns_main_earlyfatal("bad mkeytimer");
dns_zone_mkey_hour = atoi(p);
if (dns_zone_mkey_hour == 0)
ns_main_earlyfatal("bad mkeytimer");
if (p == NULL) {
(24 * dns_zone_mkey_hour);
(30 * dns_zone_mkey_day);
break;
}
dns_zone_mkey_day = atoi(p);
ns_main_earlyfatal("bad mkeytimer");
if (p == NULL) {
(30 * dns_zone_mkey_day);
break;
}
dns_zone_mkey_month = atoi(p);
ns_main_earlyfatal("bad mkeytimer");
else
break;
case 'U':
"number of UDP listeners "
"per interface");
break;
case 'u':
break;
case 'v':
printf("%s %s%s%s <id:%s>\n",
exit(0);
case 'V':
printf("built by %s with %s\n",
#ifdef __clang__
#else
#if defined(__ICC) || defined(__INTEL_COMPILER)
#else
#ifdef __GNUC__
#endif
#endif
#endif
#ifdef _MSC_VER
#endif
#ifdef __SUNPRO_C
#endif
#ifdef OPENSSL
printf("compiled with OpenSSL version: %s\n",
printf("linked to OpenSSL version: %s\n",
#endif
#ifdef HAVE_LIBXML2
printf("compiled with libxml2 version: %s\n",
printf("linked to libxml2 version: %s\n",
#endif
#if defined(HAVE_JSON) && defined(JSON_C_VERSION)
printf("compiled with libjson-c version: %s\n",
printf("linked to libjson-c version: %s\n",
json_c_version());
#endif
#if defined(HAVE_ZLIB) && defined(ZLIB_VERSION)
printf("compiled with zlib version: %s\n",
printf("linked to zlib version: %s\n",
zlibVersion());
#endif
#ifdef ISC_PLATFORM_USETHREADS
printf("threads support is enabled\n");
#else
printf("threads support is disabled\n");
#endif
exit(0);
case 'x':
/* Obsolete. No longer in use. Ignore. */
break;
case 'X':
else
break;
case 'F':
/* Reserved for FIPS mode */
/* FALLTHROUGH */
case '?':
usage();
if (isc_commandline_option == '?')
exit(0);
if (p == NULL || *++p != ':')
ns_main_earlyfatal("unknown option '-%c'",
else
ns_main_earlyfatal("option '-%c' requires "
"an argument",
/* FALLTHROUGH */
default:
}
}
if (argc > 0) {
usage();
ns_main_earlyfatal("extra command line arguments");
}
}
static isc_result_t
create_managers(void) {
unsigned int socks;
INSIST(ns_g_cpus_detected > 0);
#ifdef ISC_PLATFORM_USETHREADS
if (ns_g_cpus == 0)
ISC_LOG_INFO, "found %u CPU%s, using %u worker thread%s",
#else
ns_g_cpus = 1;
#endif
#ifdef WIN32
ns_g_udpdisp = 1;
#else
if (ns_g_udpdisp == 0) {
if (ns_g_cpus_detected == 1)
ns_g_udpdisp = 1;
else
}
if (ns_g_udpdisp > ns_g_cpus)
#endif
#ifdef ISC_PLATFORM_USETHREADS
ISC_LOG_INFO, "using %u UDP listener%s per interface",
#endif
if (result != ISC_R_SUCCESS) {
"isc_taskmgr_create() failed: %s",
return (ISC_R_UNEXPECTED);
}
if (result != ISC_R_SUCCESS) {
"isc_timermgr_create() failed: %s",
return (ISC_R_UNEXPECTED);
}
if (result != ISC_R_SUCCESS) {
"isc_socketmgr_create() failed: %s",
return (ISC_R_UNEXPECTED);
}
if (result == ISC_R_SUCCESS) {
}
if (result != ISC_R_SUCCESS) {
"isc_entropy_create() failed: %s",
return (ISC_R_UNEXPECTED);
}
if (result != ISC_R_SUCCESS) {
"isc_hash_create() failed: %s",
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
static void
destroy_managers(void) {
/*
* isc_taskmgr_destroy() will block until all tasks have exited,
*/
/*
* isc_hash_destroy() cannot be called as long as a resolver may be
* running. Calling this after isc_taskmgr_destroy() ensures the
* call is safe.
*/
}
static void
dump_symboltable(void) {
int i;
const char *fname;
const void *addr;
if (isc__backtrace_nsymbols == 0)
return;
return;
if (result == ISC_R_SUCCESS) {
}
}
}
#ifdef HAVE_LIBSECCOMP
static void
setup_seccomp() {
unsigned int i;
int ret;
/* Make sure the lists are in sync */
INSIST((sizeof(scmp_syscalls) / sizeof(int)) ==
(sizeof(scmp_syscall_names) / sizeof(const char *)));
"libseccomp activation failed");
return;
}
for (i = 0 ; i < sizeof(scmp_syscalls)/sizeof(*(scmp_syscalls)); i++) {
scmp_syscalls[i], 0);
if (ret < 0)
"libseccomp rule failed: %s",
scmp_syscall_names[i]);
else
"added libseccomp rule: %s",
scmp_syscall_names[i]);
}
if (ret < 0) {
"libseccomp unable to load filter");
} else {
"libseccomp sandboxing active");
}
/*
* Release filter in ctx. Filters already loaded are not
* affected.
*/
}
#endif /* HAVE_LIBSECCOMP */
static void
setup(void) {
#ifdef HAVE_LIBSCF
#endif
/*
* Get the user and group information before changing the root
* directory, so the administrator does not need to keep a copy
* of the user and group databases in the chroot'ed environment.
*/
/*
* Initialize time conversion information
*/
ns_os_tzset();
#ifdef HAVE_LIBSCF
/* Check if named is under smf control, before chroot. */
/* We don't care about instance, just check if we got one. */
if (result == ISC_R_SUCCESS)
ns_smf_got_instance = 1;
else
ns_smf_got_instance = 0;
#endif /* HAVE_LIBSCF */
#ifdef PATH_RANDOMDEV
/*
* Initialize system's random device as fallback entropy source
* if running chroot'ed.
*/
if (ns_g_chrootdir != NULL) {
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("isc_entropy_create() failed: %s",
if (result != ISC_R_SUCCESS) {
ns_main_earlywarning("could not open pre-chroot "
"entropy source %s: %s",
}
}
#endif
#ifdef ISC_PLATFORM_USETHREADS
/*
* Check for the number of cpu's before ns_os_chroot().
*/
#endif
/*
* For operating systems which have a capability mechanism, now
* is the time to switch to minimal privs and change our user id.
* On traditional UNIX systems, this call will be a no-op, and we
* will change the user ID after reading the config file the first
* time. (We need to read the config file to know which possibly
* privileged ports to bind() to.)
*/
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("ns_log_init() failed: %s",
/*
* Now is the time to daemonize (if we're not running in the
* foreground). We waited until now because we wanted to get
* a valid logging context setup. We cannot daemonize any later,
* because calling create_managers() will create threads, which
* would be lost after fork().
*/
if (!ns_g_foreground)
/*
* We call isc_app_start() here as some versions of FreeBSD's fork()
* destroys all the signal handling it sets up.
*/
result = isc_app_start();
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("isc_app_start() failed: %s",
ISC_LOG_NOTICE, "starting %s %s%s%s <id:%s>",
ISC_LOG_NOTICE, "running as: %s%s",
"----------------------------------------------------");
"BIND 9 is maintained by Internet Systems Consortium,");
"Inc. (ISC), a non-profit 501(c)(3) public-benefit ");
"corporation. Support and training for BIND 9 are ");
"available at https://www.isc.org/support");
"----------------------------------------------------");
/*
* Get the initial resource limits.
*/
/*
* System resources cannot effectively be tuned on some systems.
* Raise the limit in such cases for safety.
*/
if (old_openfiles != ns_g_initopenfiles) {
"adjusted limit on open files from "
}
/*
* If the named configuration filename is relative, prepend the current
* directory's name before possibly changing to another directory.
*/
if (! isc_file_isabsolute(ns_g_conffile)) {
sizeof(absolute_conffile));
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("could not construct absolute path "
"of configuration file: %s",
}
/*
* Record the server's startup time.
*/
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("isc_time_now() failed: %s",
result = create_managers();
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("create_managers() failed: %s",
/*
* Add calls to register sdb drivers here.
*/
/* xxdb_init(); */
#ifdef ISC_DLZ_DLOPEN
/*
* Register the DLZ "dlopen" driver.
*/
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("dlz_dlopen_init() failed: %s",
#endif
#if CONTRIB_DLZ
/*
* Register any other contributed DLZ drivers.
*/
result = dlz_drivers_init();
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("dlz_drivers_init() failed: %s",
#endif
#ifdef HAVE_LIBSECCOMP
#endif /* HAVE_LIBSECCOMP */
}
static void
cleanup(void) {
if (ns_g_fallbackentropy != NULL)
/*
* Add calls to unregister sdb drivers here.
*/
/* xxdb_clear(); */
#ifdef CONTRIB_DLZ
/*
* Unregister contributed DLZ drivers.
*/
#endif
#ifdef ISC_DLZ_DLOPEN
/*
* Unregister "dlopen" DLZ driver.
*/
#endif
ISC_LOG_NOTICE, "exiting");
}
void
ns_main_setmemstats(const char *filename) {
/*
* Caller has to ensure locking.
*/
}
return;
if (memstats)
}
#ifdef HAVE_LIBSCF
/*
* Get FMRI for the named process.
*/
scf_handle_t *h = NULL;
int namelen;
char *instance;
if (debug)
"scf_handle_create() failed: %s",
scf_strerror(scf_error()));
return (ISC_R_FAILURE);
}
if (scf_handle_bind(h) == -1) {
if (debug)
"scf_handle_bind() failed: %s",
scf_strerror(scf_error()));
return (ISC_R_FAILURE);
}
if (debug)
"scf_myname() failed: %s",
scf_strerror(scf_error()));
return (ISC_R_FAILURE);
}
"ns_smf_get_instance memory "
"allocation failed: %s",
return (ISC_R_FAILURE);
}
if (debug)
"scf_myname() failed: %s",
scf_strerror(scf_error()));
return (ISC_R_FAILURE);
}
return (ISC_R_SUCCESS);
}
#endif /* HAVE_LIBSCF */
/* main entry point, possibly hooked */
int
#ifdef HAVE_LIBSCF
#endif
#ifdef HAVE_GPERFTOOLS_PROFILER
(void) ProfilerStart(NULL);
#endif
/*
* Record version in core image.
* strings named.core | grep "named version:"
*/
#if defined(NO_VERSION_DATE) || !defined(__DATE__)
#else
#endif
sizeof(version));
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("program name too long");
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("failed to build internal symbol table");
#ifdef PKCS11CRYPTO
#endif
#ifdef ENABLE_AFL
if (ns_g_fuzz_type != ns_fuzz_none) {
}
if (ns_g_fuzz_type == ns_fuzz_resolver) {
} else if (ns_g_fuzz_type == ns_fuzz_http) {
}
#endif
/*
* Warn about common configuration error.
*/
if (ns_g_chrootdir != NULL) {
ns_main_earlywarning("config filename (-c %s) contains "
"chroot path (-t %s)",
}
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("isc_mem_create() failed: %s",
setup();
/*
* Start things running and then wait for a shutdown request
* or reload.
*/
do {
result = isc_app_run();
if (result == ISC_R_RELOAD) {
} else if (result != ISC_R_SUCCESS) {
"isc_app_run(): %s",
/*
* Force exit.
*/
}
} while (result != ISC_R_SUCCESS);
#ifdef HAVE_LIBSCF
if (ns_smf_want_disable == 1) {
if (smf_disable_instance(instance, 0) != 0)
"smf_disable_instance() "
"failed for %s : %s",
scf_strerror(scf_error()));
}
}
#endif /* HAVE_LIBSCF */
cleanup();
if (want_stats) {
}
if (result == ISC_R_SUCCESS) {
}
}
#ifdef HAVE_GPERFTOOLS_PROFILER
ProfilerStop();
#endif
return (0);
}