1 /* $OpenBSD: res_comp.c,v 1.11 2003/06/02 20:18:36 millert Exp $ */ 2 3 /* 4 * ++Copyright++ 1985, 1993 5 * - 6 * Copyright (c) 1985, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. 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 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 34 * 35 * Permission to use, copy, modify, and distribute this software for any 36 * purpose with or without fee is hereby granted, provided that the above 37 * copyright notice and this permission notice appear in all copies, and that 38 * the name of Digital Equipment Corporation not be used in advertising or 39 * publicity pertaining to distribution of the document or software without 40 * specific, written prior permission. 41 * 42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 49 * SOFTWARE. 50 * - 51 * --Copyright-- 52 */ 53 54 #if defined(LIBC_SCCS) && !defined(lint) 55 #if 0 56 static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; 57 static char rcsid[] = "$From: res_comp.c,v 8.11 1996/12/02 09:17:22 vixie Exp $"; 58 #else 59 static char rcsid[] = "$OpenBSD: res_comp.c,v 1.11 2003/06/02 20:18:36 millert Exp $"; 60 #endif 61 #endif /* LIBC_SCCS and not lint */ 62 63 #include <sys/types.h> 64 #include <sys/param.h> 65 #include <netinet/in.h> 66 #include <arpa/nameser.h> 67 68 #include <stdio.h> 69 #include <resolv.h> 70 #include <ctype.h> 71 72 #include <unistd.h> 73 #include <string.h> 74 75 static int dn_find(u_char *, u_char *, u_char **, u_char **); 76 77 /* 78 * Expand compressed domain name 'comp_dn' to full domain name. 79 * 'msg' is a pointer to the begining of the message, 80 * 'eomorig' points to the first location after the message, 81 * 'exp_dn' is a pointer to a buffer of size 'length' for the result. 82 * Return size of compressed name or -1 if there was an error. 83 */ 84 int 85 dn_expand(msg, eomorig, comp_dn, exp_dn, length) 86 const u_char *msg, *eomorig, *comp_dn; 87 char *exp_dn; 88 int length; 89 { 90 register const u_char *cp; 91 register char *dn; 92 register int n, c; 93 char *eom; 94 int len = -1, checked = 0; 95 96 dn = exp_dn; 97 cp = comp_dn; 98 if (length > MAXHOSTNAMELEN-1) 99 length = MAXHOSTNAMELEN-1; 100 eom = exp_dn + length; 101 /* 102 * fetch next label in domain name 103 */ 104 while ((n = *cp++)) { 105 /* 106 * Check for indirection 107 */ 108 switch (n & INDIR_MASK) { 109 case 0: 110 if (dn != exp_dn) { 111 if (dn >= eom) 112 return (-1); 113 *dn++ = '.'; 114 } 115 if (dn+n >= eom) 116 return (-1); 117 checked += n + 1; 118 while (--n >= 0) { 119 if (((c = *cp++) == '.') || (c == '\\')) { 120 if (dn + n + 2 >= eom) 121 return (-1); 122 *dn++ = '\\'; 123 } 124 *dn++ = c; 125 if (cp >= eomorig) /* out of range */ 126 return (-1); 127 } 128 break; 129 130 case INDIR_MASK: 131 if (len < 0) 132 len = cp - comp_dn + 1; 133 cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 134 if (cp < msg || cp >= eomorig) /* out of range */ 135 return (-1); 136 checked += 2; 137 /* 138 * Check for loops in the compressed name; 139 * if we've looked at the whole message, 140 * there must be a loop. 141 */ 142 if (checked >= eomorig - msg) 143 return (-1); 144 break; 145 146 default: 147 return (-1); /* flag error */ 148 } 149 } 150 *dn = '\0'; 151 if (len < 0) 152 len = cp - comp_dn; 153 return (len); 154 } 155 156 /* 157 * Compress domain name 'exp_dn' into 'comp_dn'. 158 * Return the size of the compressed name or -1. 159 * 'length' is the size of the array pointed to by 'comp_dn'. 160 * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] 161 * is a pointer to the beginning of the message. The list ends with NULL. 162 * 'lastdnptr' is a pointer to the end of the arrary pointed to 163 * by 'dnptrs'. Side effect is to update the list of pointers for 164 * labels inserted into the message as we compress the name. 165 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' 166 * is NULL, we don't update the list. 167 */ 168 int 169 dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) 170 const char *exp_dn; 171 u_char *comp_dn, **dnptrs, **lastdnptr; 172 int length; 173 { 174 register u_char *cp, *dn; 175 register int c, l; 176 u_char **cpp, **lpp, *sp, *eob; 177 u_char *msg; 178 179 dn = (u_char *)exp_dn; 180 cp = comp_dn; 181 eob = cp + length; 182 lpp = cpp = NULL; 183 if (dnptrs != NULL) { 184 if ((msg = *dnptrs++) != NULL) { 185 for (cpp = dnptrs; *cpp != NULL; cpp++) 186 ; 187 lpp = cpp; /* end of list to search */ 188 } 189 } else 190 msg = NULL; 191 for (c = *dn++; c != '\0'; ) { 192 /* look to see if we can use pointers */ 193 if (msg != NULL) { 194 if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { 195 if (cp+1 >= eob) 196 return (-1); 197 *cp++ = (l >> 8) | INDIR_MASK; 198 *cp++ = l % 256; 199 return (cp - comp_dn); 200 } 201 /* not found, save it */ 202 if (lastdnptr != NULL && cpp < lastdnptr-1) { 203 *cpp++ = cp; 204 *cpp = NULL; 205 } 206 } 207 sp = cp++; /* save ptr to length byte */ 208 do { 209 if (c == '.') { 210 c = *dn++; 211 break; 212 } 213 if (c == '\\') { 214 if ((c = *dn++) == '\0') 215 break; 216 } 217 if (cp >= eob) { 218 if (msg != NULL) 219 *lpp = NULL; 220 return (-1); 221 } 222 *cp++ = c; 223 } while ((c = *dn++) != '\0'); 224 /* catch trailing '.'s but not '..' */ 225 if ((l = cp - sp - 1) == 0 && c == '\0') { 226 cp--; 227 break; 228 } 229 if (l <= 0 || l > MAXLABEL) { 230 if (msg != NULL) 231 *lpp = NULL; 232 return (-1); 233 } 234 *sp = l; 235 } 236 if (cp >= eob) { 237 if (msg != NULL) 238 *lpp = NULL; 239 return (-1); 240 } 241 *cp++ = '\0'; 242 return (cp - comp_dn); 243 } 244 245 /* 246 * Skip over a compressed domain name. Return the size or -1. 247 */ 248 int 249 __dn_skipname(comp_dn, eom) 250 const u_char *comp_dn, *eom; 251 { 252 register const u_char *cp; 253 register int n; 254 255 cp = comp_dn; 256 while (cp < eom && (n = *cp++)) { 257 /* 258 * check for indirection 259 */ 260 switch (n & INDIR_MASK) { 261 case 0: /* normal case, n == len */ 262 cp += n; 263 continue; 264 case INDIR_MASK: /* indirection */ 265 cp++; 266 break; 267 default: /* illegal type */ 268 return (-1); 269 } 270 break; 271 } 272 if (cp > eom) 273 return (-1); 274 return (cp - comp_dn); 275 } 276 277 static int 278 mklower(ch) 279 register int ch; 280 { 281 if (isascii(ch) && isupper(ch)) 282 return (tolower(ch)); 283 return (ch); 284 } 285 286 /* 287 * Search for expanded name from a list of previously compressed names. 288 * Return the offset from msg if found or -1. 289 * dnptrs is the pointer to the first name on the list, 290 * not the pointer to the start of the message. 291 */ 292 static int 293 dn_find(exp_dn, msg, dnptrs, lastdnptr) 294 u_char *exp_dn, *msg; 295 u_char **dnptrs, **lastdnptr; 296 { 297 register u_char *dn, *cp, **cpp; 298 register int n; 299 u_char *sp; 300 301 for (cpp = dnptrs; cpp < lastdnptr; cpp++) { 302 dn = exp_dn; 303 sp = cp = *cpp; 304 while ((n = *cp++)) { 305 /* 306 * check for indirection 307 */ 308 switch (n & INDIR_MASK) { 309 case 0: /* normal case, n == len */ 310 while (--n >= 0) { 311 if (*dn == '.') 312 goto next; 313 if (*dn == '\\') 314 dn++; 315 if (mklower(*dn++) != mklower(*cp++)) 316 goto next; 317 } 318 if ((n = *dn++) == '\0' && *cp == '\0') 319 return (sp - msg); 320 if (n == '.') 321 continue; 322 goto next; 323 324 case INDIR_MASK: /* indirection */ 325 cp = msg + (((n & 0x3f) << 8) | *cp); 326 break; 327 328 default: /* illegal type */ 329 return (-1); 330 } 331 } 332 if (*dn == '\0') 333 return (sp - msg); 334 next: ; 335 } 336 return (-1); 337 } 338 339 /* 340 * Verify that a domain name uses an acceptable character set. 341 */ 342 343 /* 344 * Note the conspicuous absence of ctype macros in these definitions. On 345 * non-ASCII hosts, we can't depend on string literals or ctype macros to 346 * tell us anything about network-format data. The rest of the BIND system 347 * is not careful about this, but for some reason, we're doing it right here. 348 */ 349 #define PERIOD 0x2e 350 #define hyphenchar(c) ((c) == 0x2d) 351 #define bslashchar(c) ((c) == 0x5c) 352 #define periodchar(c) ((c) == PERIOD) 353 #define asterchar(c) ((c) == 0x2a) 354 #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \ 355 || ((c) >= 0x61 && (c) <= 0x7a)) 356 #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39) 357 358 #define borderchar(c) (alphachar(c) || digitchar(c)) 359 #define middlechar(c) (borderchar(c) || hyphenchar(c)) 360 #define domainchar(c) ((c) > 0x20 && (c) < 0x7f) 361 362 int 363 res_hnok(dn) 364 const char *dn; 365 { 366 int pch = PERIOD, ch = *dn++; 367 368 while (ch != '\0') { 369 int nch = *dn++; 370 371 if (periodchar(ch)) { 372 ; 373 } else if (periodchar(pch)) { 374 if (!borderchar(ch)) 375 return (0); 376 } else if (periodchar(nch) || nch == '\0') { 377 if (!borderchar(ch)) 378 return (0); 379 } else { 380 if (!middlechar(ch)) 381 return (0); 382 } 383 pch = ch, ch = nch; 384 } 385 return (1); 386 } 387 388 /* 389 * hostname-like (A, MX, WKS) owners can have "*" as their first label 390 * but must otherwise be as a host name. 391 */ 392 int 393 res_ownok(dn) 394 const char *dn; 395 { 396 if (asterchar(dn[0])) { 397 if (periodchar(dn[1])) 398 return (res_hnok(dn+2)); 399 if (dn[1] == '\0') 400 return (1); 401 } 402 return (res_hnok(dn)); 403 } 404 405 /* 406 * SOA RNAMEs and RP RNAMEs can have any printable character in their first 407 * label, but the rest of the name has to look like a host name. 408 */ 409 int 410 res_mailok(dn) 411 const char *dn; 412 { 413 int ch, escaped = 0; 414 415 /* "." is a valid missing representation */ 416 if (*dn == '\0') 417 return(1); 418 419 /* otherwise <label>.<hostname> */ 420 while ((ch = *dn++) != '\0') { 421 if (!domainchar(ch)) 422 return (0); 423 if (!escaped && periodchar(ch)) 424 break; 425 if (escaped) 426 escaped = 0; 427 else if (bslashchar(ch)) 428 escaped = 1; 429 } 430 if (periodchar(ch)) 431 return (res_hnok(dn)); 432 return(0); 433 } 434 435 /* 436 * This function is quite liberal, since RFC 1034's character sets are only 437 * recommendations. 438 */ 439 int 440 res_dnok(dn) 441 const char *dn; 442 { 443 int ch; 444 445 while ((ch = *dn++) != '\0') 446 if (!domainchar(ch)) 447 return (0); 448 return (1); 449 } 450 451 /* 452 * Routines to insert/extract short/long's. 453 */ 454 455 u_int16_t 456 _getshort(msgp) 457 register const u_char *msgp; 458 { 459 register u_int16_t u; 460 461 GETSHORT(u, msgp); 462 return (u); 463 } 464 465 #ifdef NeXT 466 /* 467 * nExt machines have some funky library conventions, which we must maintain. 468 */ 469 u_int16_t 470 res_getshort(msgp) 471 register const u_char *msgp; 472 { 473 return (_getshort(msgp)); 474 } 475 #endif 476 477 u_int32_t 478 _getlong(msgp) 479 register const u_char *msgp; 480 { 481 register u_int32_t u; 482 483 GETLONG(u, msgp); 484 return (u); 485 } 486 487 void 488 __putshort(register u_int16_t s, register u_char *msgp) 489 { 490 PUTSHORT(s, msgp); 491 } 492 493 void 494 __putlong(l, msgp) 495 register u_int32_t l; 496 register u_char *msgp; 497 { 498 PUTLONG(l, msgp); 499 } 500