Correction.
[glibc.git] / resolv / res_comp.c
blob103ebd9a52531dc4467d761efdf0363ad7bafcba
1 /*
2 * ++Copyright++ 1985, 1993
3 * -
4 * Copyright (c) 1985, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
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
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 * -
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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 * --Copyright--
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
58 static char rcsid[] = "$Id$";
59 #endif /* LIBC_SCCS and not lint */
61 #include <sys/param.h>
62 #include <netinet/in.h>
63 #include <arpa/nameser.h>
65 #include <stdio.h>
66 #include <resolv.h>
67 #include <ctype.h>
69 #if defined(BSD) && (BSD >= 199103)
70 # include <unistd.h>
71 # include <string.h>
72 #else
73 # include "../conf/portability.h"
74 #endif
76 static int dn_find __P((u_char *exp_dn, u_char *msg,
77 u_char **dnptrs, u_char **lastdnptr));
80 * Expand compressed domain name 'comp_dn' to full domain name.
81 * 'msg' is a pointer to the begining of the message,
82 * 'eomorig' points to the first location after the message,
83 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
84 * Return size of compressed name or -1 if there was an error.
86 int
87 dn_expand(msg, eomorig, comp_dn, exp_dn, length)
88 const u_char *msg, *eomorig, *comp_dn;
89 char *exp_dn;
90 int length;
92 register const u_char *cp;
93 register char *dn;
94 register int n, c;
95 char *eom;
96 int len = -1, checked = 0;
98 dn = exp_dn;
99 cp = comp_dn;
100 eom = exp_dn + length;
102 * fetch next label in domain name
104 while (n = *cp++) {
106 * Check for indirection
108 switch (n & INDIR_MASK) {
109 case 0:
110 if (dn != exp_dn) {
111 if (dn >= eom)
112 return (-1);
113 *dn++ = '.';
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++ = '\\';
124 *dn++ = c;
125 if (cp >= eomorig) /* out of range */
126 return (-1);
128 break;
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;
138 * Check for loops in the compressed name;
139 * if we've looked at the whole message,
140 * there must be a loop.
142 if (checked >= eomorig - msg)
143 return (-1);
144 break;
146 default:
147 return (-1); /* flag error */
150 *dn = '\0';
151 if (len < 0)
152 len = cp - comp_dn;
153 return (len);
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.
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;
174 register u_char *cp, *dn;
175 register int c, l;
176 u_char **cpp, **lpp, *sp, *eob;
177 u_char *msg;
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++)
187 lpp = cpp; /* end of list to search */
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);
201 /* not found, save it */
202 if (lastdnptr != NULL && cpp < lastdnptr-1) {
203 *cpp++ = cp;
204 *cpp = NULL;
207 sp = cp++; /* save ptr to length byte */
208 do {
209 if (c == '.') {
210 c = *dn++;
211 break;
213 if (c == '\\') {
214 if ((c = *dn++) == '\0')
215 break;
217 if (cp >= eob) {
218 if (msg != NULL)
219 *lpp = NULL;
220 return (-1);
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;
229 if (l <= 0 || l > MAXLABEL) {
230 if (msg != NULL)
231 *lpp = NULL;
232 return (-1);
234 *sp = l;
236 if (cp >= eob) {
237 if (msg != NULL)
238 *lpp = NULL;
239 return (-1);
241 *cp++ = '\0';
242 return (cp - comp_dn);
246 * Skip over a compressed domain name. Return the size or -1.
249 __dn_skipname(comp_dn, eom)
250 const u_char *comp_dn, *eom;
252 register const u_char *cp;
253 register int n;
255 cp = comp_dn;
256 while (cp < eom && (n = *cp++)) {
258 * check for indirection
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);
270 break;
272 if (cp > eom)
273 return (-1);
274 return (cp - comp_dn);
277 static int
278 mklower(ch)
279 register int ch;
281 if (isascii(ch) && isupper(ch))
282 return (tolower(ch));
283 return (ch);
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.
292 static int
293 dn_find(exp_dn, msg, dnptrs, lastdnptr)
294 u_char *exp_dn, *msg;
295 u_char **dnptrs, **lastdnptr;
297 register u_char *dn, *cp, **cpp;
298 register int n;
299 u_char *sp;
301 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
302 dn = exp_dn;
303 sp = cp = *cpp;
304 while (n = *cp++) {
306 * check for indirection
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;
318 if ((n = *dn++) == '\0' && *cp == '\0')
319 return (sp - msg);
320 if (n == '.')
321 continue;
322 goto next;
324 case INDIR_MASK: /* indirection */
325 cp = msg + (((n & 0x3f) << 8) | *cp);
326 break;
328 default: /* illegal type */
329 return (-1);
332 if (*dn == '\0')
333 return (sp - msg);
334 next: ;
336 return (-1);
340 * Verify that a domain name uses an acceptable character set.
343 /****
344 To: "Lawrence R. Rogers" <lrr@cert.org>
345 cc: cert@cert.org, pvm@home.net
346 Subject: Re: VU#14542
347 In-reply-to: Your message of "Mon, 19 Feb 1996 17:16:27 PST."
348 Date: Tue, 20 Feb 1996 22:37:21 -0800
349 From: Paul A Vixie <vixie@wisdom.home.vix.com>
351 in retrospect,
353 hostname = firstlabel ( "." otherlabel )+
354 firstchar = [a-zA-Z0-9_]
355 otherchar = [a-zA-Z0-9_-/]
356 firstlabel = firstchar otherchar*
357 otherlabel = otherchar+
359 should have been
361 hostname = label ( "." label )+
362 firstchar = [a-zA-Z0-9]
363 otherchar = [a-zA-Z0-9_-]
364 label = firstchar otherchar*
366 i know of no example of a real host name that needs the looser rule i sent
367 earlier. since i'm only trying to bend the spec to fit actual known uses,
368 i should not have widened the rules as far as i did earlier.
369 ****/
372 * Note the conspicuous absence of ctype macros in these definitions. On
373 * non-ASCII hosts, we can't depend on string literals or ctype macros to
374 * tell us anything about network-format data. The rest of the BIND system
375 * is not careful about this, but for some reason, we're doing it right here.
377 #define PERIOD 0x2e
378 #define hyphenchar(c) ((c) == 0x2d)
379 #define bslashchar(c) ((c) == 0x5c)
380 #define periodchar(c) ((c) == PERIOD)
381 #define asterchar(c) ((c) == 0x2a)
382 #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
383 || ((c) >= 0x61 && (c) <= 0x7a))
384 #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
386 #define borderchar(c) (alphachar(c) || digitchar(c))
387 #define middlechar(c) (borderchar(c) || hyphenchar(c))
388 #define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
391 res_hnok(dn)
392 const char *dn;
394 int ppch = '\0', pch = PERIOD, ch = *dn++;
396 while (ch != '\0') {
397 int nch = *dn++;
399 if (periodchar(ch)) {
400 /* NULL */;
401 } else if (periodchar(pch)) {
402 if (!borderchar(ch))
403 return (0);
404 } else if (periodchar(nch) || nch == '\0') {
405 if (!borderchar(ch))
406 return (0);
407 } else {
408 if (!middlechar(ch))
409 return (0);
411 ppch = pch, pch = ch, ch = nch;
413 return (1);
417 * hostname-like (A, MX, WKS) owners can have "*" as their first label
418 * but must otherwise be as a host name.
421 res_ownok(dn)
422 const char *dn;
424 if (asterchar(dn[0]) && periodchar(dn[1]))
425 dn += 2;
426 return (res_hnok(dn));
430 * SOA RNAMEs and RP RNAMEs can have any printable character in their first
431 * label, but the rest of the name has to look like a host name.
434 res_mailok(dn)
435 const char *dn;
437 int ch, pch;
439 pch = '\0';
440 while ((ch = *dn++) != '\0') {
441 if (!domainchar(ch))
442 return (0);
443 if (periodchar(ch) && !bslashchar(pch))
444 break;
445 pch = ch;
447 return (res_hnok(dn));
451 * This function is quite liberal, since RFC 1034's character sets are only
452 * recommendations.
455 res_dnok(dn)
456 const char *dn;
458 int ch;
460 while ((ch = *dn++) != '\0')
461 if (!domainchar(ch))
462 return (0);
463 return (1);
467 * Routines to insert/extract short/long's.
470 u_int16_t
471 _getshort(msgp)
472 register const u_char *msgp;
474 register u_int16_t u;
476 GETSHORT(u, msgp);
477 return (u);
480 #ifdef NeXT
482 * nExt machines have some funky library conventions, which we must maintain.
484 u_int16_t
485 res_getshort(msgp)
486 register const u_char *msgp;
488 return (_getshort(msgp));
490 #endif
492 u_int32_t
493 _getlong(msgp)
494 register const u_char *msgp;
496 register u_int32_t u;
498 GETLONG(u, msgp);
499 return (u);
502 void
503 #if defined(__STDC__) || defined(__cplusplus)
504 __putshort(register u_int16_t s, register u_char *msgp) /* must match proto */
505 #else
506 __putshort(s, msgp)
507 register u_int16_t s;
508 register u_char *msgp;
509 #endif
511 PUTSHORT(s, msgp);
514 void
515 __putlong(l, msgp)
516 register u_int32_t l;
517 register u_char *msgp;
519 PUTLONG(l, msgp);
522 #ifdef ultrix
523 /* ultrix 4.0 had some icky packaging in its libc.a. alias for it here.
524 * there is more gunk of this kind over in res_debug.c.
526 #undef putshort
527 void
528 #if defined(__STDC__) || defined(__cplusplus)
529 putshort(register u_short s, register u_char *msgp)
530 #else
531 putshort(s, msgp)
532 register u_short s;
533 register u_char *msgp;
534 #endif
536 __putshort(s, msgp);
538 #undef putlong
539 void
540 putlong(l, msgp)
541 register u_int32_t l;
542 register u_char *msgp;
544 __putlong(l, msgp);
547 #undef dn_skipname
548 dn_skipname(comp_dn, eom)
549 const u_char *comp_dn, *eom;
551 return (__dn_skipname(comp_dn, eom));
553 #endif /* Ultrix 4.0 hackery */