10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6812Sraf * Common Development and Distribution License (the "License").
6*6812Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211219Sraf
220Sstevel@tonic-gate /*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate * The Regents of the University of California
330Sstevel@tonic-gate * All Rights Reserved
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate * contributors.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <arpa/nameser.h>
450Sstevel@tonic-gate
46237Sanay static int dn_find(u_char *exp_dn, u_char *msg, u_char **dnptrs,
47237Sanay u_char **lastdnptr);
480Sstevel@tonic-gate
490Sstevel@tonic-gate
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate * Expand compressed domain name 'comp_dn' to full domain name.
520Sstevel@tonic-gate * 'msg' is a pointer to the begining of the message,
530Sstevel@tonic-gate * 'eomorig' points to the first location after the message,
540Sstevel@tonic-gate * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
550Sstevel@tonic-gate * Return size of compressed name or -1 if there was an error.
560Sstevel@tonic-gate */
57237Sanay int
dn_expand(msg,eomorig,comp_dn,exp_dn,length)580Sstevel@tonic-gate dn_expand(msg, eomorig, comp_dn, exp_dn, length)
590Sstevel@tonic-gate u_char *msg, *eomorig, *comp_dn, *exp_dn;
600Sstevel@tonic-gate int length;
610Sstevel@tonic-gate {
620Sstevel@tonic-gate register u_char *cp, *dn;
630Sstevel@tonic-gate register int n, c;
640Sstevel@tonic-gate u_char *eom;
650Sstevel@tonic-gate int len = -1, checked = 0;
660Sstevel@tonic-gate
670Sstevel@tonic-gate dn = exp_dn;
680Sstevel@tonic-gate cp = comp_dn;
690Sstevel@tonic-gate eom = exp_dn + length;
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate * fetch next label in domain name
720Sstevel@tonic-gate */
730Sstevel@tonic-gate while (n = *cp++) {
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate * Check for indirection
760Sstevel@tonic-gate */
770Sstevel@tonic-gate switch (n & INDIR_MASK) {
780Sstevel@tonic-gate case 0:
790Sstevel@tonic-gate if (dn != exp_dn) {
800Sstevel@tonic-gate if (dn >= eom)
810Sstevel@tonic-gate return (-1);
820Sstevel@tonic-gate *dn++ = '.';
830Sstevel@tonic-gate }
840Sstevel@tonic-gate if (dn+n >= eom)
850Sstevel@tonic-gate return (-1);
860Sstevel@tonic-gate checked += n + 1;
870Sstevel@tonic-gate while (--n >= 0) {
880Sstevel@tonic-gate if ((c = *cp++) == '.') {
890Sstevel@tonic-gate if (dn + n + 2 >= eom)
900Sstevel@tonic-gate return (-1);
910Sstevel@tonic-gate *dn++ = '\\';
920Sstevel@tonic-gate }
930Sstevel@tonic-gate *dn++ = c;
940Sstevel@tonic-gate if (cp >= eomorig) /* out of range */
950Sstevel@tonic-gate return (-1);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate break;
980Sstevel@tonic-gate
990Sstevel@tonic-gate case INDIR_MASK:
1000Sstevel@tonic-gate if (len < 0)
1010Sstevel@tonic-gate len = cp - comp_dn + 1;
1020Sstevel@tonic-gate cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
1030Sstevel@tonic-gate if (cp < msg || cp >= eomorig) /* out of range */
1040Sstevel@tonic-gate return (-1);
1050Sstevel@tonic-gate checked += 2;
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate * Check for loops in the compressed name;
1080Sstevel@tonic-gate * if we've looked at the whole message,
1090Sstevel@tonic-gate * there must be a loop.
1100Sstevel@tonic-gate */
1110Sstevel@tonic-gate if (checked >= eomorig - msg)
1120Sstevel@tonic-gate return (-1);
1130Sstevel@tonic-gate break;
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate default:
1160Sstevel@tonic-gate return (-1); /* flag error */
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate *dn = '\0';
1200Sstevel@tonic-gate if (len < 0)
1210Sstevel@tonic-gate len = cp - comp_dn;
1220Sstevel@tonic-gate return (len);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate * Compress domain name 'exp_dn' into 'comp_dn'.
1270Sstevel@tonic-gate * Return the size of the compressed name or -1.
1280Sstevel@tonic-gate * 'length' is the size of the array pointed to by 'comp_dn'.
1290Sstevel@tonic-gate * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
1300Sstevel@tonic-gate * is a pointer to the beginning of the message. The list ends with NULL.
1310Sstevel@tonic-gate * 'lastdnptr' is a pointer to the end of the arrary pointed to
1320Sstevel@tonic-gate * by 'dnptrs'. Side effect is to update the list of pointers for
1330Sstevel@tonic-gate * labels inserted into the message as we compress the name.
1340Sstevel@tonic-gate * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
1350Sstevel@tonic-gate * is NULL, we don't update the list.
1360Sstevel@tonic-gate */
137237Sanay int
dn_comp(exp_dn,comp_dn,length,dnptrs,lastdnptr)1380Sstevel@tonic-gate dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
1390Sstevel@tonic-gate u_char *exp_dn, *comp_dn;
1400Sstevel@tonic-gate int length;
1410Sstevel@tonic-gate u_char **dnptrs, **lastdnptr;
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate register u_char *cp, *dn;
1440Sstevel@tonic-gate register int c, l;
1450Sstevel@tonic-gate u_char **cpp, **lpp, *sp, *eob;
1460Sstevel@tonic-gate u_char *msg;
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate dn = exp_dn;
1490Sstevel@tonic-gate cp = comp_dn;
1500Sstevel@tonic-gate eob = cp + length;
1510Sstevel@tonic-gate if (dnptrs != NULL) {
1520Sstevel@tonic-gate if ((msg = *dnptrs++) != NULL) {
1530Sstevel@tonic-gate for (cpp = dnptrs; *cpp != NULL; cpp++)
1540Sstevel@tonic-gate ;
1550Sstevel@tonic-gate lpp = cpp; /* end of list to search */
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate } else
1580Sstevel@tonic-gate msg = NULL;
1590Sstevel@tonic-gate for (c = *dn++; c != '\0'; /*EMPTY*/) {
1600Sstevel@tonic-gate /* look to see if we can use pointers */
1610Sstevel@tonic-gate if (msg != NULL) {
1620Sstevel@tonic-gate if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
1630Sstevel@tonic-gate if (cp+1 >= eob)
1640Sstevel@tonic-gate return (-1);
1650Sstevel@tonic-gate *cp++ = (l >> 8) | INDIR_MASK;
1660Sstevel@tonic-gate *cp++ = l % 256;
1670Sstevel@tonic-gate return (cp - comp_dn);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate /* not found, save it */
1700Sstevel@tonic-gate if (lastdnptr != NULL && cpp < lastdnptr-1) {
1710Sstevel@tonic-gate *cpp++ = cp;
1720Sstevel@tonic-gate *cpp = NULL;
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate sp = cp++; /* save ptr to length byte */
1760Sstevel@tonic-gate do {
1770Sstevel@tonic-gate if (c == '.') {
1780Sstevel@tonic-gate c = *dn++;
1790Sstevel@tonic-gate break;
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate if (c == '\\') {
1820Sstevel@tonic-gate if ((c = *dn++) == '\0')
1830Sstevel@tonic-gate break;
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate if (cp >= eob) {
1860Sstevel@tonic-gate if (msg != NULL)
1870Sstevel@tonic-gate *lpp = NULL;
1880Sstevel@tonic-gate return (-1);
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate *cp++ = c;
1910Sstevel@tonic-gate } while ((c = *dn++) != '\0');
1920Sstevel@tonic-gate /* catch trailing '.'s but not '..' */
1930Sstevel@tonic-gate if ((l = cp - sp - 1) == 0 && c == '\0') {
1940Sstevel@tonic-gate cp--;
1950Sstevel@tonic-gate break;
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate if (l <= 0 || l > MAXLABEL) {
1980Sstevel@tonic-gate if (msg != NULL)
1990Sstevel@tonic-gate *lpp = NULL;
2000Sstevel@tonic-gate return (-1);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate *sp = l;
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate if (cp >= eob) {
2050Sstevel@tonic-gate if (msg != NULL)
2060Sstevel@tonic-gate *lpp = NULL;
2070Sstevel@tonic-gate return (-1);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate *cp++ = '\0';
2100Sstevel@tonic-gate return (cp - comp_dn);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate * Skip over a compressed domain name. Return the size or -1.
2150Sstevel@tonic-gate */
216237Sanay int
dn_skipname(comp_dn,eom)2170Sstevel@tonic-gate dn_skipname(comp_dn, eom)
2180Sstevel@tonic-gate u_char *comp_dn, *eom;
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate register u_char *cp;
2210Sstevel@tonic-gate register int n;
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate cp = comp_dn;
2240Sstevel@tonic-gate while (cp < eom && (n = *cp++)) {
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate * check for indirection
2270Sstevel@tonic-gate */
2280Sstevel@tonic-gate switch (n & INDIR_MASK) {
2290Sstevel@tonic-gate case 0: /* normal case, n == len */
2300Sstevel@tonic-gate cp += n;
2310Sstevel@tonic-gate continue;
2320Sstevel@tonic-gate default: /* illegal type */
2330Sstevel@tonic-gate return (-1);
2340Sstevel@tonic-gate case INDIR_MASK: /* indirection */
2350Sstevel@tonic-gate cp++;
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate break;
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate return (cp - comp_dn);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate * Search for expanded name from a list of previously compressed names.
2440Sstevel@tonic-gate * Return the offset from msg if found or -1.
2450Sstevel@tonic-gate * dnptrs is the pointer to the first name on the list,
2460Sstevel@tonic-gate * not the pointer to the start of the message.
2470Sstevel@tonic-gate */
248237Sanay static int
dn_find(u_char * exp_dn,u_char * msg,u_char ** dnptrs,u_char ** lastdnptr)249237Sanay dn_find(u_char *exp_dn, u_char *msg, u_char **dnptrs, u_char **lastdnptr)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate register u_char *dn, *cp, **cpp;
2520Sstevel@tonic-gate register int n;
2530Sstevel@tonic-gate u_char *sp;
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
2560Sstevel@tonic-gate dn = exp_dn;
2570Sstevel@tonic-gate sp = cp = *cpp;
2580Sstevel@tonic-gate while (n = *cp++) {
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate * check for indirection
2610Sstevel@tonic-gate */
2620Sstevel@tonic-gate switch (n & INDIR_MASK) {
2630Sstevel@tonic-gate case 0: /* normal case, n == len */
2640Sstevel@tonic-gate while (--n >= 0) {
2650Sstevel@tonic-gate if (*dn == '.')
2660Sstevel@tonic-gate goto next;
2670Sstevel@tonic-gate if (*dn == '\\')
2680Sstevel@tonic-gate dn++;
2690Sstevel@tonic-gate if (*dn++ != *cp++)
2700Sstevel@tonic-gate goto next;
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate if ((n = *dn++) == '\0' && *cp == '\0')
2730Sstevel@tonic-gate return (sp - msg);
2740Sstevel@tonic-gate if (n == '.')
2750Sstevel@tonic-gate continue;
2760Sstevel@tonic-gate goto next;
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate default: /* illegal type */
2790Sstevel@tonic-gate return (-1);
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate case INDIR_MASK: /* indirection */
2820Sstevel@tonic-gate cp = msg + (((n & 0x3f) << 8) | *cp);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate if (*dn == '\0')
2860Sstevel@tonic-gate return (sp - msg);
2870Sstevel@tonic-gate next: /*EMPTY*/;
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate return (-1);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate * Routines to insert/extract short/long's. Must account for byte
2940Sstevel@tonic-gate * order and non-alignment problems. This code at least has the
2950Sstevel@tonic-gate * advantage of being portable.
2960Sstevel@tonic-gate *
2970Sstevel@tonic-gate * used by sendmail.
2980Sstevel@tonic-gate */
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate u_short
_getshort(msgp)3010Sstevel@tonic-gate _getshort(msgp)
3020Sstevel@tonic-gate u_char *msgp;
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate register u_char *p = (u_char *) msgp;
3050Sstevel@tonic-gate #ifdef vax
3060Sstevel@tonic-gate /*
3070Sstevel@tonic-gate * vax compiler doesn't put shorts in registers
3080Sstevel@tonic-gate */
3090Sstevel@tonic-gate register u_long u;
3100Sstevel@tonic-gate #else
3110Sstevel@tonic-gate register u_short u;
3120Sstevel@tonic-gate #endif
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate u = *p++ << 8;
3150Sstevel@tonic-gate return ((u_short)(u | *p));
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate u_long
_getlong(msgp)3190Sstevel@tonic-gate _getlong(msgp)
3200Sstevel@tonic-gate u_char *msgp;
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate register u_char *p = (u_char *) msgp;
3230Sstevel@tonic-gate register u_long u;
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate u = *p++; u <<= 8;
3260Sstevel@tonic-gate u |= *p++; u <<= 8;
3270Sstevel@tonic-gate u |= *p++; u <<= 8;
3280Sstevel@tonic-gate return (u | *p);
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate
331237Sanay void
putshort(s,msgp)3320Sstevel@tonic-gate putshort(s, msgp)
3330Sstevel@tonic-gate register u_short s;
3340Sstevel@tonic-gate register u_char *msgp;
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate msgp[1] = s;
3380Sstevel@tonic-gate msgp[0] = s >> 8;
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate
341237Sanay void
putlong(l,msgp)3420Sstevel@tonic-gate putlong(l, msgp)
3430Sstevel@tonic-gate register u_long l;
3440Sstevel@tonic-gate register u_char *msgp;
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate msgp[3] = l;
3480Sstevel@tonic-gate msgp[2] = (l >>= 8);
3490Sstevel@tonic-gate msgp[1] = (l >>= 8);
3500Sstevel@tonic-gate msgp[0] = l >> 8;
3510Sstevel@tonic-gate }
352