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