]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libcapsicum/libcapsicum_dns.c
Add simple support for CloudABI processes to kdump(1).
[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 <sys/nv.h>
34
35 #include <assert.h>
36 #include <netdb.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include "libcapsicum.h"
42 #include "libcapsicum_dns.h"
43
44 static struct hostent hent;
45
46 static void
47 hostent_free(struct hostent *hp)
48 {
49         unsigned int ii;
50
51         free(hp->h_name);
52         hp->h_name = NULL;
53         if (hp->h_aliases != NULL) {
54                 for (ii = 0; hp->h_aliases[ii] != NULL; ii++)
55                         free(hp->h_aliases[ii]);
56                 free(hp->h_aliases);
57                 hp->h_aliases = NULL;
58         }
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;
64         }
65 }
66
67 static struct hostent *
68 hostent_unpack(const nvlist_t *nvl, struct hostent *hp)
69 {
70         unsigned int ii, nitems;
71         char nvlname[64];
72         int n;
73
74         hostent_free(hp);
75
76         hp->h_name = strdup(nvlist_get_string(nvl, "name"));
77         if (hp->h_name == NULL)
78                 goto fail;
79         hp->h_addrtype = (int)nvlist_get_number(nvl, "addrtype");
80         hp->h_length = (int)nvlist_get_number(nvl, "length");
81
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)
85                 goto fail;
86         for (ii = 0; ii < nitems; ii++) {
87                 n = snprintf(nvlname, sizeof(nvlname), "alias%u", ii);
88                 assert(n > 0 && n < (int)sizeof(nvlname));
89                 hp->h_aliases[ii] =
90                     strdup(nvlist_get_string(nvl, nvlname));
91                 if (hp->h_aliases[ii] == NULL)
92                         goto fail;
93         }
94         hp->h_aliases[ii] = NULL;
95
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)
99                 goto fail;
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)
103                         goto fail;
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);
108         }
109         hp->h_addr_list[ii] = NULL;
110
111         return (hp);
112 fail:
113         hostent_free(hp);
114         h_errno = NO_RECOVERY;
115         return (NULL);
116 }
117
118 struct hostent *
119 cap_gethostbyname(cap_channel_t *chan, const char *name)
120 {
121
122         return (cap_gethostbyname2(chan, name, AF_INET));
123 }
124
125 struct hostent *
126 cap_gethostbyname2(cap_channel_t *chan, const char *name, int type)
127 {
128         struct hostent *hp;
129         nvlist_t *nvl;
130
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);
136         if (nvl == NULL) {
137                 h_errno = NO_RECOVERY;
138                 return (NULL);
139         }
140         if (nvlist_get_number(nvl, "error") != 0) {
141                 h_errno = (int)nvlist_get_number(nvl, "error");
142                 nvlist_destroy(nvl);
143                 return (NULL);
144         }
145
146         hp = hostent_unpack(nvl, &hent);
147         nvlist_destroy(nvl);
148         return (hp);
149 }
150
151 struct hostent *
152 cap_gethostbyaddr(cap_channel_t *chan, const void *addr, socklen_t len,
153     int type)
154 {
155         struct hostent *hp;
156         nvlist_t *nvl;
157
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);
163         if (nvl == NULL) {
164                 h_errno = NO_RECOVERY;
165                 return (NULL);
166         }
167         if (nvlist_get_number(nvl, "error") != 0) {
168                 h_errno = (int)nvlist_get_number(nvl, "error");
169                 nvlist_destroy(nvl);
170                 return (NULL);
171         }
172         hp = hostent_unpack(nvl, &hent);
173         nvlist_destroy(nvl);
174         return (hp);
175 }
176
177 static struct addrinfo *
178 addrinfo_unpack(const nvlist_t *nvl)
179 {
180         struct addrinfo *ai;
181         const void *addr;
182         size_t addrlen;
183         const char *canonname;
184
185         addr = nvlist_get_binary(nvl, "ai_addr", &addrlen);
186         ai = malloc(sizeof(*ai) + addrlen);
187         if (ai == NULL)
188                 return (NULL);
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) {
198                         free(ai);
199                         return (NULL);
200                 }
201         } else {
202                 ai->ai_canonname = NULL;
203         }
204         ai->ai_addr = (void *)(ai + 1);
205         bcopy(addr, ai->ai_addr, addrlen);
206         ai->ai_next = NULL;
207
208         return (ai);
209 }
210
211 int
212 cap_getaddrinfo(cap_channel_t *chan, const char *hostname, const char *servname,
213     const struct addrinfo *hints, struct addrinfo **res)
214 {
215         struct addrinfo *firstai, *prevai, *curai;
216         unsigned int ii;
217         const nvlist_t *nvlai;
218         char nvlname[64];
219         nvlist_t *nvl;
220         int error, n;
221
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);
226         if (hints != NULL) {
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);
235         }
236         nvl = cap_xfer_nvlist(chan, nvl, 0);
237         if (nvl == NULL)
238                 return (EAI_MEMORY);
239         if (nvlist_get_number(nvl, "error") != 0) {
240                 error = (int)nvlist_get_number(nvl, "error");
241                 nvlist_destroy(nvl);
242                 return (error);
243         }
244
245         nvlai = NULL;
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))
251                         break;
252                 nvlai = nvlist_get_nvlist(nvl, nvlname);
253                 curai = addrinfo_unpack(nvlai);
254                 if (curai == NULL)
255                         break;
256                 if (prevai != NULL)
257                         prevai->ai_next = curai;
258                 else if (firstai == NULL)
259                         firstai = curai;
260                 prevai = curai;
261         }
262         nvlist_destroy(nvl);
263         if (curai == NULL && nvlai != NULL) {
264                 if (firstai == NULL)
265                         freeaddrinfo(firstai);
266                 return (EAI_MEMORY);
267         }
268
269         *res = firstai;
270         return (0);
271 }
272
273 int
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)
276 {
277         nvlist_t *nvl;
278         int error;
279
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);
287         if (nvl == NULL)
288                 return (EAI_MEMORY);
289         if (nvlist_get_number(nvl, "error") != 0) {
290                 error = (int)nvlist_get_number(nvl, "error");
291                 nvlist_destroy(nvl);
292                 return (error);
293         }
294
295         if (host != NULL)
296                 strlcpy(host, nvlist_get_string(nvl, "host"), hostlen + 1);
297         if (serv != NULL)
298                 strlcpy(serv, nvlist_get_string(nvl, "serv"), servlen + 1);
299         nvlist_destroy(nvl);
300         return (0);
301 }
302
303 static void
304 limit_remove(nvlist_t *limits, const char *prefix)
305 {
306         const char *name;
307         size_t prefixlen;
308         void *cookie;
309
310         prefixlen = strlen(prefix);
311 again:
312         cookie = NULL;
313         while ((name = nvlist_next(limits, NULL, &cookie)) != NULL) {
314                 if (strncmp(name, prefix, prefixlen) == 0) {
315                         nvlist_free(limits, name);
316                         goto again;
317                 }
318         }
319 }
320
321 int
322 cap_dns_type_limit(cap_channel_t *chan, const char * const *types,
323     size_t ntypes)
324 {
325         nvlist_t *limits;
326         unsigned int i;
327         char nvlname[64];
328         int n;
329
330         if (cap_limit_get(chan, &limits) < 0)
331                 return (-1);
332         if (limits == NULL)
333                 limits = nvlist_create(0);
334         else
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]);
340         }
341         return (cap_limit_set(chan, limits));
342 }
343
344 int
345 cap_dns_family_limit(cap_channel_t *chan, const int *families,
346     size_t nfamilies)
347 {
348         nvlist_t *limits;
349         unsigned int i;
350         char nvlname[64];
351         int n;
352
353         if (cap_limit_get(chan, &limits) < 0)
354                 return (-1);
355         if (limits == NULL)
356                 limits = nvlist_create(0);
357         else
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]);
363         }
364         return (cap_limit_set(chan, limits));
365 }