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$");
40 #include "libcapsicum.h"
41 #include "libcapsicum_dns.h"
43 static struct hostent hent;
46 hostent_free(struct hostent *hp)
52 if (hp->h_aliases != NULL) {
53 for (ii = 0; hp->h_aliases[ii] != NULL; ii++)
54 free(hp->h_aliases[ii]);
58 if (hp->h_addr_list != NULL) {
59 for (ii = 0; hp->h_addr_list[ii] != NULL; ii++)
60 free(hp->h_addr_list[ii]);
61 free(hp->h_addr_list);
62 hp->h_addr_list = NULL;
66 static struct hostent *
67 hostent_unpack(const nvlist_t *nvl, struct hostent *hp)
69 unsigned int ii, nitems;
73 hp->h_name = strdup(nvlist_get_string(nvl, "name"));
74 if (hp->h_name == NULL)
76 hp->h_addrtype = (int)nvlist_get_number(nvl, "addrtype");
77 hp->h_length = (int)nvlist_get_number(nvl, "length");
79 nitems = (unsigned int)nvlist_get_number(nvl, "naliases");
80 hp->h_aliases = calloc(sizeof(hp->h_aliases[0]), nitems + 1);
81 if (hp->h_aliases == NULL)
83 for (ii = 0; ii < nitems; ii++) {
85 strdup(nvlist_getf_string(nvl, "alias%u", ii));
86 if (hp->h_aliases[ii] == NULL)
89 hp->h_aliases[ii] = NULL;
91 nitems = (unsigned int)nvlist_get_number(nvl, "naddrs");
92 hp->h_addr_list = calloc(sizeof(hp->h_addr_list[0]), nitems + 1);
93 if (hp->h_addr_list == NULL)
95 for (ii = 0; ii < nitems; ii++) {
96 hp->h_addr_list[ii] = malloc(hp->h_length);
97 if (hp->h_addr_list[ii] == NULL)
99 bcopy(nvlist_getf_binary(nvl, NULL, "addr%u", ii),
100 hp->h_addr_list[ii], hp->h_length);
102 hp->h_addr_list[ii] = NULL;
107 h_errno = NO_RECOVERY;
112 cap_gethostbyname(cap_channel_t *chan, const char *name)
115 return (cap_gethostbyname2(chan, name, AF_INET));
119 cap_gethostbyname2(cap_channel_t *chan, const char *name, int type)
124 nvl = nvlist_create(0);
125 nvlist_add_string(nvl, "cmd", "gethostbyname");
126 nvlist_add_number(nvl, "family", (uint64_t)type);
127 nvlist_add_string(nvl, "name", name);
128 nvl = cap_xfer_nvlist(chan, nvl);
130 h_errno = NO_RECOVERY;
133 if (nvlist_get_number(nvl, "error") != 0) {
134 h_errno = (int)nvlist_get_number(nvl, "error");
139 hp = hostent_unpack(nvl, &hent);
145 cap_gethostbyaddr(cap_channel_t *chan, const void *addr, socklen_t len,
151 nvl = nvlist_create(0);
152 nvlist_add_string(nvl, "cmd", "gethostbyaddr");
153 nvlist_add_binary(nvl, "addr", addr, (size_t)len);
154 nvlist_add_number(nvl, "family", (uint64_t)type);
155 nvl = cap_xfer_nvlist(chan, nvl);
157 h_errno = NO_RECOVERY;
160 if (nvlist_get_number(nvl, "error") != 0) {
161 h_errno = (int)nvlist_get_number(nvl, "error");
165 hp = hostent_unpack(nvl, &hent);
170 static struct addrinfo *
171 addrinfo_unpack(const nvlist_t *nvl)
176 const char *canonname;
178 addr = nvlist_get_binary(nvl, "ai_addr", &addrlen);
179 ai = malloc(sizeof(*ai) + addrlen);
182 ai->ai_flags = (int)nvlist_get_number(nvl, "ai_flags");
183 ai->ai_family = (int)nvlist_get_number(nvl, "ai_family");
184 ai->ai_socktype = (int)nvlist_get_number(nvl, "ai_socktype");
185 ai->ai_protocol = (int)nvlist_get_number(nvl, "ai_protocol");
186 ai->ai_addrlen = (socklen_t)addrlen;
187 canonname = nvlist_get_string(nvl, "ai_canonname");
188 if (canonname != NULL) {
189 ai->ai_canonname = strdup(canonname);
190 if (ai->ai_canonname == NULL) {
195 ai->ai_canonname = NULL;
197 ai->ai_addr = (void *)(ai + 1);
198 bcopy(addr, ai->ai_addr, addrlen);
205 cap_getaddrinfo(cap_channel_t *chan, const char *hostname, const char *servname,
206 const struct addrinfo *hints, struct addrinfo **res)
208 struct addrinfo *firstai, *prevai, *curai;
210 const nvlist_t *nvlai;
214 nvl = nvlist_create(0);
215 nvlist_add_string(nvl, "cmd", "getaddrinfo");
216 nvlist_add_string(nvl, "hostname", hostname);
217 nvlist_add_string(nvl, "servname", servname);
219 nvlist_add_number(nvl, "hints.ai_flags",
220 (uint64_t)hints->ai_flags);
221 nvlist_add_number(nvl, "hints.ai_family",
222 (uint64_t)hints->ai_family);
223 nvlist_add_number(nvl, "hints.ai_socktype",
224 (uint64_t)hints->ai_socktype);
225 nvlist_add_number(nvl, "hints.ai_protocol",
226 (uint64_t)hints->ai_protocol);
228 nvl = cap_xfer_nvlist(chan, nvl);
231 if (nvlist_get_number(nvl, "error") != 0) {
232 error = (int)nvlist_get_number(nvl, "error");
238 firstai = prevai = curai = NULL;
239 for (ii = 0; ; ii++) {
240 if (!nvlist_existsf_nvlist(nvl, "res%u", ii))
242 nvlai = nvlist_getf_nvlist(nvl, "res%u", ii);
243 curai = addrinfo_unpack(nvlai);
247 prevai->ai_next = curai;
248 else if (firstai == NULL)
253 if (curai == NULL && nvlai != NULL) {
255 freeaddrinfo(firstai);
264 cap_getnameinfo(cap_channel_t *chan, const struct sockaddr *sa, socklen_t salen,
265 char *host, size_t hostlen, char *serv, size_t servlen, int flags)
270 nvl = nvlist_create(0);
271 nvlist_add_string(nvl, "cmd", "getnameinfo");
272 nvlist_add_number(nvl, "hostlen", (uint64_t)hostlen);
273 nvlist_add_number(nvl, "servlen", (uint64_t)servlen);
274 nvlist_add_binary(nvl, "sa", sa, (size_t)salen);
275 nvlist_add_number(nvl, "flags", (uint64_t)flags);
276 nvl = cap_xfer_nvlist(chan, nvl);
279 if (nvlist_get_number(nvl, "error") != 0) {
280 error = (int)nvlist_get_number(nvl, "error");
286 strlcpy(host, nvlist_get_string(nvl, "host"), hostlen + 1);
288 strlcpy(serv, nvlist_get_string(nvl, "serv"), servlen + 1);
294 limit_remove(nvlist_t *limits, const char *prefix)
300 prefixlen = strlen(prefix);
303 while ((name = nvlist_next(limits, NULL, &cookie)) != NULL) {
304 if (strncmp(name, prefix, prefixlen) == 0) {
305 nvlist_free(limits, name);
312 cap_dns_type_limit(cap_channel_t *chan, const char * const *types,
318 if (cap_limit_get(chan, &limits) < 0)
321 limits = nvlist_create(0);
323 limit_remove(limits, "type");
324 for (i = 0; i < ntypes; i++)
325 nvlist_addf_string(limits, types[i], "type%u", i);
326 return (cap_limit_set(chan, limits));
330 cap_dns_family_limit(cap_channel_t *chan, const int *families,
336 if (cap_limit_get(chan, &limits) < 0)
339 limits = nvlist_create(0);
341 limit_remove(limits, "family");
342 for (i = 0; i < nfamilies; i++) {
343 nvlist_addf_number(limits, (uint64_t)families[i],
346 return (cap_limit_set(chan, limits));