summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/res_comp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/res_comp.c')
-rw-r--r--src/lib/libc/net/res_comp.c227
1 files changed, 183 insertions, 44 deletions
diff --git a/src/lib/libc/net/res_comp.c b/src/lib/libc/net/res_comp.c
index 9d7bbecdda..25e196b2cf 100644
--- a/src/lib/libc/net/res_comp.c
+++ b/src/lib/libc/net/res_comp.c
@@ -1,9 +1,11 @@
1/* $NetBSD: res_comp.c,v 1.6 1995/02/25 06:20:55 cgd Exp $ */ 1/* $OpenBSD: res_comp.c,v 1.10 2002/02/19 19:39:36 millert Exp $ */
2 2
3/*- 3/*
4 * ++Copyright++ 1985, 1993
5 * -
4 * Copyright (c) 1985, 1993 6 * Copyright (c) 1985, 1993
5 * The Regents of the University of California. All rights reserved. 7 * The Regents of the University of California. All rights reserved.
6 * 8 *
7 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
9 * are met: 11 * are met:
@@ -14,12 +16,12 @@
14 * documentation and/or other materials provided with the distribution. 16 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 17 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement: 18 * must display the following acknowledgement:
17 * This product includes software developed by the University of 19 * This product includes software developed by the University of
18 * California, Berkeley and its contributors. 20 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors 21 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software 22 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission. 23 * without specific prior written permission.
22 * 24 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -56,19 +58,25 @@
56#if defined(LIBC_SCCS) && !defined(lint) 58#if defined(LIBC_SCCS) && !defined(lint)
57#if 0 59#if 0
58static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; 60static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
59static char rcsid[] = "$Id: res_comp.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; 61static char rcsid[] = "$From: res_comp.c,v 8.11 1996/12/02 09:17:22 vixie Exp $";
60#else 62#else
61static char rcsid[] = "$NetBSD: res_comp.c,v 1.6 1995/02/25 06:20:55 cgd Exp $"; 63static char rcsid[] = "$OpenBSD: res_comp.c,v 1.10 2002/02/19 19:39:36 millert Exp $";
62#endif 64#endif
63#endif /* LIBC_SCCS and not lint */ 65#endif /* LIBC_SCCS and not lint */
64 66
67#include <sys/types.h>
65#include <sys/param.h> 68#include <sys/param.h>
66#include <arpa/nameser.h>
67#include <netinet/in.h> 69#include <netinet/in.h>
68#include <resolv.h> 70#include <arpa/nameser.h>
71
69#include <stdio.h> 72#include <stdio.h>
73#include <resolv.h>
74#include <ctype.h>
75
76#include <unistd.h>
77#include <string.h>
70 78
71static int dn_find(); 79static int dn_find(u_char *, u_char *, u_char **, u_char **);
72 80
73/* 81/*
74 * Expand compressed domain name 'comp_dn' to full domain name. 82 * Expand compressed domain name 'comp_dn' to full domain name.
@@ -77,23 +85,27 @@ static int dn_find();
77 * 'exp_dn' is a pointer to a buffer of size 'length' for the result. 85 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
78 * Return size of compressed name or -1 if there was an error. 86 * Return size of compressed name or -1 if there was an error.
79 */ 87 */
88int
80dn_expand(msg, eomorig, comp_dn, exp_dn, length) 89dn_expand(msg, eomorig, comp_dn, exp_dn, length)
81 const u_char *msg, *eomorig, *comp_dn; 90 const u_char *msg, *eomorig, *comp_dn;
82 u_char *exp_dn; 91 char *exp_dn;
83 int length; 92 int length;
84{ 93{
85 register u_char *cp, *dn; 94 register const u_char *cp;
95 register char *dn;
86 register int n, c; 96 register int n, c;
87 u_char *eom; 97 char *eom;
88 int len = -1, checked = 0; 98 int len = -1, checked = 0;
89 99
90 dn = exp_dn; 100 dn = exp_dn;
91 cp = (u_char *)comp_dn; 101 cp = comp_dn;
102 if (length > MAXHOSTNAMELEN-1)
103 length = MAXHOSTNAMELEN-1;
92 eom = exp_dn + length; 104 eom = exp_dn + length;
93 /* 105 /*
94 * fetch next label in domain name 106 * fetch next label in domain name
95 */ 107 */
96 while (n = *cp++) { 108 while ((n = *cp++)) {
97 /* 109 /*
98 * Check for indirection 110 * Check for indirection
99 */ 111 */
@@ -108,23 +120,23 @@ dn_expand(msg, eomorig, comp_dn, exp_dn, length)
108 return (-1); 120 return (-1);
109 checked += n + 1; 121 checked += n + 1;
110 while (--n >= 0) { 122 while (--n >= 0) {
111 if ((c = *cp++) == '.') { 123 if (((c = *cp++) == '.') || (c == '\\')) {
112 if (dn + n + 2 >= eom) 124 if (dn + n + 2 >= eom)
113 return (-1); 125 return (-1);
114 *dn++ = '\\'; 126 *dn++ = '\\';
115 } 127 }
116 *dn++ = c; 128 *dn++ = c;
117 if (cp >= eomorig) /* out of range */ 129 if (cp >= eomorig) /* out of range */
118 return(-1); 130 return (-1);
119 } 131 }
120 break; 132 break;
121 133
122 case INDIR_MASK: 134 case INDIR_MASK:
123 if (len < 0) 135 if (len < 0)
124 len = cp - comp_dn + 1; 136 len = cp - comp_dn + 1;
125 cp = (u_char *)msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 137 cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
126 if (cp < msg || cp >= eomorig) /* out of range */ 138 if (cp < msg || cp >= eomorig) /* out of range */
127 return(-1); 139 return (-1);
128 checked += 2; 140 checked += 2;
129 /* 141 /*
130 * Check for loops in the compressed name; 142 * Check for loops in the compressed name;
@@ -157,8 +169,9 @@ dn_expand(msg, eomorig, comp_dn, exp_dn, length)
157 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' 169 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
158 * is NULL, we don't update the list. 170 * is NULL, we don't update the list.
159 */ 171 */
172int
160dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) 173dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
161 const u_char *exp_dn; 174 const char *exp_dn;
162 u_char *comp_dn, **dnptrs, **lastdnptr; 175 u_char *comp_dn, **dnptrs, **lastdnptr;
163 int length; 176 int length;
164{ 177{
@@ -170,6 +183,7 @@ dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
170 dn = (u_char *)exp_dn; 183 dn = (u_char *)exp_dn;
171 cp = comp_dn; 184 cp = comp_dn;
172 eob = cp + length; 185 eob = cp + length;
186 lpp = cpp = NULL;
173 if (dnptrs != NULL) { 187 if (dnptrs != NULL) {
174 if ((msg = *dnptrs++) != NULL) { 188 if ((msg = *dnptrs++) != NULL) {
175 for (cpp = dnptrs; *cpp != NULL; cpp++) 189 for (cpp = dnptrs; *cpp != NULL; cpp++)
@@ -235,13 +249,14 @@ dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
235/* 249/*
236 * Skip over a compressed domain name. Return the size or -1. 250 * Skip over a compressed domain name. Return the size or -1.
237 */ 251 */
252int
238__dn_skipname(comp_dn, eom) 253__dn_skipname(comp_dn, eom)
239 const u_char *comp_dn, *eom; 254 const u_char *comp_dn, *eom;
240{ 255{
241 register u_char *cp; 256 register const u_char *cp;
242 register int n; 257 register int n;
243 258
244 cp = (u_char *)comp_dn; 259 cp = comp_dn;
245 while (cp < eom && (n = *cp++)) { 260 while (cp < eom && (n = *cp++)) {
246 /* 261 /*
247 * check for indirection 262 * check for indirection
@@ -259,10 +274,19 @@ __dn_skipname(comp_dn, eom)
259 break; 274 break;
260 } 275 }
261 if (cp > eom) 276 if (cp > eom)
262 return -1; 277 return (-1);
263 return (cp - comp_dn); 278 return (cp - comp_dn);
264} 279}
265 280
281static int
282mklower(ch)
283 register int ch;
284{
285 if (isascii(ch) && isupper(ch))
286 return (tolower(ch));
287 return (ch);
288}
289
266/* 290/*
267 * Search for expanded name from a list of previously compressed names. 291 * Search for expanded name from a list of previously compressed names.
268 * Return the offset from msg if found or -1. 292 * Return the offset from msg if found or -1.
@@ -281,7 +305,7 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
281 for (cpp = dnptrs; cpp < lastdnptr; cpp++) { 305 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
282 dn = exp_dn; 306 dn = exp_dn;
283 sp = cp = *cpp; 307 sp = cp = *cpp;
284 while (n = *cp++) { 308 while ((n = *cp++)) {
285 /* 309 /*
286 * check for indirection 310 * check for indirection
287 */ 311 */
@@ -292,7 +316,7 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
292 goto next; 316 goto next;
293 if (*dn == '\\') 317 if (*dn == '\\')
294 dn++; 318 dn++;
295 if (*dn++ != *cp++) 319 if (mklower(*dn++) != mklower(*cp++))
296 goto next; 320 goto next;
297 } 321 }
298 if ((n = *dn++) == '\0' && *cp == '\0') 322 if ((n = *dn++) == '\0' && *cp == '\0')
@@ -301,11 +325,12 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
301 continue; 325 continue;
302 goto next; 326 goto next;
303 327
304 default: /* illegal type */
305 return (-1);
306
307 case INDIR_MASK: /* indirection */ 328 case INDIR_MASK: /* indirection */
308 cp = msg + (((n & 0x3f) << 8) | *cp); 329 cp = msg + (((n & 0x3f) << 8) | *cp);
330 break;
331
332 default: /* illegal type */
333 return (-1);
309 } 334 }
310 } 335 }
311 if (*dn == '\0') 336 if (*dn == '\0')
@@ -316,16 +341,124 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
316} 341}
317 342
318/* 343/*
319 * Routines to insert/extract short/long's. Must account for byte 344 * Verify that a domain name uses an acceptable character set.
320 * order and non-alignment problems. This code at least has the 345 */
321 * advantage of being portable. 346
322 * 347/*
323 * used by sendmail. 348 * Note the conspicuous absence of ctype macros in these definitions. On
349 * non-ASCII hosts, we can't depend on string literals or ctype macros to
350 * tell us anything about network-format data. The rest of the BIND system
351 * is not careful about this, but for some reason, we're doing it right here.
324 */ 352 */
353#define PERIOD 0x2e
354#define hyphenchar(c) ((c) == 0x2d)
355#define bslashchar(c) ((c) == 0x5c)
356#define periodchar(c) ((c) == PERIOD)
357#define asterchar(c) ((c) == 0x2a)
358#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
359 || ((c) >= 0x61 && (c) <= 0x7a))
360#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
361
362#define borderchar(c) (alphachar(c) || digitchar(c))
363#define middlechar(c) (borderchar(c) || hyphenchar(c))
364#define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
365
366int
367res_hnok(dn)
368 const char *dn;
369{
370 int pch = PERIOD, ch = *dn++;
325 371
326u_short 372 while (ch != '\0') {
373 int nch = *dn++;
374
375 if (periodchar(ch)) {
376 ;
377 } else if (periodchar(pch)) {
378 if (!borderchar(ch))
379 return (0);
380 } else if (periodchar(nch) || nch == '\0') {
381 if (!borderchar(ch))
382 return (0);
383 } else {
384 if (!middlechar(ch))
385 return (0);
386 }
387 pch = ch, ch = nch;
388 }
389 return (1);
390}
391
392/*
393 * hostname-like (A, MX, WKS) owners can have "*" as their first label
394 * but must otherwise be as a host name.
395 */
396int
397res_ownok(dn)
398 const char *dn;
399{
400 if (asterchar(dn[0])) {
401 if (periodchar(dn[1]))
402 return (res_hnok(dn+2));
403 if (dn[1] == '\0')
404 return (1);
405 }
406 return (res_hnok(dn));
407}
408
409/*
410 * SOA RNAMEs and RP RNAMEs can have any printable character in their first
411 * label, but the rest of the name has to look like a host name.
412 */
413int
414res_mailok(dn)
415 const char *dn;
416{
417 int ch, escaped = 0;
418
419 /* "." is a valid missing representation */
420 if (*dn == '\0')
421 return(1);
422
423 /* otherwise <label>.<hostname> */
424 while ((ch = *dn++) != '\0') {
425 if (!domainchar(ch))
426 return (0);
427 if (!escaped && periodchar(ch))
428 break;
429 if (escaped)
430 escaped = 0;
431 else if (bslashchar(ch))
432 escaped = 1;
433 }
434 if (periodchar(ch))
435 return (res_hnok(dn));
436 return(0);
437}
438
439/*
440 * This function is quite liberal, since RFC 1034's character sets are only
441 * recommendations.
442 */
443int
444res_dnok(dn)
445 const char *dn;
446{
447 int ch;
448
449 while ((ch = *dn++) != '\0')
450 if (!domainchar(ch))
451 return (0);
452 return (1);
453}
454
455/*
456 * Routines to insert/extract short/long's.
457 */
458
459u_int16_t
327_getshort(msgp) 460_getshort(msgp)
328 register u_char *msgp; 461 register const u_char *msgp;
329{ 462{
330 register u_int16_t u; 463 register u_int16_t u;
331 464
@@ -333,9 +466,21 @@ _getshort(msgp)
333 return (u); 466 return (u);
334} 467}
335 468
469#ifdef NeXT
470/*
471 * nExt machines have some funky library conventions, which we must maintain.
472 */
473u_int16_t
474res_getshort(msgp)
475 register const u_char *msgp;
476{
477 return (_getshort(msgp));
478}
479#endif
480
336u_int32_t 481u_int32_t
337_getlong(msgp) 482_getlong(msgp)
338 register u_char *msgp; 483 register const u_char *msgp;
339{ 484{
340 register u_int32_t u; 485 register u_int32_t u;
341 486
@@ -344,13 +489,7 @@ _getlong(msgp)
344} 489}
345 490
346void 491void
347#if defined(__STDC__) || defined(__cplusplus)
348__putshort(register u_int16_t s, register u_char *msgp) 492__putshort(register u_int16_t s, register u_char *msgp)
349#else
350__putshort(s, msgp)
351 register u_int16_t s;
352 register u_char *msgp;
353#endif
354{ 493{
355 PUTSHORT(s, msgp); 494 PUTSHORT(s, msgp);
356} 495}