os.c revision 51e0ad287f1b345f0c3316f0633aab14d0e8bb65
/*
* Copyright (C) 1999, 2000 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* 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 INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM 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.
*/
/* $Id: os.c,v 1.32 2000/08/29 17:54:23 bwelling Exp $ */
#include <config.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 <syslog.h>
#include <unistd.h>
/*
* If there's no <linux/capability.h>, we don't care about <linux/prctl.h>
*/
#ifndef HAVE_LINUX_CAPABILITY_H
#endif
/*
* Linux defines:
* (T) HAVE_LINUXTHREADS
* (C) HAVE_LINUX_CAPABILITY_H
* (P) HAVE_LINUX_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
/*
* 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
#ifdef HAVE_LINUX_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_LINUX_PRCTL_H */
#ifndef SYS_capset
#define SYS_capset __NR_capset
#endif
static void
linux_setcaps(unsigned int caps) {
struct __user_cap_header_struct caphead;
struct __user_cap_data_struct cap;
return;
}
static void
linux_initialprivs(void) {
unsigned int caps;
/*
* 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.
*/
caps = 0;
/*
* We need to be able to bind() to privileged ports, notably port 53!
*/
/*
* We need chroot() initially too.
*/
#if defined(HAVE_LINUX_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.
*/
/*
* XXX We might want to add CAP_SYS_RESOURCE, though it's not
* clear it would work right given the way linuxthreads work.
*/
}
static void
linux_minprivs(void) {
unsigned int caps;
/*
* 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.
*/
caps = 0;
}
#ifdef HAVE_LINUX_PRCTL_H
static void
linux_keepcaps(void) {
/*
* Ask the kernel to allow us to keep our capabilities after we
* setuid().
*/
ns_main_earlyfatal("prctl() failed: %s",
} 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
}
void
ns_os_daemonize(void) {
int fd;
if (pid == -1)
if (pid != 0)
_exit(0);
/*
* 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 (fd != -1) {
if (fd != STDIN_FILENO &&
fd != STDOUT_FILENO &&
fd != STDERR_FILENO)
}
}
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) {
if (chdir("/") < 0)
}
}
void
ns_os_inituserinfo(const char *username) {
return;
if (all_digits(username))
else
endpwent();
if (getuid() == 0) {
}
}
void
ns_os_changeuser(void) {
return;
#ifdef HAVE_LINUXTHREADS
#ifdef HAVE_LINUX_CAPABILITY_H
if (!non_root_caps)
#endif
"-u not supported on Linux kernels older than "
"2.3.99-pre3 when using threads");
#endif
#if defined(HAVE_LINUX_CAPABILITY_H) && !defined(HAVE_LINUXTHREADS)
#endif
}
void
ns_os_minprivs(void) {
#ifdef HAVE_LINUX_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
return (-1);
return (-1);
}
static void
cleanup_pidfile(void) {
}
void
ns_os_writepidfile(const char *filename) {
int fd;
/*
* The caller must ensure any required synchronization.
*/
ns_main_earlyfatal("couldn't malloc '%s': %s",
/* This is safe. */
if (fd < 0)
ns_main_earlyfatal("couldn't open pid file '%s': %s",
ns_main_earlyfatal("could not fdopen() pid file '%s': %s",
#ifdef HAVE_LINUXTHREADS
#else
#endif
ns_main_earlyfatal("fprintf() to pid file '%s' failed",
filename);
ns_main_earlyfatal("fflush() to pid file '%s' failed",
filename);
}
void
ns_os_shutdown(void) {
closelog();
}