Adam Tkac aa8fd3
diff -up bind-9.5.0b2/configure.in.libcap bind-9.5.0b2/configure.in
Adam Tkac aa8fd3
--- bind-9.5.0b2/configure.in.libcap	2008-02-20 14:23:42.000000000 +0100
Adam Tkac aa8fd3
+++ bind-9.5.0b2/configure.in	2008-02-20 14:47:11.000000000 +0100
Adam Tkac aa8fd3
@@ -1875,7 +1875,8 @@ AC_ARG_ENABLE(linux-caps,
Adam Tkac aa8fd3
 	[  --disable-linux-caps	disable linux capabilities])
Adam Tkac aa8fd3
 case "$enable_linux_caps" in
Adam Tkac aa8fd3
 	yes|'')
Adam Tkac aa8fd3
-		AC_CHECK_HEADERS(linux/capability.h)
Adam Tkac aa8fd3
+		AC_CHECK_HEADERS(sys/capability.h)
Adam Tkac aa8fd3
+		AC_CHECK_LIB(cap, cap_set_proc, LIBS="-lcap $LIBS")
Adam Tkac aa8fd3
 		;;
Adam Tkac aa8fd3
 	no)
Adam Tkac aa8fd3
 		;;
Adam Tkac aa8fd3
diff -up bind-9.5.0b2/bin/named/unix/os.c.libcap bind-9.5.0b2/bin/named/unix/os.c
Adam Tkac aa8fd3
--- bind-9.5.0b2/bin/named/unix/os.c.libcap	2008-02-20 14:24:31.000000000 +0100
Adam Tkac aa8fd3
+++ bind-9.5.0b2/bin/named/unix/os.c	2008-02-20 14:45:25.000000000 +0100
Adam Tkac aa8fd3
@@ -60,16 +60,16 @@ static int devnullfd = -1;
Adam Tkac aa8fd3
 #endif
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 /*
Adam Tkac aa8fd3
- * If there's no <linux/capability.h>, we don't care about <sys/prctl.h>
Adam Tkac aa8fd3
+ * If there's no <sys/capability.h>, we don't care about <sys/prctl.h>
Adam Tkac aa8fd3
  */
Adam Tkac aa8fd3
-#ifndef HAVE_LINUX_CAPABILITY_H
Adam Tkac aa8fd3
+#ifndef HAVE_SYS_CAPABILITY_H
Adam Tkac aa8fd3
 #undef HAVE_SYS_PRCTL_H
Adam Tkac aa8fd3
 #endif
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 /*
Adam Tkac aa8fd3
  * Linux defines:
Adam Tkac aa8fd3
  *	(T) HAVE_LINUXTHREADS
Adam Tkac aa8fd3
- *	(C) HAVE_LINUX_CAPABILITY_H
Adam Tkac aa8fd3
+ *	(C) HAVE_SYS_CAPABILITY_H
Adam Tkac aa8fd3
  *	(P) HAVE_SYS_PRCTL_H
Adam Tkac aa8fd3
  * The possible cases are:
Adam Tkac aa8fd3
  *	none:	setuid() normally
Adam Tkac aa8fd3
@@ -111,7 +111,7 @@ static struct passwd *runas_pw = NULL;
Adam Tkac aa8fd3
 static isc_boolean_t done_setuid = ISC_FALSE;
Adam Tkac aa8fd3
 static int dfd[2] = { -1, -1 };
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
-#ifdef HAVE_LINUX_CAPABILITY_H
Adam Tkac aa8fd3
+#ifdef HAVE_SYS_CAPABILITY_H
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 static isc_boolean_t non_root = ISC_FALSE;
Adam Tkac aa8fd3
 static isc_boolean_t non_root_caps = ISC_FALSE;
Adam Tkac aa8fd3
@@ -125,7 +125,7 @@ static isc_boolean_t non_root_caps = ISC
Adam Tkac aa8fd3
 #define _LINUX_FS_H
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 #include <sys/syscall.h>	/* Required for syscall(). */
Adam Tkac aa8fd3
-#include <linux/capability.h>	/* Required for _LINUX_CAPABILITY_VERSION. */
Adam Tkac aa8fd3
+#include <sys/capability.h>	/* Required for _LINUX_CAPABILITY_VERSION. */
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 #ifdef HAVE_SYS_PRCTL_H
Adam Tkac aa8fd3
 #include <sys/prctl.h>		/* Required for prctl(). */
Adam Tkac aa8fd3
@@ -142,32 +142,16 @@ static isc_boolean_t non_root_caps = ISC
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 #endif /* HAVE_SYS_PRCTL_H */
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
-#ifndef SYS_capset
Adam Tkac aa8fd3
-#ifndef __NR_capset
Adam Tkac aa8fd3
-#include <asm/unistd.h> /* Slackware 4.0 needs this. */
Adam Tkac aa8fd3
-#endif
Adam Tkac aa8fd3
-#define SYS_capset __NR_capset
Adam Tkac aa8fd3
-#endif
Adam Tkac aa8fd3
-
Adam Tkac aa8fd3
 static void
Adam Tkac aa8fd3
-linux_setcaps(unsigned int caps) {
Adam Tkac aa8fd3
-	struct __user_cap_header_struct caphead;
Adam Tkac aa8fd3
-	struct __user_cap_data_struct cap;
Adam Tkac aa8fd3
+linux_setcaps(cap_t caps) {
Adam Tkac aa8fd3
 	char strbuf[ISC_STRERRORSIZE];
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 	if ((getuid() != 0 && !non_root_caps) || non_root)
Adam Tkac aa8fd3
 		return;
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
-	memset(&caphead, 0, sizeof(caphead));
Adam Tkac aa8fd3
-	caphead.version = _LINUX_CAPABILITY_VERSION;
Adam Tkac aa8fd3
-	caphead.pid = 0;
Adam Tkac aa8fd3
-	memset(&cap, 0, sizeof(cap));
Adam Tkac aa8fd3
-	cap.effective = caps;
Adam Tkac aa8fd3
-	cap.permitted = caps;
Adam Tkac aa8fd3
-	cap.inheritable = 0;
Adam Tkac aa8fd3
-	if (syscall(SYS_capset, &caphead, &cap) < 0) {
Adam Tkac aa8fd3
+	if (cap_set_proc(caps) < 0) {
Adam Tkac aa8fd3
 		isc__strerror(errno, strbuf, sizeof(strbuf));
Adam Tkac aa8fd3
-		ns_main_earlyfatal("capset failed: %s:"
Adam Tkac aa8fd3
+		ns_main_earlyfatal("cap_set_proc failed: %s:"
Adam Tkac aa8fd3
 				   " please ensure that the capset kernel"
Adam Tkac aa8fd3
 				   " module is loaded.  see insmod(8)",
Adam Tkac aa8fd3
 				   strbuf);
Adam Tkac aa8fd3
@@ -176,7 +160,9 @@ linux_setcaps(unsigned int caps) {
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 static void
Adam Tkac aa8fd3
 linux_initialprivs(void) {
Adam Tkac aa8fd3
-	unsigned int caps;
Adam Tkac aa8fd3
+	cap_t caps;
Adam Tkac aa8fd3
+	cap_value_t capval;
Adam Tkac aa8fd3
+	char strbuf[ISC_STRERRORSIZE];
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 	/*%
Adam Tkac aa8fd3
 	 * We don't need most privileges, so we drop them right away.
Adam Tkac aa8fd3
@@ -184,17 +170,26 @@ linux_initialprivs(void) {
Adam Tkac aa8fd3
 	 * capabilities to the minimum needed to run the server.
Adam Tkac aa8fd3
 	 */
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
-	caps = 0;
Adam Tkac aa8fd3
+	if ( (caps = cap_init()) == NULL) {
Adam Tkac aa8fd3
+		isc__strerror(errno, strbuf, sizeof (strbuf));
Adam Tkac aa8fd3
+		ns_main_earlyfatal("cap_init failed: %s", strbuf);
Adam Tkac aa8fd3
+	}
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 	/*
Adam Tkac aa8fd3
 	 * We need to be able to bind() to privileged ports, notably port 53!
Adam Tkac aa8fd3
 	 */
Adam Tkac aa8fd3
-	caps |= (1 << CAP_NET_BIND_SERVICE);
Adam Tkac aa8fd3
+
Adam Tkac aa8fd3
+	capval = CAP_NET_BIND_SERVICE;
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_EFFECTIVE, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_PERMITTED, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 	/*
Adam Tkac aa8fd3
 	 * We need chroot() initially too.
Adam Tkac aa8fd3
 	 */
Adam Tkac aa8fd3
-	caps |= (1 << CAP_SYS_CHROOT);
Adam Tkac aa8fd3
+
Adam Tkac aa8fd3
+	capval = CAP_SYS_CHROOT;
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_EFFECTIVE, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_PERMITTED, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 #if defined(HAVE_SYS_PRCTL_H) || !defined(HAVE_LINUXTHREADS)
Adam Tkac aa8fd3
 	/*
Adam Tkac aa8fd3
@@ -203,19 +198,25 @@ linux_initialprivs(void) {
Adam Tkac aa8fd3
 	 * tried) or we're not using threads.  If either of these is
Adam Tkac aa8fd3
 	 * true, we want the setuid capability.
Adam Tkac aa8fd3
 	 */
Adam Tkac aa8fd3
-	caps |= (1 << CAP_SETUID);
Adam Tkac aa8fd3
+	capval = CAP_SETUID;
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_EFFECTIVE, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_PERMITTED, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
 #endif
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 	/*
Adam Tkac aa8fd3
 	 * Since we call initgroups, we need this.
Adam Tkac aa8fd3
 	 */
Adam Tkac aa8fd3
-	caps |= (1 << CAP_SETGID);
Adam Tkac aa8fd3
+	capval = CAP_SETGID;
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_EFFECTIVE, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_PERMITTED, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 	/*
Adam Tkac aa8fd3
 	 * Without this, we run into problems reading a configuration file
Adam Tkac aa8fd3
 	 * owned by a non-root user and non-world-readable on startup.
Adam Tkac aa8fd3
 	 */
Adam Tkac aa8fd3
-	caps |= (1 << CAP_DAC_READ_SEARCH);
Adam Tkac aa8fd3
+	capval = CAP_DAC_READ_SEARCH;
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_EFFECTIVE, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_PERMITTED, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 	/*
Adam Tkac aa8fd3
 	 * XXX  We might want to add CAP_SYS_RESOURCE, though it's not
Adam Tkac aa8fd3
@@ -224,14 +225,18 @@ linux_initialprivs(void) {
Adam Tkac aa8fd3
 	 * of files, the stack size, data size, and core dump size to
Adam Tkac aa8fd3
 	 * support named.conf options, this is now being added to test.
Adam Tkac aa8fd3
 	 */
Adam Tkac aa8fd3
-	caps |= (1 << CAP_SYS_RESOURCE);
Adam Tkac aa8fd3
+	capval = CAP_SYS_RESOURCE;
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_EFFECTIVE, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_PERMITTED, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 	linux_setcaps(caps);
Adam Tkac aa8fd3
 }
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 static void
Adam Tkac aa8fd3
 linux_minprivs(void) {
Adam Tkac aa8fd3
-	unsigned int caps;
Adam Tkac aa8fd3
+	cap_t caps;
Adam Tkac aa8fd3
+	cap_value_t capval;
Adam Tkac aa8fd3
+	char strbuf[ISC_STRERRORSIZE];
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 	/*%
Adam Tkac aa8fd3
 	 * Drop all privileges except the ability to bind() to privileged
Adam Tkac aa8fd3
@@ -241,8 +246,14 @@ linux_minprivs(void) {
Adam Tkac aa8fd3
 	 * chroot() could be used to escape from the chrooted area.
Adam Tkac aa8fd3
 	 */
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
-	caps = 0;
Adam Tkac aa8fd3
-	caps |= (1 << CAP_NET_BIND_SERVICE);
Adam Tkac aa8fd3
+	if ( (caps = cap_init()) == NULL) {
Adam Tkac aa8fd3
+		isc__strerror(errno, strbuf, sizeof (strbuf));
Adam Tkac aa8fd3
+		ns_main_earlyfatal("cap_init failed: %s", strbuf);
Adam Tkac aa8fd3
+	}
Adam Tkac aa8fd3
+
Adam Tkac aa8fd3
+	capval = CAP_NET_BIND_SERVICE;
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_EFFECTIVE, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_PERMITTED, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 	/*
Adam Tkac aa8fd3
 	 * XXX  We might want to add CAP_SYS_RESOURCE, though it's not
Adam Tkac aa8fd3
@@ -251,7 +262,9 @@ linux_minprivs(void) {
Adam Tkac aa8fd3
 	 * of files, the stack size, data size, and core dump size to
Adam Tkac aa8fd3
 	 * support named.conf options, this is now being added to test.
Adam Tkac aa8fd3
 	 */
Adam Tkac aa8fd3
-	caps |= (1 << CAP_SYS_RESOURCE);
Adam Tkac aa8fd3
+	capval = CAP_SYS_RESOURCE;
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_EFFECTIVE, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
+	cap_set_flag(caps, CAP_PERMITTED, 1, &capval, CAP_SET);
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 	linux_setcaps(caps);
Adam Tkac aa8fd3
 }
Adam Tkac aa8fd3
@@ -278,7 +291,7 @@ linux_keepcaps(void) {
Adam Tkac aa8fd3
 }
Adam Tkac aa8fd3
 #endif
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
-#endif	/* HAVE_LINUX_CAPABILITY_H */
Adam Tkac aa8fd3
+#endif	/* HAVE_SYS_CAPABILITY_H */
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 static void
Adam Tkac aa8fd3
@@ -295,7 +308,7 @@ setup_syslog(const char *progname) {
Adam Tkac aa8fd3
 void
Adam Tkac aa8fd3
 ns_os_init(const char *progname) {
Adam Tkac aa8fd3
 	setup_syslog(progname);
Adam Tkac aa8fd3
-#ifdef HAVE_LINUX_CAPABILITY_H
Adam Tkac aa8fd3
+#ifdef HAVE_SYS_CAPABILITY_H
Adam Tkac aa8fd3
 	linux_initialprivs();
Adam Tkac aa8fd3
 #endif
Adam Tkac aa8fd3
 #ifdef HAVE_LINUXTHREADS
Adam Tkac aa8fd3
@@ -474,7 +487,7 @@ ns_os_changeuser(void) {
Adam Tkac aa8fd3
 	done_setuid = ISC_TRUE;
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 #ifdef HAVE_LINUXTHREADS
Adam Tkac aa8fd3
-#ifdef HAVE_LINUX_CAPABILITY_H
Adam Tkac aa8fd3
+#ifdef HAVE_SYS_CAPABILITY_H
Adam Tkac aa8fd3
 	if (!non_root_caps)
Adam Tkac aa8fd3
 		ns_main_earlyfatal("-u with Linux threads not supported: "
Adam Tkac aa8fd3
 				   "requires kernel support for "
Adam Tkac aa8fd3
@@ -507,7 +520,7 @@ ns_os_changeuser(void) {
Adam Tkac aa8fd3
 				     strbuf);
Adam Tkac aa8fd3
 	}
Adam Tkac aa8fd3
 #endif
Adam Tkac aa8fd3
-#if defined(HAVE_LINUX_CAPABILITY_H) && !defined(HAVE_LINUXTHREADS)
Adam Tkac aa8fd3
+#if defined(HAVE_SYS_CAPABILITY_H) && !defined(HAVE_LINUXTHREADS)
Adam Tkac aa8fd3
 	linux_minprivs();
Adam Tkac aa8fd3
 #endif
Adam Tkac aa8fd3
 }
Adam Tkac aa8fd3
@@ -522,7 +535,7 @@ ns_os_minprivs(void) {
Adam Tkac aa8fd3
 	ns_os_changeuser(); /* Call setuid() before threads are started */
Adam Tkac aa8fd3
 #endif
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
-#if defined(HAVE_LINUX_CAPABILITY_H) && defined(HAVE_LINUXTHREADS)
Adam Tkac aa8fd3
+#if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_LINUXTHREADS)
Adam Tkac aa8fd3
 	linux_minprivs();
Adam Tkac aa8fd3
 #endif
Adam Tkac aa8fd3
 }
Adam Tkac aa8fd3
diff -up bind-9.5.0b2/config.h.in.libcap bind-9.5.0b2/config.h.in
Adam Tkac aa8fd3
--- bind-9.5.0b2/config.h.in.libcap	2008-02-20 14:23:57.000000000 +0100
Adam Tkac aa8fd3
+++ bind-9.5.0b2/config.h.in	2008-02-20 14:24:17.000000000 +0100
Adam Tkac aa8fd3
@@ -196,8 +196,8 @@ int sigwait(const unsigned int *set, int
Adam Tkac aa8fd3
 /* Define if libxml2 was found */
Adam Tkac aa8fd3
 #undef HAVE_LIBXML2
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
-/* Define to 1 if you have the <linux/capability.h> header file. */
Adam Tkac aa8fd3
-#undef HAVE_LINUX_CAPABILITY_H
Adam Tkac aa8fd3
+/* Define to 1 if you have the <sys/capability.h> header file. */
Adam Tkac aa8fd3
+#undef HAVE_SYS_CAPABILITY_H
Adam Tkac aa8fd3
 
Adam Tkac aa8fd3
 /* Define to 1 if you have the <locale.h> header file. */
Adam Tkac aa8fd3
 #undef HAVE_LOCALE_H