2 * Copyright (c) 2012-2013 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
41 #include "libcapsicum.h"
42 #include "libcapsicum_dns.h"
44 static struct hostent hent;
47 hostent_free(struct hostent *hp)
53 if (hp->h_aliases != NULL) {
54 for (ii = 0; hp->h_aliases[ii] != NULL; ii++)
55 free(hp->h_aliases[ii]);
59 if (hp->h_addr_list != NULL) {
60 for (ii = 0; hp->h_addr_list[ii] != NULL; ii++)
61 free(hp->h_addr_list[ii]);
62 free(hp->h_addr_list);
63 hp->h_addr_list = NULL;
67 static struct hostent *
68 hostent_unpack(const nvlist_t *nvl, struct hostent *hp)
70 unsigned int ii, nitems;
76 hp->h_name = strdup(nvlist_get_string(nvl, "name"));
77 if (hp->h_name == NULL)
79 hp->h_addrtype = (int)nvlist_get_number(nvl, "addrtype");
80 hp->h_length = (int)nvlist_get_number(nvl, "length");
82 nitems = (unsigned int)nvlist_get_number(nvl, "naliases");
83 hp->h_aliases = calloc(sizeof(hp->h_aliases[0]), nitems + 1);
84 if (hp->h_aliases == NULL)
86 for (ii = 0; ii < nitems; ii++) {
87 n = snprintf(nvlname, sizeof(nvlname), "alias%u", ii);
88 assert(n > 0 && n < (int)sizeof(nvlname));
90 strdup(nvlist_get_string(nvl, nvlname));
91 if (hp->h_aliases[ii] == NULL)
94 hp->h_aliases[ii] = NULL;
96 nitems = (unsigned int)nvlist_get_number(nvl, "naddrs");
97 hp->h_addr_list = calloc(sizeof(hp->h_addr_list[0]), nitems + 1);
98 if (hp->h_addr_list == NULL)
100 for (ii = 0; ii < nitems; ii++) {
101 hp->h_addr_list[ii] = malloc(hp->h_length);
102 if (hp->h_addr_list[ii] == NULL)
104 n = snprintf(nvlname, sizeof(nvlname), "addr%u", ii);
105 assert(n > 0 && n < (int)sizeof(nvlname));
106 bcopy(nvlist_get_binary(nvl, nvlname, NULL),
107 hp->h_addr_list[ii], hp->h_length);
109 hp->h_addr_list[ii] = NULL;
114 h_errno = NO_RECOVERY;
119 cap_gethostbyname(cap_channel_t *chan, const char *name)
122 return (cap_gethostbyname2(chan, name, AF_INET));
126 cap_gethostbyname2(cap_channel_t *chan, const char *name, int type)
131 nvl = nvlist_create(0);
132 nvlist_add_string(nvl, "cmd", "gethostbyname");
133 nvlist_add_number(nvl, "family", (uint64_t)type);
134 nvlist_add_string(nvl, "name", name);
135 nvl = cap_xfer_nvlist(chan, nvl, 0);
137 h_errno = NO_RECOVERY;
140 if (nvlist_get_number(nvl, "error") != 0) {
141 h_errno = (int)nvlist_get_number(nvl, "error");
146 hp = hostent_unpack(nvl, &hent);
152 cap_gethostbyaddr(cap_channel_t *chan, const void *addr, socklen_t len,
158 nvl = nvlist_create(0);
159 nvlist_add_string(nvl, "cmd", "gethostbyaddr");
160 nvlist_add_binary(nvl, "addr", addr, (size_t)len);
161 nvlist_add_number(nvl, "family", (uint64_t)type);
162 nvl = cap_xfer_nvlist(chan, nvl, 0);
164 h_errno = NO_RECOVERY;
167 if (nvlist_get_number(nvl, "error") != 0) {
168 h_errno = (int)nvlist_get_number(nvl, "error");
172 hp = hostent_unpack(nvl, &hent);
177 static struct addrinfo *
178 addrinfo_unpack(const nvlist_t *nvl)
183 const char *canonname;
185 addr = nvlist_get_binary(nvl, "ai_addr", &addrlen);
186 ai = malloc(sizeof(*ai) + addrlen);
189 ai->ai_flags = (int)nvlist_get_number(nvl, "ai_flags");
190 ai->ai_family = (int)nvlist_get_number(nvl, "ai_family");
191 ai->ai_socktype = (int)nvlist_get_number(nvl, "ai_socktype");
192 ai->ai_protocol = (int)nvlist_get_number(nvl, "ai_protocol");
193 ai->ai_addrlen = (socklen_t)addrlen;
194 canonname = nvlist_get_string(nvl, "ai_canonname");
195 if (canonname != NULL) {
196 ai->ai_canonname = strdup(canonname);
197 if (ai->ai_canonname == NULL) {
202 ai->ai_canonname = NULL;
204 ai->ai_addr = (void *)(ai + 1);
205 bcopy(addr, ai->ai_addr, addrlen);
212 cap_getaddrinfo(cap_channel_t *chan, const char *hostname, const char *servname,
213 const struct addrinfo *hints, struct addrinfo **res)
215 struct addrinfo *firstai, *prevai, *curai;
217 const nvlist_t *nvlai;
222 nvl = nvlist_create(0);
223 nvlist_add_string(nvl, "cmd", "getaddrinfo");
224 nvlist_add_string(nvl, "hostname", hostname);
225 nvlist_add_string(nvl, "servname", servname);
227 nvlist_add_number(nvl, "hints.ai_flags",
228 (uint64_t)hints->ai_flags);
229 nvlist_add_number(nvl, "hints.ai_family",
230 (uint64_t)hints->ai_family);
231 nvlist_add_number(nvl, "hints.ai_socktype",
232 (uint64_t)hints->ai_socktype);
233 nvlist_add_number(nvl, "hints.ai_protocol",
234 (uint64_t)hints->ai_protocol);
236 nvl = cap_xfer_nvlist(chan, nvl, 0);
239 if (nvlist_get_number(nvl, "error") != 0) {
240 error = (int)nvlist_get_number(nvl, "error");
246 firstai = prevai = curai = NULL;
247 for (ii = 0; ; ii++) {
248 n = snprintf(nvlname, sizeof(nvlname), "res%u", ii);
249 assert(n > 0 && n < (int)sizeof(nvlname));
250 if (!nvlist_exists_nvlist(nvl, nvlname))
252 nvlai = nvlist_get_nvlist(nvl, nvlname);
253 curai = addrinfo_unpack(nvlai);
257 prevai->ai_next = curai;
258 else if (firstai == NULL)
263 if (curai == NULL && nvlai != NULL) {
265 freeaddrinfo(firstai);
274 cap_getnameinfo(cap_channel_t *chan, const struct sockaddr *sa, socklen_t salen,
275 char *host, size_t hostlen, char *serv, size_t servlen, int flags)
280 nvl = nvlist_create(0);
281 nvlist_add_string(nvl, "cmd", "getnameinfo");
282 nvlist_add_number(nvl, "hostlen", (uint64_t)hostlen);
283 nvlist_add_number(nvl, "servlen", (uint64_t)servlen);
284 nvlist_add_binary(nvl, "sa", sa, (size_t)salen);
285 nvlist_add_number(nvl, "flags", (uint64_t)flags);
286 nvl = cap_xfer_nvlist(chan, nvl, 0);
289 if (nvlist_get_number(nvl, "error") != 0) {
290 error = (int)nvlist_get_number(nvl, "error");
296 strlcpy(host, nvlist_get_string(nvl, "host"), hostlen + 1);
298 strlcpy(serv, nvlist_get_string(nvl, "serv"), servlen + 1);
304 limit_remove(nvlist_t *limits, const char *prefix)
310 prefixlen = strlen(prefix);
313 while ((name = nvlist_next(limits, NULL, &cookie)) != NULL) {
314 if (strncmp(name, prefix, prefixlen) == 0) {
315 nvlist_free(limits, name);
322 cap_dns_type_limit(cap_channel_t *chan, const char * const *types,
330 if (cap_limit_get(chan, &limits) < 0)
333 limits = nvlist_create(0);
335 limit_remove(limits, "type");
336 for (i = 0; i < ntypes; i++) {
337 n = snprintf(nvlname, sizeof(nvlname), "type%u", i);
338 assert(n > 0 && n < (int)sizeof(nvlname));
339 nvlist_add_string(limits, nvlname, types[i]);
341 return (cap_limit_set(chan, limits));
345 cap_dns_family_limit(cap_channel_t *chan, const int *families,
353 if (cap_limit_get(chan, &limits) < 0)
356 limits = nvlist_create(0);
358 limit_remove(limits, "family");
359 for (i = 0; i < nfamilies; i++) {
360 n = snprintf(nvlname, sizeof(nvlname), "family%u", i);
361 assert(n > 0 && n < (int)sizeof(nvlname));
362 nvlist_add_number(limits, nvlname, (uint64_t)families[i]);
364 return (cap_limit_set(chan, limits));