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 #pragma ident "%Z%%M% %I% %E% SMI" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <sys/types.h> 437c478bd9Sstevel@tonic-gate #include <stdio.h> 447c478bd9Sstevel@tonic-gate #include <arpa/nameser.h> 457c478bd9Sstevel@tonic-gate 466a1c6faaSanay static int dn_find(u_char *exp_dn, u_char *msg, u_char **dnptrs, 476a1c6faaSanay u_char **lastdnptr); 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate /* 517c478bd9Sstevel@tonic-gate * Expand compressed domain name 'comp_dn' to full domain name. 527c478bd9Sstevel@tonic-gate * 'msg' is a pointer to the begining of the message, 537c478bd9Sstevel@tonic-gate * 'eomorig' points to the first location after the message, 547c478bd9Sstevel@tonic-gate * 'exp_dn' is a pointer to a buffer of size 'length' for the result. 557c478bd9Sstevel@tonic-gate * Return size of compressed name or -1 if there was an error. 567c478bd9Sstevel@tonic-gate */ 576a1c6faaSanay int 587c478bd9Sstevel@tonic-gate dn_expand(msg, eomorig, comp_dn, exp_dn, length) 597c478bd9Sstevel@tonic-gate u_char *msg, *eomorig, *comp_dn, *exp_dn; 607c478bd9Sstevel@tonic-gate int length; 617c478bd9Sstevel@tonic-gate { 627c478bd9Sstevel@tonic-gate register u_char *cp, *dn; 637c478bd9Sstevel@tonic-gate register int n, c; 647c478bd9Sstevel@tonic-gate u_char *eom; 657c478bd9Sstevel@tonic-gate int len = -1, checked = 0; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate dn = exp_dn; 687c478bd9Sstevel@tonic-gate cp = comp_dn; 697c478bd9Sstevel@tonic-gate eom = exp_dn + length; 707c478bd9Sstevel@tonic-gate /* 717c478bd9Sstevel@tonic-gate * fetch next label in domain name 727c478bd9Sstevel@tonic-gate */ 737c478bd9Sstevel@tonic-gate while (n = *cp++) { 747c478bd9Sstevel@tonic-gate /* 757c478bd9Sstevel@tonic-gate * Check for indirection 767c478bd9Sstevel@tonic-gate */ 777c478bd9Sstevel@tonic-gate switch (n & INDIR_MASK) { 787c478bd9Sstevel@tonic-gate case 0: 797c478bd9Sstevel@tonic-gate if (dn != exp_dn) { 807c478bd9Sstevel@tonic-gate if (dn >= eom) 817c478bd9Sstevel@tonic-gate return (-1); 827c478bd9Sstevel@tonic-gate *dn++ = '.'; 837c478bd9Sstevel@tonic-gate } 847c478bd9Sstevel@tonic-gate if (dn+n >= eom) 857c478bd9Sstevel@tonic-gate return (-1); 867c478bd9Sstevel@tonic-gate checked += n + 1; 877c478bd9Sstevel@tonic-gate while (--n >= 0) { 887c478bd9Sstevel@tonic-gate if ((c = *cp++) == '.') { 897c478bd9Sstevel@tonic-gate if (dn + n + 2 >= eom) 907c478bd9Sstevel@tonic-gate return (-1); 917c478bd9Sstevel@tonic-gate *dn++ = '\\'; 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate *dn++ = c; 947c478bd9Sstevel@tonic-gate if (cp >= eomorig) /* out of range */ 957c478bd9Sstevel@tonic-gate return (-1); 967c478bd9Sstevel@tonic-gate } 977c478bd9Sstevel@tonic-gate break; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate case INDIR_MASK: 1007c478bd9Sstevel@tonic-gate if (len < 0) 1017c478bd9Sstevel@tonic-gate len = cp - comp_dn + 1; 1027c478bd9Sstevel@tonic-gate cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 1037c478bd9Sstevel@tonic-gate if (cp < msg || cp >= eomorig) /* out of range */ 1047c478bd9Sstevel@tonic-gate return (-1); 1057c478bd9Sstevel@tonic-gate checked += 2; 1067c478bd9Sstevel@tonic-gate /* 1077c478bd9Sstevel@tonic-gate * Check for loops in the compressed name; 1087c478bd9Sstevel@tonic-gate * if we've looked at the whole message, 1097c478bd9Sstevel@tonic-gate * there must be a loop. 1107c478bd9Sstevel@tonic-gate */ 1117c478bd9Sstevel@tonic-gate if (checked >= eomorig - msg) 1127c478bd9Sstevel@tonic-gate return (-1); 1137c478bd9Sstevel@tonic-gate break; 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate default: 1167c478bd9Sstevel@tonic-gate return (-1); /* flag error */ 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate } 1197c478bd9Sstevel@tonic-gate *dn = '\0'; 1207c478bd9Sstevel@tonic-gate if (len < 0) 1217c478bd9Sstevel@tonic-gate len = cp - comp_dn; 1227c478bd9Sstevel@tonic-gate return (len); 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate /* 1267c478bd9Sstevel@tonic-gate * Compress domain name 'exp_dn' into 'comp_dn'. 1277c478bd9Sstevel@tonic-gate * Return the size of the compressed name or -1. 1287c478bd9Sstevel@tonic-gate * 'length' is the size of the array pointed to by 'comp_dn'. 1297c478bd9Sstevel@tonic-gate * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] 1307c478bd9Sstevel@tonic-gate * is a pointer to the beginning of the message. The list ends with NULL. 1317c478bd9Sstevel@tonic-gate * 'lastdnptr' is a pointer to the end of the arrary pointed to 1327c478bd9Sstevel@tonic-gate * by 'dnptrs'. Side effect is to update the list of pointers for 1337c478bd9Sstevel@tonic-gate * labels inserted into the message as we compress the name. 1347c478bd9Sstevel@tonic-gate * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' 1357c478bd9Sstevel@tonic-gate * is NULL, we don't update the list. 1367c478bd9Sstevel@tonic-gate */ 1376a1c6faaSanay int 1387c478bd9Sstevel@tonic-gate dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) 1397c478bd9Sstevel@tonic-gate u_char *exp_dn, *comp_dn; 1407c478bd9Sstevel@tonic-gate int length; 1417c478bd9Sstevel@tonic-gate u_char **dnptrs, **lastdnptr; 1427c478bd9Sstevel@tonic-gate { 1437c478bd9Sstevel@tonic-gate register u_char *cp, *dn; 1447c478bd9Sstevel@tonic-gate register int c, l; 1457c478bd9Sstevel@tonic-gate u_char **cpp, **lpp, *sp, *eob; 1467c478bd9Sstevel@tonic-gate u_char *msg; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate dn = exp_dn; 1497c478bd9Sstevel@tonic-gate cp = comp_dn; 1507c478bd9Sstevel@tonic-gate eob = cp + length; 1517c478bd9Sstevel@tonic-gate if (dnptrs != NULL) { 1527c478bd9Sstevel@tonic-gate if ((msg = *dnptrs++) != NULL) { 1537c478bd9Sstevel@tonic-gate for (cpp = dnptrs; *cpp != NULL; cpp++) 1547c478bd9Sstevel@tonic-gate ; 1557c478bd9Sstevel@tonic-gate lpp = cpp; /* end of list to search */ 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate } else 1587c478bd9Sstevel@tonic-gate msg = NULL; 1597c478bd9Sstevel@tonic-gate for (c = *dn++; c != '\0'; /*EMPTY*/) { 1607c478bd9Sstevel@tonic-gate /* look to see if we can use pointers */ 1617c478bd9Sstevel@tonic-gate if (msg != NULL) { 1627c478bd9Sstevel@tonic-gate if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { 1637c478bd9Sstevel@tonic-gate if (cp+1 >= eob) 1647c478bd9Sstevel@tonic-gate return (-1); 1657c478bd9Sstevel@tonic-gate *cp++ = (l >> 8) | INDIR_MASK; 1667c478bd9Sstevel@tonic-gate *cp++ = l % 256; 1677c478bd9Sstevel@tonic-gate return (cp - comp_dn); 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate /* not found, save it */ 1707c478bd9Sstevel@tonic-gate if (lastdnptr != NULL && cpp < lastdnptr-1) { 1717c478bd9Sstevel@tonic-gate *cpp++ = cp; 1727c478bd9Sstevel@tonic-gate *cpp = NULL; 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate sp = cp++; /* save ptr to length byte */ 1767c478bd9Sstevel@tonic-gate do { 1777c478bd9Sstevel@tonic-gate if (c == '.') { 1787c478bd9Sstevel@tonic-gate c = *dn++; 1797c478bd9Sstevel@tonic-gate break; 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate if (c == '\\') { 1827c478bd9Sstevel@tonic-gate if ((c = *dn++) == '\0') 1837c478bd9Sstevel@tonic-gate break; 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate if (cp >= eob) { 1867c478bd9Sstevel@tonic-gate if (msg != NULL) 1877c478bd9Sstevel@tonic-gate *lpp = NULL; 1887c478bd9Sstevel@tonic-gate return (-1); 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate *cp++ = c; 1917c478bd9Sstevel@tonic-gate } while ((c = *dn++) != '\0'); 1927c478bd9Sstevel@tonic-gate /* catch trailing '.'s but not '..' */ 1937c478bd9Sstevel@tonic-gate if ((l = cp - sp - 1) == 0 && c == '\0') { 1947c478bd9Sstevel@tonic-gate cp--; 1957c478bd9Sstevel@tonic-gate break; 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate if (l <= 0 || l > MAXLABEL) { 1987c478bd9Sstevel@tonic-gate if (msg != NULL) 1997c478bd9Sstevel@tonic-gate *lpp = NULL; 2007c478bd9Sstevel@tonic-gate return (-1); 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate *sp = l; 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate if (cp >= eob) { 2057c478bd9Sstevel@tonic-gate if (msg != NULL) 2067c478bd9Sstevel@tonic-gate *lpp = NULL; 2077c478bd9Sstevel@tonic-gate return (-1); 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate *cp++ = '\0'; 2107c478bd9Sstevel@tonic-gate return (cp - comp_dn); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* 2147c478bd9Sstevel@tonic-gate * Skip over a compressed domain name. Return the size or -1. 2157c478bd9Sstevel@tonic-gate */ 2166a1c6faaSanay int 2177c478bd9Sstevel@tonic-gate dn_skipname(comp_dn, eom) 2187c478bd9Sstevel@tonic-gate u_char *comp_dn, *eom; 2197c478bd9Sstevel@tonic-gate { 2207c478bd9Sstevel@tonic-gate register u_char *cp; 2217c478bd9Sstevel@tonic-gate register int n; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate cp = comp_dn; 2247c478bd9Sstevel@tonic-gate while (cp < eom && (n = *cp++)) { 2257c478bd9Sstevel@tonic-gate /* 2267c478bd9Sstevel@tonic-gate * check for indirection 2277c478bd9Sstevel@tonic-gate */ 2287c478bd9Sstevel@tonic-gate switch (n & INDIR_MASK) { 2297c478bd9Sstevel@tonic-gate case 0: /* normal case, n == len */ 2307c478bd9Sstevel@tonic-gate cp += n; 2317c478bd9Sstevel@tonic-gate continue; 2327c478bd9Sstevel@tonic-gate default: /* illegal type */ 2337c478bd9Sstevel@tonic-gate return (-1); 2347c478bd9Sstevel@tonic-gate case INDIR_MASK: /* indirection */ 2357c478bd9Sstevel@tonic-gate cp++; 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate break; 2387c478bd9Sstevel@tonic-gate } 2397c478bd9Sstevel@tonic-gate return (cp - comp_dn); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate /* 2437c478bd9Sstevel@tonic-gate * Search for expanded name from a list of previously compressed names. 2447c478bd9Sstevel@tonic-gate * Return the offset from msg if found or -1. 2457c478bd9Sstevel@tonic-gate * dnptrs is the pointer to the first name on the list, 2467c478bd9Sstevel@tonic-gate * not the pointer to the start of the message. 2477c478bd9Sstevel@tonic-gate */ 2486a1c6faaSanay static int 2496a1c6faaSanay dn_find(u_char *exp_dn, u_char *msg, u_char **dnptrs, u_char **lastdnptr) 2507c478bd9Sstevel@tonic-gate { 2517c478bd9Sstevel@tonic-gate register u_char *dn, *cp, **cpp; 2527c478bd9Sstevel@tonic-gate register int n; 2537c478bd9Sstevel@tonic-gate u_char *sp; 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate for (cpp = dnptrs; cpp < lastdnptr; cpp++) { 2567c478bd9Sstevel@tonic-gate dn = exp_dn; 2577c478bd9Sstevel@tonic-gate sp = cp = *cpp; 2587c478bd9Sstevel@tonic-gate while (n = *cp++) { 2597c478bd9Sstevel@tonic-gate /* 2607c478bd9Sstevel@tonic-gate * check for indirection 2617c478bd9Sstevel@tonic-gate */ 2627c478bd9Sstevel@tonic-gate switch (n & INDIR_MASK) { 2637c478bd9Sstevel@tonic-gate case 0: /* normal case, n == len */ 2647c478bd9Sstevel@tonic-gate while (--n >= 0) { 2657c478bd9Sstevel@tonic-gate if (*dn == '.') 2667c478bd9Sstevel@tonic-gate goto next; 2677c478bd9Sstevel@tonic-gate if (*dn == '\\') 2687c478bd9Sstevel@tonic-gate dn++; 2697c478bd9Sstevel@tonic-gate if (*dn++ != *cp++) 2707c478bd9Sstevel@tonic-gate goto next; 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate if ((n = *dn++) == '\0' && *cp == '\0') 2737c478bd9Sstevel@tonic-gate return (sp - msg); 2747c478bd9Sstevel@tonic-gate if (n == '.') 2757c478bd9Sstevel@tonic-gate continue; 2767c478bd9Sstevel@tonic-gate goto next; 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate default: /* illegal type */ 2797c478bd9Sstevel@tonic-gate return (-1); 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate case INDIR_MASK: /* indirection */ 2827c478bd9Sstevel@tonic-gate cp = msg + (((n & 0x3f) << 8) | *cp); 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate if (*dn == '\0') 2867c478bd9Sstevel@tonic-gate return (sp - msg); 2877c478bd9Sstevel@tonic-gate next: /*EMPTY*/; 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate return (-1); 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate /* 2937c478bd9Sstevel@tonic-gate * Routines to insert/extract short/long's. Must account for byte 2947c478bd9Sstevel@tonic-gate * order and non-alignment problems. This code at least has the 2957c478bd9Sstevel@tonic-gate * advantage of being portable. 2967c478bd9Sstevel@tonic-gate * 2977c478bd9Sstevel@tonic-gate * used by sendmail. 2987c478bd9Sstevel@tonic-gate */ 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate u_short 3017c478bd9Sstevel@tonic-gate _getshort(msgp) 3027c478bd9Sstevel@tonic-gate u_char *msgp; 3037c478bd9Sstevel@tonic-gate { 3047c478bd9Sstevel@tonic-gate register u_char *p = (u_char *) msgp; 3057c478bd9Sstevel@tonic-gate #ifdef vax 3067c478bd9Sstevel@tonic-gate /* 3077c478bd9Sstevel@tonic-gate * vax compiler doesn't put shorts in registers 3087c478bd9Sstevel@tonic-gate */ 3097c478bd9Sstevel@tonic-gate register u_long u; 3107c478bd9Sstevel@tonic-gate #else 3117c478bd9Sstevel@tonic-gate register u_short u; 3127c478bd9Sstevel@tonic-gate #endif 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate u = *p++ << 8; 3157c478bd9Sstevel@tonic-gate return ((u_short)(u | *p)); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate u_long 3197c478bd9Sstevel@tonic-gate _getlong(msgp) 3207c478bd9Sstevel@tonic-gate u_char *msgp; 3217c478bd9Sstevel@tonic-gate { 3227c478bd9Sstevel@tonic-gate register u_char *p = (u_char *) msgp; 3237c478bd9Sstevel@tonic-gate register u_long u; 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate u = *p++; u <<= 8; 3267c478bd9Sstevel@tonic-gate u |= *p++; u <<= 8; 3277c478bd9Sstevel@tonic-gate u |= *p++; u <<= 8; 3287c478bd9Sstevel@tonic-gate return (u | *p); 3297c478bd9Sstevel@tonic-gate } 3307c478bd9Sstevel@tonic-gate 3316a1c6faaSanay void 3327c478bd9Sstevel@tonic-gate putshort(s, msgp) 3337c478bd9Sstevel@tonic-gate register u_short s; 3347c478bd9Sstevel@tonic-gate register u_char *msgp; 3357c478bd9Sstevel@tonic-gate { 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate msgp[1] = s; 3387c478bd9Sstevel@tonic-gate msgp[0] = s >> 8; 3397c478bd9Sstevel@tonic-gate } 3407c478bd9Sstevel@tonic-gate 3416a1c6faaSanay void 3427c478bd9Sstevel@tonic-gate putlong(l, msgp) 3437c478bd9Sstevel@tonic-gate register u_long l; 3447c478bd9Sstevel@tonic-gate register u_char *msgp; 3457c478bd9Sstevel@tonic-gate { 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate msgp[3] = l; 3487c478bd9Sstevel@tonic-gate msgp[2] = (l >>= 8); 3497c478bd9Sstevel@tonic-gate msgp[1] = (l >>= 8); 3507c478bd9Sstevel@tonic-gate msgp[0] = l >> 8; 3517c478bd9Sstevel@tonic-gate } 352