]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libcapsicum/libcapsicum_dns.c
MFV r267565:
[FreeBSD/FreeBSD.git] / lib / libcapsicum / libcapsicum_dns.c
1 /*-
2  * Copyright (c) 2012-2013 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Pawel Jakub Dawidek under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <netdb.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include <nv.h>
39
40 #include "libcapsicum.h"
41 #include "libcapsicum_dns.h"
42
43 static struct hostent hent;
44
45 static void
46 hostent_free(struct hostent *hp)
47 {
48         unsigned int ii;
49
50         free(hp->h_name);
51         hp->h_name = NULL;
52         if (hp->h_aliases != NULL) {
53                 for (ii = 0; hp->h_aliases[ii] != NULL; ii++)
54                         free(hp->h_aliases[ii]);
55                 free(hp->h_aliases);
56                 hp->h_aliases = NULL;
57         }
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;
63         }
64 }
65
66 static struct hostent *
67 hostent_unpack(const nvlist_t *nvl, struct hostent *hp)
68 {
69         unsigned int ii, nitems;
70
71         hostent_free(hp);
72
73         hp->h_name = strdup(nvlist_get_string(nvl, "name"));
74         if (hp->h_name == NULL)
75                 goto fail;
76         hp->h_addrtype = (int)nvlist_get_number(nvl, "addrtype");
77         hp->h_length = (int)nvlist_get_number(nvl, "length");
78
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)
82                 goto fail;
83         for (ii = 0; ii < nitems; ii++) {
84                 hp->h_aliases[ii] =
85                     strdup(nvlist_getf_string(nvl, "alias%u", ii));
86                 if (hp->h_aliases[ii] == NULL)
87                         goto fail;
88         }
89         hp->h_aliases[ii] = NULL;
90
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)
94                 goto fail;
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)
98                         goto fail;
99                 bcopy(nvlist_getf_binary(nvl, NULL, "addr%u", ii),
100                     hp->h_addr_list[ii], hp->h_length);
101         }
102         hp->h_addr_list[ii] = NULL;
103
104         return (hp);
105 fail:
106         hostent_free(hp);
107         h_errno = NO_RECOVERY;
108         return (NULL);
109 }
110
111 struct hostent *
112 cap_gethostbyname(cap_channel_t *chan, const char *name)
113 {
114
115         return (cap_gethostbyname2(chan, name, AF_INET));
116 }
117
118 struct hostent *
119 cap_gethostbyname2(cap_channel_t *chan, const char *name, int type)
120 {
121         struct hostent *hp;
122         nvlist_t *nvl;
123
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);
129         if (nvl == NULL) {
130                 h_errno = NO_RECOVERY;
131                 return (NULL);
132         }
133         if (nvlist_get_number(nvl, "error") != 0) {
134                 h_errno = (int)nvlist_get_number(nvl, "error");
135                 nvlist_destroy(nvl);
136                 return (NULL);
137         }
138
139         hp = hostent_unpack(nvl, &hent);
140         nvlist_destroy(nvl);
141         return (hp);
142 }
143
144 struct hostent *
145 cap_gethostbyaddr(cap_channel_t *chan, const void *addr, socklen_t len,
146     int type)
147 {
148         struct hostent *hp;
149         nvlist_t *nvl;
150
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);
156         if (nvl == NULL) {
157                 h_errno = NO_RECOVERY;
158                 return (NULL);
159         }
160         if (nvlist_get_number(nvl, "error") != 0) {
161                 h_errno = (int)nvlist_get_number(nvl, "error");
162                 nvlist_destroy(nvl);
163                 return (NULL);
164         }
165         hp = hostent_unpack(nvl, &hent);
166         nvlist_destroy(nvl);
167         return (hp);
168 }
169
170 static struct addrinfo *
171 addrinfo_unpack(const nvlist_t *nvl)
172 {
173         struct addrinfo *ai;
174         const void *addr;
175         size_t addrlen;
176         const char *canonname;
177
178         addr = nvlist_get_binary(nvl, "ai_addr", &addrlen);
179         ai = malloc(sizeof(*ai) + addrlen);
180         if (ai == NULL)
181                 return (NULL);
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) {
191                         free(ai);
192                         return (NULL);
193                 }
194         } else {
195                 ai->ai_canonname = NULL;
196         }
197         ai->ai_addr = (void *)(ai + 1);
198         bcopy(addr, ai->ai_addr, addrlen);
199         ai->ai_next = NULL;
200
201         return (ai);
202 }
203
204 int
205 cap_getaddrinfo(cap_channel_t *chan, const char *hostname, const char *servname,
206     const struct addrinfo *hints, struct addrinfo **res)
207 {
208         struct addrinfo *firstai, *prevai, *curai;
209         unsigned int ii;
210         const nvlist_t *nvlai;
211         nvlist_t *nvl;
212         int error;
213
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);
218         if (hints != NULL) {
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);
227         }
228         nvl = cap_xfer_nvlist(chan, nvl);
229         if (nvl == NULL)
230                 return (EAI_MEMORY);
231         if (nvlist_get_number(nvl, "error") != 0) {
232                 error = (int)nvlist_get_number(nvl, "error");
233                 nvlist_destroy(nvl);
234                 return (error);
235         }
236
237         nvlai = NULL;
238         firstai = prevai = curai = NULL;
239         for (ii = 0; ; ii++) {
240                 if (!nvlist_existsf_nvlist(nvl, "res%u", ii))
241                         break;
242                 nvlai = nvlist_getf_nvlist(nvl, "res%u", ii);
243                 curai = addrinfo_unpack(nvlai);
244                 if (curai == NULL)
245                         break;
246                 if (prevai != NULL)
247                         prevai->ai_next = curai;
248                 else if (firstai == NULL)
249                         firstai = curai;
250                 prevai = curai;
251         }
252         nvlist_destroy(nvl);
253         if (curai == NULL && nvlai != NULL) {
254                 if (firstai == NULL)
255                         freeaddrinfo(firstai);
256                 return (EAI_MEMORY);
257         }
258
259         *res = firstai;
260         return (0);
261 }
262
263 int
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)
266 {
267         nvlist_t *nvl;
268         int error;
269
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);
277         if (nvl == NULL)
278                 return (EAI_MEMORY);
279         if (nvlist_get_number(nvl, "error") != 0) {
280                 error = (int)nvlist_get_number(nvl, "error");
281                 nvlist_destroy(nvl);
282                 return (error);
283         }
284
285         if (host != NULL)
286                 strlcpy(host, nvlist_get_string(nvl, "host"), hostlen + 1);
287         if (serv != NULL)
288                 strlcpy(serv, nvlist_get_string(nvl, "serv"), servlen + 1);
289         nvlist_destroy(nvl);
290         return (0);
291 }
292
293 static void
294 limit_remove(nvlist_t *limits, const char *prefix)
295 {
296         const char *name;
297         size_t prefixlen;
298         void *cookie;
299
300         prefixlen = strlen(prefix);
301 again:
302         cookie = NULL;
303         while ((name = nvlist_next(limits, NULL, &cookie)) != NULL) {
304                 if (strncmp(name, prefix, prefixlen) == 0) {
305                         nvlist_free(limits, name);
306                         goto again;
307                 }
308         }
309 }
310
311 int
312 cap_dns_type_limit(cap_channel_t *chan, const char * const *types,
313     size_t ntypes)
314 {
315         nvlist_t *limits;
316         unsigned int i;
317
318         if (cap_limit_get(chan, &limits) < 0)
319                 return (-1);
320         if (limits == NULL)
321                 limits = nvlist_create(0);
322         else
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));
327 }
328
329 int
330 cap_dns_family_limit(cap_channel_t *chan, const int *families,
331     size_t nfamilies)
332 {
333         nvlist_t *limits;
334         unsigned int i;
335
336         if (cap_limit_get(chan, &limits) < 0)
337                 return (-1);
338         if (limits == NULL)
339                 limits = nvlist_create(0);
340         else
341                 limit_remove(limits, "family");
342         for (i = 0; i < nfamilies; i++) {
343                 nvlist_addf_number(limits, (uint64_t)families[i],
344                     "family%u", i);
345         }
346         return (cap_limit_set(chan, limits));
347 }