Index: head/lib/libcasper/libcasper/libcasper.3 =================================================================== --- head/lib/libcasper/libcasper/libcasper.3 (revision 329911) +++ head/lib/libcasper/libcasper/libcasper.3 (revision 329912) @@ -1,316 +1,325 @@ .\" 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 February 17, 2018 +.Dd February 24, 2018 .Dt LIBCASPER 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 libcasper .Sh SYNOPSIS .Fd #define WITH_CASPER .In sys/nv.h .In libcasper.h .Ft "cap_channel_t *" .Fn cap_init "void" .Ft "cap_channel_t *" .Fn cap_wrap "int sock" "int flags" .Ft "int" .Fn cap_unwrap "cap_channel_t *chan" "int *flags" .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" .Ft "cap_channel_t *" .Fn cap_service_open "const cap_channel_t *chan" "const char *name" .Sh DESCRIPTION The .Nm libcasper library allows to manage application capabilities through the casper process. .Pp The application capability (represented by the .Vt cap_channel_t type) is a communication channel between the caller and the casper process daemon or an instance of one of its services. A capability to the casper process 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 casper process. .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. The .Fa flags argument defines the channel behavior. The supported flags are: .Bl -ohang -offset indent .It CASPER_NO_UNIQ The communication between process and casper uses no unique version of nvlist. .El .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 limits are provided as +.Xr nvlist 9 . 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. +function sends the given +.Xr nvlist 9 +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. +function receives the given +.Xr nvlist 9 +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. +function sends the given +.Xr nvlist 9, +destroys it and receives new +.Xr nvlist 9 +in response over the given capability. +It does not matter if the function succeeds or fails, the +.Xr nvlist 9 +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. Casper supports the following services in the base system: .Bl -tag -width "system.random" -compact -offset indent .Pp .It system.dns provides DNS libc compatible API .It system.grp provides .Xr getgrent 3 compatible API .It system.pwd provides .Xr getpwent 3 compatible API .It system.random allows to obtain entropy from .Pa /dev/random .It system.sysctl provides .Xr sysctlbyname 3 compatible API .It system.syslog provides .Xr syslog 3 compatible API .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 casper 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. */ capcas = cap_init(); if (capcas == NULL) err(1, "Unable to contact Casper"); /* 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(1, "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 .Sh AUTHORS The .Nm libcasper library was implemented by .An Pawel Jakub Dawidek Aq Mt pawel@dawidek.net under sponsorship from the FreeBSD Foundation. The .Nm libcasper new architecture was implemented by .An Mariusz Zaborski Aq Mt oshogbo@FreeBSD.org . Index: head/lib/libcasper/services/cap_dns/cap_dns.3 =================================================================== --- head/lib/libcasper/services/cap_dns/cap_dns.3 (revision 329911) +++ head/lib/libcasper/services/cap_dns/cap_dns.3 (revision 329912) @@ -1,205 +1,207 @@ .\" Copyright (c) 2018 Mariusz Zaborski .\" All rights reserved. .\" .\" 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 January 10, 2018 +.Dd February 24, 2018 .Dt CAP_DNS 3 .Os .Sh NAME .Nm cap_gethostbyname , .Nm cap_gethostbyname2 , .Nm cap_gethostbyaddr , .Nm cap_getnameinfo , .Nm cap_dns_type_limit , .Nm cap_dns_family_limit .Nd "library for getting network host entry in capability mode" .Sh LIBRARY .Lb libcap_dns .Sh SYNOPSIS .In sys/nv.h .In libcasper.h .In casper/cap_dns.h .Ft "struct hostent *" .Fn cap_gethostbyname "const cap_channel_t *chan" "const char *name" .Ft "struct hostent *" .Fn cap_gethostbyname2 "const cap_channel_t *chan" "const char *name" "int af" .Ft "struct hostent *" .Fn cap_gethostbyaddr "const cap_channel_t *chan" "const void *addr" "socklen_t len" "int af" .Ft "int" .Fn cap_getnameinfo "const cap_channel_t *chan" "const void *name" "int namelen" .Ft "int" .Fn cap_dns_type_limit "cap_channel_t *chan" "const char * const *types" "size_t ntypes" .Ft "int" .Fn cap_dns_family_limit "const cap_channel_t *chan" "const int *families" "size_t nfamilies" .Sh DESCRIPTION The functions .Fn cap_gethostbyname , .Fn cap_gethostbyname2 , .Fn cep_gethostbyaddr and .Fn cap_getnameinfo are respectively equivalent to .Xr gethostbyname 2 , .Xr gethostbyname2 2 , .Xr gethostbyaddr 2 and .Xr getnameinfo 2 except that the connection to the .Nm system.dns service needs to be provided. .Pp The .Fn cap_dns_type_limit function limits the functions allowed in the service. The .Fa types variable can be set to .Dv ADDR or .Dv NAME . See the .Sx LIMITS section for more details. The .Fa ntpyes variable contains the number of .Fa types provided. .Pp The .Fn cap_dns_family_limit functions allows to limit address families. For details see .Sx LIMITS . The .Fa nfamilies variable contains the number of .Fa families provided. .Sh LIMITS The preferred way of setting limits is to use the .Fn cap_dns_type_limit and .Fn cap_dns_family_limit functions, but the limits of service can be set also using .Xr cap_limit_set 3 . -The nvlist for that function can contain the following values and types: +The +.Xr nvlist 9 +for that function can contain the following values and types: .Bl -ohang -offset indent .It type ( NV_TYPE_STRING ) The .Va type can have two values: .Dv ADDR or .Dv NAME . The .Dv ADDR means that functions .Fn cap_gethostbyname , .Fn cap_gethostbyname2 and .Fn cap_gethostbyaddr are allowed. In case when .Va type is set to .Dv NAME the .Fn cap_getnameinfo function is allowed. .It family ( NV_TYPE_NUMBER ) The .Va family limits service to one of the address families (e.g. .Dv AF_INET , AF_INET6 , etc.). .Sh EXAMPLES The following example first opens a capability to casper and then uses this capability to create the .Nm system.dns casper service and uses it to resolve an IP address. .Bd -literal cap_channel_t *capcas, *capdns; const char *typelimit = "ADDR"; int familylimit; const char *ipstr = "127.0.0.1"; struct in_addr ip; struct hostent *hp; /* Open capability to Casper. */ capcas = cap_init(); if (capcas == NULL) err(1, "Unable to contact Casper"); /* 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. */ if (cap_dns_type_limit(capdns, &typelimit, 1) < 0) err(1, "Unable to limit access to the system.dns service"); /* Limit system.dns to reserve IPv4 addresses */ familylimit = AF_INET; if (cap_dns_family_limit(capdns, &familylimit, 1) < 0) err(1, "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 err 3 , .Xr gethostbyaddr 3 , .Xr gethostbyname 3 , .Xr gethostbyname2 3 , .Xr getnameinfo 3, .Xr nv 3 , .Xr capsicum 4 .Sh AUTHORS The .Nm cap_dns service was implemented by .An Pawel Jakub Dawidek Aq Mt pawel@dawidek.net under sponsorship from the FreeBSD Foundation. .Pp This manual page was written by .An Mariusz Zaborski Aq Mt oshogbo@FreeBSD.org .