⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os.c

📁 bind 源码 最新实现 linux/unix/windows平台
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 2004, 2005  Internet Systems Consortium, Inc. ("ISC") * Copyright (C) 1999-2002  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 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. *//* $Id: os.c,v 1.46.2.4.8.22 2005/05/20 01:37:19 marka Exp $ */#include <config.h>#include <stdarg.h>#include <sys/types.h>	/* dev_t FreeBSD 2.1 */#include <sys/stat.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/buffer.h>#include <isc/file.h>#include <isc/print.h>#include <isc/result.h>#include <isc/strerror.h>#include <isc/string.h>#include <named/main.h>#include <named/os.h>#ifdef HAVE_LIBSCF#include <named/ns_smf_globals.h>#endifstatic char *pidfile = NULL;static int devnullfd = -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#undef HAVE_SYS_PRCTL_H#endif/* * Linux defines: *	(T) HAVE_LINUXTHREADS *	(C) 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_LINUXTHREADSstatic pid_t mainpid = 0;#endifstatic struct passwd *runas_pw = NULL;static isc_boolean_t done_setuid = ISC_FALSE;static int dfd[2] = { -1, -1 };#ifdef HAVE_LINUX_CAPABILITY_Hstatic isc_boolean_t non_root = ISC_FALSE;static isc_boolean_t non_root_caps = ISC_FALSE;/* * 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#include <sys/syscall.h>	/* Required for syscall(). */#include <linux/capability.h>	/* Required for _LINUX_CAPABILITY_VERSION. */#ifdef HAVE_SYS_PRCTL_H#include <sys/prctl.h>		/* Required for prctl(). *//* * If the value of PR_SET_KEEPCAPS is not in <sys/prctl.h>, define it * here.  This allows setuid() to work on systems running a new enough * kernel but with /usr/include/linux pointing to "standard" kernel * headers. */#ifndef PR_SET_KEEPCAPS#define PR_SET_KEEPCAPS 8#endif#endif /* HAVE_SYS_PRCTL_H */#ifndef SYS_capset#ifndef __NR_capset#include <asm/unistd.h> /* Slackware 4.0 needs this. */#endif#define SYS_capset __NR_capset#endifstatic voidlinux_setcaps(unsigned int caps) {	struct __user_cap_header_struct caphead;	struct __user_cap_data_struct cap;	char strbuf[ISC_STRERRORSIZE];	if ((getuid() != 0 && !non_root_caps) || non_root)		return;	memset(&caphead, 0, sizeof(caphead));	caphead.version = _LINUX_CAPABILITY_VERSION;	caphead.pid = 0;	memset(&cap, 0, sizeof(cap));	cap.effective = caps;	cap.permitted = caps;	cap.inheritable = 0;	if (syscall(SYS_capset, &caphead, &cap) < 0) {		isc__strerror(errno, strbuf, sizeof(strbuf));		ns_main_earlyfatal("capset failed: %s:"				   " please ensure that the capset kernel"				   " module is loaded.  see insmod(8)",				   strbuf);	}}static voidlinux_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!	 */	caps |= (1 << CAP_NET_BIND_SERVICE);	/*	 * We need chroot() initially too.	 */	caps |= (1 << CAP_SYS_CHROOT);#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.	 */	caps |= (1 << CAP_SETUID);#endif	/*	 * Since we call initgroups, we need this.	 */	caps |= (1 << CAP_SETGID);	/*	 * 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);	/*	 * 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);	linux_setcaps(caps);}static voidlinux_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;	caps |= (1 << CAP_NET_BIND_SERVICE);	/*	 * 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);	linux_setcaps(caps);}#ifdef HAVE_SYS_PRCTL_Hstatic voidlinux_keepcaps(void) {	char strbuf[ISC_STRERRORSIZE];	/*	 * Ask the kernel to allow us to keep our capabilities after we	 * setuid().	 */	if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0) {		if (errno != EINVAL) {			isc__strerror(errno, strbuf, sizeof(strbuf));			ns_main_earlyfatal("prctl() failed: %s", strbuf);		}	} else {		non_root_caps = ISC_TRUE;		if (getuid() != 0)			non_root = ISC_TRUE;	}}#endif#endif	/* HAVE_LINUX_CAPABILITY_H */static voidsetup_syslog(const char *progname) {	int options;	options = LOG_PID;#ifdef LOG_NDELAY	options |= LOG_NDELAY;#endif	openlog(isc_file_basename(progname), options, ISC_FACILITY);}voidns_os_init(const char *progname) {	setup_syslog(progname);#ifdef HAVE_LINUX_CAPABILITY_H	linux_initialprivs();#endif#ifdef HAVE_LINUXTHREADS	mainpid = getpid();#endif#ifdef SIGXFSZ	signal(SIGXFSZ, SIG_IGN);#endif}voidns_os_daemonize(void) {	pid_t pid;	char strbuf[ISC_STRERRORSIZE];	if (pipe(dfd) == -1) {		isc__strerror(errno, strbuf, sizeof(strbuf));		ns_main_earlyfatal("pipe(): %s", strbuf);	}	pid = fork();	if (pid == -1) {		isc__strerror(errno, strbuf, sizeof(strbuf));		ns_main_earlyfatal("fork(): %s", strbuf);	}	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.		 */		(void)close(dfd[1]);		do {			char buf;			n = read(dfd[0], &buf, 1);			if (n == 1)				_exit(0);		} while (n == -1 && errno == EINTR);		_exit(1);	}	(void)close(dfd[0]);	/*	 * We're the child.	 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -