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