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 ,
34 .Nm cap_dns_type_limit ,
35 .Nm cap_dns_family_limit
36 .Nd "library for getting network host entry in capability mode"
44 .Fn cap_getaddrinfo "cap_channel_t *chan" "const char *hostname" "const char *servname" "const struct addrinfo *hints" "struct addrinfo **res"
46 .Fn cap_getnameinfo "cap_channel_t *chan" "const struct sockaddr *sa" "socklen_t salen" "char *host" "size_t hostlen" "char *serv" "size_t servlen" "int flags"
47 .Ft "struct hostent *"
48 .Fn cap_gethostbyname "const cap_channel_t *chan" "const char *name"
49 .Ft "struct hostent *"
50 .Fn cap_gethostbyname2 "const cap_channel_t *chan" "const char *name" "int af"
51 .Ft "struct hostent *"
52 .Fn cap_gethostbyaddr "const cap_channel_t *chan" "const void *addr" "socklen_t len" "int af"
54 .Fn cap_dns_type_limit "cap_channel_t *chan" "const char * const *types" "size_t ntypes"
56 .Fn cap_dns_family_limit "const cap_channel_t *chan" "const int *families" "size_t nfamilies"
59 This service is obsolete and
61 should be used instead.
66 functions are preferred over the
67 .Fn cap_gethostbyname ,
68 .Fn cap_gethostbyname2 ,
75 .Fn cap_gethostbyname ,
76 .Fn cap_gethostbyname2 ,
80 are respectively equivalent to
82 .Xr gethostbyname2 3 ,
86 except that the connection to the
88 service needs to be provided.
91 .Fn cap_dns_type_limit
92 function limits the functions allowed in the service.
95 variable can be set to
101 section for more details.
104 variable contains the number of
109 .Fn cap_dns_family_limit
110 functions allows to limit address families.
115 variable contains the number of
119 The preferred way of setting limits is to use the
120 .Fn cap_dns_type_limit
122 .Fn cap_dns_family_limit
123 functions, but the limits of service can be set also using
124 .Xr cap_limit_set 3 .
127 for that function can contain the following values and types:
128 .Bl -ohang -offset indent
129 .It type ( NV_TYPE_STRING )
138 means that reverse DNS lookups are allowed with
141 .Fn cap_gethostbyaddr
147 the name resolution is allowed with
148 .Fn cap_getaddrinfo ,
149 .Fn cap_gethostbyname ,
151 .Fn cap_gethostbyname2
153 .It family ( NV_TYPE_NUMBER )
156 limits service to one of the address families (e.g.
157 .Dv AF_INET , AF_INET6 ,
161 The following example first opens a capability to casper and then uses this
162 capability to create the
164 casper service and uses it to resolve an IP address.
166 cap_channel_t *capcas, *capdns;
167 int familylimit, error;
168 const char *ipstr = "127.0.0.1";
169 const char *typelimit = "ADDR2NAME";
170 char hname[NI_MAXHOST];
171 struct addrinfo hints, *res;
173 /* Open capability to Casper. */
176 err(1, "Unable to contact Casper");
178 /* Cache NLA for gai_strerror. */
179 caph_cache_catpages();
181 /* Enter capability mode sandbox. */
182 if (caph_enter() < 0)
183 err(1, "Unable to enter capability mode");
185 /* Use Casper capability to create capability to the system.dns service. */
186 capdns = cap_service_open(capcas, "system.dns");
188 err(1, "Unable to open system.dns service");
190 /* Close Casper capability, we don't need it anymore. */
193 /* Limit system.dns to reserve IPv4 addresses */
194 familylimit = AF_INET;
195 if (cap_dns_family_limit(capdns, &familylimit, 1) < 0)
196 err(1, "Unable to limit access to the system.dns service");
198 /* Convert IP address in C-string to struct sockaddr. */
199 memset(&hints, 0, sizeof(hints));
200 hints.ai_family = familylimit;
201 hints.ai_flags = AI_NUMERICHOST;
202 error = cap_getaddrinfo(capdns, ipstr, NULL, &hints, &res);
204 errx(1, "cap_getaddrinfo(): %s: %s", ipstr, gai_strerror(error));
206 /* Limit system.dns to reverse DNS lookups. */
207 if (cap_dns_type_limit(capdns, &typelimit, 1) < 0)
208 err(1, "Unable to limit access to the system.dns service");
210 /* Find hostname for the given IP address. */
211 error = cap_getnameinfo(capdns, res->ai_addr, res->ai_addrlen, hname, sizeof(hname),
214 errx(1, "cap_getnameinfo(): %s: %s", ipstr, gai_strerror(error));
216 printf("Name associated with %s is %s.\\n", ipstr, hname);
222 .Xr gethostbyaddr 3 ,
223 .Xr gethostbyname 3 ,
224 .Xr gethostbyname2 3 ,
231 service first appeared in
236 service was implemented by
237 .An Pawel Jakub Dawidek Aq Mt pawel@dawidek.net
238 under sponsorship from the FreeBSD Foundation.
240 This manual page was written by
241 .An Mariusz Zaborski Aq Mt oshogbo@FreeBSD.org .