Adam Tkac bd6af4
diff -up bind-9.5.0b3/bin/named/unix/os.c.libcap bind-9.5.0b3/bin/named/unix/os.c
Adam Tkac bd6af4
--- bind-9.5.0b3/bin/named/unix/os.c.libcap	2008-01-30 05:55:51.000000000 +0100
Adam Tkac e00252
+++ bind-9.5.0b3/bin/named/unix/os.c	2008-05-05 13:30:47.000000000 +0200
Adam Tkac bd6af4
@@ -69,7 +69,7 @@ static int devnullfd = -1;
Adam Tkac bd6af4
 /*
Adam Tkac bd6af4
  * Linux defines:
Adam Tkac bd6af4
  *	(T) HAVE_LINUXTHREADS
Adam Tkac bd6af4
- *	(C) HAVE_LINUX_CAPABILITY_H
Adam Tkac bd6af4
+ *	(C) HAVE_SYS_CAPABILITY_H (or HAVE_LINUX_CAPABILITY_H)
Adam Tkac bd6af4
  *	(P) HAVE_SYS_PRCTL_H
Adam Tkac bd6af4
  * The possible cases are:
Adam Tkac bd6af4
  *	none:	setuid() normally
Adam Tkac bd6af4
@@ -116,16 +116,9 @@ static int dfd[2] = { -1, -1 };
Adam Tkac bd6af4
 static isc_boolean_t non_root = ISC_FALSE;
Adam Tkac bd6af4
 static isc_boolean_t non_root_caps = ISC_FALSE;
Adam Tkac bd6af4
 
Adam Tkac bd6af4
-#if defined(HAVE_CAPSET)
Adam Tkac bd6af4
-#undef _POSIX_SOURCE
Adam Tkac bd6af4
 #ifdef HAVE_SYS_CAPABILITY_H
Adam Tkac bd6af4
 #include <sys/capability.h>
Adam Tkac bd6af4
 #else
Adam Tkac bd6af4
-#include <linux/capability.h>
Adam Tkac bd6af4
-int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
Adam Tkac bd6af4
-#endif
Adam Tkac bd6af4
-#include <sys/prctl.h>
Adam Tkac bd6af4
-#else
Adam Tkac bd6af4
 /*%
Adam Tkac bd6af4
  * We define _LINUX_FS_H to prevent it from being included.  We don't need
Adam Tkac bd6af4
  * anything from it, and the files it includes cause warnings with 2.2
Adam Tkac e00252
@@ -133,9 +126,15 @@ int capset(cap_user_header_t hdrp, const
Adam Tkac bd6af4
  * and <string.h>) on 2.3 kernels.
Adam Tkac bd6af4
  */
Adam Tkac bd6af4
 #define _LINUX_FS_H
Adam Tkac bd6af4
-
Adam Tkac bd6af4
-#include <sys/syscall.h>	/* Required for syscall(). */
Adam Tkac bd6af4
-#include <linux/capability.h>	/* Required for _LINUX_CAPABILITY_VERSION. */
Adam Tkac bd6af4
+#include <linux/capability.h>
Adam Tkac bd6af4
+#include <syscall.h>
Adam Tkac bd6af4
+#ifndef SYS_capset
Adam Tkac bd6af4
+#ifndef __NR_capset
Adam Tkac bd6af4
+#include <asm/unistd.h> /* Slackware 4.0 needs this. */
Adam Tkac bd6af4
+#endif /* __NR_capset */
Adam Tkac bd6af4
+#define SYS_capset __NR_capset
Adam Tkac bd6af4
+#endif /* SYS_capset */
Adam Tkac bd6af4
+#endif /* HAVE_SYS_CAPABILITY_H */
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 #ifdef HAVE_SYS_PRCTL_H
Adam Tkac bd6af4
 #include <sys/prctl.h>		/* Required for prctl(). */
Adam Tkac e00252
@@ -152,23 +151,24 @@ int capset(cap_user_header_t hdrp, const
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 #endif /* HAVE_SYS_PRCTL_H */
Adam Tkac bd6af4
 
Adam Tkac bd6af4
-#ifndef SYS_capset
Adam Tkac bd6af4
-#ifndef __NR_capset
Adam Tkac bd6af4
-#include <asm/unistd.h> /* Slackware 4.0 needs this. */
Adam Tkac bd6af4
-#endif
Adam Tkac bd6af4
-#define SYS_capset __NR_capset
Adam Tkac bd6af4
-#endif
Adam Tkac bd6af4
-#endif
Adam Tkac bd6af4
+#ifdef HAVE_LIBCAP
Adam Tkac bd6af4
+#define SETCAPS_FUNC "cap_set_proc "
Adam Tkac bd6af4
+#else
Adam Tkac bd6af4
+typedef unsigned int cap_t;
Adam Tkac bd6af4
+#define SETCAPS_FUNC "syscall(capset) "
Adam Tkac bd6af4
+#endif /* HAVE_LIBCAP */
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 static void
Adam Tkac bd6af4
-linux_setcaps(unsigned int caps) {
Adam Tkac bd6af4
+linux_setcaps(cap_t caps) {
Adam Tkac bd6af4
+#ifndef HAVE_LIBCAP
Adam Tkac bd6af4
 	struct __user_cap_header_struct caphead;
Adam Tkac bd6af4
 	struct __user_cap_data_struct cap;
Adam Tkac bd6af4
+#endif
Adam Tkac bd6af4
 	char strbuf[ISC_STRERRORSIZE];
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 	if ((getuid() != 0 && !non_root_caps) || non_root)
Adam Tkac bd6af4
 		return;
Adam Tkac bd6af4
-
Adam Tkac bd6af4
+#ifndef HAVE_LIBCAP
Adam Tkac bd6af4
 	memset(&caphead, 0, sizeof(caphead));
Adam Tkac bd6af4
 	caphead.version = _LINUX_CAPABILITY_VERSION;
Adam Tkac bd6af4
 	caphead.pid = 0;
Adam Tkac e00252
@@ -176,46 +176,74 @@ linux_setcaps(unsigned int caps) {
Adam Tkac bd6af4
 	cap.effective = caps;
Adam Tkac bd6af4
 	cap.permitted = caps;
Adam Tkac bd6af4
 	cap.inheritable = 0;
Adam Tkac bd6af4
-#ifdef HAVE_CAPSET
Adam Tkac e00252
-	if (capset(&caphead, &cap) < 0 ) {
Adam Tkac bd6af4
-		isc__strerror(errno, strbuf, sizeof(strbuf));
Adam Tkac bd6af4
-		ns_main_earlyfatal("capset failed: %s:"
Adam Tkac bd6af4
-				   " please ensure that the capset kernel"
Adam Tkac bd6af4
-				   " module is loaded.  see insmod(8)",
Adam Tkac bd6af4
-				   strbuf);
Adam Tkac bd6af4
-	}
Adam Tkac e00252
+#endif
Adam Tkac e00252
+#ifdef HAVE_LIBCAP
Adam Tkac e00252
+	if (cap_set_proc(caps) < 0) {
Adam Tkac bd6af4
 #else
Adam Tkac bd6af4
 	if (syscall(SYS_capset, &caphead, &cap) < 0) {
Adam Tkac bd6af4
+#endif
Adam Tkac bd6af4
 		isc__strerror(errno, strbuf, sizeof(strbuf));
Adam Tkac bd6af4
-		ns_main_earlyfatal("syscall(capset) failed: %s:"
Adam Tkac bd6af4
+		ns_main_earlyfatal(SETCAPS_FUNC "failed: %s:"
Adam Tkac bd6af4
 				   " please ensure that the capset kernel"
Adam Tkac bd6af4
 				   " module is loaded.  see insmod(8)",
Adam Tkac bd6af4
 				   strbuf);
Adam Tkac bd6af4
 	}
Adam Tkac bd6af4
-#endif
Adam Tkac bd6af4
 }
Adam Tkac bd6af4
 
Adam Tkac bd6af4
+#ifdef HAVE_LIBCAP
Adam Tkac bd6af4
+#define SET_CAP(flag) \
Adam Tkac bd6af4
+	do { \
Adam Tkac bd6af4
+		capval = (flag); \
Adam Tkac bd6af4
+		err = cap_set_flag(caps, CAP_EFFECTIVE, 1, &capval, CAP_SET); \
Adam Tkac bd6af4
+		if (err == -1) { \
Adam Tkac bd6af4
+			isc__strerror(errno, strbuf, sizeof(strbuf)); \
Adam Tkac bd6af4
+			ns_main_earlyfatal("cap_set_proc failed: %s", strbuf); \
Adam Tkac bd6af4
+		} \
Adam Tkac bd6af4
+		\
Adam Tkac bd6af4
+		err = cap_set_flag(caps, CAP_PERMITTED, 1, &capval, CAP_SET); \
Adam Tkac bd6af4
+		if (err == -1) { \
Adam Tkac bd6af4
+			isc__strerror(errno, strbuf, sizeof(strbuf)); \
Adam Tkac bd6af4
+			ns_main_earlyfatal("cap_set_proc failed: %s", strbuf); \
Adam Tkac bd6af4
+		} \
Adam Tkac bd6af4
+	} while (0)
Adam Tkac bd6af4
+#define INIT_CAP \
Adam Tkac bd6af4
+	do { \
Adam Tkac bd6af4
+		caps = cap_init(); \
Adam Tkac bd6af4
+		if (caps == NULL) { \
Adam Tkac bd6af4
+			isc__strerror(errno, strbuf, sizeof(strbuf)); \
Adam Tkac bd6af4
+			ns_main_earlyfatal("cap_init failed: %s", strbuf); \
Adam Tkac bd6af4
+		} \
Adam Tkac bd6af4
+	} while (0)
Adam Tkac bd6af4
+#else
Adam Tkac bd6af4
+#define SET_CAP(flag) { caps |= (1 << (flag)); }
Adam Tkac bd6af4
+#define INIT_CAP { caps = 0; }
Adam Tkac bd6af4
+#endif /* HAVE_LIBCAP */
Adam Tkac bd6af4
+
Adam Tkac bd6af4
 static void
Adam Tkac bd6af4
 linux_initialprivs(void) {
Adam Tkac bd6af4
-	unsigned int caps;
Adam Tkac bd6af4
+	cap_t caps;
Adam Tkac bd6af4
+#ifdef HAVE_LIBCAP
Adam Tkac bd6af4
+	cap_value_t capval;
Adam Tkac bd6af4
+	char strbuf[ISC_STRERRORSIZE];
Adam Tkac bd6af4
+	int err;
Adam Tkac bd6af4
+#endif
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 	/*%
Adam Tkac bd6af4
 	 * We don't need most privileges, so we drop them right away.
Adam Tkac bd6af4
 	 * Later on linux_minprivs() will be called, which will drop our
Adam Tkac bd6af4
 	 * capabilities to the minimum needed to run the server.
Adam Tkac bd6af4
 	 */
Adam Tkac bd6af4
-
Adam Tkac bd6af4
-	caps = 0;
Adam Tkac bd6af4
+	INIT_CAP;
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 	/*
Adam Tkac bd6af4
 	 * We need to be able to bind() to privileged ports, notably port 53!
Adam Tkac bd6af4
 	 */
Adam Tkac bd6af4
-	caps |= (1 << CAP_NET_BIND_SERVICE);
Adam Tkac bd6af4
+	SET_CAP(CAP_NET_BIND_SERVICE);
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 	/*
Adam Tkac bd6af4
 	 * We need chroot() initially too.
Adam Tkac bd6af4
 	 */
Adam Tkac bd6af4
-	caps |= (1 << CAP_SYS_CHROOT);
Adam Tkac bd6af4
+	SET_CAP(CAP_SYS_CHROOT);
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 #if defined(HAVE_SYS_PRCTL_H) || !defined(HAVE_LINUXTHREADS)
Adam Tkac bd6af4
 	/*
Adam Tkac e00252
@@ -224,19 +252,19 @@ linux_initialprivs(void) {
Adam Tkac bd6af4
 	 * tried) or we're not using threads.  If either of these is
Adam Tkac bd6af4
 	 * true, we want the setuid capability.
Adam Tkac bd6af4
 	 */
Adam Tkac bd6af4
-	caps |= (1 << CAP_SETUID);
Adam Tkac bd6af4
+	SET_CAP(CAP_SETUID);
Adam Tkac bd6af4
 #endif
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 	/*
Adam Tkac bd6af4
 	 * Since we call initgroups, we need this.
Adam Tkac bd6af4
 	 */
Adam Tkac bd6af4
-	caps |= (1 << CAP_SETGID);
Adam Tkac bd6af4
+	SET_CAP(CAP_SETGID);
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 	/*
Adam Tkac bd6af4
 	 * Without this, we run into problems reading a configuration file
Adam Tkac bd6af4
 	 * owned by a non-root user and non-world-readable on startup.
Adam Tkac bd6af4
 	 */
Adam Tkac bd6af4
-	caps |= (1 << CAP_DAC_READ_SEARCH);
Adam Tkac bd6af4
+	SET_CAP(CAP_DAC_READ_SEARCH);
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 	/*
Adam Tkac bd6af4
 	 * XXX  We might want to add CAP_SYS_RESOURCE, though it's not
Adam Tkac e00252
@@ -245,15 +273,21 @@ linux_initialprivs(void) {
Adam Tkac bd6af4
 	 * of files, the stack size, data size, and core dump size to
Adam Tkac bd6af4
 	 * support named.conf options, this is now being added to test.
Adam Tkac bd6af4
 	 */
Adam Tkac bd6af4
-	caps |= (1 << CAP_SYS_RESOURCE);
Adam Tkac bd6af4
+	SET_CAP(CAP_SYS_RESOURCE);
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 	linux_setcaps(caps);
Adam Tkac bd6af4
 }
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 static void
Adam Tkac bd6af4
 linux_minprivs(void) {
Adam Tkac bd6af4
-	unsigned int caps;
Adam Tkac bd6af4
+	cap_t caps;
Adam Tkac bd6af4
+#ifdef HAVE_LIBCAP
Adam Tkac bd6af4
+	cap_value_t capval;
Adam Tkac bd6af4
+	char strbuf[ISC_STRERRORSIZE];
Adam Tkac bd6af4
+	int err;
Adam Tkac bd6af4
+#endif
Adam Tkac bd6af4
 
Adam Tkac bd6af4
+	INIT_CAP;
Adam Tkac bd6af4
 	/*%
Adam Tkac bd6af4
 	 * Drop all privileges except the ability to bind() to privileged
Adam Tkac bd6af4
 	 * ports.
Adam Tkac e00252
@@ -262,8 +296,7 @@ linux_minprivs(void) {
Adam Tkac bd6af4
 	 * chroot() could be used to escape from the chrooted area.
Adam Tkac bd6af4
 	 */
Adam Tkac bd6af4
 
Adam Tkac bd6af4
-	caps = 0;
Adam Tkac bd6af4
-	caps |= (1 << CAP_NET_BIND_SERVICE);
Adam Tkac bd6af4
+	SET_CAP(CAP_NET_BIND_SERVICE);
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 	/*
Adam Tkac bd6af4
 	 * XXX  We might want to add CAP_SYS_RESOURCE, though it's not
Adam Tkac e00252
@@ -272,7 +305,7 @@ linux_minprivs(void) {
Adam Tkac bd6af4
 	 * of files, the stack size, data size, and core dump size to
Adam Tkac bd6af4
 	 * support named.conf options, this is now being added to test.
Adam Tkac bd6af4
 	 */
Adam Tkac bd6af4
-	caps |= (1 << CAP_SYS_RESOURCE);
Adam Tkac bd6af4
+	SET_CAP(CAP_SYS_RESOURCE);
Adam Tkac bd6af4
 
Adam Tkac bd6af4
 	linux_setcaps(caps);
Adam Tkac bd6af4
 }
Adam Tkac bd6af4
diff -up bind-9.5.0b3/configure.in.libcap bind-9.5.0b3/configure.in
Adam Tkac bd6af4
--- bind-9.5.0b3/configure.in.libcap	2008-01-30 05:55:51.000000000 +0100
Adam Tkac e00252
+++ bind-9.5.0b3/configure.in	2008-05-05 13:28:34.000000000 +0200
Adam Tkac e00252
@@ -1876,7 +1876,7 @@ AC_ARG_ENABLE(linux-caps,
Adam Tkac bd6af4
 case "$enable_linux_caps" in
Adam Tkac bd6af4
 	yes|'')
Adam Tkac bd6af4
 		AC_CHECK_HEADERS(linux/capability.h sys/capability.h)
Adam Tkac e00252
-		AC_CHECK_FUNCS(capset)
Adam Tkac bd6af4
+		AC_CHECK_LIB(cap, cap_set_proc)
Adam Tkac bd6af4
 		;;
Adam Tkac bd6af4
 	no)
Adam Tkac e00252
 		;;