svn commit: r258838 - in head: . contrib/mdocml etc etc/casper etc/mtree lib lib/libcapsicum libexec libexec/casper libexec/casper/dns libexec/casper/grp libexec/casper/pwd libexec/casper/random li...

Pawel Jakub Dawidek pjd at FreeBSD.org
Mon Dec 2 08:21:32 UTC 2013


Author: pjd
Date: Mon Dec  2 08:21:28 2013
New Revision: 258838
URL: http://svnweb.freebsd.org/changeset/base/258838

Log:
  Please welcome casperd daemon. It (and its services) will be responsible for
  giving access to functionality that is not available in capability mode
  sandbox. The functionality can be precisely restricted.
  
  Start with the following services:
  - system.dns - provides API compatible to:
  	- gethostbyname(3),
  	- gethostbyname2(3),
  	- gethostbyaddr(3),
  	- getaddrinfo(3),
  	- getnameinfo(3),
  - system.grp - provides getgrent(3)-compatible API,
  - system.pwd - provides getpwent(3)-compatible API,
  - system.random - allows to obtain entropy from /dev/random,
  - system.sysctl - provides sysctlbyname(3-compatible API.
  
  Sponsored by:	The FreeBSD Foundation

Added:
  head/etc/casper/
  head/etc/casper/Makefile   (contents, props changed)
  head/etc/casper/system.dns   (contents, props changed)
  head/etc/casper/system.grp   (contents, props changed)
  head/etc/casper/system.pwd   (contents, props changed)
  head/etc/casper/system.random   (contents, props changed)
  head/etc/casper/system.sysctl   (contents, props changed)
  head/lib/libcapsicum/
  head/lib/libcapsicum/Makefile   (contents, props changed)
  head/lib/libcapsicum/libcapsicum.3   (contents, props changed)
  head/lib/libcapsicum/libcapsicum.c   (contents, props changed)
  head/lib/libcapsicum/libcapsicum.h   (contents, props changed)
  head/lib/libcapsicum/libcapsicum_dns.c   (contents, props changed)
  head/lib/libcapsicum/libcapsicum_dns.h   (contents, props changed)
  head/lib/libcapsicum/libcapsicum_grp.c   (contents, props changed)
  head/lib/libcapsicum/libcapsicum_grp.h   (contents, props changed)
  head/lib/libcapsicum/libcapsicum_impl.h   (contents, props changed)
  head/lib/libcapsicum/libcapsicum_pwd.c   (contents, props changed)
  head/lib/libcapsicum/libcapsicum_pwd.h   (contents, props changed)
  head/lib/libcapsicum/libcapsicum_random.c   (contents, props changed)
  head/lib/libcapsicum/libcapsicum_random.h   (contents, props changed)
  head/lib/libcapsicum/libcapsicum_service.c   (contents, props changed)
  head/lib/libcapsicum/libcapsicum_service.h   (contents, props changed)
  head/lib/libcapsicum/libcapsicum_sysctl.c   (contents, props changed)
  head/lib/libcapsicum/libcapsicum_sysctl.h   (contents, props changed)
  head/libexec/casper/
  head/libexec/casper/Makefile   (contents, props changed)
  head/libexec/casper/dns/
  head/libexec/casper/dns/Makefile   (contents, props changed)
  head/libexec/casper/dns/dns.c   (contents, props changed)
  head/libexec/casper/grp/
  head/libexec/casper/grp/Makefile   (contents, props changed)
  head/libexec/casper/grp/grp.c   (contents, props changed)
  head/libexec/casper/pwd/
  head/libexec/casper/pwd/Makefile   (contents, props changed)
  head/libexec/casper/pwd/pwd.c   (contents, props changed)
  head/libexec/casper/random/
  head/libexec/casper/random/Makefile   (contents, props changed)
  head/libexec/casper/random/random.c   (contents, props changed)
  head/libexec/casper/sysctl/
  head/libexec/casper/sysctl/Makefile   (contents, props changed)
  head/libexec/casper/sysctl/sysctl.c   (contents, props changed)
  head/sbin/casperd/
  head/sbin/casperd/Makefile   (contents, props changed)
  head/sbin/casperd/casperd.8   (contents, props changed)
  head/sbin/casperd/casperd.c   (contents, props changed)
  head/sbin/casperd/zygote.c   (contents, props changed)
  head/sbin/casperd/zygote.h   (contents, props changed)
  head/tools/build/options/WITHOUT_CASPER   (contents, props changed)
Modified:
  head/Makefile.inc1
  head/contrib/mdocml/lib.in
  head/etc/Makefile
  head/etc/mtree/BSD.root.dist
  head/lib/Makefile
  head/libexec/Makefile
  head/sbin/Makefile
  head/share/mk/bsd.libnames.mk
  head/share/mk/bsd.own.mk

Modified: head/Makefile.inc1
==============================================================================
--- head/Makefile.inc1	Mon Dec  2 06:17:29 2013	(r258837)
+++ head/Makefile.inc1	Mon Dec  2 08:21:28 2013	(r258838)
@@ -1499,7 +1499,8 @@ _prebuild_libs=	${_kerberos5_lib_libasn1
 		lib/libbz2 ${_libcom_err} lib/libcrypt \
 		lib/libelf lib/libexpat \
 		${_lib_libgssapi} ${_lib_libipx} \
-		lib/libkiconv lib/libkvm lib/liblzma lib/libmd \
+		lib/libkiconv lib/libkvm lib/liblzma lib/libmd lib/libnv \
+		${_lib_libcapsicum} \
 		lib/ncurses/ncurses lib/ncurses/ncursesw \
 		lib/libopie lib/libpam ${_lib_libthr} \
 		lib/libradius lib/libsbuf lib/libtacplus \
@@ -1521,6 +1522,11 @@ _lib_libthr=	lib/libthr
 _ofed_lib=	contrib/ofed/usr.lib/
 .endif
 
+.if ${MK_CASPER} != "no"
+_lib_libcapsicum=lib/libcapsicum
+.endif
+
+lib/libcapsicum__L: lib/libnv__L
 lib/libpjdlog__L: lib/libutil__L
 
 _generic_libs=	${_cddl_lib} gnu/lib ${_kerberos5_lib} lib ${_secure_lib} usr.bin/lex/lib ${_ofed_lib}

Modified: head/contrib/mdocml/lib.in
==============================================================================
--- head/contrib/mdocml/lib.in	Mon Dec  2 06:17:29 2013	(r258837)
+++ head/contrib/mdocml/lib.in	Mon Dec  2 08:21:28 2013	(r258838)
@@ -32,6 +32,7 @@ LINE("libc",		"Standard C\\~Library (lib
 LINE("libc_r",		"Reentrant C\\~Library (libc_r, \\-lc_r)")
 LINE("libcalendar",	"Calendar Arithmetic Library (libcalendar, \\-lcalendar)")
 LINE("libcam",		"Common Access Method User Library (libcam, \\-lcam)")
+LINE("libcapsicum",	"Capsicum Library (libcapsicum, \\-lcapsicum)")
 LINE("libcdk",		"Curses Development Kit Library (libcdk, \\-lcdk)")
 LINE("libcipher",	"FreeSec Crypt Library (libcipher, \\-lcipher)")
 LINE("libcompat",	"Compatibility Library (libcompat, \\-lcompat)")

Modified: head/etc/Makefile
==============================================================================
--- head/etc/Makefile	Mon Dec  2 06:17:29 2013	(r258837)
+++ head/etc/Makefile	Mon Dec  2 08:21:28 2013	(r258838)
@@ -221,6 +221,9 @@ distribution:
 .if ${MK_BLUETOOTH} != "no"
 	${_+_}cd ${.CURDIR}/bluetooth; ${MAKE} install
 .endif
+.if ${MK_CASPER} != "no"
+	${_+_}cd ${.CURDIR}/casper; ${MAKE} install
+.endif
 	${_+_}cd ${.CURDIR}/defaults; ${MAKE} install
 	${_+_}cd ${.CURDIR}/devd; ${MAKE} install
 	${_+_}cd ${.CURDIR}/gss; ${MAKE} install

Added: head/etc/casper/Makefile
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/etc/casper/Makefile	Mon Dec  2 08:21:28 2013	(r258838)
@@ -0,0 +1,12 @@
+# $FreeBSD$
+
+FILES=	system.dns
+FILES+=	system.grp
+FILES+=	system.pwd
+FILES+=	system.random
+FILES+=	system.sysctl
+
+NO_OBJ=
+FILESDIR= /etc/casper
+
+.include <bsd.prog.mk>

Added: head/etc/casper/system.dns
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/etc/casper/system.dns	Mon Dec  2 08:21:28 2013	(r258838)
@@ -0,0 +1 @@
+/libexec/casper/dns

Added: head/etc/casper/system.grp
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/etc/casper/system.grp	Mon Dec  2 08:21:28 2013	(r258838)
@@ -0,0 +1 @@
+/libexec/casper/grp

Added: head/etc/casper/system.pwd
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/etc/casper/system.pwd	Mon Dec  2 08:21:28 2013	(r258838)
@@ -0,0 +1 @@
+/libexec/casper/pwd

Added: head/etc/casper/system.random
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/etc/casper/system.random	Mon Dec  2 08:21:28 2013	(r258838)
@@ -0,0 +1 @@
+/libexec/casper/random

Added: head/etc/casper/system.sysctl
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/etc/casper/system.sysctl	Mon Dec  2 08:21:28 2013	(r258838)
@@ -0,0 +1 @@
+/libexec/casper/sysctl

Modified: head/etc/mtree/BSD.root.dist
==============================================================================
--- head/etc/mtree/BSD.root.dist	Mon Dec  2 06:17:29 2013	(r258837)
+++ head/etc/mtree/BSD.root.dist	Mon Dec  2 08:21:28 2013	(r258838)
@@ -26,6 +26,8 @@
         ..
         bluetooth
         ..
+        casper
+        ..
         defaults
         ..
         devd
@@ -74,6 +76,8 @@
         ..
     ..
     libexec
+        casper
+        ..
         resolvconf
         ..
     ..

Modified: head/lib/Makefile
==============================================================================
--- head/lib/Makefile	Mon Dec  2 06:17:29 2013	(r258837)
+++ head/lib/Makefile	Mon Dec  2 08:21:28 2013	(r258838)
@@ -37,6 +37,8 @@ SUBDIR_ORDERED=	${_csu} \
 	libauditd \
 	libutil \
 	libpjdlog \
+	libnv \
+	${_libcapsicum} \
 	libcompiler_rt \
 	libcrypt \
 	libelf \
@@ -70,6 +72,7 @@ SUBDIR=	${SUBDIR_ORDERED} \
 	libbz2 \
 	libcalendar \
 	libcam \
+	${_libcasper} \
 	libcompat \
 	libdevinfo \
 	libdevstat \
@@ -157,6 +160,11 @@ _libsdp=	libsdp
 _libbsnmp=	libbsnmp
 .endif
 
+.if ${MK_CASPER} != "no"
+_libcapsicum=	libcapsicum
+_libcasper=	libcasper
+.endif
+
 .if ${MK_CLANG} != "no" && !defined(COMPAT_32BIT)
 _clang=		clang
 .endif

Added: head/lib/libcapsicum/Makefile
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/lib/libcapsicum/Makefile	Mon Dec  2 08:21:28 2013	(r258838)
@@ -0,0 +1,46 @@
+# $FreeBSD$
+
+LIB=	capsicum
+
+SHLIB_MAJOR=	0
+
+SRCS=	libcapsicum.c
+SRCS+=	libcapsicum_dns.c
+SRCS+=	libcapsicum_grp.c
+SRCS+=	libcapsicum_pwd.c
+SRCS+=	libcapsicum_random.c
+SRCS+=	libcapsicum_service.c
+SRCS+=	libcapsicum_sysctl.c
+
+INCS=	libcapsicum.h
+INCS+=	libcapsicum_dns.h
+INCS+=	libcapsicum_grp.h
+INCS+=	libcapsicum_pwd.h
+INCS+=	libcapsicum_random.h
+INCS+=	libcapsicum_service.h
+INCS+=	libcapsicum_sysctl.h
+
+DPADD=	${LIBNV}
+LDADD=	-lnv
+
+CFLAGS+=-I${.CURDIR}
+CFLAGS+=-I${.CURDIR}/../libnv
+
+WARNS?=	6
+
+MAN+=	libcapsicum.3
+
+MLINKS+=libcapsicum.3 cap_init.3
+MLINKS+=libcapsicum.3 cap_wrap.3
+MLINKS+=libcapsicum.3 cap_unwrap.3
+MLINKS+=libcapsicum.3 cap_sock.3
+MLINKS+=libcapsicum.3 cap_clone.3
+MLINKS+=libcapsicum.3 cap_close.3
+MLINKS+=libcapsicum.3 cap_limit_get.3
+MLINKS+=libcapsicum.3 cap_limit_set.3
+MLINKS+=libcapsicum.3 cap_send_nvlist.3
+MLINKS+=libcapsicum.3 cap_recv_nvlist.3
+MLINKS+=libcapsicum.3 cap_xfer_nvlist.3
+MLINKS+=libcapsicum.3 cap_service_open.3
+
+.include <bsd.lib.mk>

Added: head/lib/libcapsicum/libcapsicum.3
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/lib/libcapsicum/libcapsicum.3	Mon Dec  2 08:21:28 2013	(r258838)
@@ -0,0 +1,288 @@
+.\" Copyright (c) 2013 The FreeBSD Foundation
+.\" All rights reserved.
+.\"
+.\" This documentation was written by Pawel Jakub Dawidek under sponsorship
+.\" from the FreeBSD Foundation.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd October 26, 2013
+.Dt LIBCAPSICUM 3
+.Os
+.Sh NAME
+.Nm cap_init ,
+.Nm cap_wrap ,
+.Nm cap_unwrap ,
+.Nm cap_sock ,
+.Nm cap_clone ,
+.Nm cap_close ,
+.Nm cap_limit_get ,
+.Nm cap_limit_set ,
+.Nm cap_send_nvlist ,
+.Nm cap_recv_nvlist ,
+.Nm cap_xfer_nvlist ,
+.Nm cap_service_open
+.Nd "library for handling application capabilities"
+.Sh LIBRARY
+.Lb libcapsicum
+.Sh SYNOPSIS
+.In libcapsicum.h
+.In nv.h
+.Ft "cap_channel_t *"
+.Fn cap_init "void"
+.Ft "cap_channel_t *"
+.Fn cap_wrap "int sock"
+.Ft "int"
+.Fn cap_unwrap "cap_channel_t *chan"
+.Ft "int"
+.Fn cap_sock "const cap_channel_t *chan"
+.Ft "cap_channel_t *"
+.Fn cap_clone "const cap_channel_t *chan"
+.Ft "void"
+.Fn cap_close "cap_channel_t *chan"
+.Ft "int"
+.Fn cap_limit_get "const cap_channel_t *chan" "nvlist_t **limitsp"
+.Ft "int"
+.Fn cap_limit_set "const cap_channel_t *chan" "nvlist_t *limits"
+.Ft "int"
+.Fn cap_send_nvlist "const cap_channel_t *chan" "const nvlist_t *nvl"
+.Ft "nvlist_t *"
+.Fn cap_recv_nvlist "const cap_channel_t *chan"
+.Ft "nvlist_t *"
+.Fn cap_xfer_nvlist "const cap_channel_t *chan" "nvlist_t *nvl"
+.In libcapsicum_service.h
+.Ft "cap_channel_t *"
+.Fn cap_service_open "const cap_channel_t *chan" "const char *name"
+.Sh DESCRIPTION
+The
+.Nm libcapsicum
+library allows to manage application capabilities through the
+.Xr casperd 8
+daemon.
+.Pp
+The application capability (represented by the
+.Vt cap_channel_t
+type) is a communication channel between the caller and the
+.Xr casperd 8
+daemon or an instance of one of its services.
+A capability to the
+.Xr casperd 8
+daemon obtained with the
+.Fn cap_init
+function allows to create capabilities to casper's services via the
+.Fn cap_service_open
+function.
+.Pp
+The
+.Fn cap_init
+function opens capability to the
+.Xr casperd 8
+daemon.
+.Pp
+The
+.Fn cap_wrap
+function creates
+.Vt cap_channel_t
+based on the given socket.
+The function is used when capability is inherited through
+.Xr execve 2
+or send over
+.Xr unix 4
+domain socket as a regular file descriptor and has to be represented as
+.Vt cap_channel_t
+again.
+.Pp
+The
+.Fn cap_unwrap
+function is the opposite of the
+.Fn cap_wrap
+function.
+It frees the
+.Vt cap_channel_t
+structure and returns
+.Xr unix 4
+domain socket associated with it.
+.Pp
+The
+.Fn cap_clone
+function clones the given capability.
+.Pp
+The
+.Fn cap_close
+function closes the given capability.
+.Pp
+The
+.Fn cap_sock
+function returns
+.Xr unix 4
+domain socket descriptor associated with the given capability for use with
+system calls like
+.Xr kevent 2 ,
+.Xr poll 2
+and
+.Xr select 2 .
+.Pp
+The
+.Fn cap_limit_get
+function stores current limits of the given capability in the
+.Fa limitsp
+argument.
+If the function return
+.Va 0
+and
+.Dv NULL
+is stored in
+.Fa limitsp
+it means there are no limits set.
+.Pp
+The
+.Fn cap_limit_set
+function sets limits for the given capability.
+The limits are provided as nvlist.
+The exact format depends on the service the capability represents.
+.Pp
+The
+.Fn cap_send_nvlist
+function sends the given nvlist over the given capability.
+This is low level interface to communicate with casper services.
+Most services should provide higher level API.
+.Pp
+The
+.Fn cap_recv_nvlist
+function receives the given nvlist over the given capability.
+.Pp
+The
+.Fn cap_xfer_nvlist
+function sends the given nvlist, destroys it and receives new nvlist in
+response over the given capability.
+It does not matter if the function succeeds or fails, the nvlist given
+for sending will always be destroyed once the function returns.
+.Pp
+The
+.Fn cap_service_open
+function opens casper service of the given name through casper capability
+obtained via the
+.Fn cap_init
+function.
+The function returns capability that provides access to opened service.
+.Sh RETURN VALUES
+The
+.Fn cap_clone ,
+.Fn cap_init ,
+.Fn cap_recv_nvlist ,
+.Fn cap_service_open ,
+.Fn cap_wrap
+and
+.Fn cap_xfer_nvlist
+functions return
+.Dv NULL
+and set the
+.Va errno
+variable on failure.
+.Pp
+The
+.Fn cap_limit_get ,
+.Fn cap_limit_set
+and
+.Fn cap_send_nvlist
+functions return
+.Dv -1
+and set the
+.Va errno
+variable on failure.
+.Pp
+The
+.Fn cap_close ,
+.Fn cap_sock
+and
+.Fn cap_unwrap
+functions always succeed.
+.Sh EXAMPLES
+The following example first opens capability to the
+.Xr casperd 8
+daemon, then using this capability creates new capability to the
+.Nm system.dns
+casper service and uses the latter capability to resolve IP address.
+.Bd -literal
+cap_channel_t *capcas, *capdns;
+nvlist_t *limits;
+const char *ipstr = "127.0.0.1";
+struct in_addr ip;
+struct hostent *hp;
+
+/* Open capability to the Casper daemon. */
+capcas = cap_init();
+if (capcas == NULL)
+	err(1, "Unable to contact Casper daemon");
+
+/* Enter capability mode sandbox. */
+if (cap_enter() < 0 && errno != ENOSYS)
+	err(1, "Unable to enter capability mode");
+
+/* Use Casper capability to create capability to the system.dns service. */
+capdns = cap_service_open(capcas, "system.dns");
+if (capdns == NULL)
+	err(1, "Unable to open system.dns service");
+
+/* Close Casper capability, we don't need it anymore. */
+cap_close(capcas);
+
+/* Limit system.dns to reverse DNS lookups and IPv4 addresses. */
+limits = nvlist_create(0);
+nvlist_add_string(limits, "type", "ADDR");
+nvlist_add_number(limits, "family", (uint64_t)AF_INET);
+if (cap_limit_set(capdns, limits) < 0)
+	err("Unable to limit access to the system.dns service");
+
+/* Convert IP address in C-string to in_addr. */
+if (!inet_aton(ipstr, &ip))
+	errx(1, "Unable to parse IP address %s.", ipstr);
+
+/* Find hostname for the given IP address. */
+hp = cap_gethostbyaddr(capdns, (const void *)&ip, sizeof(ip), AF_INET);
+if (hp == NULL)
+	errx(1, "No name associated with %s.", ipstr);
+
+printf("Name associated with %s is %s.\\n", ipstr, hp->h_name);
+.Ed
+.Sh SEE ALSO
+.Xr cap_enter 2 ,
+.Xr execve 2 ,
+.Xr kevent 2 ,
+.Xr poll 2 ,
+.Xr select 2 ,
+.Xr cap_gethostbyaddr 3 ,
+.Xr err 3 ,
+.Xr gethostbyaddr 3 ,
+.Xr inet_aton 3 ,
+.Xr nv 3 ,
+.Xr capsicum 4 ,
+.Xr unix 4 ,
+.Xr casperd 8
+.Sh AUTHORS
+The
+.Nm libcapsicum
+library was implemented by
+.An Pawel Jakub Dawidek Aq pawel at dawidek.net
+under sponsorship from the FreeBSD Foundation.

Added: head/lib/libcapsicum/libcapsicum.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/lib/libcapsicum/libcapsicum.c	Mon Dec  2 08:21:28 2013	(r258838)
@@ -0,0 +1,266 @@
+/*-
+ * Copyright (c) 2012-2013 The FreeBSD Foundation
+ * All rights reserved.
+ *
+ * This software was developed by Pawel Jakub Dawidek under sponsorship from
+ * the FreeBSD Foundation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "libcapsicum.h"
+#include "libcapsicum_impl.h"
+#include "nv.h"
+
+/*
+ * Structure describing communication channel between two separated processes.
+ */
+#define	CAP_CHANNEL_MAGIC	0xcac8a31
+struct cap_channel {
+	/*
+	 * Magic value helps to ensure that a pointer to the right structure is
+	 * passed to our functions.
+	 */
+	int	cch_magic;
+	/* Socket descriptor for IPC. */
+	int	cch_sock;
+};
+
+bool
+fd_is_valid(int fd)
+{
+
+	return (fcntl(fd, F_GETFL) != -1 || errno != EBADF);
+}
+
+cap_channel_t *
+cap_init(void)
+{
+	cap_channel_t *chan;
+	struct sockaddr_un sun;
+	int serrno, sock;
+
+	bzero(&sun, sizeof(sun));
+	sun.sun_family = AF_UNIX;
+	strlcpy(sun.sun_path, CASPER_SOCKPATH, sizeof(sun.sun_path));
+	sun.sun_len = SUN_LEN(&sun);
+
+	sock = socket(AF_UNIX, SOCK_STREAM, 0);
+	if (sock == -1)
+		return (NULL);
+	if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
+		serrno = errno;
+		close(sock);
+		errno = serrno;
+		return (NULL);
+	}
+	chan = cap_wrap(sock);
+	if (chan == NULL) {
+		serrno = errno;
+		close(sock);
+		errno = serrno;
+		return (NULL);
+	}
+	return (chan);
+}
+
+cap_channel_t *
+cap_wrap(int sock)
+{
+	cap_channel_t *chan;
+
+	if (!fd_is_valid(sock))
+		return (NULL);
+
+	chan = malloc(sizeof(*chan));
+	if (chan != NULL) {
+		chan->cch_sock = sock;
+		chan->cch_magic = CAP_CHANNEL_MAGIC;
+	}
+
+	return (chan);
+}
+
+int
+cap_unwrap(cap_channel_t *chan)
+{
+	int sock;
+
+	assert(chan != NULL);
+	assert(chan->cch_magic == CAP_CHANNEL_MAGIC);
+
+	sock = chan->cch_sock;
+	chan->cch_magic = 0;
+	free(chan);
+
+	return (sock);
+}
+
+cap_channel_t *
+cap_clone(const cap_channel_t *chan)
+{
+	cap_channel_t *newchan;
+	nvlist_t *nvl;
+	int newsock;
+
+	assert(chan != NULL);
+	assert(chan->cch_magic == CAP_CHANNEL_MAGIC);
+
+	nvl = nvlist_create(0);
+	nvlist_add_string(nvl, "cmd", "clone");
+	nvl = cap_xfer_nvlist(chan, nvl);
+	if (nvl == NULL)
+		return (NULL);
+	if (nvlist_get_number(nvl, "error") != 0) {
+		errno = (int)nvlist_get_number(nvl, "error");
+		nvlist_destroy(nvl);
+		return (NULL);
+	}
+	newsock = nvlist_take_descriptor(nvl, "sock");
+	nvlist_destroy(nvl);
+	newchan = cap_wrap(newsock);
+	if (newchan == NULL) {
+		int serrno;
+
+		serrno = errno;
+		close(newsock);
+		errno = serrno;
+	}
+
+	return (newchan);
+}
+
+void
+cap_close(cap_channel_t *chan)
+{
+
+	assert(chan != NULL);
+	assert(chan->cch_magic == CAP_CHANNEL_MAGIC);
+
+	chan->cch_magic = 0;
+	close(chan->cch_sock);
+	free(chan);
+}
+
+int
+cap_sock(const cap_channel_t *chan)
+{
+
+	assert(chan != NULL);
+	assert(chan->cch_magic == CAP_CHANNEL_MAGIC);
+
+	return (chan->cch_sock);
+}
+
+int
+cap_limit_set(const cap_channel_t *chan, nvlist_t *limits)
+{
+	nvlist_t *nvlmsg;
+	int error;
+
+	nvlmsg = nvlist_create(0);
+	nvlist_add_string(nvlmsg, "cmd", "limit_set");
+	nvlist_add_nvlist(nvlmsg, "limits", limits);
+	nvlmsg = cap_xfer_nvlist(chan, nvlmsg);
+	if (nvlmsg == NULL) {
+		nvlist_destroy(limits);
+		return (-1);
+	}
+	error = (int)nvlist_get_number(nvlmsg, "error");
+	nvlist_destroy(nvlmsg);
+	nvlist_destroy(limits);
+	if (error != 0) {
+		errno = error;
+		return (-1);
+	}
+	return (0);
+}
+
+int
+cap_limit_get(const cap_channel_t *chan, nvlist_t **limitsp)
+{
+	nvlist_t *nvlmsg;
+	int error;
+
+	nvlmsg = nvlist_create(0);
+	nvlist_add_string(nvlmsg, "cmd", "limit_get");
+	nvlmsg = cap_xfer_nvlist(chan, nvlmsg);
+	if (nvlmsg == NULL)
+		return (-1);
+	error = (int)nvlist_get_number(nvlmsg, "error");
+	if (error != 0) {
+		nvlist_destroy(nvlmsg);
+		errno = error;
+		return (-1);
+	}
+	if (nvlist_exists_null(nvlmsg, "limits"))
+		*limitsp = NULL;
+	else
+		*limitsp = nvlist_take_nvlist(nvlmsg, "limits");
+	nvlist_destroy(nvlmsg);
+	return (0);
+}
+
+int
+cap_send_nvlist(const cap_channel_t *chan, const nvlist_t *nvl)
+{
+
+	assert(chan != NULL);
+	assert(chan->cch_magic == CAP_CHANNEL_MAGIC);
+
+	return (nvlist_send(chan->cch_sock, nvl));
+}
+
+nvlist_t *
+cap_recv_nvlist(const cap_channel_t *chan)
+{
+
+	assert(chan != NULL);
+	assert(chan->cch_magic == CAP_CHANNEL_MAGIC);
+
+	return (nvlist_recv(chan->cch_sock));
+}
+
+nvlist_t *
+cap_xfer_nvlist(const cap_channel_t *chan, nvlist_t *nvl)
+{
+
+	assert(chan != NULL);
+	assert(chan->cch_magic == CAP_CHANNEL_MAGIC);
+
+	return (nvlist_xfer(chan->cch_sock, nvl));
+}

Added: head/lib/libcapsicum/libcapsicum.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/lib/libcapsicum/libcapsicum.h	Mon Dec  2 08:21:28 2013	(r258838)
@@ -0,0 +1,115 @@
+/*-
+ * Copyright (c) 2012-2013 The FreeBSD Foundation
+ * All rights reserved.
+ *
+ * This software was developed by Pawel Jakub Dawidek under sponsorship from
+ * the FreeBSD Foundation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef	_LIBCAPSICUM_H_
+#define	_LIBCAPSICUM_H_
+
+#ifndef	_NVLIST_T_DECLARED
+#define	_NVLIST_T_DECLARED
+struct nvlist;
+
+typedef struct nvlist nvlist_t;
+#endif
+
+#ifndef	_CAP_CHANNEL_T_DECLARED
+#define	_CAP_CHANNEL_T_DECLARED
+struct cap_channel;
+
+typedef struct cap_channel cap_channel_t;
+#endif
+
+/*
+ * The function opens unrestricted communication channel to Casper.
+ */
+cap_channel_t *cap_init(void);
+
+/*
+ * The function creates cap_channel_t based on the given socket.
+ */
+cap_channel_t *cap_wrap(int sock);
+
+/*
+ * The function returns communication socket and frees cap_channel_t.
+ */
+int	cap_unwrap(cap_channel_t *chan);
+
+/*
+ * The function clones the given capability.
+ */
+cap_channel_t *cap_clone(const cap_channel_t *chan);
+
+/*
+ * The function closes the given capability.
+ */
+void	cap_close(cap_channel_t *chan);
+
+/*
+ * The function returns socket descriptor associated with the given
+ * cap_channel_t for use with select(2)/kqueue(2)/etc.
+ */
+int	cap_sock(const cap_channel_t *chan);
+
+/*
+ * The function limits the given capability.
+ * It always destroys 'limits' on return.
+ */
+int	cap_limit_set(const cap_channel_t *chan, nvlist_t *limits);
+
+/*
+ * The function returns current limits of the given capability.
+ */
+int	cap_limit_get(const cap_channel_t *chan, nvlist_t **limitsp);
+
+#ifdef TODO
+/*
+ * The function registers a service within provided Casper's capability.
+ * It will run with the same privileges the process has at the time of
+ * calling this function.
+ */
+int	cap_service_register(cap_channel_t *chan, const char *name,
+	    cap_func_t *func);
+#endif
+
+/*
+ * Function sends nvlist over the given capability.
+ */
+int	cap_send_nvlist(const cap_channel_t *chan, const nvlist_t *nvl);
+/*
+ * Function receives nvlist over the given capability.
+ */
+nvlist_t *cap_recv_nvlist(const cap_channel_t *chan);
+/*
+ * Function sends the given nvlist, destroys it and receives new nvlist in
+ * response over the given capability.
+ */
+nvlist_t *cap_xfer_nvlist(const cap_channel_t *chan, nvlist_t *nvl);
+
+#endif	/* !_LIBCAPSICUM_H_ */

Added: head/lib/libcapsicum/libcapsicum_dns.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/lib/libcapsicum/libcapsicum_dns.c	Mon Dec  2 08:21:28 2013	(r258838)
@@ -0,0 +1,346 @@
+/*-
+ * Copyright (c) 2012-2013 The FreeBSD Foundation
+ * All rights reserved.
+ *
+ * This software was developed by Pawel Jakub Dawidek under sponsorship from
+ * the FreeBSD Foundation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <netdb.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nv.h>
+
+#include "libcapsicum.h"
+#include "libcapsicum_dns.h"
+
+static struct hostent hent;
+
+static void
+hostent_free(struct hostent *hp)
+{
+	unsigned int ii;
+
+	free(hp->h_name);
+	hp->h_name = NULL;
+	if (hp->h_aliases != NULL) {
+		for (ii = 0; hp->h_aliases[ii] != NULL; ii++)
+			free(hp->h_aliases[ii]);
+		free(hp->h_aliases);
+		hp->h_aliases = NULL;
+	}
+	if (hp->h_addr_list != NULL) {
+		for (ii = 0; hp->h_addr_list[ii] != NULL; ii++)
+			free(hp->h_addr_list[ii]);
+		free(hp->h_addr_list);
+		hp->h_addr_list = NULL;
+	}
+}
+
+static struct hostent *
+hostent_unpack(const nvlist_t *nvl, struct hostent *hp)
+{
+	unsigned int ii, nitems;
+
+	hostent_free(hp);
+
+	hp->h_name = strdup(nvlist_get_string(nvl, "name"));
+	if (hp->h_name == NULL)
+		goto fail;
+	hp->h_addrtype = (int)nvlist_get_number(nvl, "addrtype");
+	hp->h_length = (int)nvlist_get_number(nvl, "length");
+
+	nitems = (unsigned int)nvlist_get_number(nvl, "naliases");
+	hp->h_aliases = calloc(sizeof(hp->h_aliases[0]), nitems + 1);
+	if (hp->h_aliases == NULL)
+		goto fail;
+	for (ii = 0; ii < nitems; ii++) {
+		hp->h_aliases[ii] =
+		    strdup(nvlist_getf_string(nvl, "alias%u", ii));
+		if (hp->h_aliases[ii] == NULL)
+			goto fail;
+	}
+	hp->h_aliases[ii] = NULL;
+
+	nitems = (unsigned int)nvlist_get_number(nvl, "naddrs");
+	hp->h_addr_list = calloc(sizeof(hp->h_addr_list[0]), nitems + 1);
+	if (hp->h_addr_list == NULL)
+		goto fail;
+	for (ii = 0; ii < nitems; ii++) {
+		hp->h_addr_list[ii] = malloc(hp->h_length);
+		if (hp->h_addr_list[ii] == NULL)
+			goto fail;
+		bcopy(nvlist_getf_binary(nvl, NULL, "addr%u", ii),
+		    hp->h_addr_list[ii], hp->h_length);
+	}
+	hp->h_addr_list[ii] = NULL;
+
+	return (hp);
+fail:
+	hostent_free(hp);
+	h_errno = NO_RECOVERY;
+	return (NULL);
+}
+
+struct hostent *
+cap_gethostbyname(cap_channel_t *chan, const char *name)
+{
+
+	return (cap_gethostbyname2(chan, name, AF_INET));
+}
+
+struct hostent *
+cap_gethostbyname2(cap_channel_t *chan, const char *name, int type)
+{
+	struct hostent *hp;
+	nvlist_t *nvl;
+
+	nvl = nvlist_create(0);
+	nvlist_add_string(nvl, "cmd", "gethostbyname");
+	nvlist_add_number(nvl, "family", (uint64_t)type);
+	nvlist_add_string(nvl, "name", name);
+	nvl = cap_xfer_nvlist(chan, nvl);
+	if (nvl == NULL) {
+		h_errno = NO_RECOVERY;
+		return (NULL);
+	}
+	if (nvlist_get_number(nvl, "error") != 0) {
+		h_errno = (int)nvlist_get_number(nvl, "error");
+		nvlist_destroy(nvl);
+		return (NULL);
+	}
+

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***


More information about the svn-src-all mailing list