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