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