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