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