]>
Commit | Line | Data |
---|---|---|
ed62c46b AC |
1 | /* |
2 | * Copyright (c) 1985, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 4. Neither the name of the University nor the names of its contributors | |
14 | * may be used to endorse or promote products derived from this software | |
15 | * without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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 | /* | |
31 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | |
32 | * | |
33 | * Permission to use, copy, modify, and distribute this software for any | |
34 | * purpose with or without fee is hereby granted, provided that the above | |
35 | * copyright notice and this permission notice appear in all copies, and that | |
36 | * the name of Digital Equipment Corporation not be used in advertising or | |
37 | * publicity pertaining to distribution of the document or software without | |
38 | * specific, written prior permission. | |
39 | * | |
40 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | |
41 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | |
42 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | |
43 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
44 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
45 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
46 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
47 | * SOFTWARE. | |
48 | */ | |
49 | ||
50 | /* | |
51 | * Portions Copyright (c) 1996-1999 by Internet Software Consortium. | |
52 | * | |
53 | * Permission to use, copy, modify, and distribute this software for any | |
54 | * purpose with or without fee is hereby granted, provided that the above | |
55 | * copyright notice and this permission notice appear in all copies. | |
56 | * | |
57 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | |
58 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | |
59 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | |
60 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
61 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
62 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
63 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
64 | * SOFTWARE. | |
65 | */ | |
66 | ||
67 | /* Original copyright ISC as above. | |
68 | * Code modified specifically for ircd use from the following orginal files | |
69 | * in bind ... | |
70 | * | |
71 | * res_comp.c | |
72 | * ns_name.c | |
73 | * ns_netint.c | |
74 | * res_init.c | |
75 | * | |
76 | * - Dianora | |
77 | */ | |
78 | ||
79 | #include "stdinc.h" | |
80 | #include "ircd_defs.h" | |
81 | #include "common.h" | |
82 | #include "ircd.h" | |
83 | #include "res.h" | |
84 | #include "reslib.h" | |
85 | #include "match.h" | |
86 | #include "logger.h" | |
87 | ||
88 | #define NS_TYPE_ELT 0x40 /* EDNS0 extended label type */ | |
89 | #define DNS_LABELTYPE_BITSTRING 0x41 | |
90 | #define DNS_MAXLINE 128 | |
91 | ||
92 | /* $Id: reslib.c 1695 2006-06-27 15:11:23Z jilles $ */ | |
93 | /* from Hybrid Id: reslib.c 177 2005-10-22 09:05:05Z michael $ */ | |
94 | ||
95 | struct rb_sockaddr_storage irc_nsaddr_list[IRCD_MAXNS]; | |
96 | int irc_nscount = 0; | |
97 | char irc_domain[IRCD_RES_HOSTLEN + 1]; | |
98 | ||
99 | static const char digitvalue[256] = { | |
100 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/ | |
101 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/ | |
102 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/ | |
103 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/ | |
104 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/ | |
105 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/ | |
106 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/ | |
107 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/ | |
108 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
109 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
110 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
111 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
112 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
113 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
114 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
115 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/ | |
116 | }; | |
117 | ||
118 | static int parse_resvconf(void); | |
119 | static void add_nameserver(const char *); | |
120 | ||
121 | static const char digits[] = "0123456789"; | |
122 | static int labellen(const unsigned char *lp); | |
123 | static int special(int ch); | |
124 | static int printable(int ch); | |
125 | static int irc_decode_bitstring(const char **cpp, char *dn, const char *eom); | |
126 | static int irc_ns_name_compress(const char *src, unsigned char *dst, size_t dstsiz, | |
127 | const unsigned char **dnptrs, const unsigned char **lastdnptr); | |
128 | static int irc_dn_find(const unsigned char *, const unsigned char *, const unsigned char * const *, | |
129 | const unsigned char * const *); | |
130 | static int irc_encode_bitsring(const char **, const char *, unsigned char **, unsigned char **, | |
131 | const char *); | |
132 | static int irc_ns_name_uncompress(const unsigned char *, const unsigned char *, | |
133 | const unsigned char *, char *, size_t); | |
134 | static int irc_ns_name_unpack(const unsigned char *, const unsigned char *, | |
135 | const unsigned char *, unsigned char *, | |
136 | size_t); | |
137 | static int irc_ns_name_ntop(const char *, char *, size_t); | |
138 | static int irc_ns_name_skip(const unsigned char **, const unsigned char *); | |
139 | static int mklower(int ch); | |
140 | ||
141 | int | |
142 | irc_res_init(void) | |
143 | { | |
144 | irc_nscount = 0; | |
145 | parse_resvconf(); | |
146 | if (irc_nscount == 0) | |
147 | add_nameserver("127.0.0.1"); | |
148 | return 0; | |
149 | } | |
150 | ||
151 | /* parse_resvconf() | |
152 | * | |
153 | * inputs - NONE | |
154 | * output - -1 if failure 0 if success | |
155 | * side effects - fills in irc_nsaddr_list | |
156 | */ | |
157 | static int | |
158 | parse_resvconf(void) | |
159 | { | |
160 | char *p; | |
161 | char *opt; | |
162 | char *arg; | |
163 | char input[DNS_MAXLINE]; | |
164 | FILE *file; | |
165 | ||
166 | /* XXX "/etc/resolv.conf" should be from a define in setup.h perhaps | |
167 | * for cygwin support etc. this hardcodes it to unix for now -db | |
168 | */ | |
169 | if ((file = fopen("/etc/resolv.conf", "r")) == NULL) | |
170 | return -1; | |
171 | ||
172 | while (fgets(input, sizeof(input), file) != NULL) | |
173 | { | |
174 | /* blow away any newline */ | |
175 | if ((p = strpbrk(input, "\r\n")) != NULL) | |
176 | *p = '\0'; | |
177 | ||
178 | p = input; | |
179 | /* skip until something thats not a space is seen */ | |
180 | while (isspace(*p)) | |
181 | p++; | |
182 | /* if at this point, have a '\0' then continue */ | |
183 | if (*p == '\0') | |
184 | continue; | |
185 | ||
186 | /* Ignore comment lines immediately */ | |
187 | if (*p == '#' || *p == ';') | |
188 | continue; | |
189 | ||
190 | /* skip until a space is found */ | |
191 | opt = p; | |
192 | while (!isspace(*p) && *p != '\0') | |
193 | p++; | |
194 | if (*p == '\0') | |
195 | continue; /* no arguments?.. ignore this line */ | |
196 | /* blow away the space character */ | |
197 | *p++ = '\0'; | |
198 | ||
199 | /* skip these spaces that are before the argument */ | |
200 | while (isspace(*p)) | |
201 | p++; | |
202 | /* Now arg should be right where p is pointing */ | |
203 | arg = p; | |
204 | if ((p = strpbrk(arg, " \t")) != NULL) | |
205 | *p = '\0'; /* take the first word */ | |
206 | ||
207 | if (strcasecmp(opt, "domain") == 0) | |
208 | rb_strlcpy(irc_domain, arg, sizeof(irc_domain)); | |
209 | else if (strcasecmp(opt, "nameserver") == 0) | |
210 | add_nameserver(arg); | |
211 | } | |
212 | ||
213 | fclose(file); | |
214 | return 0; | |
215 | } | |
216 | ||
217 | /* add_nameserver() | |
218 | * | |
219 | * input - either an IPV4 address in dotted quad | |
220 | * or an IPV6 address in : format | |
221 | * output - NONE | |
222 | * side effects - entry in irc_nsaddr_list is filled in as needed | |
223 | */ | |
224 | static void | |
225 | add_nameserver(const char *arg) | |
226 | { | |
227 | struct addrinfo hints, *res; | |
228 | ||
229 | /* Done max number of nameservers? */ | |
230 | if (irc_nscount >= IRCD_MAXNS) | |
231 | return; | |
232 | ||
233 | memset(&hints, 0, sizeof(hints)); | |
234 | hints.ai_family = PF_UNSPEC; | |
235 | hints.ai_socktype = SOCK_DGRAM; | |
236 | hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; | |
237 | ||
238 | if (getaddrinfo(arg, "domain", &hints, &res)) | |
239 | return; | |
240 | ||
241 | if (res == NULL) | |
242 | return; | |
243 | ||
244 | memcpy(&irc_nsaddr_list[irc_nscount], res->ai_addr, res->ai_addrlen); | |
245 | SET_SS_LEN(&irc_nsaddr_list[irc_nscount], res->ai_addrlen); | |
246 | irc_nscount++; | |
247 | freeaddrinfo(res); | |
248 | } | |
249 | ||
250 | /* | |
251 | * Expand compressed domain name 'comp_dn' to full domain name. | |
252 | * 'msg' is a pointer to the begining of the message, | |
253 | * 'eomorig' points to the first location after the message, | |
254 | * 'exp_dn' is a pointer to a buffer of size 'length' for the result. | |
255 | * Return size of compressed name or -1 if there was an error. | |
256 | */ | |
257 | int | |
258 | irc_dn_expand(const unsigned char *msg, const unsigned char *eom, | |
259 | const unsigned char *src, char *dst, int dstsiz) | |
260 | { | |
261 | int n = irc_ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz); | |
262 | ||
263 | if (n > 0 && dst[0] == '.') | |
264 | dst[0] = '\0'; | |
265 | return(n); | |
266 | } | |
267 | ||
268 | /* | |
269 | * irc_ns_name_uncompress(msg, eom, src, dst, dstsiz) | |
270 | * Expand compressed domain name to presentation format. | |
271 | * return: | |
272 | * Number of bytes read out of `src', or -1 (with errno set). | |
273 | * note: | |
274 | * Root domain returns as "." not "". | |
275 | */ | |
276 | static int | |
277 | irc_ns_name_uncompress(const unsigned char *msg, const unsigned char *eom, | |
278 | const unsigned char *src, char *dst, size_t dstsiz) | |
279 | { | |
280 | unsigned char tmp[NS_MAXCDNAME]; | |
281 | int n; | |
282 | ||
283 | if ((n = irc_ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1) | |
284 | return(-1); | |
285 | if (irc_ns_name_ntop((char*)tmp, dst, dstsiz) == -1) | |
286 | return(-1); | |
287 | return(n); | |
288 | } | |
289 | /* | |
290 | * irc_ns_name_unpack(msg, eom, src, dst, dstsiz) | |
291 | * Unpack a domain name from a message, source may be compressed. | |
292 | * return: | |
293 | * -1 if it fails, or consumed octets if it succeeds. | |
294 | */ | |
295 | static int | |
296 | irc_ns_name_unpack(const unsigned char *msg, const unsigned char *eom, | |
297 | const unsigned char *src, unsigned char *dst, | |
298 | size_t dstsiz) | |
299 | { | |
300 | const unsigned char *srcp, *dstlim; | |
301 | unsigned char *dstp; | |
302 | int n, len, checked, l; | |
303 | ||
304 | len = -1; | |
305 | checked = 0; | |
306 | dstp = dst; | |
307 | srcp = src; | |
308 | dstlim = dst + dstsiz; | |
309 | if (srcp < msg || srcp >= eom) { | |
310 | errno = EMSGSIZE; | |
311 | return (-1); | |
312 | } | |
313 | /* Fetch next label in domain name. */ | |
314 | while ((n = *srcp++) != 0) { | |
315 | /* Check for indirection. */ | |
316 | switch (n & NS_CMPRSFLGS) { | |
317 | case 0: | |
318 | case NS_TYPE_ELT: | |
319 | /* Limit checks. */ | |
320 | if ((l = labellen(srcp - 1)) < 0) { | |
321 | errno = EMSGSIZE; | |
322 | return(-1); | |
323 | } | |
324 | if (dstp + l + 1 >= dstlim || srcp + l >= eom) { | |
325 | errno = EMSGSIZE; | |
326 | return (-1); | |
327 | } | |
328 | checked += l + 1; | |
329 | *dstp++ = n; | |
330 | memcpy(dstp, srcp, l); | |
331 | dstp += l; | |
332 | srcp += l; | |
333 | break; | |
334 | ||
335 | case NS_CMPRSFLGS: | |
336 | if (srcp >= eom) { | |
337 | errno = EMSGSIZE; | |
338 | return (-1); | |
339 | } | |
340 | if (len < 0) | |
341 | len = srcp - src + 1; | |
342 | srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff)); | |
343 | if (srcp < msg || srcp >= eom) { /* Out of range. */ | |
344 | errno = EMSGSIZE; | |
345 | return (-1); | |
346 | } | |
347 | checked += 2; | |
348 | /* | |
349 | * Check for loops in the compressed name; | |
350 | * if we've looked at the whole message, | |
351 | * there must be a loop. | |
352 | */ | |
353 | if (checked >= eom - msg) { | |
354 | errno = EMSGSIZE; | |
355 | return (-1); | |
356 | } | |
357 | break; | |
358 | ||
359 | default: | |
360 | errno = EMSGSIZE; | |
361 | return (-1); /* flag error */ | |
362 | } | |
363 | } | |
364 | *dstp = '\0'; | |
365 | if (len < 0) | |
366 | len = srcp - src; | |
367 | return (len); | |
368 | } | |
369 | ||
370 | /* | |
371 | * irc_ns_name_ntop(src, dst, dstsiz) | |
372 | * Convert an encoded domain name to printable ascii as per RFC1035. | |
373 | * return: | |
374 | * Number of bytes written to buffer, or -1 (with errno set) | |
375 | * notes: | |
376 | * The root is returned as "." | |
377 | * All other domains are returned in non absolute form | |
378 | */ | |
379 | static int | |
380 | irc_ns_name_ntop(const char *src, char *dst, size_t dstsiz) | |
381 | { | |
382 | const char *cp; | |
383 | char *dn, *eom; | |
384 | unsigned char c; | |
385 | unsigned int n; | |
386 | int l; | |
387 | ||
388 | cp = src; | |
389 | dn = dst; | |
390 | eom = dst + dstsiz; | |
391 | ||
392 | while ((n = *cp++) != 0) { | |
393 | if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { | |
394 | /* Some kind of compression pointer. */ | |
395 | errno = EMSGSIZE; | |
396 | return (-1); | |
397 | } | |
398 | if (dn != dst) { | |
399 | if (dn >= eom) { | |
400 | errno = EMSGSIZE; | |
401 | return (-1); | |
402 | } | |
403 | *dn++ = '.'; | |
404 | } | |
405 | if ((l = labellen((const unsigned char*)(cp - 1))) < 0) { | |
406 | errno = EMSGSIZE; /* XXX */ | |
407 | return(-1); | |
408 | } | |
409 | if (dn + l >= eom) { | |
410 | errno = EMSGSIZE; | |
411 | return (-1); | |
412 | } | |
413 | if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) { | |
414 | int m; | |
415 | ||
416 | if (n != DNS_LABELTYPE_BITSTRING) { | |
417 | /* XXX: labellen should reject this case */ | |
418 | errno = EINVAL; | |
419 | return(-1); | |
420 | } | |
421 | if ((m = irc_decode_bitstring(&cp, dn, eom)) < 0) | |
422 | { | |
423 | errno = EMSGSIZE; | |
424 | return(-1); | |
425 | } | |
426 | dn += m; | |
427 | continue; | |
428 | } | |
429 | for ((void)NULL; l > 0; l--) { | |
430 | c = *cp++; | |
431 | if (special(c)) { | |
432 | if (dn + 1 >= eom) { | |
433 | errno = EMSGSIZE; | |
434 | return (-1); | |
435 | } | |
436 | *dn++ = '\\'; | |
437 | *dn++ = (char)c; | |
438 | } else if (!printable(c)) { | |
439 | if (dn + 3 >= eom) { | |
440 | errno = EMSGSIZE; | |
441 | return (-1); | |
442 | } | |
443 | *dn++ = '\\'; | |
444 | *dn++ = digits[c / 100]; | |
445 | *dn++ = digits[(c % 100) / 10]; | |
446 | *dn++ = digits[c % 10]; | |
447 | } else { | |
448 | if (dn >= eom) { | |
449 | errno = EMSGSIZE; | |
450 | return (-1); | |
451 | } | |
452 | *dn++ = (char)c; | |
453 | } | |
454 | } | |
455 | } | |
456 | if (dn == dst) { | |
457 | if (dn >= eom) { | |
458 | errno = EMSGSIZE; | |
459 | return (-1); | |
460 | } | |
461 | *dn++ = '.'; | |
462 | } | |
463 | if (dn >= eom) { | |
464 | errno = EMSGSIZE; | |
465 | return (-1); | |
466 | } | |
467 | *dn++ = '\0'; | |
468 | return (dn - dst); | |
469 | } | |
470 | ||
471 | /* | |
472 | * Pack domain name 'exp_dn' in presentation form into 'comp_dn'. | |
473 | * Return the size of the compressed name or -1. | |
474 | * 'length' is the size of the array pointed to by 'comp_dn'. | |
475 | */ | |
476 | static int | |
477 | irc_dn_comp(const char *src, unsigned char *dst, int dstsiz, | |
478 | const unsigned char **dnptrs, const unsigned char **lastdnptr) | |
479 | { | |
480 | return(irc_ns_name_compress(src, dst, (size_t)dstsiz, | |
481 | (const unsigned char **)dnptrs, | |
482 | (const unsigned char **)lastdnptr)); | |
483 | } | |
484 | ||
485 | /* | |
486 | * Skip over a compressed domain name. Return the size or -1. | |
487 | */ | |
488 | int | |
489 | irc_dn_skipname(const unsigned char *ptr, const unsigned char *eom) { | |
490 | const unsigned char *saveptr = ptr; | |
491 | ||
492 | if (irc_ns_name_skip(&ptr, eom) == -1) | |
493 | return(-1); | |
494 | return(ptr - saveptr); | |
495 | } | |
496 | ||
497 | /* | |
498 | * ns_name_skip(ptrptr, eom) | |
499 | * Advance *ptrptr to skip over the compressed name it points at. | |
500 | * return: | |
501 | * 0 on success, -1 (with errno set) on failure. | |
502 | */ | |
503 | static int | |
504 | irc_ns_name_skip(const unsigned char **ptrptr, const unsigned char *eom) | |
505 | { | |
506 | const unsigned char *cp; | |
507 | unsigned int n; | |
508 | int l; | |
509 | ||
510 | cp = *ptrptr; | |
511 | ||
512 | while (cp < eom && (n = *cp++) != 0) | |
513 | { | |
514 | /* Check for indirection. */ | |
515 | switch (n & NS_CMPRSFLGS) | |
516 | { | |
517 | case 0: /* normal case, n == len */ | |
518 | cp += n; | |
519 | continue; | |
520 | case NS_TYPE_ELT: /* EDNS0 extended label */ | |
521 | if ((l = labellen(cp - 1)) < 0) | |
522 | { | |
523 | errno = EMSGSIZE; /* XXX */ | |
524 | return(-1); | |
525 | } | |
526 | ||
527 | cp += l; | |
528 | continue; | |
529 | case NS_CMPRSFLGS: /* indirection */ | |
530 | cp++; | |
531 | break; | |
532 | default: /* illegal type */ | |
533 | errno = EMSGSIZE; | |
534 | return(-1); | |
535 | } | |
536 | ||
537 | break; | |
538 | } | |
539 | ||
540 | if (cp > eom) | |
541 | { | |
542 | errno = EMSGSIZE; | |
543 | return (-1); | |
544 | } | |
545 | ||
546 | *ptrptr = cp; | |
547 | return(0); | |
548 | } | |
549 | ||
550 | unsigned int | |
551 | irc_ns_get16(const unsigned char *src) | |
552 | { | |
553 | unsigned int dst; | |
554 | ||
555 | IRC_NS_GET16(dst, src); | |
556 | return(dst); | |
557 | } | |
558 | ||
559 | unsigned long | |
560 | irc_ns_get32(const unsigned char *src) | |
561 | { | |
562 | unsigned long dst; | |
563 | ||
564 | IRC_NS_GET32(dst, src); | |
565 | return(dst); | |
566 | } | |
567 | ||
568 | void | |
569 | irc_ns_put16(unsigned int src, unsigned char *dst) | |
570 | { | |
571 | IRC_NS_PUT16(src, dst); | |
572 | } | |
573 | ||
574 | void | |
575 | irc_ns_put32(unsigned long src, unsigned char *dst) | |
576 | { | |
577 | IRC_NS_PUT32(src, dst); | |
578 | } | |
579 | ||
580 | /* From ns_name.c */ | |
581 | ||
582 | /* | |
583 | * special(ch) | |
584 | * Thinking in noninternationalized USASCII (per the DNS spec), | |
585 | * is this characted special ("in need of quoting") ? | |
586 | * return: | |
587 | * boolean. | |
588 | */ | |
589 | static int | |
590 | special(int ch) | |
591 | { | |
592 | switch (ch) | |
593 | { | |
594 | case 0x22: /* '"' */ | |
595 | case 0x2E: /* '.' */ | |
596 | case 0x3B: /* ';' */ | |
597 | case 0x5C: /* '\\' */ | |
598 | case 0x28: /* '(' */ | |
599 | case 0x29: /* ')' */ | |
600 | /* Special modifiers in zone files. */ | |
601 | case 0x40: /* '@' */ | |
602 | case 0x24: /* '$' */ | |
603 | return(1); | |
604 | default: | |
605 | return(0); | |
606 | } | |
607 | } | |
608 | ||
609 | static int | |
610 | labellen(const unsigned char *lp) | |
611 | { | |
612 | int bitlen; | |
613 | unsigned char l = *lp; | |
614 | ||
615 | if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) | |
616 | { | |
617 | /* should be avoided by the caller */ | |
618 | return(-1); | |
619 | } | |
620 | ||
621 | if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) | |
622 | { | |
623 | if (l == DNS_LABELTYPE_BITSTRING) | |
624 | { | |
625 | if ((bitlen = *(lp + 1)) == 0) | |
626 | bitlen = 256; | |
627 | return((bitlen + 7 ) / 8 + 1); | |
628 | } | |
629 | ||
630 | return(-1); /* unknwon ELT */ | |
631 | } | |
632 | ||
633 | return(l); | |
634 | } | |
635 | ||
636 | ||
637 | /* | |
638 | * printable(ch) | |
639 | * Thinking in noninternationalized USASCII (per the DNS spec), | |
640 | * is this character visible and not a space when printed ? | |
641 | * return: | |
642 | * boolean. | |
643 | */ | |
644 | static int | |
645 | printable(int ch) | |
646 | { | |
647 | return(ch > 0x20 && ch < 0x7f); | |
648 | } | |
649 | ||
650 | static int | |
651 | irc_decode_bitstring(const char **cpp, char *dn, const char *eom) | |
652 | { | |
653 | const char *cp = *cpp; | |
654 | char *beg = dn, tc; | |
655 | int b, blen, plen; | |
656 | ||
657 | if ((blen = (*cp & 0xff)) == 0) | |
658 | blen = 256; | |
659 | plen = (blen + 3) / 4; | |
660 | plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1); | |
661 | if (dn + plen >= eom) | |
662 | return(-1); | |
663 | ||
664 | cp++; | |
665 | dn += sprintf(dn, "\\[x"); | |
666 | for (b = blen; b > 7; b -= 8, cp++) | |
667 | dn += sprintf(dn, "%02x", *cp & 0xff); | |
668 | if (b > 4) { | |
669 | tc = *cp++; | |
670 | dn += sprintf(dn, "%02x", tc & (0xff << (8 - b))); | |
671 | } else if (b > 0) { | |
672 | tc = *cp++; | |
673 | dn += sprintf(dn, "%1x", | |
674 | ((tc >> 4) & 0x0f) & (0x0f << (4 - b))); | |
675 | } | |
676 | dn += sprintf(dn, "/%d]", blen); | |
677 | ||
678 | *cpp = cp; | |
679 | return(dn - beg); | |
680 | } | |
681 | ||
682 | /* | |
683 | * irc_ns_name_pton(src, dst, dstsiz) | |
684 | * Convert a ascii string into an encoded domain name as per RFC1035. | |
685 | * return: | |
686 | * -1 if it fails | |
687 | * 1 if string was fully qualified | |
688 | * 0 is string was not fully qualified | |
689 | * notes: | |
690 | * Enforces label and domain length limits. | |
691 | */ | |
692 | static int | |
693 | irc_ns_name_pton(const char *src, unsigned char *dst, size_t dstsiz) | |
694 | { | |
695 | unsigned char *label, *bp, *eom; | |
696 | char *cp; | |
697 | int c, n, escaped, e = 0; | |
698 | ||
699 | escaped = 0; | |
700 | bp = dst; | |
701 | eom = dst + dstsiz; | |
702 | label = bp++; | |
703 | ||
704 | ||
705 | while ((c = *src++) != 0) { | |
706 | if (escaped) { | |
707 | if (c == '[') { /* start a bit string label */ | |
708 | if ((cp = strchr(src, ']')) == NULL) { | |
709 | errno = EINVAL; /* ??? */ | |
710 | return(-1); | |
711 | } | |
712 | if ((e = irc_encode_bitsring(&src, | |
713 | cp + 2, | |
714 | &label, | |
715 | &bp, | |
716 | (const char *)eom)) | |
717 | != 0) { | |
718 | errno = e; | |
719 | return(-1); | |
720 | } | |
721 | escaped = 0; | |
722 | label = bp++; | |
723 | if ((c = *src++) == 0) | |
724 | goto done; | |
725 | else if (c != '.') { | |
726 | errno = EINVAL; | |
727 | return(-1); | |
728 | } | |
729 | continue; | |
730 | } | |
731 | else if ((cp = strchr(digits, c)) != NULL) { | |
732 | n = (cp - digits) * 100; | |
733 | if ((c = *src++) == 0 || | |
734 | (cp = strchr(digits, c)) == NULL) { | |
735 | errno = EMSGSIZE; | |
736 | return (-1); | |
737 | } | |
738 | n += (cp - digits) * 10; | |
739 | if ((c = *src++) == 0 || | |
740 | (cp = strchr(digits, c)) == NULL) { | |
741 | errno = EMSGSIZE; | |
742 | return (-1); | |
743 | } | |
744 | n += (cp - digits); | |
745 | if (n > 255) { | |
746 | errno = EMSGSIZE; | |
747 | return (-1); | |
748 | } | |
749 | c = n; | |
750 | } | |
751 | escaped = 0; | |
752 | } else if (c == '\\') { | |
753 | escaped = 1; | |
754 | continue; | |
755 | } else if (c == '.') { | |
756 | c = (bp - label - 1); | |
757 | if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */ | |
758 | errno = EMSGSIZE; | |
759 | return (-1); | |
760 | } | |
761 | if (label >= eom) { | |
762 | errno = EMSGSIZE; | |
763 | return (-1); | |
764 | } | |
765 | *label = c; | |
766 | /* Fully qualified ? */ | |
767 | if (*src == '\0') { | |
768 | if (c != 0) { | |
769 | if (bp >= eom) { | |
770 | errno = EMSGSIZE; | |
771 | return (-1); | |
772 | } | |
773 | *bp++ = '\0'; | |
774 | } | |
775 | if ((bp - dst) > NS_MAXCDNAME) { | |
776 | errno = EMSGSIZE; | |
777 | return (-1); | |
778 | } | |
779 | return (1); | |
780 | } | |
781 | if (c == 0 || *src == '.') { | |
782 | errno = EMSGSIZE; | |
783 | return (-1); | |
784 | } | |
785 | label = bp++; | |
786 | continue; | |
787 | } | |
788 | if (bp >= eom) { | |
789 | errno = EMSGSIZE; | |
790 | return (-1); | |
791 | } | |
792 | *bp++ = (unsigned char)c; | |
793 | } | |
794 | c = (bp - label - 1); | |
795 | if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */ | |
796 | errno = EMSGSIZE; | |
797 | return (-1); | |
798 | } | |
799 | done: | |
800 | if (label >= eom) { | |
801 | errno = EMSGSIZE; | |
802 | return (-1); | |
803 | } | |
804 | *label = c; | |
805 | if (c != 0) { | |
806 | if (bp >= eom) { | |
807 | errno = EMSGSIZE; | |
808 | return (-1); | |
809 | } | |
810 | *bp++ = 0; | |
811 | } | |
812 | ||
813 | if ((bp - dst) > NS_MAXCDNAME) | |
814 | { /* src too big */ | |
815 | errno = EMSGSIZE; | |
816 | return (-1); | |
817 | } | |
818 | ||
819 | return (0); | |
820 | } | |
821 | ||
822 | /* | |
823 | * irc_ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr) | |
824 | * Pack domain name 'domain' into 'comp_dn'. | |
825 | * return: | |
826 | * Size of the compressed name, or -1. | |
827 | * notes: | |
828 | * 'dnptrs' is an array of pointers to previous compressed names. | |
829 | * dnptrs[0] is a pointer to the beginning of the message. The array | |
830 | * ends with NULL. | |
831 | * 'lastdnptr' is a pointer to the end of the array pointed to | |
832 | * by 'dnptrs'. | |
833 | * Side effects: | |
834 | * The list of pointers in dnptrs is updated for labels inserted into | |
835 | * the message as we compress the name. If 'dnptr' is NULL, we don't | |
836 | * try to compress names. If 'lastdnptr' is NULL, we don't update the | |
837 | * list. | |
838 | */ | |
839 | static int | |
840 | irc_ns_name_pack(const unsigned char *src, unsigned char *dst, int dstsiz, | |
841 | const unsigned char **dnptrs, const unsigned char **lastdnptr) | |
842 | { | |
843 | unsigned char *dstp; | |
844 | const unsigned char **cpp, **lpp, *eob, *msg; | |
845 | const unsigned char *srcp; | |
846 | int n, l, first = 1; | |
847 | ||
848 | srcp = src; | |
849 | dstp = dst; | |
850 | eob = dstp + dstsiz; | |
851 | lpp = cpp = NULL; | |
852 | if (dnptrs != NULL) { | |
853 | if ((msg = *dnptrs++) != NULL) { | |
854 | for (cpp = dnptrs; *cpp != NULL; cpp++) | |
855 | (void)NULL; | |
856 | lpp = cpp; /* end of list to search */ | |
857 | } | |
858 | } else | |
859 | msg = NULL; | |
860 | ||
861 | /* make sure the domain we are about to add is legal */ | |
862 | l = 0; | |
863 | do { | |
864 | int l0; | |
865 | ||
866 | n = *srcp; | |
867 | if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { | |
868 | errno = EMSGSIZE; | |
869 | return (-1); | |
870 | } | |
871 | if ((l0 = labellen(srcp)) < 0) { | |
872 | errno = EINVAL; | |
873 | return(-1); | |
874 | } | |
875 | l += l0 + 1; | |
876 | if (l > NS_MAXCDNAME) { | |
877 | errno = EMSGSIZE; | |
878 | return (-1); | |
879 | } | |
880 | srcp += l0 + 1; | |
881 | } while (n != 0); | |
882 | ||
883 | /* from here on we need to reset compression pointer array on error */ | |
884 | srcp = src; | |
885 | do { | |
886 | /* Look to see if we can use pointers. */ | |
887 | n = *srcp; | |
888 | if (n != 0 && msg != NULL) { | |
889 | l = irc_dn_find(srcp, msg, (const unsigned char * const *)dnptrs, | |
890 | (const unsigned char * const *)lpp); | |
891 | if (l >= 0) { | |
892 | if (dstp + 1 >= eob) { | |
893 | goto cleanup; | |
894 | } | |
895 | *dstp++ = (l >> 8) | NS_CMPRSFLGS; | |
896 | *dstp++ = l % 256; | |
897 | return (dstp - dst); | |
898 | } | |
899 | /* Not found, save it. */ | |
900 | if (lastdnptr != NULL && cpp < lastdnptr - 1 && | |
901 | (dstp - msg) < 0x4000 && first) { | |
902 | *cpp++ = dstp; | |
903 | *cpp = NULL; | |
904 | first = 0; | |
905 | } | |
906 | } | |
907 | /* copy label to buffer */ | |
908 | if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { | |
909 | /* Should not happen. */ | |
910 | goto cleanup; | |
911 | } | |
912 | n = labellen(srcp); | |
913 | if (dstp + 1 + n >= eob) { | |
914 | goto cleanup; | |
915 | } | |
916 | memcpy(dstp, srcp, n + 1); | |
917 | srcp += n + 1; | |
918 | dstp += n + 1; | |
919 | } while (n != 0); | |
920 | ||
921 | if (dstp > eob) { | |
922 | cleanup: | |
923 | if (msg != NULL) | |
924 | *lpp = NULL; | |
925 | errno = EMSGSIZE; | |
926 | return (-1); | |
927 | } | |
928 | return(dstp - dst); | |
929 | } | |
930 | ||
931 | static int | |
932 | irc_ns_name_compress(const char *src, unsigned char *dst, size_t dstsiz, | |
933 | const unsigned char **dnptrs, const unsigned char **lastdnptr) | |
934 | { | |
935 | unsigned char tmp[NS_MAXCDNAME]; | |
936 | ||
937 | if (irc_ns_name_pton(src, tmp, sizeof tmp) == -1) | |
938 | return(-1); | |
939 | return(irc_ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr)); | |
940 | } | |
941 | ||
942 | static int | |
943 | irc_encode_bitsring(const char **bp, const char *end, unsigned char **labelp, | |
944 | unsigned char **dst, const char *eom) | |
945 | { | |
946 | int afterslash = 0; | |
947 | const char *cp = *bp; | |
948 | char *tp, c; | |
949 | const char *beg_blen; | |
950 | char *end_blen = NULL; | |
951 | int value = 0, count = 0, tbcount = 0, blen = 0; | |
952 | ||
953 | beg_blen = end_blen = NULL; | |
954 | ||
955 | /* a bitstring must contain at least 2 characters */ | |
956 | if (end - cp < 2) | |
957 | return(EINVAL); | |
958 | ||
959 | /* XXX: currently, only hex strings are supported */ | |
960 | if (*cp++ != 'x') | |
961 | return(EINVAL); | |
962 | if (!isxdigit((*cp) & 0xff)) /* reject '\[x/BLEN]' */ | |
963 | return(EINVAL); | |
964 | ||
965 | for (tp = (char*)(dst + 1); cp < end && tp < eom; cp++) { | |
966 | switch((c = *cp)) { | |
967 | case ']': /* end of the bitstring */ | |
968 | if (afterslash) { | |
969 | if (beg_blen == NULL) | |
970 | return(EINVAL); | |
971 | blen = (int)strtol(beg_blen, &end_blen, 10); | |
972 | if (*end_blen != ']') | |
973 | return(EINVAL); | |
974 | } | |
975 | if (count) | |
976 | *tp++ = ((value << 4) & 0xff); | |
977 | cp++; /* skip ']' */ | |
978 | goto done; | |
979 | case '/': | |
980 | afterslash = 1; | |
981 | break; | |
982 | default: | |
983 | if (afterslash) { | |
984 | if (!isdigit(c&0xff)) | |
985 | return(EINVAL); | |
986 | if (beg_blen == NULL) { | |
987 | ||
988 | if (c == '0') { | |
989 | /* blen never begings with 0 */ | |
990 | return(EINVAL); | |
991 | } | |
992 | beg_blen = cp; | |
993 | } | |
994 | } else { | |
995 | if (!isxdigit(c&0xff)) | |
996 | return(EINVAL); | |
997 | value <<= 4; | |
998 | value += digitvalue[(int)c]; | |
999 | count += 4; | |
1000 | tbcount += 4; | |
1001 | if (tbcount > 256) | |
1002 | return(EINVAL); | |
1003 | if (count == 8) { | |
1004 | *tp++ = value; | |
1005 | count = 0; | |
1006 | } | |
1007 | } | |
1008 | break; | |
1009 | } | |
1010 | } | |
1011 | done: | |
1012 | if (cp >= end || tp >= eom) | |
1013 | return(EMSGSIZE); | |
1014 | ||
1015 | /* | |
1016 | * bit length validation: | |
1017 | * If a <length> is present, the number of digits in the <bit-data> | |
1018 | * MUST be just sufficient to contain the number of bits specified | |
1019 | * by the <length>. If there are insignificant bits in a final | |
1020 | * hexadecimal or octal digit, they MUST be zero. | |
1021 | * RFC 2673, Section 3.2. | |
1022 | */ | |
1023 | if (blen > 0) { | |
1024 | int traillen; | |
1025 | ||
1026 | if (((blen + 3) & ~3) != tbcount) | |
1027 | return(EINVAL); | |
1028 | traillen = tbcount - blen; /* between 0 and 3 */ | |
1029 | if (((value << (8 - traillen)) & 0xff) != 0) | |
1030 | return(EINVAL); | |
1031 | } | |
1032 | else | |
1033 | blen = tbcount; | |
1034 | if (blen == 256) | |
1035 | blen = 0; | |
1036 | ||
1037 | /* encode the type and the significant bit fields */ | |
1038 | **labelp = DNS_LABELTYPE_BITSTRING; | |
1039 | **dst = blen; | |
1040 | ||
1041 | *bp = cp; | |
1042 | *dst = (unsigned char*)tp; | |
1043 | ||
1044 | return(0); | |
1045 | } | |
1046 | ||
1047 | /* | |
1048 | * dn_find(domain, msg, dnptrs, lastdnptr) | |
1049 | * Search for the counted-label name in an array of compressed names. | |
1050 | * return: | |
1051 | * offset from msg if found, or -1. | |
1052 | * notes: | |
1053 | * dnptrs is the pointer to the first name on the list, | |
1054 | * not the pointer to the start of the message. | |
1055 | */ | |
1056 | static int | |
1057 | irc_dn_find(const unsigned char *domain, const unsigned char *msg, | |
1058 | const unsigned char * const *dnptrs, | |
1059 | const unsigned char * const *lastdnptr) | |
1060 | { | |
1061 | const unsigned char *dn, *cp, *sp; | |
1062 | const unsigned char * const *cpp; | |
1063 | unsigned int n; | |
1064 | ||
1065 | for (cpp = dnptrs; cpp < lastdnptr; cpp++) | |
1066 | { | |
1067 | sp = *cpp; | |
1068 | /* | |
1069 | * terminate search on: | |
1070 | * root label | |
1071 | * compression pointer | |
1072 | * unusable offset | |
1073 | */ | |
1074 | while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 && | |
1075 | (sp - msg) < 0x4000) { | |
1076 | dn = domain; | |
1077 | cp = sp; | |
1078 | while ((n = *cp++) != 0) { | |
1079 | /* | |
1080 | * check for indirection | |
1081 | */ | |
1082 | switch (n & NS_CMPRSFLGS) { | |
1083 | case 0: /* normal case, n == len */ | |
1084 | n = labellen(cp - 1); /* XXX */ | |
1085 | ||
1086 | if (n != *dn++) | |
1087 | goto next; | |
1088 | ||
1089 | for ((void)NULL; n > 0; n--) | |
1090 | if (mklower(*dn++) != | |
1091 | mklower(*cp++)) | |
1092 | goto next; | |
1093 | /* Is next root for both ? */ | |
1094 | if (*dn == '\0' && *cp == '\0') | |
1095 | return (sp - msg); | |
1096 | if (*dn) | |
1097 | continue; | |
1098 | goto next; | |
1099 | case NS_CMPRSFLGS: /* indirection */ | |
1100 | cp = msg + (((n & 0x3f) << 8) | *cp); | |
1101 | break; | |
1102 | ||
1103 | default: /* illegal type */ | |
1104 | errno = EMSGSIZE; | |
1105 | return (-1); | |
1106 | } | |
1107 | } | |
1108 | next: ; | |
1109 | sp += *sp + 1; | |
1110 | } | |
1111 | } | |
1112 | errno = ENOENT; | |
1113 | return (-1); | |
1114 | } | |
1115 | ||
1116 | /* | |
1117 | * * Thinking in noninternationalized USASCII (per the DNS spec), | |
1118 | * * convert this character to lower case if it's upper case. | |
1119 | * */ | |
1120 | static int | |
1121 | mklower(int ch) | |
1122 | { | |
1123 | if (ch >= 0x41 && ch <= 0x5A) | |
1124 | return(ch + 0x20); | |
1125 | ||
1126 | return(ch); | |
1127 | } | |
1128 | ||
1129 | /* From resolv/mkquery.c */ | |
1130 | ||
1131 | /* | |
1132 | * Form all types of queries. | |
1133 | * Returns the size of the result or -1. | |
1134 | */ | |
1135 | int | |
1136 | irc_res_mkquery( | |
1137 | const char *dname, /* domain name */ | |
1138 | int class, int type, /* class and type of query */ | |
1139 | unsigned char *buf, /* buffer to put query */ | |
1140 | int buflen) /* size of buffer */ | |
1141 | { | |
1142 | HEADER *hp; | |
1143 | unsigned char *cp; | |
1144 | int n; | |
1145 | const unsigned char *dnptrs[20], **dpp, **lastdnptr; | |
1146 | ||
1147 | /* | |
1148 | * Initialize header fields. | |
1149 | */ | |
1150 | if ((buf == NULL) || (buflen < HFIXEDSZ)) | |
1151 | return (-1); | |
1152 | memset(buf, 0, HFIXEDSZ); | |
1153 | hp = (HEADER *)(void *)buf; | |
1154 | ||
1155 | hp->id = 0; | |
1156 | hp->opcode = QUERY; | |
1157 | hp->rd = 1; /* recurse */ | |
1158 | hp->rcode = NO_ERRORS; | |
1159 | cp = buf + HFIXEDSZ; | |
1160 | buflen -= HFIXEDSZ; | |
1161 | dpp = dnptrs; | |
1162 | *dpp++ = buf; | |
1163 | *dpp++ = NULL; | |
1164 | lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0]; | |
1165 | ||
1166 | if ((buflen -= QFIXEDSZ) < 0) | |
1167 | return (-1); | |
1168 | if ((n = irc_dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0) | |
1169 | return (-1); | |
1170 | ||
1171 | cp += n; | |
1172 | buflen -= n; | |
1173 | IRC_NS_PUT16(type, cp); | |
1174 | IRC_NS_PUT16(class, cp); | |
1175 | hp->qdcount = htons(1); | |
1176 | ||
1177 | return (cp - buf); | |
1178 | } |