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