1999-10-23 01:08:57 +00:00
|
|
|
/*
|
2001-01-09 22:01:04 +00:00
|
|
|
* Copyright (C) 1999-2001 Internet Software Consortium.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
1999-10-23 01:08:57 +00:00
|
|
|
* 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.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2000-07-27 09:55:03 +00:00
|
|
|
* 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.
|
1999-10-23 01:08:57 +00:00
|
|
|
*/
|
|
|
|
|
2001-10-08 07:46:11 +00:00
|
|
|
/* $Id: os.c,v 1.51 2001/10/08 07:46:07 marka Exp $ */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1999-10-23 01:08:57 +00:00
|
|
|
#include <config.h>
|
2000-11-07 23:49:42 +00:00
|
|
|
#include <stdarg.h>
|
1999-10-23 01:08:57 +00:00
|
|
|
|
2001-05-25 07:42:15 +00:00
|
|
|
#include <sys/types.h> /* dev_t FreeBSD 2.1 */
|
2000-01-29 01:38:09 +00:00
|
|
|
#include <sys/stat.h>
|
1999-10-23 01:08:57 +00:00
|
|
|
|
2000-01-28 20:09:14 +00:00
|
|
|
#include <ctype.h>
|
1999-10-23 01:08:57 +00:00
|
|
|
#include <errno.h>
|
2000-01-26 21:12:33 +00:00
|
|
|
#include <fcntl.h>
|
2000-05-14 13:52:53 +00:00
|
|
|
#include <grp.h> /* Required for initgroups() on IRIX. */
|
2000-01-28 20:09:14 +00:00
|
|
|
#include <pwd.h>
|
2000-05-14 13:52:53 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
1999-10-23 01:08:57 +00:00
|
|
|
|
2001-05-03 19:06:25 +00:00
|
|
|
#include <isc/file.h>
|
2000-11-14 23:57:45 +00:00
|
|
|
#include <isc/print.h>
|
2000-11-07 23:49:42 +00:00
|
|
|
#include <isc/result.h>
|
2001-08-31 05:57:58 +00:00
|
|
|
#include <isc/strerror.h>
|
2000-11-14 23:57:45 +00:00
|
|
|
#include <isc/string.h>
|
1999-10-23 01:08:57 +00:00
|
|
|
|
|
|
|
#include <named/main.h>
|
|
|
|
#include <named/os.h>
|
|
|
|
|
2000-01-29 01:38:09 +00:00
|
|
|
static char *pidfile = NULL;
|
2000-08-29 17:54:23 +00:00
|
|
|
|
|
|
|
/*
|
2001-02-24 23:06:34 +00:00
|
|
|
* If there's no <linux/capability.h>, we don't care about <sys/prctl.h>
|
2000-08-29 17:54:23 +00:00
|
|
|
*/
|
|
|
|
#ifndef HAVE_LINUX_CAPABILITY_H
|
2001-02-24 23:05:09 +00:00
|
|
|
#undef HAVE_SYS_PRCTL_H
|
2000-08-29 17:54:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Linux defines:
|
|
|
|
* (T) HAVE_LINUXTHREADS
|
|
|
|
* (C) HAVE_LINUX_CAPABILITY_H
|
2001-02-24 23:05:09 +00:00
|
|
|
* (P) HAVE_SYS_PRCTL_H
|
2000-08-29 17:54:23 +00:00
|
|
|
* 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().
|
|
|
|
*/
|
|
|
|
|
2000-01-29 01:38:09 +00:00
|
|
|
#ifdef HAVE_LINUXTHREADS
|
|
|
|
static pid_t mainpid = 0;
|
|
|
|
#endif
|
1999-10-23 01:08:57 +00:00
|
|
|
|
2000-07-07 22:10:54 +00:00
|
|
|
static struct passwd *runas_pw = NULL;
|
2000-07-07 23:53:35 +00:00
|
|
|
static isc_boolean_t done_setuid = ISC_FALSE;
|
2000-07-01 00:48:06 +00:00
|
|
|
|
1999-10-23 01:08:57 +00:00
|
|
|
#ifdef HAVE_LINUX_CAPABILITY_H
|
2000-01-28 20:09:14 +00:00
|
|
|
|
2000-08-29 17:54:23 +00:00
|
|
|
static isc_boolean_t non_root = ISC_FALSE;
|
|
|
|
static isc_boolean_t non_root_caps = ISC_FALSE;
|
|
|
|
|
2000-03-28 19:46:53 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* kernels, and compilation failures (due to conflicts between <linux/string.h>
|
|
|
|
* and <string.h>) on 2.3 kernels.
|
|
|
|
*/
|
|
|
|
#define _LINUX_FS_H
|
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <sys/syscall.h> /* Required for syscall(). */
|
|
|
|
#include <linux/capability.h> /* Required for _LINUX_CAPABILITY_VERSION. */
|
1999-10-23 01:08:57 +00:00
|
|
|
|
2001-02-24 23:05:09 +00:00
|
|
|
#ifdef HAVE_SYS_PRCTL_H
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <sys/prctl.h> /* Required for prctl(). */
|
2000-08-10 18:56:58 +00:00
|
|
|
|
|
|
|
/*
|
2001-02-24 23:06:34 +00:00
|
|
|
* If the value of PR_SET_KEEPCAPS is not in <sys/prctl.h>, define it
|
2000-08-10 18:56:58 +00:00
|
|
|
* here. This allows setuid() to work on systems running a new enough
|
|
|
|
* kernel but with /usr/include/linux pointing to "standard" kernel
|
|
|
|
* headers.
|
|
|
|
*/
|
2000-08-10 00:31:59 +00:00
|
|
|
#ifndef PR_SET_KEEPCAPS
|
|
|
|
#define PR_SET_KEEPCAPS 8
|
2000-04-11 18:51:19 +00:00
|
|
|
#endif
|
2000-08-10 18:56:58 +00:00
|
|
|
|
2001-02-24 23:05:09 +00:00
|
|
|
#endif /* HAVE_SYS_PRCTL_H */
|
2000-04-11 18:51:19 +00:00
|
|
|
|
1999-10-23 01:08:57 +00:00
|
|
|
#ifndef SYS_capset
|
|
|
|
#define SYS_capset __NR_capset
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
2000-01-28 20:09:14 +00:00
|
|
|
linux_setcaps(unsigned int caps) {
|
1999-10-23 01:08:57 +00:00
|
|
|
struct __user_cap_header_struct caphead;
|
|
|
|
struct __user_cap_data_struct cap;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
1999-10-23 01:08:57 +00:00
|
|
|
|
2000-06-16 07:35:54 +00:00
|
|
|
if ((getuid() != 0 && !non_root_caps) || non_root)
|
1999-10-23 01:08:57 +00:00
|
|
|
return;
|
|
|
|
|
2001-09-19 23:08:24 +00:00
|
|
|
memset(&caphead, 0, sizeof(caphead));
|
1999-10-23 01:08:57 +00:00
|
|
|
caphead.version = _LINUX_CAPABILITY_VERSION;
|
|
|
|
caphead.pid = 0;
|
2001-09-19 23:08:24 +00:00
|
|
|
memset(&cap, 0, sizeof(cap));
|
1999-10-23 01:08:57 +00:00
|
|
|
cap.effective = caps;
|
|
|
|
cap.permitted = caps;
|
|
|
|
cap.inheritable = caps;
|
2001-08-31 05:57:58 +00:00
|
|
|
if (syscall(SYS_capset, &caphead, &cap) < 0) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
ns_main_earlyfatal("capset failed: %s", strbuf);
|
|
|
|
}
|
1999-10-23 01:08:57 +00:00
|
|
|
}
|
2000-01-28 20:09:14 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
linux_initialprivs(void) {
|
|
|
|
unsigned int caps;
|
|
|
|
|
|
|
|
/*
|
2000-04-11 18:51:19 +00:00
|
|
|
* 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.
|
2000-01-28 20:09:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
caps = 0;
|
2000-04-11 18:51:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to be able to bind() to privileged ports, notably port 53!
|
|
|
|
*/
|
2000-01-28 20:09:14 +00:00
|
|
|
caps |= (1 << CAP_NET_BIND_SERVICE);
|
2000-04-11 18:51:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need chroot() initially too.
|
|
|
|
*/
|
2000-01-28 20:09:14 +00:00
|
|
|
caps |= (1 << CAP_SYS_CHROOT);
|
2000-04-11 18:51:19 +00:00
|
|
|
|
2001-02-24 23:05:09 +00:00
|
|
|
#if defined(HAVE_SYS_PRCTL_H) || !defined(HAVE_LINUXTHREADS)
|
2000-04-11 18:51:19 +00:00
|
|
|
/*
|
2000-08-29 17:54:23 +00:00
|
|
|
* 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.
|
2000-04-11 18:51:19 +00:00
|
|
|
*/
|
|
|
|
caps |= (1 << CAP_SETUID);
|
|
|
|
#endif
|
|
|
|
|
2000-07-08 00:12:21 +00:00
|
|
|
/*
|
|
|
|
* Since we call initgroups, we need this.
|
|
|
|
*/
|
|
|
|
caps |= (1 << CAP_SETGID);
|
|
|
|
|
2001-01-09 00:10:42 +00:00
|
|
|
/*
|
|
|
|
* Without this, we run into problems reading a configuration file
|
|
|
|
* owned by a non-root user and non-world-readable on startup.
|
|
|
|
*/
|
|
|
|
caps |= (1 << CAP_DAC_READ_SEARCH);
|
|
|
|
|
2000-02-01 20:17:32 +00:00
|
|
|
/*
|
|
|
|
* XXX We might want to add CAP_SYS_RESOURCE, though it's not
|
|
|
|
* clear it would work right given the way linuxthreads work.
|
2000-11-14 23:57:45 +00:00
|
|
|
* 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.
|
2000-02-01 20:17:32 +00:00
|
|
|
*/
|
2000-11-14 23:57:45 +00:00
|
|
|
caps |= (1 << CAP_SYS_RESOURCE);
|
2000-04-11 18:51:19 +00:00
|
|
|
|
2000-01-28 20:09:14 +00:00
|
|
|
linux_setcaps(caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
linux_minprivs(void) {
|
|
|
|
unsigned int caps;
|
|
|
|
|
|
|
|
/*
|
2000-04-11 18:51:19 +00:00
|
|
|
* Drop all privileges except the ability to bind() to privileged
|
2000-02-01 20:17:32 +00:00
|
|
|
* ports.
|
2000-04-11 18:51:19 +00:00
|
|
|
*
|
|
|
|
* It's important that we drop CAP_SYS_CHROOT. If we didn't, it
|
|
|
|
* chroot() could be used to escape from the chrooted area.
|
2000-01-28 20:09:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
caps = 0;
|
|
|
|
caps |= (1 << CAP_NET_BIND_SERVICE);
|
|
|
|
|
2000-12-14 18:29:57 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
caps |= (1 << CAP_SYS_RESOURCE);
|
|
|
|
|
2000-01-28 20:09:14 +00:00
|
|
|
linux_setcaps(caps);
|
|
|
|
}
|
|
|
|
|
2001-02-24 23:05:09 +00:00
|
|
|
#ifdef HAVE_SYS_PRCTL_H
|
2000-04-11 18:51:19 +00:00
|
|
|
static void
|
|
|
|
linux_keepcaps(void) {
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2000-04-11 18:51:19 +00:00
|
|
|
/*
|
|
|
|
* Ask the kernel to allow us to keep our capabilities after we
|
|
|
|
* setuid().
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0) {
|
2001-08-31 05:57:58 +00:00
|
|
|
if (errno != EINVAL) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
ns_main_earlyfatal("prctl() failed: %s", strbuf);
|
|
|
|
}
|
2000-06-16 07:35:54 +00:00
|
|
|
} else {
|
2000-04-11 18:51:19 +00:00
|
|
|
non_root_caps = ISC_TRUE;
|
2000-06-16 07:35:54 +00:00
|
|
|
if (getuid() != 0)
|
|
|
|
non_root = ISC_TRUE;
|
|
|
|
}
|
2000-04-11 18:51:19 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-01-28 20:09:14 +00:00
|
|
|
#endif /* HAVE_LINUX_CAPABILITY_H */
|
|
|
|
|
1999-10-23 01:08:57 +00:00
|
|
|
|
1999-10-25 18:59:38 +00:00
|
|
|
static void
|
2000-07-18 17:50:02 +00:00
|
|
|
setup_syslog(const char *progname) {
|
1999-10-25 18:59:38 +00:00
|
|
|
int options;
|
|
|
|
|
|
|
|
options = LOG_PID;
|
|
|
|
#ifdef LOG_NDELAY
|
|
|
|
options |= LOG_NDELAY;
|
|
|
|
#endif
|
|
|
|
|
2001-05-03 19:06:25 +00:00
|
|
|
openlog(isc_file_basename(progname), options, LOG_DAEMON);
|
1999-10-25 18:59:38 +00:00
|
|
|
}
|
1999-10-23 01:08:57 +00:00
|
|
|
|
2000-01-28 20:09:14 +00:00
|
|
|
void
|
2000-07-18 17:50:02 +00:00
|
|
|
ns_os_init(const char *progname) {
|
|
|
|
setup_syslog(progname);
|
1999-10-23 01:08:57 +00:00
|
|
|
#ifdef HAVE_LINUX_CAPABILITY_H
|
2000-01-28 20:09:14 +00:00
|
|
|
linux_initialprivs();
|
1999-10-23 01:08:57 +00:00
|
|
|
#endif
|
2000-01-29 01:38:09 +00:00
|
|
|
#ifdef HAVE_LINUXTHREADS
|
|
|
|
mainpid = getpid();
|
|
|
|
#endif
|
1999-10-23 01:08:57 +00:00
|
|
|
}
|
|
|
|
|
2000-01-28 20:09:14 +00:00
|
|
|
void
|
2000-01-26 21:12:33 +00:00
|
|
|
ns_os_daemonize(void) {
|
|
|
|
pid_t pid;
|
|
|
|
int fd;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2000-01-26 21:12:33 +00:00
|
|
|
|
|
|
|
pid = fork();
|
2001-08-31 05:57:58 +00:00
|
|
|
if (pid == -1) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
ns_main_earlyfatal("fork(): %s", strbuf);
|
|
|
|
}
|
2000-01-26 21:12:33 +00:00
|
|
|
if (pid != 0)
|
2000-03-28 00:10:02 +00:00
|
|
|
_exit(0);
|
2000-01-26 21:12:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We're the child.
|
|
|
|
*/
|
|
|
|
|
2000-01-29 01:38:09 +00:00
|
|
|
#ifdef HAVE_LINUXTHREADS
|
|
|
|
mainpid = getpid();
|
|
|
|
#endif
|
|
|
|
|
2001-08-31 05:57:58 +00:00
|
|
|
if (setsid() == -1) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
ns_main_earlyfatal("setsid(): %s", strbuf);
|
|
|
|
}
|
2000-01-26 21:12:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to set stdin, stdout, and stderr to /dev/null, but press
|
|
|
|
* on even if it fails.
|
2000-06-28 16:26:40 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2000-01-26 21:12:33 +00:00
|
|
|
*/
|
|
|
|
fd = open("/dev/null", O_RDWR, 0);
|
|
|
|
if (fd != -1) {
|
2000-06-28 16:26:40 +00:00
|
|
|
close(STDIN_FILENO);
|
2000-01-26 21:12:33 +00:00
|
|
|
(void)dup2(fd, STDIN_FILENO);
|
2000-06-28 16:26:40 +00:00
|
|
|
close(STDOUT_FILENO);
|
2000-01-26 21:12:33 +00:00
|
|
|
(void)dup2(fd, STDOUT_FILENO);
|
2000-06-28 16:26:40 +00:00
|
|
|
close(STDERR_FILENO);
|
2000-01-26 21:12:33 +00:00
|
|
|
(void)dup2(fd, STDERR_FILENO);
|
2000-01-28 20:09:14 +00:00
|
|
|
if (fd != STDIN_FILENO &&
|
|
|
|
fd != STDOUT_FILENO &&
|
|
|
|
fd != STDERR_FILENO)
|
|
|
|
(void)close(fd);
|
2000-01-26 21:12:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-28 20:09:14 +00:00
|
|
|
static isc_boolean_t
|
|
|
|
all_digits(const char *s) {
|
|
|
|
if (*s == '\0')
|
|
|
|
return (ISC_FALSE);
|
|
|
|
while (*s != '\0') {
|
2000-02-02 00:58:15 +00:00
|
|
|
if (!isdigit((*s)&0xff))
|
2000-01-28 20:09:14 +00:00
|
|
|
return (ISC_FALSE);
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_os_chroot(const char *root) {
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2000-01-28 20:09:14 +00:00
|
|
|
if (root != NULL) {
|
2001-08-31 05:57:58 +00:00
|
|
|
if (chroot(root) < 0) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
ns_main_earlyfatal("chroot(): %s", strbuf);
|
|
|
|
}
|
|
|
|
if (chdir("/") < 0) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
ns_main_earlyfatal("chdir(/): %s", strbuf);
|
|
|
|
}
|
2000-01-28 20:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-01 00:48:06 +00:00
|
|
|
ns_os_inituserinfo(const char *username) {
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2000-07-07 23:53:35 +00:00
|
|
|
if (username == NULL)
|
2000-01-28 20:09:14 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (all_digits(username))
|
2000-07-07 22:10:54 +00:00
|
|
|
runas_pw = getpwuid((uid_t)atoi(username));
|
2000-01-28 20:09:14 +00:00
|
|
|
else
|
2000-07-07 22:10:54 +00:00
|
|
|
runas_pw = getpwnam(username);
|
2000-01-28 20:09:14 +00:00
|
|
|
endpwent();
|
2000-07-01 00:48:06 +00:00
|
|
|
|
2000-07-07 22:10:54 +00:00
|
|
|
if (runas_pw == NULL)
|
2000-01-28 20:09:14 +00:00
|
|
|
ns_main_earlyfatal("user '%s' unknown", username);
|
2000-07-08 00:12:21 +00:00
|
|
|
|
|
|
|
if (getuid() == 0) {
|
2001-08-31 05:57:58 +00:00
|
|
|
if (initgroups(runas_pw->pw_name, runas_pw->pw_gid) < 0) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
ns_main_earlyfatal("initgroups(): %s", strbuf);
|
|
|
|
}
|
2000-07-08 00:12:21 +00:00
|
|
|
}
|
|
|
|
|
2000-07-01 00:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_os_changeuser(void) {
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2000-07-07 23:53:35 +00:00
|
|
|
if (runas_pw == NULL || done_setuid)
|
2000-07-05 22:03:42 +00:00
|
|
|
return;
|
|
|
|
|
2000-07-07 23:53:35 +00:00
|
|
|
done_setuid = ISC_TRUE;
|
|
|
|
|
2000-07-05 22:03:42 +00:00
|
|
|
#ifdef HAVE_LINUXTHREADS
|
2000-08-29 17:54:23 +00:00
|
|
|
#ifdef HAVE_LINUX_CAPABILITY_H
|
2000-07-05 22:03:42 +00:00
|
|
|
if (!non_root_caps)
|
2000-08-29 17:54:23 +00:00
|
|
|
#endif
|
2000-07-05 22:03:42 +00:00
|
|
|
ns_main_earlyfatal(
|
2000-08-29 17:54:23 +00:00
|
|
|
"-u not supported on Linux kernels older than "
|
2001-03-19 22:41:22 +00:00
|
|
|
"2.3.99-pre3 or 2.2.18 when using threads");
|
2000-08-01 01:33:37 +00:00
|
|
|
#endif
|
2000-07-05 22:03:42 +00:00
|
|
|
|
2001-08-31 05:57:58 +00:00
|
|
|
if (setgid(runas_pw->pw_gid) < 0) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
ns_main_earlyfatal("setgid(): %s", strbuf);
|
|
|
|
}
|
2000-07-07 22:10:54 +00:00
|
|
|
|
2001-08-31 05:57:58 +00:00
|
|
|
if (setuid(runas_pw->pw_uid) < 0) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
ns_main_earlyfatal("setuid(): %s", strbuf);
|
|
|
|
}
|
2000-08-29 17:54:23 +00:00
|
|
|
|
|
|
|
#if defined(HAVE_LINUX_CAPABILITY_H) && !defined(HAVE_LINUXTHREADS)
|
|
|
|
linux_minprivs();
|
|
|
|
#endif
|
2000-01-28 20:09:14 +00:00
|
|
|
}
|
2000-01-29 01:38:09 +00:00
|
|
|
|
2000-04-11 18:51:19 +00:00
|
|
|
void
|
2000-07-01 00:48:06 +00:00
|
|
|
ns_os_minprivs(void) {
|
2001-02-24 23:05:09 +00:00
|
|
|
#ifdef HAVE_SYS_PRCTL_H
|
2000-07-05 22:03:42 +00:00
|
|
|
linux_keepcaps();
|
2000-04-11 18:51:19 +00:00
|
|
|
#endif
|
2000-07-01 00:48:06 +00:00
|
|
|
|
2000-08-29 17:54:23 +00:00
|
|
|
#ifdef HAVE_LINUXTHREADS
|
|
|
|
ns_os_changeuser(); /* Call setuid() before threads are started */
|
|
|
|
#endif
|
2000-07-01 00:48:06 +00:00
|
|
|
|
2000-08-29 17:54:23 +00:00
|
|
|
#if defined(HAVE_LINUX_CAPABILITY_H) && defined(HAVE_LINUXTHREADS)
|
|
|
|
linux_minprivs();
|
|
|
|
#endif
|
2000-04-11 18:51:19 +00:00
|
|
|
}
|
|
|
|
|
2000-01-29 01:38:09 +00:00
|
|
|
static int
|
2000-11-07 23:49:42 +00:00
|
|
|
safe_open(const char *filename, isc_boolean_t append) {
|
|
|
|
int fd;
|
2000-01-29 01:38:09 +00:00
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (stat(filename, &sb) == -1) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
return (-1);
|
2001-04-04 21:45:29 +00:00
|
|
|
} else if ((sb.st_mode & S_IFREG) == 0) {
|
|
|
|
errno = EOPNOTSUPP;
|
2000-01-29 01:38:09 +00:00
|
|
|
return (-1);
|
2001-04-04 21:45:29 +00:00
|
|
|
}
|
2000-01-29 01:38:09 +00:00
|
|
|
|
2000-11-07 23:49:42 +00:00
|
|
|
if (append)
|
|
|
|
fd = open(filename, O_WRONLY|O_CREAT|O_APPEND,
|
|
|
|
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
|
|
|
|
else {
|
|
|
|
(void)unlink(filename);
|
|
|
|
fd = open(filename, O_WRONLY|O_CREAT|O_EXCL,
|
|
|
|
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
|
|
|
|
}
|
|
|
|
return (fd);
|
2000-01-29 01:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup_pidfile(void) {
|
2000-11-07 23:49:42 +00:00
|
|
|
if (pidfile != NULL) {
|
2000-01-29 01:38:09 +00:00
|
|
|
(void)unlink(pidfile);
|
2000-11-07 23:49:42 +00:00
|
|
|
free(pidfile);
|
|
|
|
}
|
2000-01-29 01:38:09 +00:00
|
|
|
pidfile = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_os_writepidfile(const char *filename) {
|
|
|
|
int fd;
|
|
|
|
FILE *lockfile;
|
|
|
|
size_t len;
|
|
|
|
pid_t pid;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2000-01-29 01:38:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The caller must ensure any required synchronization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
cleanup_pidfile();
|
|
|
|
|
|
|
|
len = strlen(filename);
|
|
|
|
pidfile = malloc(len + 1);
|
2001-08-31 05:57:58 +00:00
|
|
|
if (pidfile == NULL) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
2000-01-29 01:38:09 +00:00
|
|
|
ns_main_earlyfatal("couldn't malloc '%s': %s",
|
2001-08-31 05:57:58 +00:00
|
|
|
filename, strbuf);
|
|
|
|
}
|
2000-01-29 01:38:09 +00:00
|
|
|
/* This is safe. */
|
|
|
|
strcpy(pidfile, filename);
|
|
|
|
|
2000-11-07 23:49:42 +00:00
|
|
|
fd = safe_open(filename, ISC_FALSE);
|
2001-08-31 05:57:58 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
2000-01-29 01:38:09 +00:00
|
|
|
ns_main_earlyfatal("couldn't open pid file '%s': %s",
|
2001-08-31 05:57:58 +00:00
|
|
|
filename, strbuf);
|
|
|
|
}
|
2000-01-29 01:38:09 +00:00
|
|
|
lockfile = fdopen(fd, "w");
|
2001-08-31 05:57:58 +00:00
|
|
|
if (lockfile == NULL) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
2000-01-29 01:38:09 +00:00
|
|
|
ns_main_earlyfatal("could not fdopen() pid file '%s': %s",
|
2001-08-31 05:57:58 +00:00
|
|
|
filename, strbuf);
|
|
|
|
}
|
2000-01-29 01:38:09 +00:00
|
|
|
#ifdef HAVE_LINUXTHREADS
|
|
|
|
pid = mainpid;
|
|
|
|
#else
|
|
|
|
pid = getpid();
|
|
|
|
#endif
|
|
|
|
if (fprintf(lockfile, "%ld\n", (long)pid) < 0)
|
|
|
|
ns_main_earlyfatal("fprintf() to pid file '%s' failed",
|
|
|
|
filename);
|
|
|
|
if (fflush(lockfile) == EOF)
|
|
|
|
ns_main_earlyfatal("fflush() to pid file '%s' failed",
|
|
|
|
filename);
|
|
|
|
(void)fclose(lockfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_os_shutdown(void) {
|
|
|
|
closelog();
|
|
|
|
cleanup_pidfile();
|
|
|
|
}
|
2001-10-08 07:46:11 +00:00
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
ns_os_gethostname(char *buf, size_t len) {
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = gethostname(buf, len);
|
|
|
|
return ((n == 0) ? ISC_R_SUCCESS : ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
|