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