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