os.c revision 2c329da87c5c886e7f4468c69a9e6323121068cb
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Copyright (C) 1999-2002 Internet Software Consortium.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Permission to use, copy, modify, and distribute this software for any
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * purpose with or without fee is hereby granted, provided that the above
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * copyright notice and this permission notice appear in all copies.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User/* $Id: os.c,v 1.70 2004/09/29 06:45:37 marka Exp $ */
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson#include <sys/types.h> /* dev_t FreeBSD 2.1 */
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson#include <grp.h> /* Required for initgroups() on IRIX. */
a26e1cacef2047b1048febd5e8d756060b4bddb1Evan Hunt * If there's no <linux/capability.h>, we don't care about <sys/prctl.h>
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson * Linux defines:
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson * (T) HAVE_LINUXTHREADS
a26e1cacef2047b1048febd5e8d756060b4bddb1Evan Hunt * (C) HAVE_LINUX_CAPABILITY_H
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson * (P) HAVE_SYS_PRCTL_H
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson * The possible cases are:
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson * none: setuid() normally
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson * T: no setuid()
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson * C: setuid() normally, drop caps (keep CAP_SETUID)
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson * T+C: no setuid(), drop caps (don't keep CAP_SETUID)
38bcbbf947f0611bcae2e127d004dab761770c64Mark Andrews * T+C+P: setuid() early, drop caps (keep CAP_SETUID)
38bcbbf947f0611bcae2e127d004dab761770c64Mark Andrews * C+P: setuid() normally, drop caps (keep CAP_SETUID)
38bcbbf947f0611bcae2e127d004dab761770c64Mark Andrews * P: not possible
38bcbbf947f0611bcae2e127d004dab761770c64Mark Andrews * T+P: not possible
38bcbbf947f0611bcae2e127d004dab761770c64Mark Andrews * caps = BIND_SERVICE + CHROOT + SETGID
38bcbbf947f0611bcae2e127d004dab761770c64Mark Andrews * if ((T && C && P) || !T)
38bcbbf947f0611bcae2e127d004dab761770c64Mark Andrews * caps += SETUID
38bcbbf947f0611bcae2e127d004dab761770c64Mark Andrews * capset(caps)
38bcbbf947f0611bcae2e127d004dab761770c64Mark Andrews * if (T && C && P && -u)
38bcbbf947f0611bcae2e127d004dab761770c64Mark Andrews * else if (T && -u)
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson * --> start threads
38bcbbf947f0611bcae2e127d004dab761770c64Mark Andrews * if (!T && -u)
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson * if (C && (P || !-u))
c4213ed935163b25bde9273a7c1a79a6296a0662Andreas Gustafsson * caps = BIND_SERVICE
c4213ed935163b25bde9273a7c1a79a6296a0662Andreas Gustafsson * capset(caps)
c4213ed935163b25bde9273a7c1a79a6296a0662Andreas Gustafsson * It will be nice when Linux threads work properly with setuid().
c4213ed935163b25bde9273a7c1a79a6296a0662Andreas Gustafssonstatic isc_boolean_t non_root_caps = ISC_FALSE;
c4213ed935163b25bde9273a7c1a79a6296a0662Andreas Gustafsson * We define _LINUX_FS_H to prevent it from being included. We don't need
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson * anything from it, and the files it includes cause warnings with 2.2
38bcbbf947f0611bcae2e127d004dab761770c64Mark Andrews * kernels, and compilation failures (due to conflicts between <linux/string.h>
a26e1cacef2047b1048febd5e8d756060b4bddb1Evan Hunt * and <string.h>) on 2.3 kernels.
38bcbbf947f0611bcae2e127d004dab761770c64Mark Andrews#include <sys/syscall.h> /* Required for syscall(). */
a26e1cacef2047b1048febd5e8d756060b4bddb1Evan Hunt#include <linux/capability.h> /* Required for _LINUX_CAPABILITY_VERSION. */
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson#include <sys/prctl.h> /* Required for prctl(). */
c4213ed935163b25bde9273a7c1a79a6296a0662Andreas Gustafsson * If the value of PR_SET_KEEPCAPS is not in <sys/prctl.h>, define it
c4213ed935163b25bde9273a7c1a79a6296a0662Andreas Gustafsson * here. This allows setuid() to work on systems running a new enough
c4213ed935163b25bde9273a7c1a79a6296a0662Andreas Gustafsson * kernel but with /usr/include/linux pointing to "standard" kernel
c4213ed935163b25bde9273a7c1a79a6296a0662Andreas Gustafsson#endif /* HAVE_SYS_PRCTL_H */
6e8a02d5d3a845ca1768e92c88bdc0a4da47f95bAndreas Gustafsson#include <asm/unistd.h> /* Slackware 4.0 needs this. */
8a86c12ec245eb3838f48ffbc5a01fb9b7666a60Evan Hunt if ((getuid() != 0 && !non_root_caps) || non_root)
61d7ab455fe6c2c1be112e7f48a768b9db1f65bbMark Andrews " please ensure that the capset kernel"
61d7ab455fe6c2c1be112e7f48a768b9db1f65bbMark Andrews " module is loaded. see insmod(8)",
c85ffa76df4b94e0c44b2924f7a7f2e875a1ea86Mark Andrews unsigned int caps;
c85ffa76df4b94e0c44b2924f7a7f2e875a1ea86Mark Andrews * We don't need most privileges, so we drop them right away.
c85ffa76df4b94e0c44b2924f7a7f2e875a1ea86Mark Andrews * Later on linux_minprivs() will be called, which will drop our
c85ffa76df4b94e0c44b2924f7a7f2e875a1ea86Mark Andrews * capabilities to the minimum needed to run the server.
3d2ce18535b3d2d828bd40aec7f4686519f305feMark Andrews * We need to be able to bind() to privileged ports, notably port 53!
3d2ce18535b3d2d828bd40aec7f4686519f305feMark Andrews * We need chroot() initially too.
4347f7ac1286aaa114935823c3cd59151de0166bMark Andrews#if defined(HAVE_SYS_PRCTL_H) || !defined(HAVE_LINUXTHREADS)
4347f7ac1286aaa114935823c3cd59151de0166bMark Andrews * We can setuid() only if either the kernel supports keeping
4347f7ac1286aaa114935823c3cd59151de0166bMark Andrews * capabilities after setuid() (which we don't know until we've
4347f7ac1286aaa114935823c3cd59151de0166bMark Andrews * tried) or we're not using threads. If either of these is
4347f7ac1286aaa114935823c3cd59151de0166bMark Andrews * true, we want the setuid capability.
4347f7ac1286aaa114935823c3cd59151de0166bMark Andrews * Since we call initgroups, we need this.
4347f7ac1286aaa114935823c3cd59151de0166bMark Andrews * Without this, we run into problems reading a configuration file
4347f7ac1286aaa114935823c3cd59151de0166bMark Andrews * owned by a non-root user and non-world-readable on startup.
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson * XXX We might want to add CAP_SYS_RESOURCE, though it's not
1090574e9153ad41c403f812d9b6cef0334e828eMark Andrews * clear it would work right given the way linuxthreads work.
d49d75f5073294d798aa500728116309398bb535Andreas Gustafsson * XXXDCL But since we need to be able to set the maximum number
* support named.conf options, this is now being added to test.
linux_minprivs(void) {
unsigned int caps;
caps = 0;
* support named.conf options, this is now being added to test.
#ifdef HAVE_SYS_PRCTL_H
linux_keepcaps(void) {
if (getuid() != 0)
int options;
#ifdef LOG_NDELAY
#ifdef HAVE_LINUX_CAPABILITY_H
#ifdef HAVE_LINUXTHREADS
#ifdef SIGXFSZ
ns_os_daemonize(void) {
if (pid != 0) {
char buf;
_exit(0);
#ifdef HAVE_LINUXTHREADS
ns_os_started(void) {
char buf = 0;
ns_os_opendevnull(void) {
ns_os_closedevnull(void) {
static isc_boolean_t
all_digits(const char *s) {
return (ISC_FALSE);
return (ISC_FALSE);
return (ISC_TRUE);
endpwent();
if (getuid() == 0) {
ns_os_changeuser(void) {
#ifdef HAVE_LINUXTHREADS
#ifdef HAVE_LINUX_CAPABILITY_H
if (!non_root_caps)
ns_os_minprivs(void) {
#ifdef HAVE_SYS_PRCTL_H
#ifdef HAVE_LINUXTHREADS
int fd;
if (append)
return (fd);
cleanup_pidfile(void) {
int fd;
void (*report)(const char *, ...);
if (fd < 0) {
#ifdef HAVE_LINUXTHREADS
ns_os_shutdown(void) {
closelog();
char *res;
return (res);
#ifdef HAVE_LINUXTHREADS
ns_os_tzset(void) {
#ifdef HAVE_TZSET
tzset();