os.c revision 7ae96d882326357448f8f440c52f47ac1b1fa455
/*
* Copyright (C) 2004-2011, 2013, 2014 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1999-2002 Internet Software Consortium.
*
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/*! \file */
#include <config.h>
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h> /* Required for initgroups() on IRIX. */
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <syslog.h>
#ifdef HAVE_TZSET
#include <time.h>
#endif
#include <unistd.h>
#include <isc/resource.h>
#include <isc/strerror.h>
#ifdef HAVE_LIBSCF
#include <named/ns_smf_globals.h>
#endif
static int devnullfd = -1;
static int singletonfd = -1;
#ifndef ISC_FACILITY
#define ISC_FACILITY LOG_DAEMON
#endif
/*
* If there's no <linux/capability.h>, we don't care about <sys/prctl.h>
*/
#ifndef HAVE_LINUX_CAPABILITY_H
#endif
/*
* Linux defines:
* (T) HAVE_LINUXTHREADS
* (C) HAVE_SYS_CAPABILITY_H (or HAVE_LINUX_CAPABILITY_H)
* (P) HAVE_SYS_PRCTL_H
* The possible cases are:
* none: setuid() normally
* T: no setuid()
* C: setuid() normally, drop caps (keep CAP_SETUID)
* T+C: no setuid(), drop caps (don't keep CAP_SETUID)
* T+C+P: setuid() early, drop caps (keep CAP_SETUID)
* C+P: setuid() normally, drop caps (keep CAP_SETUID)
* P: not possible
* T+P: not possible
*
* if (C)
* caps = BIND_SERVICE + CHROOT + SETGID
* if ((T && C && P) || !T)
* caps += SETUID
* endif
* capset(caps)
* endif
* if (T && C && P && -u)
* setuid()
* else if (T && -u)
* fail
* --> start threads
* if (!T && -u)
* setuid()
* if (C && (P || !-u))
* caps = BIND_SERVICE
* capset(caps)
* endif
*
* It will be nice when Linux threads work properly with setuid().
*/
#ifdef HAVE_LINUXTHREADS
#endif
#ifdef HAVE_LINUX_CAPABILITY_H
#ifdef HAVE_SYS_CAPABILITY_H
#include <sys/capability.h>
#else
#ifdef HAVE_LINUX_TYPES_H
#endif
/*%
* We define _LINUX_FS_H to prevent it from being included. We don't need
* anything from it, and the files it includes cause warnings with 2.2
* and <string.h>) on 2.3 kernels.
*/
#define _LINUX_FS_H
#include <linux/capability.h>
#include <syscall.h>
#ifndef SYS_capset
#ifndef __NR_capset
#endif /* __NR_capset */
#define SYS_capset __NR_capset
#endif /* SYS_capset */
#endif /* HAVE_SYS_CAPABILITY_H */
#ifdef HAVE_SYS_PRCTL_H
/*
* here. This allows setuid() to work on systems running a new enough
* headers.
*/
#ifndef PR_SET_KEEPCAPS
#define PR_SET_KEEPCAPS 8
#endif
#endif /* HAVE_SYS_PRCTL_H */
#ifdef HAVE_LIBCAP
#define SETCAPS_FUNC "cap_set_proc "
#else
typedef unsigned int cap_t;
#define SETCAPS_FUNC "syscall(capset) "
#endif /* HAVE_LIBCAP */
static void
#ifndef HAVE_LIBCAP
struct __user_cap_header_struct caphead;
struct __user_cap_data_struct cap;
#endif
char strbuf[ISC_STRERRORSIZE];
return;
#ifndef HAVE_LIBCAP
cap.inheritable = 0;
#endif
#ifdef HAVE_LIBCAP
if (cap_set_proc(caps) < 0) {
#else
#endif
" please ensure that the capset kernel"
" module is loaded. see insmod(8)",
strbuf);
}
}
#ifdef HAVE_LIBCAP
do { \
if (err == -1) { \
} \
\
if (err == -1) { \
} \
} \
} while (0)
#define INIT_CAP \
do { \
} \
curcaps = cap_get_proc(); \
} \
} while (0)
#define FREE_CAP \
{ \
} while (0)
#else
#endif /* HAVE_LIBCAP */
static void
linux_initialprivs(void) {
#ifdef HAVE_LIBCAP
char strbuf[ISC_STRERRORSIZE];
int err;
#endif
/*%
* We don't need most privileges, so we drop them right away.
* Later on linux_minprivs() will be called, which will drop our
* capabilities to the minimum needed to run the server.
*/
/*
* We need to be able to bind() to privileged ports, notably port 53!
*/
/*
* We need chroot() initially too.
*/
#if defined(HAVE_SYS_PRCTL_H) || !defined(HAVE_LINUXTHREADS)
/*
* We can setuid() only if either the kernel supports keeping
* capabilities after setuid() (which we don't know until we've
* tried) or we're not using threads. If either of these is
* true, we want the setuid capability.
*/
#endif
/*
* Since we call initgroups, we need this.
*/
/*
* Without this, we run into problems reading a configuration file
* owned by a non-root user and non-world-readable on startup.
*/
/*
* XXX We might want to add CAP_SYS_RESOURCE, though it's not
* clear it would work right given the way linuxthreads work.
* XXXDCL But since we need to be able to set the maximum number
* of files, the stack size, data size, and core dump size to
* support named.conf options, this is now being added to test.
*/
/*
* We need to be able to set the ownership of the containing
* directory of the pid file when we create it.
*/
#ifdef HAVE_LIBCAP
#endif
}
static void
linux_minprivs(void) {
#ifdef HAVE_LIBCAP
char strbuf[ISC_STRERRORSIZE];
int err;
#endif
/*%
* Drop all privileges except the ability to bind() to privileged
* ports.
*
* It's important that we drop CAP_SYS_CHROOT. If we didn't, it
* chroot() could be used to escape from the chrooted area.
*/
/*
* XXX We might want to add CAP_SYS_RESOURCE, though it's not
* clear it would work right given the way linuxthreads work.
* XXXDCL But since we need to be able to set the maximum number
* of files, the stack size, data size, and core dump size to
* support named.conf options, this is now being added to test.
*/
#ifdef HAVE_LIBCAP
#endif
}
#ifdef HAVE_SYS_PRCTL_H
static void
linux_keepcaps(void) {
char strbuf[ISC_STRERRORSIZE];
/*%
* Ask the kernel to allow us to keep our capabilities after we
* setuid().
*/
}
} else {
if (getuid() != 0)
}
}
#endif
#endif /* HAVE_LINUX_CAPABILITY_H */
static void
setup_syslog(const char *progname) {
int options;
#ifdef LOG_NDELAY
options |= LOG_NDELAY;
#endif
}
void
ns_os_init(const char *progname) {
#ifdef HAVE_LINUX_CAPABILITY_H
#endif
#ifdef HAVE_LINUXTHREADS
#endif
#ifdef SIGXFSZ
#endif
}
void
ns_os_daemonize(void) {
char strbuf[ISC_STRERRORSIZE];
}
if (pid == -1) {
}
if (pid != 0) {
int n;
/*
* Wait for the child to finish loading for the first time.
* This would be so much simpler if fork() worked once we
* were multi-threaded.
*/
do {
char buf;
if (n == 1)
_exit(0);
_exit(1);
}
/*
* We're the child.
*/
#ifdef HAVE_LINUXTHREADS
#endif
if (setsid() == -1) {
}
/*
* on even if it fails.
*
* XXXMLG The close() calls here are unneeded on all but NetBSD, but
* are harmless to include everywhere. dup2() is supposed to close
* the FD if it is in use, but unproven-pthreads-0.16 is broken
* and will end up closing the wrong FD. This will be fixed eventually,
* and these calls will be removed.
*/
if (devnullfd != -1) {
if (devnullfd != STDIN_FILENO) {
(void)close(STDIN_FILENO);
}
if (devnullfd != STDOUT_FILENO) {
(void)close(STDOUT_FILENO);
}
if (devnullfd != STDERR_FILENO) {
(void)close(STDERR_FILENO);
}
}
}
void
ns_os_started(void) {
char buf = 0;
/*
* Signal to the parent that we started successfully.
*/
ns_main_earlyfatal("unable to signal parent that we "
"otherwise started successfully.");
}
}
void
ns_os_opendevnull(void) {
}
void
ns_os_closedevnull(void) {
if (devnullfd != STDIN_FILENO &&
devnullfd != STDOUT_FILENO &&
devnullfd != STDERR_FILENO) {
devnullfd = -1;
}
}
static isc_boolean_t
all_digits(const char *s) {
if (*s == '\0')
return (ISC_FALSE);
while (*s != '\0') {
if (!isdigit((*s)&0xff))
return (ISC_FALSE);
s++;
}
return (ISC_TRUE);
}
void
ns_os_chroot(const char *root) {
char strbuf[ISC_STRERRORSIZE];
#ifdef HAVE_LIBSCF
ns_smf_chroot = 0;
#endif
#ifdef HAVE_CHROOT
}
#else
ns_main_earlyfatal("chroot(): disabled");
#endif
if (chdir("/") < 0) {
}
#ifdef HAVE_LIBSCF
/* Set ns_smf_chroot flag on successful chroot. */
ns_smf_chroot = 1;
#endif
}
}
void
ns_os_inituserinfo(const char *username) {
char strbuf[ISC_STRERRORSIZE];
return;
if (all_digits(username))
else
endpwent();
if (getuid() == 0) {
}
}
}
void
ns_os_changeuser(void) {
char strbuf[ISC_STRERRORSIZE];
return;
#ifdef HAVE_LINUXTHREADS
#ifdef HAVE_LINUX_CAPABILITY_H
if (!non_root_caps)
ns_main_earlyfatal("-u with Linux threads not supported: "
"requires kernel support for "
"prctl(PR_SET_KEEPCAPS)");
#else
ns_main_earlyfatal("-u with Linux threads not supported: "
"no capabilities support or capabilities "
"disabled at build time");
#endif
#endif
}
}
#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_DUMPABLE)
/*
* Restore the ability of named to drop core after the setuid()
* call has disabled it.
*/
ns_main_earlywarning("prctl(PR_SET_DUMPABLE) failed: %s",
strbuf);
}
#endif
#if defined(HAVE_LINUX_CAPABILITY_H) && !defined(HAVE_LINUXTHREADS)
#endif
}
void
ns_os_adjustnofile(void) {
#ifdef HAVE_LINUXTHREADS
/*
* Linux: max number of open files specified by one thread doesn't seem
* to apply to other threads on Linux.
*/
if (result != ISC_R_SUCCESS)
ns_main_earlywarning("couldn't adjust limit on open files");
#endif
}
void
ns_os_minprivs(void) {
#ifdef HAVE_SYS_PRCTL_H
#endif
#ifdef HAVE_LINUXTHREADS
ns_os_changeuser(); /* Call setuid() before threads are started */
#endif
#if defined(HAVE_LINUX_CAPABILITY_H) && defined(HAVE_LINUXTHREADS)
#endif
}
static int
int fd;
return (-1);
errno = EOPNOTSUPP;
return (-1);
}
if (append)
else {
return (-1);
}
return (fd);
}
static void
cleanup_pidfile(void) {
int n;
}
}
static void
cleanup_lockfile(void) {
if (singletonfd != -1) {
singletonfd = -1;
}
}
}
/*
* Ensure that a directory exists.
* NOTE: This function overwrites the '/' characters in 'filename' with
* nulls. The caller should copy the filename to a fresh buffer first.
*/
static int
char strbuf[ISC_STRERRORSIZE];
unsigned int mode;
*slash = '\0';
strbuf);
goto error;
}
goto error;
/*
* Handle "//", "/./" and "/../" in path.
*/
*slash = '/';
return (0);
}
strbuf);
goto error;
}
strbuf);
}
}
*slash = '/';
}
return (0);
*slash = '/';
return (-1);
}
static void
char strbuf[ISC_STRERRORSIZE];
#if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID)
#endif
#if !defined(HAVE_SETEUID) && defined(HAVE_SETRESUID)
#endif
#if defined(HAVE_SETEGID)
ns_main_earlywarning("unable to set effective gid to %ld: %s",
}
#elif defined(HAVE_SETRESGID)
ns_main_earlywarning("unable to set effective "
}
}
#endif
#if defined(HAVE_SETEUID)
ns_main_earlywarning("unable to set effective uid to %ld: %s",
}
#elif defined(HAVE_SETRESUID)
ns_main_earlywarning("unable to set effective "
}
}
#endif
}
FILE *
char strbuf[ISC_STRERRORSIZE], *f;
int fd;
/*
* Make the containing directory if it doesn't exist.
*/
if (f == NULL) {
ns_main_earlywarning("couldn't strdup() '%s': %s",
return (NULL);
}
free(f);
return (NULL);
}
free(f);
#ifndef HAVE_LINUXTHREADS
#endif
#ifndef HAVE_LINUXTHREADS
#endif /* HAVE_LINUXTHREADS */
if (fd == -1) {
#ifndef HAVE_LINUXTHREADS
if (fd != -1) {
ns_main_earlywarning("Required root "
"permissions to open "
"'%s'.", filename);
} else {
ns_main_earlywarning("Could not open "
"'%s'.", filename);
}
ns_main_earlywarning("Please check file and "
"directory permissions "
"or reconfigure the filename.");
#else /* HAVE_LINUXTHREADS */
ns_main_earlywarning("Could not open "
"'%s'.", filename);
ns_main_earlywarning("Please check file and "
"directory permissions "
"or reconfigure the filename.");
#endif /* HAVE_LINUXTHREADS */
}
} else {
}
if (fd < 0) {
ns_main_earlywarning("could not open file '%s': %s",
return (NULL);
}
ns_main_earlywarning("could not fdopen() file '%s': %s",
}
return (fp);
}
void
char strbuf[ISC_STRERRORSIZE];
void (*report)(const char *, ...);
/*
* The caller must ensure any required synchronization.
*/
return;
return;
}
return;
}
#ifdef HAVE_LINUXTHREADS
#else
#endif
return;
}
return;
}
}
ns_os_issingleton(const char *filename) {
char strbuf[ISC_STRERRORSIZE];
if (singletonfd != -1)
return (ISC_TRUE);
return (ISC_TRUE);
/*
* Make the containing directory if it doesn't exist.
*/
ns_main_earlyfatal("couldn't allocate memory for '%s': %s",
} else {
if (ret == -1) {
return (ISC_FALSE);
}
}
/*
* ns_os_openfile() uses safeopen() which removes any existing
* files. We can't use that here.
*/
if (singletonfd == -1) {
return (ISC_FALSE);
}
/* Non-blocking (does not wait for lock) */
singletonfd = -1;
return (ISC_FALSE);
}
return (ISC_TRUE);
}
void
ns_os_shutdown(void) {
closelog();
}
int n;
return ((n == 0) ? ISC_R_SUCCESS : ISC_R_FAILURE);
}
static char *
char *res;
do {
break;
} while (*res == '\0');
return (res);
}
void
unsigned int n;
/* Skip the command name. */
return;
return;
return;
#ifdef HAVE_LINUXTHREADS
#else
#endif
"pid: %ld", (long)pid);
/* Only send a message if it is complete. */
if (n > 0 && n < isc_buffer_availablelength(text))
isc_buffer_add(text, n);
}
void
ns_os_tzset(void) {
#ifdef HAVE_TZSET
tzset();
#endif
}