1 .\" Copyright (c) 2018 Mariusz Zaborski <oshogbo@FreeBSD.org>
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .Nm cap_gethostbyname ,
32 .Nm cap_gethostbyname2 ,
33 .Nm cap_gethostbyaddr ,
35 .Nm cap_dns_type_limit ,
36 .Nm cap_dns_family_limit
37 .Nd "library for getting network host entry in capability mode"
44 .Ft "struct hostent *"
45 .Fn cap_gethostbyname "const cap_channel_t *chan" "const char *name"
46 .Ft "struct hostent *"
47 .Fn cap_gethostbyname2 "const cap_channel_t *chan" "const char *name" "int af"
48 .Ft "struct hostent *"
49 .Fn cap_gethostbyaddr "const cap_channel_t *chan" "const void *addr" "socklen_t len" "int af"
51 .Fn cap_getnameinfo "const cap_channel_t *chan" "const void *name" "int namelen"
53 .Fn cap_dns_type_limit "cap_channel_t *chan" "const char * const *types" "size_t ntypes"
55 .Fn cap_dns_family_limit "const cap_channel_t *chan" "const int *families" "size_t nfamilies"
58 .Fn cap_gethostbyname ,
59 .Fn cap_gethostbyname2 ,
63 are respectively equivalent to
65 .Xr gethostbyname2 2 ,
69 except that the connection to the
71 service needs to be provided.
74 .Fn cap_dns_type_limit
75 function limits the functions allowed in the service.
78 variable can be set to
84 section for more details.
87 variable contains the number of
92 .Fn cap_dns_family_limit
93 functions allows to limit address families.
98 variable contains the number of
102 The preferred way of setting limits is to use the
103 .Fn cap_dns_type_limit
105 .Fn cap_dns_family_limit
106 functions, but the limits of service can be set also using
107 .Xr cap_limit_set 3 .
110 for that function can contain the following values and types:
111 .Bl -ohang -offset indent
112 .It type ( NV_TYPE_STRING )
121 means that reverse DNS lookups are allowed with
124 .Fn cap_gethostbyaddr
130 the name resolution is allowed with
131 .Fn cap_getaddrinfo ,
132 .Fn cap_gethostbyname ,
134 .Fn cap_gethostbyname2
136 .It family ( NV_TYPE_NUMBER )
139 limits service to one of the address families (e.g.
140 .Dv AF_INET , AF_INET6 ,
143 The following example first opens a capability to casper and then uses this
144 capability to create the
146 casper service and uses it to resolve an IP address.
148 cap_channel_t *capcas, *capdns;
149 const char *typelimit = "ADDR";
151 const char *ipstr = "127.0.0.1";
155 /* Open capability to Casper. */
158 err(1, "Unable to contact Casper");
160 /* Enter capability mode sandbox. */
161 if (cap_enter() < 0 && errno != ENOSYS)
162 err(1, "Unable to enter capability mode");
164 /* Use Casper capability to create capability to the system.dns service. */
165 capdns = cap_service_open(capcas, "system.dns");
167 err(1, "Unable to open system.dns service");
169 /* Close Casper capability, we don't need it anymore. */
172 /* Limit system.dns to reverse DNS lookups. */
173 if (cap_dns_type_limit(capdns, &typelimit, 1) < 0)
174 err(1, "Unable to limit access to the system.dns service");
176 /* Limit system.dns to reserve IPv4 addresses */
177 familylimit = AF_INET;
178 if (cap_dns_family_limit(capdns, &familylimit, 1) < 0)
179 err(1, "Unable to limit access to the system.dns service");
181 /* Convert IP address in C-string to in_addr. */
182 if (!inet_aton(ipstr, &ip))
183 errx(1, "Unable to parse IP address %s.", ipstr);
185 /* Find hostname for the given IP address. */
186 hp = cap_gethostbyaddr(capdns, (const void *)&ip, sizeof(ip), AF_INET);
188 errx(1, "No name associated with %s.", ipstr);
190 printf("Name associated with %s is %s.\\n", ipstr, hp->h_name);
195 .Xr gethostbyaddr 3 ,
196 .Xr gethostbyname 3 ,
197 .Xr gethostbyname2 3 ,
204 service was implemented by
205 .An Pawel Jakub Dawidek Aq Mt pawel@dawidek.net
206 under sponsorship from the FreeBSD Foundation.
208 This manual page was written by
209 .An Mariusz Zaborski Aq Mt oshogbo@FreeBSD.org .