17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21e8031f0aSraf
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate * The Regents of the University of California
337c478bd9Sstevel@tonic-gate * All Rights Reserved
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate * contributors.
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
437c478bd9Sstevel@tonic-gate
446a1c6faaSanay static int dn_find(u_char *exp_dn, u_char *msg, u_char **dnptrs,
456a1c6faaSanay u_char **lastdnptr);
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate * Expand compressed domain name 'comp_dn' to full domain name.
507c478bd9Sstevel@tonic-gate * 'msg' is a pointer to the begining of the message,
517c478bd9Sstevel@tonic-gate * 'eomorig' points to the first location after the message,
527c478bd9Sstevel@tonic-gate * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
537c478bd9Sstevel@tonic-gate * Return size of compressed name or -1 if there was an error.
547c478bd9Sstevel@tonic-gate */
556a1c6faaSanay int
dn_expand(msg,eomorig,comp_dn,exp_dn,length)567c478bd9Sstevel@tonic-gate dn_expand(msg, eomorig, comp_dn, exp_dn, length)
577c478bd9Sstevel@tonic-gate u_char *msg, *eomorig, *comp_dn, *exp_dn;
587c478bd9Sstevel@tonic-gate int length;
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate register u_char *cp, *dn;
617c478bd9Sstevel@tonic-gate register int n, c;
627c478bd9Sstevel@tonic-gate u_char *eom;
637c478bd9Sstevel@tonic-gate int len = -1, checked = 0;
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate dn = exp_dn;
667c478bd9Sstevel@tonic-gate cp = comp_dn;
677c478bd9Sstevel@tonic-gate eom = exp_dn + length;
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate * fetch next label in domain name
707c478bd9Sstevel@tonic-gate */
717c478bd9Sstevel@tonic-gate while (n = *cp++) {
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate * Check for indirection
747c478bd9Sstevel@tonic-gate */
757c478bd9Sstevel@tonic-gate switch (n & INDIR_MASK) {
767c478bd9Sstevel@tonic-gate case 0:
777c478bd9Sstevel@tonic-gate if (dn != exp_dn) {
787c478bd9Sstevel@tonic-gate if (dn >= eom)
797c478bd9Sstevel@tonic-gate return (-1);
807c478bd9Sstevel@tonic-gate *dn++ = '.';
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate if (dn+n >= eom)
837c478bd9Sstevel@tonic-gate return (-1);
847c478bd9Sstevel@tonic-gate checked += n + 1;
857c478bd9Sstevel@tonic-gate while (--n >= 0) {
867c478bd9Sstevel@tonic-gate if ((c = *cp++) == '.') {
877c478bd9Sstevel@tonic-gate if (dn + n + 2 >= eom)
887c478bd9Sstevel@tonic-gate return (-1);
897c478bd9Sstevel@tonic-gate *dn++ = '\\';
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate *dn++ = c;
927c478bd9Sstevel@tonic-gate if (cp >= eomorig) /* out of range */
937c478bd9Sstevel@tonic-gate return (-1);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate break;
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate case INDIR_MASK:
987c478bd9Sstevel@tonic-gate if (len < 0)
997c478bd9Sstevel@tonic-gate len = cp - comp_dn + 1;
1007c478bd9Sstevel@tonic-gate cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
1017c478bd9Sstevel@tonic-gate if (cp < msg || cp >= eomorig) /* out of range */
1027c478bd9Sstevel@tonic-gate return (-1);
1037c478bd9Sstevel@tonic-gate checked += 2;
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate * Check for loops in the compressed name;
1067c478bd9Sstevel@tonic-gate * if we've looked at the whole message,
1077c478bd9Sstevel@tonic-gate * there must be a loop.
1087c478bd9Sstevel@tonic-gate */
1097c478bd9Sstevel@tonic-gate if (checked >= eomorig - msg)
1107c478bd9Sstevel@tonic-gate return (-1);
1117c478bd9Sstevel@tonic-gate break;
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate default:
1147c478bd9Sstevel@tonic-gate return (-1); /* flag error */
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate *dn = '\0';
1187c478bd9Sstevel@tonic-gate if (len < 0)
1197c478bd9Sstevel@tonic-gate len = cp - comp_dn;
1207c478bd9Sstevel@tonic-gate return (len);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate * Compress domain name 'exp_dn' into 'comp_dn'.
1257c478bd9Sstevel@tonic-gate * Return the size of the compressed name or -1.
1267c478bd9Sstevel@tonic-gate * 'length' is the size of the array pointed to by 'comp_dn'.
1277c478bd9Sstevel@tonic-gate * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
1287c478bd9Sstevel@tonic-gate * is a pointer to the beginning of the message. The list ends with NULL.
1297c478bd9Sstevel@tonic-gate * 'lastdnptr' is a pointer to the end of the arrary pointed to
1307c478bd9Sstevel@tonic-gate * by 'dnptrs'. Side effect is to update the list of pointers for
1317c478bd9Sstevel@tonic-gate * labels inserted into the message as we compress the name.
1327c478bd9Sstevel@tonic-gate * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
1337c478bd9Sstevel@tonic-gate * is NULL, we don't update the list.
1347c478bd9Sstevel@tonic-gate */
1356a1c6faaSanay int
dn_comp(exp_dn,comp_dn,length,dnptrs,lastdnptr)1367c478bd9Sstevel@tonic-gate dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
1377c478bd9Sstevel@tonic-gate u_char *exp_dn, *comp_dn;
1387c478bd9Sstevel@tonic-gate int length;
1397c478bd9Sstevel@tonic-gate u_char **dnptrs, **lastdnptr;
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate register u_char *cp, *dn;
1427c478bd9Sstevel@tonic-gate register int c, l;
1437c478bd9Sstevel@tonic-gate u_char **cpp, **lpp, *sp, *eob;
1447c478bd9Sstevel@tonic-gate u_char *msg;
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate dn = exp_dn;
1477c478bd9Sstevel@tonic-gate cp = comp_dn;
1487c478bd9Sstevel@tonic-gate eob = cp + length;
1497c478bd9Sstevel@tonic-gate if (dnptrs != NULL) {
1507c478bd9Sstevel@tonic-gate if ((msg = *dnptrs++) != NULL) {
1517c478bd9Sstevel@tonic-gate for (cpp = dnptrs; *cpp != NULL; cpp++)
1527c478bd9Sstevel@tonic-gate ;
1537c478bd9Sstevel@tonic-gate lpp = cpp; /* end of list to search */
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate } else
1567c478bd9Sstevel@tonic-gate msg = NULL;
1577c478bd9Sstevel@tonic-gate for (c = *dn++; c != '\0'; /*EMPTY*/) {
1587c478bd9Sstevel@tonic-gate /* look to see if we can use pointers */
1597c478bd9Sstevel@tonic-gate if (msg != NULL) {
1607c478bd9Sstevel@tonic-gate if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
1617c478bd9Sstevel@tonic-gate if (cp+1 >= eob)
1627c478bd9Sstevel@tonic-gate return (-1);
1637c478bd9Sstevel@tonic-gate *cp++ = (l >> 8) | INDIR_MASK;
1647c478bd9Sstevel@tonic-gate *cp++ = l % 256;
1657c478bd9Sstevel@tonic-gate return (cp - comp_dn);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate /* not found, save it */
1687c478bd9Sstevel@tonic-gate if (lastdnptr != NULL && cpp < lastdnptr-1) {
1697c478bd9Sstevel@tonic-gate *cpp++ = cp;
1707c478bd9Sstevel@tonic-gate *cpp = NULL;
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate sp = cp++; /* save ptr to length byte */
1747c478bd9Sstevel@tonic-gate do {
1757c478bd9Sstevel@tonic-gate if (c == '.') {
1767c478bd9Sstevel@tonic-gate c = *dn++;
1777c478bd9Sstevel@tonic-gate break;
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate if (c == '\\') {
1807c478bd9Sstevel@tonic-gate if ((c = *dn++) == '\0')
1817c478bd9Sstevel@tonic-gate break;
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate if (cp >= eob) {
1847c478bd9Sstevel@tonic-gate if (msg != NULL)
1857c478bd9Sstevel@tonic-gate *lpp = NULL;
1867c478bd9Sstevel@tonic-gate return (-1);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate *cp++ = c;
1897c478bd9Sstevel@tonic-gate } while ((c = *dn++) != '\0');
1907c478bd9Sstevel@tonic-gate /* catch trailing '.'s but not '..' */
1917c478bd9Sstevel@tonic-gate if ((l = cp - sp - 1) == 0 && c == '\0') {
1927c478bd9Sstevel@tonic-gate cp--;
1937c478bd9Sstevel@tonic-gate break;
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate if (l <= 0 || l > MAXLABEL) {
1967c478bd9Sstevel@tonic-gate if (msg != NULL)
1977c478bd9Sstevel@tonic-gate *lpp = NULL;
1987c478bd9Sstevel@tonic-gate return (-1);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate *sp = l;
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate if (cp >= eob) {
2037c478bd9Sstevel@tonic-gate if (msg != NULL)
2047c478bd9Sstevel@tonic-gate *lpp = NULL;
2057c478bd9Sstevel@tonic-gate return (-1);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate *cp++ = '\0';
2087c478bd9Sstevel@tonic-gate return (cp - comp_dn);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate * Skip over a compressed domain name. Return the size or -1.
2137c478bd9Sstevel@tonic-gate */
2146a1c6faaSanay int
dn_skipname(comp_dn,eom)2157c478bd9Sstevel@tonic-gate dn_skipname(comp_dn, eom)
2167c478bd9Sstevel@tonic-gate u_char *comp_dn, *eom;
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate register u_char *cp;
2197c478bd9Sstevel@tonic-gate register int n;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate cp = comp_dn;
2227c478bd9Sstevel@tonic-gate while (cp < eom && (n = *cp++)) {
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate * check for indirection
2257c478bd9Sstevel@tonic-gate */
2267c478bd9Sstevel@tonic-gate switch (n & INDIR_MASK) {
2277c478bd9Sstevel@tonic-gate case 0: /* normal case, n == len */
2287c478bd9Sstevel@tonic-gate cp += n;
2297c478bd9Sstevel@tonic-gate continue;
2307c478bd9Sstevel@tonic-gate default: /* illegal type */
2317c478bd9Sstevel@tonic-gate return (-1);
2327c478bd9Sstevel@tonic-gate case INDIR_MASK: /* indirection */
2337c478bd9Sstevel@tonic-gate cp++;
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate break;
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate return (cp - comp_dn);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate * Search for expanded name from a list of previously compressed names.
2427c478bd9Sstevel@tonic-gate * Return the offset from msg if found or -1.
2437c478bd9Sstevel@tonic-gate * dnptrs is the pointer to the first name on the list,
2447c478bd9Sstevel@tonic-gate * not the pointer to the start of the message.
2457c478bd9Sstevel@tonic-gate */
2466a1c6faaSanay static int
dn_find(u_char * exp_dn,u_char * msg,u_char ** dnptrs,u_char ** lastdnptr)2476a1c6faaSanay dn_find(u_char *exp_dn, u_char *msg, u_char **dnptrs, u_char **lastdnptr)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate register u_char *dn, *cp, **cpp;
2507c478bd9Sstevel@tonic-gate register int n;
2517c478bd9Sstevel@tonic-gate u_char *sp;
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
2547c478bd9Sstevel@tonic-gate dn = exp_dn;
2557c478bd9Sstevel@tonic-gate sp = cp = *cpp;
2567c478bd9Sstevel@tonic-gate while (n = *cp++) {
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate * check for indirection
2597c478bd9Sstevel@tonic-gate */
2607c478bd9Sstevel@tonic-gate switch (n & INDIR_MASK) {
2617c478bd9Sstevel@tonic-gate case 0: /* normal case, n == len */
2627c478bd9Sstevel@tonic-gate while (--n >= 0) {
2637c478bd9Sstevel@tonic-gate if (*dn == '.')
2647c478bd9Sstevel@tonic-gate goto next;
2657c478bd9Sstevel@tonic-gate if (*dn == '\\')
2667c478bd9Sstevel@tonic-gate dn++;
2677c478bd9Sstevel@tonic-gate if (*dn++ != *cp++)
2687c478bd9Sstevel@tonic-gate goto next;
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate if ((n = *dn++) == '\0' && *cp == '\0')
2717c478bd9Sstevel@tonic-gate return (sp - msg);
2727c478bd9Sstevel@tonic-gate if (n == '.')
2737c478bd9Sstevel@tonic-gate continue;
2747c478bd9Sstevel@tonic-gate goto next;
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate default: /* illegal type */
2777c478bd9Sstevel@tonic-gate return (-1);
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate case INDIR_MASK: /* indirection */
2807c478bd9Sstevel@tonic-gate cp = msg + (((n & 0x3f) << 8) | *cp);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate if (*dn == '\0')
2847c478bd9Sstevel@tonic-gate return (sp - msg);
2857c478bd9Sstevel@tonic-gate next: /*EMPTY*/;
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate return (-1);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate /*
2917c478bd9Sstevel@tonic-gate * Routines to insert/extract short/long's. Must account for byte
2927c478bd9Sstevel@tonic-gate * order and non-alignment problems. This code at least has the
2937c478bd9Sstevel@tonic-gate * advantage of being portable.
2947c478bd9Sstevel@tonic-gate *
2957c478bd9Sstevel@tonic-gate * used by sendmail.
2967c478bd9Sstevel@tonic-gate */
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate u_short
_getshort(msgp)2997c478bd9Sstevel@tonic-gate _getshort(msgp)
3007c478bd9Sstevel@tonic-gate u_char *msgp;
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate register u_char *p = (u_char *) msgp;
3037c478bd9Sstevel@tonic-gate #ifdef vax
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate * vax compiler doesn't put shorts in registers
3067c478bd9Sstevel@tonic-gate */
3077c478bd9Sstevel@tonic-gate register u_long u;
3087c478bd9Sstevel@tonic-gate #else
3097c478bd9Sstevel@tonic-gate register u_short u;
3107c478bd9Sstevel@tonic-gate #endif
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate u = *p++ << 8;
3137c478bd9Sstevel@tonic-gate return ((u_short)(u | *p));
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate u_long
_getlong(msgp)3177c478bd9Sstevel@tonic-gate _getlong(msgp)
3187c478bd9Sstevel@tonic-gate u_char *msgp;
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate register u_char *p = (u_char *) msgp;
3217c478bd9Sstevel@tonic-gate register u_long u;
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate u = *p++; u <<= 8;
3247c478bd9Sstevel@tonic-gate u |= *p++; u <<= 8;
3257c478bd9Sstevel@tonic-gate u |= *p++; u <<= 8;
3267c478bd9Sstevel@tonic-gate return (u | *p);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate
3296a1c6faaSanay void
putshort(s,msgp)3307c478bd9Sstevel@tonic-gate putshort(s, msgp)
3317c478bd9Sstevel@tonic-gate register u_short s;
3327c478bd9Sstevel@tonic-gate register u_char *msgp;
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate msgp[1] = s;
3367c478bd9Sstevel@tonic-gate msgp[0] = s >> 8;
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate
3396a1c6faaSanay void
putlong(l,msgp)3407c478bd9Sstevel@tonic-gate putlong(l, msgp)
3417c478bd9Sstevel@tonic-gate register u_long l;
3427c478bd9Sstevel@tonic-gate register u_char *msgp;
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate msgp[3] = l;
3467c478bd9Sstevel@tonic-gate msgp[2] = (l >>= 8);
3477c478bd9Sstevel@tonic-gate msgp[1] = (l >>= 8);
3487c478bd9Sstevel@tonic-gate msgp[0] = l >> 8;
3497c478bd9Sstevel@tonic-gate }
350