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.c229
1 files changed, 182 insertions, 47 deletions
diff --git a/src/lib/libc/net/res_comp.c b/src/lib/libc/net/res_comp.c
index 9d7bbecdda..c10eb52931 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.11 2003/06/02 20:18: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:
@@ -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
@@ -56,19 +54,25 @@
56#if defined(LIBC_SCCS) && !defined(lint) 54#if defined(LIBC_SCCS) && !defined(lint)
57#if 0 55#if 0
58static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; 56static 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 "; 57static char rcsid[] = "$From: res_comp.c,v 8.11 1996/12/02 09:17:22 vixie Exp $";
60#else 58#else
61static char rcsid[] = "$NetBSD: res_comp.c,v 1.6 1995/02/25 06:20:55 cgd Exp $"; 59static char rcsid[] = "$OpenBSD: res_comp.c,v 1.11 2003/06/02 20:18:36 millert Exp $";
62#endif 60#endif
63#endif /* LIBC_SCCS and not lint */ 61#endif /* LIBC_SCCS and not lint */
64 62
63#include <sys/types.h>
65#include <sys/param.h> 64#include <sys/param.h>
66#include <arpa/nameser.h>
67#include <netinet/in.h> 65#include <netinet/in.h>
68#include <resolv.h> 66#include <arpa/nameser.h>
67
69#include <stdio.h> 68#include <stdio.h>
69#include <resolv.h>
70#include <ctype.h>
71
72#include <unistd.h>
73#include <string.h>
70 74
71static int dn_find(); 75static int dn_find(u_char *, u_char *, u_char **, u_char **);
72 76
73/* 77/*
74 * Expand compressed domain name 'comp_dn' to full domain name. 78 * Expand compressed domain name 'comp_dn' to full domain name.
@@ -77,23 +81,27 @@ static int dn_find();
77 * 'exp_dn' is a pointer to a buffer of size 'length' for the result. 81 * '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. 82 * Return size of compressed name or -1 if there was an error.
79 */ 83 */
84int
80dn_expand(msg, eomorig, comp_dn, exp_dn, length) 85dn_expand(msg, eomorig, comp_dn, exp_dn, length)
81 const u_char *msg, *eomorig, *comp_dn; 86 const u_char *msg, *eomorig, *comp_dn;
82 u_char *exp_dn; 87 char *exp_dn;
83 int length; 88 int length;
84{ 89{
85 register u_char *cp, *dn; 90 register const u_char *cp;
91 register char *dn;
86 register int n, c; 92 register int n, c;
87 u_char *eom; 93 char *eom;
88 int len = -1, checked = 0; 94 int len = -1, checked = 0;
89 95
90 dn = exp_dn; 96 dn = exp_dn;
91 cp = (u_char *)comp_dn; 97 cp = comp_dn;
98 if (length > MAXHOSTNAMELEN-1)
99 length = MAXHOSTNAMELEN-1;
92 eom = exp_dn + length; 100 eom = exp_dn + length;
93 /* 101 /*
94 * fetch next label in domain name 102 * fetch next label in domain name
95 */ 103 */
96 while (n = *cp++) { 104 while ((n = *cp++)) {
97 /* 105 /*
98 * Check for indirection 106 * Check for indirection
99 */ 107 */
@@ -108,23 +116,23 @@ dn_expand(msg, eomorig, comp_dn, exp_dn, length)
108 return (-1); 116 return (-1);
109 checked += n + 1; 117 checked += n + 1;
110 while (--n >= 0) { 118 while (--n >= 0) {
111 if ((c = *cp++) == '.') { 119 if (((c = *cp++) == '.') || (c == '\\')) {
112 if (dn + n + 2 >= eom) 120 if (dn + n + 2 >= eom)
113 return (-1); 121 return (-1);
114 *dn++ = '\\'; 122 *dn++ = '\\';
115 } 123 }
116 *dn++ = c; 124 *dn++ = c;
117 if (cp >= eomorig) /* out of range */ 125 if (cp >= eomorig) /* out of range */
118 return(-1); 126 return (-1);
119 } 127 }
120 break; 128 break;
121 129
122 case INDIR_MASK: 130 case INDIR_MASK:
123 if (len < 0) 131 if (len < 0)
124 len = cp - comp_dn + 1; 132 len = cp - comp_dn + 1;
125 cp = (u_char *)msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 133 cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
126 if (cp < msg || cp >= eomorig) /* out of range */ 134 if (cp < msg || cp >= eomorig) /* out of range */
127 return(-1); 135 return (-1);
128 checked += 2; 136 checked += 2;
129 /* 137 /*
130 * Check for loops in the compressed name; 138 * Check for loops in the compressed name;
@@ -157,8 +165,9 @@ dn_expand(msg, eomorig, comp_dn, exp_dn, length)
157 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' 165 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
158 * is NULL, we don't update the list. 166 * is NULL, we don't update the list.
159 */ 167 */
168int
160dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) 169dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
161 const u_char *exp_dn; 170 const char *exp_dn;
162 u_char *comp_dn, **dnptrs, **lastdnptr; 171 u_char *comp_dn, **dnptrs, **lastdnptr;
163 int length; 172 int length;
164{ 173{
@@ -170,6 +179,7 @@ dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
170 dn = (u_char *)exp_dn; 179 dn = (u_char *)exp_dn;
171 cp = comp_dn; 180 cp = comp_dn;
172 eob = cp + length; 181 eob = cp + length;
182 lpp = cpp = NULL;
173 if (dnptrs != NULL) { 183 if (dnptrs != NULL) {
174 if ((msg = *dnptrs++) != NULL) { 184 if ((msg = *dnptrs++) != NULL) {
175 for (cpp = dnptrs; *cpp != NULL; cpp++) 185 for (cpp = dnptrs; *cpp != NULL; cpp++)
@@ -235,13 +245,14 @@ dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
235/* 245/*
236 * Skip over a compressed domain name. Return the size or -1. 246 * Skip over a compressed domain name. Return the size or -1.
237 */ 247 */
248int
238__dn_skipname(comp_dn, eom) 249__dn_skipname(comp_dn, eom)
239 const u_char *comp_dn, *eom; 250 const u_char *comp_dn, *eom;
240{ 251{
241 register u_char *cp; 252 register const u_char *cp;
242 register int n; 253 register int n;
243 254
244 cp = (u_char *)comp_dn; 255 cp = comp_dn;
245 while (cp < eom && (n = *cp++)) { 256 while (cp < eom && (n = *cp++)) {
246 /* 257 /*
247 * check for indirection 258 * check for indirection
@@ -259,10 +270,19 @@ __dn_skipname(comp_dn, eom)
259 break; 270 break;
260 } 271 }
261 if (cp > eom) 272 if (cp > eom)
262 return -1; 273 return (-1);
263 return (cp - comp_dn); 274 return (cp - comp_dn);
264} 275}
265 276
277static int
278mklower(ch)
279 register int ch;
280{
281 if (isascii(ch) && isupper(ch))
282 return (tolower(ch));
283 return (ch);
284}
285
266/* 286/*
267 * Search for expanded name from a list of previously compressed names. 287 * Search for expanded name from a list of previously compressed names.
268 * Return the offset from msg if found or -1. 288 * Return the offset from msg if found or -1.
@@ -281,7 +301,7 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
281 for (cpp = dnptrs; cpp < lastdnptr; cpp++) { 301 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
282 dn = exp_dn; 302 dn = exp_dn;
283 sp = cp = *cpp; 303 sp = cp = *cpp;
284 while (n = *cp++) { 304 while ((n = *cp++)) {
285 /* 305 /*
286 * check for indirection 306 * check for indirection
287 */ 307 */
@@ -292,7 +312,7 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
292 goto next; 312 goto next;
293 if (*dn == '\\') 313 if (*dn == '\\')
294 dn++; 314 dn++;
295 if (*dn++ != *cp++) 315 if (mklower(*dn++) != mklower(*cp++))
296 goto next; 316 goto next;
297 } 317 }
298 if ((n = *dn++) == '\0' && *cp == '\0') 318 if ((n = *dn++) == '\0' && *cp == '\0')
@@ -301,11 +321,12 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
301 continue; 321 continue;
302 goto next; 322 goto next;
303 323
304 default: /* illegal type */
305 return (-1);
306
307 case INDIR_MASK: /* indirection */ 324 case INDIR_MASK: /* indirection */
308 cp = msg + (((n & 0x3f) << 8) | *cp); 325 cp = msg + (((n & 0x3f) << 8) | *cp);
326 break;
327
328 default: /* illegal type */
329 return (-1);
309 } 330 }
310 } 331 }
311 if (*dn == '\0') 332 if (*dn == '\0')
@@ -316,16 +337,124 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
316} 337}
317 338
318/* 339/*
319 * Routines to insert/extract short/long's. Must account for byte 340 * Verify that a domain name uses an acceptable character set.
320 * order and non-alignment problems. This code at least has the 341 */
321 * advantage of being portable. 342
322 * 343/*
323 * used by sendmail. 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.
324 */ 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
362int
363res_hnok(dn)
364 const char *dn;
365{
366 int pch = PERIOD, ch = *dn++;
325 367
326u_short 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 */
392int
393res_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 */
409int
410res_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 */
439int
440res_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
455u_int16_t
327_getshort(msgp) 456_getshort(msgp)
328 register u_char *msgp; 457 register const u_char *msgp;
329{ 458{
330 register u_int16_t u; 459 register u_int16_t u;
331 460
@@ -333,9 +462,21 @@ _getshort(msgp)
333 return (u); 462 return (u);
334} 463}
335 464
465#ifdef NeXT
466/*
467 * nExt machines have some funky library conventions, which we must maintain.
468 */
469u_int16_t
470res_getshort(msgp)
471 register const u_char *msgp;
472{
473 return (_getshort(msgp));
474}
475#endif
476
336u_int32_t 477u_int32_t
337_getlong(msgp) 478_getlong(msgp)
338 register u_char *msgp; 479 register const u_char *msgp;
339{ 480{
340 register u_int32_t u; 481 register u_int32_t u;
341 482
@@ -344,13 +485,7 @@ _getlong(msgp)
344} 485}
345 486
346void 487void
347#if defined(__STDC__) || defined(__cplusplus)
348__putshort(register u_int16_t s, register u_char *msgp) 488__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{ 489{
355 PUTSHORT(s, msgp); 490 PUTSHORT(s, msgp);
356} 491}