xref: /openbsd-src/lib/libc/asr/asr_utils.c (revision 48950c12d106c85f315112191a0228d7b83b9510)
1 /*	$OpenBSD: asr_utils.c,v 1.3 2012/11/24 15:12:48 eric Exp $	*/
2 /*
3  * Copyright (c) 2009-2012	Eric Faurot	<eric@faurot.net>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <arpa/nameser.h>
23 
24 #include <ctype.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 #include "asr.h"
31 #include "asr_private.h"
32 
33 static int dname_check_label(const char*, size_t);
34 static ssize_t dname_expand(const unsigned char*, size_t, size_t, size_t*,
35     char *, size_t);
36 
37 static int unpack_data(struct unpack*, void*, size_t);
38 static int unpack_u16(struct unpack*, uint16_t*);
39 static int unpack_u32(struct unpack*, uint32_t*);
40 static int unpack_inaddr(struct unpack*, struct in_addr*);
41 static int unpack_in6addr(struct unpack*, struct in6_addr*);
42 static int unpack_dname(struct unpack*, char*, size_t);
43 
44 static int pack_data(struct pack*, const void*, size_t);
45 static int pack_u16(struct pack*, uint16_t);
46 static int pack_dname(struct pack*, const char*);
47 
48 static int
49 dname_check_label(const char *s, size_t l)
50 {
51 	if (l == 0 || l > 63)
52 		return (-1);
53 
54 	for (l--; l; l--, s++)
55 		if (!(isalnum(*s) || *s == '_' || *s == '-'))
56 			return (-1);
57 
58 	return (0);
59 }
60 
61 ssize_t
62 dname_from_fqdn(const char *str, char *dst, size_t max)
63 {
64 	ssize_t	 res;
65 	size_t	 l, n;
66 	char	*d;
67 
68 	res = 0;
69 
70 	/* special case: the root domain */
71 	if (str[0] == '.') {
72 		if (str[1] != '\0')
73 			return (-1);
74 		if (dst && max >= 1)
75 			*dst = '\0';
76 		return (1);
77 	}
78 
79 	for (; *str; str = d + 1) {
80 
81 		d = strchr(str, '.');
82 		if (d == NULL || d == str)
83 			return (-1);
84 
85 		l = (d - str);
86 
87 		if (dname_check_label(str, l) == -1)
88 			return (-1);
89 
90 		res += l + 1;
91 
92 		if (dst) {
93 			*dst++ = l;
94 			max -= 1;
95 			n = (l > max) ? max : l;
96 			memmove(dst, str, n);
97 			max -= n;
98 			if (max == 0)
99 				dst = NULL;
100 			else
101 				dst += n;
102 		}
103 	}
104 
105 	if (dst)
106 		*dst++ = '\0';
107 
108 	return (res + 1);
109 }
110 
111 static ssize_t
112 dname_expand(const unsigned char *data, size_t len, size_t offset,
113     size_t *newoffset, char *dst, size_t max)
114 {
115 	size_t		 n, count, end, ptr, start;
116 	ssize_t		 res;
117 
118 	if (offset >= len)
119 		return (-1);
120 
121 	res = 0;
122 	end = start = offset;
123 
124 	for (; (n = data[offset]); ) {
125 		if ((n & 0xc0) == 0xc0) {
126 			if (offset + 2 > len)
127 				return (-1);
128 			ptr = 256 * (n & ~0xc0) + data[offset + 1];
129 			if (ptr >= start)
130 				return (-1);
131 			if (end < offset + 2)
132 				end = offset + 2;
133 			offset = ptr;
134 			continue;
135 		}
136 		if (offset + n + 1 > len)
137 			return (-1);
138 
139 		if (dname_check_label(data + offset + 1, n) == -1)
140 			return (-1);
141 
142 		/* copy n + at offset+1 */
143 		if (dst != NULL && max != 0) {
144 			count = (max < n + 1) ? (max) : (n + 1);
145 			memmove(dst, data + offset, count);
146 			dst += count;
147 			max -= count;
148 		}
149 		res += n + 1;
150 		offset += n + 1;
151 		if (end < offset)
152 			end = offset;
153 	}
154 	if (end < offset + 1)
155 		end = offset + 1;
156 
157 	if (dst != NULL && max != 0)
158 		dst[0] = 0;
159 	if (newoffset)
160 		*newoffset = end;
161 	return (res + 1);
162 }
163 
164 void
165 pack_init(struct pack *pack, char *buf, size_t len)
166 {
167 	pack->buf = buf;
168 	pack->len = len;
169 	pack->offset = 0;
170 	pack->err = NULL;
171 }
172 
173 void
174 unpack_init(struct unpack *unpack, const char *buf, size_t len)
175 {
176 	unpack->buf = buf;
177 	unpack->len = len;
178 	unpack->offset = 0;
179 	unpack->err = NULL;
180 }
181 
182 static int
183 unpack_data(struct unpack *p, void *data, size_t len)
184 {
185 	if (p->err)
186 		return (-1);
187 
188 	if (p->len - p->offset < len) {
189 		p->err = "too short";
190 		return (-1);
191 	}
192 
193 	memmove(data, p->buf + p->offset, len);
194 	p->offset += len;
195 
196 	return (0);
197 }
198 
199 static int
200 unpack_u16(struct unpack *p, uint16_t *u16)
201 {
202 	if (unpack_data(p, u16, 2) == -1)
203 		return (-1);
204 
205 	*u16 = ntohs(*u16);
206 
207 	return (0);
208 }
209 
210 static int
211 unpack_u32(struct unpack *p, uint32_t *u32)
212 {
213 	if (unpack_data(p, u32, 4) == -1)
214 		return (-1);
215 
216 	*u32 = ntohl(*u32);
217 
218 	return (0);
219 }
220 
221 static int
222 unpack_inaddr(struct unpack *p, struct in_addr *a)
223 {
224 	return (unpack_data(p, a, 4));
225 }
226 
227 static int
228 unpack_in6addr(struct unpack *p, struct in6_addr *a6)
229 {
230 	return (unpack_data(p, a6, 16));
231 }
232 
233 static int
234 unpack_dname(struct unpack *p, char *dst, size_t max)
235 {
236 	ssize_t e;
237 
238 	if (p->err)
239 		return (-1);
240 
241 	e = dname_expand(p->buf, p->len, p->offset, &p->offset, dst, max);
242 	if (e == -1) {
243 		p->err = "bad domain name";
244 		return (-1);
245 	}
246 	if (e < 0 || e > MAXDNAME) {
247 		p->err = "domain name too long";
248 		return (-1);
249 	}
250 
251 	return (0);
252 }
253 
254 int
255 unpack_header(struct unpack *p, struct header *h)
256 {
257 	if (unpack_data(p, h, HFIXEDSZ) == -1)
258 		return (-1);
259 
260 	h->flags = ntohs(h->flags);
261 	h->qdcount = ntohs(h->qdcount);
262 	h->ancount = ntohs(h->ancount);
263 	h->nscount = ntohs(h->nscount);
264 	h->arcount = ntohs(h->arcount);
265 
266 	return (0);
267 }
268 
269 int
270 unpack_query(struct unpack *p, struct query *q)
271 {
272 	unpack_dname(p, q->q_dname, sizeof(q->q_dname));
273 	unpack_u16(p, &q->q_type);
274 	unpack_u16(p, &q->q_class);
275 
276 	return (p->err) ? (-1) : (0);
277 }
278 
279 int
280 unpack_rr(struct unpack *p, struct rr *rr)
281 {
282 	uint16_t	rdlen;
283 	size_t		save_offset;
284 
285 	unpack_dname(p, rr->rr_dname, sizeof(rr->rr_dname));
286 	unpack_u16(p, &rr->rr_type);
287 	unpack_u16(p, &rr->rr_class);
288 	unpack_u32(p, &rr->rr_ttl);
289 	unpack_u16(p, &rdlen);
290 
291 	if (p->err)
292 		return (-1);
293 
294 	if (p->len - p->offset < rdlen) {
295 		p->err = "too short";
296 		return (-1);
297 	}
298 
299 	save_offset = p->offset;
300 
301 	switch (rr->rr_type) {
302 
303 	case T_CNAME:
304 		unpack_dname(p, rr->rr.cname.cname, sizeof(rr->rr.cname.cname));
305 		break;
306 
307 	case T_MX:
308 		unpack_u16(p, &rr->rr.mx.preference);
309 		unpack_dname(p, rr->rr.mx.exchange, sizeof(rr->rr.mx.exchange));
310 		break;
311 
312 	case T_NS:
313 		unpack_dname(p, rr->rr.ns.nsname, sizeof(rr->rr.ns.nsname));
314 		break;
315 
316 	case T_PTR:
317 		unpack_dname(p, rr->rr.ptr.ptrname, sizeof(rr->rr.ptr.ptrname));
318 		break;
319 
320 	case T_SOA:
321 		unpack_dname(p, rr->rr.soa.mname, sizeof(rr->rr.soa.mname));
322 		unpack_dname(p, rr->rr.soa.rname, sizeof(rr->rr.soa.rname));
323 		unpack_u32(p, &rr->rr.soa.serial);
324 		unpack_u32(p, &rr->rr.soa.refresh);
325 		unpack_u32(p, &rr->rr.soa.retry);
326 		unpack_u32(p, &rr->rr.soa.expire);
327 		unpack_u32(p, &rr->rr.soa.minimum);
328 		break;
329 
330 	case T_A:
331 		if (rr->rr_class != C_IN)
332 			goto other;
333 		unpack_inaddr(p, &rr->rr.in_a.addr);
334 		break;
335 
336 	case T_AAAA:
337 		if (rr->rr_class != C_IN)
338 			goto other;
339 		unpack_in6addr(p, &rr->rr.in_aaaa.addr6);
340 		break;
341 	default:
342 	other:
343 		rr->rr.other.rdata = p->buf + p->offset;
344 		rr->rr.other.rdlen = rdlen;
345 		p->offset += rdlen;
346 	}
347 
348 	if (p->err)
349 		return (-1);
350 
351 	/* make sure that the advertised rdlen is really ok */
352 	if (p->offset - save_offset != rdlen)
353 		p->err = "bad dlen";
354 
355 	return (p->err) ? (-1) : (0);
356 }
357 
358 static int
359 pack_data(struct pack *p, const void *data, size_t len)
360 {
361 	if (p->err)
362 		return (-1);
363 
364 	if (p->len < p->offset + len) {
365 		p->err = "no space";
366 		return (-1);
367 	}
368 
369 	memmove(p->buf + p->offset, data, len);
370 	p->offset += len;
371 
372 	return (0);
373 }
374 
375 static int
376 pack_u16(struct pack *p, uint16_t v)
377 {
378 	v = htons(v);
379 
380 	return (pack_data(p, &v, 2));
381 }
382 
383 static int
384 pack_dname(struct pack *p, const char *dname)
385 {
386 	/* dname compression would be nice to have here.
387 	 * need additionnal context.
388 	 */
389 	return (pack_data(p, dname, strlen(dname) + 1));
390 }
391 
392 int
393 pack_header(struct pack *p, const struct header *h)
394 {
395 	struct header c;
396 
397 	c.id = h->id;
398 	c.flags = htons(h->flags);
399 	c.qdcount = htons(h->qdcount);
400 	c.ancount = htons(h->ancount);
401 	c.nscount = htons(h->nscount);
402 	c.arcount = htons(h->arcount);
403 
404 	return (pack_data(p, &c, HFIXEDSZ));
405 }
406 
407 int
408 pack_query(struct pack *p, uint16_t type, uint16_t class, const char *dname)
409 {
410 	pack_dname(p, dname);
411 	pack_u16(p, type);
412 	pack_u16(p, class);
413 
414 	return (p->err) ? (-1) : (0);
415 }
416 
417 int
418 sockaddr_from_str(struct sockaddr *sa, int family, const char *str)
419 {
420 	struct in_addr		 ina;
421 	struct in6_addr		 in6a;
422 	struct sockaddr_in	*sin;
423 	struct sockaddr_in6	*sin6;
424 
425 	switch (family) {
426 	case PF_UNSPEC:
427 		if (sockaddr_from_str(sa, PF_INET, str) == 0)
428 			return (0);
429 		return sockaddr_from_str(sa, PF_INET6, str);
430 
431 	case PF_INET:
432 		if (inet_pton(PF_INET, str, &ina) != 1)
433 			return (-1);
434 
435 		sin = (struct sockaddr_in *)sa;
436 		memset(sin, 0, sizeof *sin);
437 		sin->sin_len = sizeof(struct sockaddr_in);
438 		sin->sin_family = PF_INET;
439 		sin->sin_addr.s_addr = ina.s_addr;
440 		return (0);
441 
442 	case PF_INET6:
443 		if (inet_pton(PF_INET6, str, &in6a) != 1)
444 			return (-1);
445 
446 		sin6 = (struct sockaddr_in6 *)sa;
447 		memset(sin6, 0, sizeof *sin6);
448 		sin6->sin6_len = sizeof(struct sockaddr_in6);
449 		sin6->sin6_family = PF_INET6;
450 		sin6->sin6_addr = in6a;
451 		return (0);
452 
453 	default:
454 		break;
455 	}
456 
457 	return (-1);
458 }
459