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.c483
1 files changed, 483 insertions, 0 deletions
diff --git a/src/lib/libc/net/res_comp.c b/src/lib/libc/net/res_comp.c
new file mode 100644
index 0000000000..62c04d8518
--- /dev/null
+++ b/src/lib/libc/net/res_comp.c
@@ -0,0 +1,483 @@
1/* $OpenBSD: res_comp.c,v 1.12 2005/03/25 13:24:12 otto 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
56static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
57static char rcsid[] = "$From: res_comp.c,v 8.11 1996/12/02 09:17:22 vixie Exp $";
58#else
59static char rcsid[] = "$OpenBSD: res_comp.c,v 1.12 2005/03/25 13:24:12 otto 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
75static 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 */
84int
85dn_expand(const u_char *msg, const u_char *eomorig, const u_char *comp_dn,
86 char *exp_dn, int length)
87{
88 const u_char *cp;
89 char *dn;
90 int n, c;
91 char *eom;
92 int len = -1, checked = 0;
93
94 dn = exp_dn;
95 cp = comp_dn;
96 if (length > MAXHOSTNAMELEN-1)
97 length = MAXHOSTNAMELEN-1;
98 eom = exp_dn + length;
99 /*
100 * fetch next label in domain name
101 */
102 while ((n = *cp++)) {
103 /*
104 * Check for indirection
105 */
106 switch (n & INDIR_MASK) {
107 case 0:
108 if (dn != exp_dn) {
109 if (dn >= eom)
110 return (-1);
111 *dn++ = '.';
112 }
113 if (dn+n >= eom)
114 return (-1);
115 checked += n + 1;
116 while (--n >= 0) {
117 if (((c = *cp++) == '.') || (c == '\\')) {
118 if (dn + n + 2 >= eom)
119 return (-1);
120 *dn++ = '\\';
121 }
122 *dn++ = c;
123 if (cp >= eomorig) /* out of range */
124 return (-1);
125 }
126 break;
127
128 case INDIR_MASK:
129 if (len < 0)
130 len = cp - comp_dn + 1;
131 cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
132 if (cp < msg || cp >= eomorig) /* out of range */
133 return (-1);
134 checked += 2;
135 /*
136 * Check for loops in the compressed name;
137 * if we've looked at the whole message,
138 * there must be a loop.
139 */
140 if (checked >= eomorig - msg)
141 return (-1);
142 break;
143
144 default:
145 return (-1); /* flag error */
146 }
147 }
148 *dn = '\0';
149 if (len < 0)
150 len = cp - comp_dn;
151 return (len);
152}
153
154/*
155 * Compress domain name 'exp_dn' into 'comp_dn'.
156 * Return the size of the compressed name or -1.
157 * 'length' is the size of the array pointed to by 'comp_dn'.
158 * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
159 * is a pointer to the beginning of the message. The list ends with NULL.
160 * 'lastdnptr' is a pointer to the end of the arrary pointed to
161 * by 'dnptrs'. Side effect is to update the list of pointers for
162 * labels inserted into the message as we compress the name.
163 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
164 * is NULL, we don't update the list.
165 */
166int
167dn_comp(const char *exp_dn, u_char *comp_dn, int length, u_char **dnptrs,
168 u_char **lastdnptr)
169{
170 u_char *cp, *dn;
171 int c, l;
172 u_char **cpp, **lpp, *sp, *eob;
173 u_char *msg;
174
175 dn = (u_char *)exp_dn;
176 cp = comp_dn;
177 eob = cp + length;
178 lpp = cpp = NULL;
179 if (dnptrs != NULL) {
180 if ((msg = *dnptrs++) != NULL) {
181 for (cpp = dnptrs; *cpp != NULL; cpp++)
182 ;
183 lpp = cpp; /* end of list to search */
184 }
185 } else
186 msg = NULL;
187 for (c = *dn++; c != '\0'; ) {
188 /* look to see if we can use pointers */
189 if (msg != NULL) {
190 if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
191 if (cp+1 >= eob)
192 return (-1);
193 *cp++ = (l >> 8) | INDIR_MASK;
194 *cp++ = l % 256;
195 return (cp - comp_dn);
196 }
197 /* not found, save it */
198 if (lastdnptr != NULL && cpp < lastdnptr-1) {
199 *cpp++ = cp;
200 *cpp = NULL;
201 }
202 }
203 sp = cp++; /* save ptr to length byte */
204 do {
205 if (c == '.') {
206 c = *dn++;
207 break;
208 }
209 if (c == '\\') {
210 if ((c = *dn++) == '\0')
211 break;
212 }
213 if (cp >= eob) {
214 if (msg != NULL)
215 *lpp = NULL;
216 return (-1);
217 }
218 *cp++ = c;
219 } while ((c = *dn++) != '\0');
220 /* catch trailing '.'s but not '..' */
221 if ((l = cp - sp - 1) == 0 && c == '\0') {
222 cp--;
223 break;
224 }
225 if (l <= 0 || l > MAXLABEL) {
226 if (msg != NULL)
227 *lpp = NULL;
228 return (-1);
229 }
230 *sp = l;
231 }
232 if (cp >= eob) {
233 if (msg != NULL)
234 *lpp = NULL;
235 return (-1);
236 }
237 *cp++ = '\0';
238 return (cp - comp_dn);
239}
240
241/*
242 * Skip over a compressed domain name. Return the size or -1.
243 */
244int
245__dn_skipname(const u_char *comp_dn, const u_char *eom)
246{
247 const u_char *cp;
248 int n;
249
250 cp = comp_dn;
251 while (cp < eom && (n = *cp++)) {
252 /*
253 * check for indirection
254 */
255 switch (n & INDIR_MASK) {
256 case 0: /* normal case, n == len */
257 cp += n;
258 continue;
259 case INDIR_MASK: /* indirection */
260 cp++;
261 break;
262 default: /* illegal type */
263 return (-1);
264 }
265 break;
266 }
267 if (cp > eom)
268 return (-1);
269 return (cp - comp_dn);
270}
271
272static int
273mklower(int ch)
274{
275 if (isascii(ch) && isupper(ch))
276 return (tolower(ch));
277 return (ch);
278}
279
280/*
281 * Search for expanded name from a list of previously compressed names.
282 * Return the offset from msg if found or -1.
283 * dnptrs is the pointer to the first name on the list,
284 * not the pointer to the start of the message.
285 */
286static int
287dn_find(u_char *exp_dn, u_char *msg, u_char **dnptrs, u_char **lastdnptr)
288{
289 u_char *dn, *cp, **cpp;
290 int n;
291 u_char *sp;
292
293 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
294 dn = exp_dn;
295 sp = cp = *cpp;
296 while ((n = *cp++)) {
297 /*
298 * check for indirection
299 */
300 switch (n & INDIR_MASK) {
301 case 0: /* normal case, n == len */
302 while (--n >= 0) {
303 if (*dn == '.')
304 goto next;
305 if (*dn == '\\')
306 dn++;
307 if (mklower(*dn++) != mklower(*cp++))
308 goto next;
309 }
310 if ((n = *dn++) == '\0' && *cp == '\0')
311 return (sp - msg);
312 if (n == '.')
313 continue;
314 goto next;
315
316 case INDIR_MASK: /* indirection */
317 cp = msg + (((n & 0x3f) << 8) | *cp);
318 break;
319
320 default: /* illegal type */
321 return (-1);
322 }
323 }
324 if (*dn == '\0')
325 return (sp - msg);
326 next: ;
327 }
328 return (-1);
329}
330
331/*
332 * Verify that a domain name uses an acceptable character set.
333 */
334
335/*
336 * Note the conspicuous absence of ctype macros in these definitions. On
337 * non-ASCII hosts, we can't depend on string literals or ctype macros to
338 * tell us anything about network-format data. The rest of the BIND system
339 * is not careful about this, but for some reason, we're doing it right here.
340 */
341#define PERIOD 0x2e
342#define hyphenchar(c) ((c) == 0x2d)
343#define bslashchar(c) ((c) == 0x5c)
344#define periodchar(c) ((c) == PERIOD)
345#define asterchar(c) ((c) == 0x2a)
346#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
347 || ((c) >= 0x61 && (c) <= 0x7a))
348#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
349
350#define borderchar(c) (alphachar(c) || digitchar(c))
351#define middlechar(c) (borderchar(c) || hyphenchar(c))
352#define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
353
354int
355res_hnok(const char *dn)
356{
357 int pch = PERIOD, ch = *dn++;
358
359 while (ch != '\0') {
360 int nch = *dn++;
361
362 if (periodchar(ch)) {
363 ;
364 } else if (periodchar(pch)) {
365 if (!borderchar(ch))
366 return (0);
367 } else if (periodchar(nch) || nch == '\0') {
368 if (!borderchar(ch))
369 return (0);
370 } else {
371 if (!middlechar(ch))
372 return (0);
373 }
374 pch = ch, ch = nch;
375 }
376 return (1);
377}
378
379/*
380 * hostname-like (A, MX, WKS) owners can have "*" as their first label
381 * but must otherwise be as a host name.
382 */
383int
384res_ownok(const char *dn)
385{
386 if (asterchar(dn[0])) {
387 if (periodchar(dn[1]))
388 return (res_hnok(dn+2));
389 if (dn[1] == '\0')
390 return (1);
391 }
392 return (res_hnok(dn));
393}
394
395/*
396 * SOA RNAMEs and RP RNAMEs can have any printable character in their first
397 * label, but the rest of the name has to look like a host name.
398 */
399int
400res_mailok(const char *dn)
401{
402 int ch, escaped = 0;
403
404 /* "." is a valid missing representation */
405 if (*dn == '\0')
406 return(1);
407
408 /* otherwise <label>.<hostname> */
409 while ((ch = *dn++) != '\0') {
410 if (!domainchar(ch))
411 return (0);
412 if (!escaped && periodchar(ch))
413 break;
414 if (escaped)
415 escaped = 0;
416 else if (bslashchar(ch))
417 escaped = 1;
418 }
419 if (periodchar(ch))
420 return (res_hnok(dn));
421 return(0);
422}
423
424/*
425 * This function is quite liberal, since RFC 1034's character sets are only
426 * recommendations.
427 */
428int
429res_dnok(const char *dn)
430{
431 int ch;
432
433 while ((ch = *dn++) != '\0')
434 if (!domainchar(ch))
435 return (0);
436 return (1);
437}
438
439/*
440 * Routines to insert/extract short/long's.
441 */
442
443u_int16_t
444_getshort(const u_char *msgp)
445{
446 u_int16_t u;
447
448 GETSHORT(u, msgp);
449 return (u);
450}
451
452#ifdef NeXT
453/*
454 * nExt machines have some funky library conventions, which we must maintain.
455 */
456u_int16_t
457res_getshort(msgp)
458 const u_char *msgp;
459{
460 return (_getshort(msgp));
461}
462#endif
463
464u_int32_t
465_getlong(const u_char *msgp)
466{
467 u_int32_t u;
468
469 GETLONG(u, msgp);
470 return (u);
471}
472
473void
474__putshort(u_int16_t s, u_char *msgp)
475{
476 PUTSHORT(s, msgp);
477}
478
479void
480__putlong(u_int32_t l, u_char *msgp)
481{
482 PUTLONG(l, msgp);
483}