1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1996 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include "synonyms.h"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include <sys/types.h>
45*0Sstevel@tonic-gate #include <stdio.h>
46*0Sstevel@tonic-gate #include <arpa/nameser.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate static dn_find();
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate /*
52*0Sstevel@tonic-gate  * Expand compressed domain name 'comp_dn' to full domain name.
53*0Sstevel@tonic-gate  * 'msg' is a pointer to the begining of the message,
54*0Sstevel@tonic-gate  * 'eomorig' points to the first location after the message,
55*0Sstevel@tonic-gate  * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
56*0Sstevel@tonic-gate  * Return size of compressed name or -1 if there was an error.
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate dn_expand(msg, eomorig, comp_dn, exp_dn, length)
59*0Sstevel@tonic-gate 	u_char *msg, *eomorig, *comp_dn, *exp_dn;
60*0Sstevel@tonic-gate 	int length;
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 	register u_char *cp, *dn;
63*0Sstevel@tonic-gate 	register int n, c;
64*0Sstevel@tonic-gate 	u_char *eom;
65*0Sstevel@tonic-gate 	int len = -1, checked = 0;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	dn = exp_dn;
68*0Sstevel@tonic-gate 	cp = comp_dn;
69*0Sstevel@tonic-gate 	eom = exp_dn + length;
70*0Sstevel@tonic-gate 	/*
71*0Sstevel@tonic-gate 	 * fetch next label in domain name
72*0Sstevel@tonic-gate 	 */
73*0Sstevel@tonic-gate 	while (n = *cp++) {
74*0Sstevel@tonic-gate 		/*
75*0Sstevel@tonic-gate 		 * Check for indirection
76*0Sstevel@tonic-gate 		 */
77*0Sstevel@tonic-gate 		switch (n & INDIR_MASK) {
78*0Sstevel@tonic-gate 		case 0:
79*0Sstevel@tonic-gate 			if (dn != exp_dn) {
80*0Sstevel@tonic-gate 				if (dn >= eom)
81*0Sstevel@tonic-gate 					return (-1);
82*0Sstevel@tonic-gate 				*dn++ = '.';
83*0Sstevel@tonic-gate 			}
84*0Sstevel@tonic-gate 			if (dn+n >= eom)
85*0Sstevel@tonic-gate 				return (-1);
86*0Sstevel@tonic-gate 			checked += n + 1;
87*0Sstevel@tonic-gate 			while (--n >= 0) {
88*0Sstevel@tonic-gate 				if ((c = *cp++) == '.') {
89*0Sstevel@tonic-gate 					if (dn + n + 2 >= eom)
90*0Sstevel@tonic-gate 						return (-1);
91*0Sstevel@tonic-gate 					*dn++ = '\\';
92*0Sstevel@tonic-gate 				}
93*0Sstevel@tonic-gate 				*dn++ = c;
94*0Sstevel@tonic-gate 				if (cp >= eomorig)	/* out of range */
95*0Sstevel@tonic-gate 					return (-1);
96*0Sstevel@tonic-gate 			}
97*0Sstevel@tonic-gate 			break;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 		case INDIR_MASK:
100*0Sstevel@tonic-gate 			if (len < 0)
101*0Sstevel@tonic-gate 				len = cp - comp_dn + 1;
102*0Sstevel@tonic-gate 			cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
103*0Sstevel@tonic-gate 			if (cp < msg || cp >= eomorig)	/* out of range */
104*0Sstevel@tonic-gate 				return (-1);
105*0Sstevel@tonic-gate 			checked += 2;
106*0Sstevel@tonic-gate 			/*
107*0Sstevel@tonic-gate 			 * Check for loops in the compressed name;
108*0Sstevel@tonic-gate 			 * if we've looked at the whole message,
109*0Sstevel@tonic-gate 			 * there must be a loop.
110*0Sstevel@tonic-gate 			 */
111*0Sstevel@tonic-gate 			if (checked >= eomorig - msg)
112*0Sstevel@tonic-gate 				return (-1);
113*0Sstevel@tonic-gate 			break;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 		default:
116*0Sstevel@tonic-gate 			return (-1);			/* flag error */
117*0Sstevel@tonic-gate 		}
118*0Sstevel@tonic-gate 	}
119*0Sstevel@tonic-gate 	*dn = '\0';
120*0Sstevel@tonic-gate 	if (len < 0)
121*0Sstevel@tonic-gate 		len = cp - comp_dn;
122*0Sstevel@tonic-gate 	return (len);
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate /*
126*0Sstevel@tonic-gate  * Compress domain name 'exp_dn' into 'comp_dn'.
127*0Sstevel@tonic-gate  * Return the size of the compressed name or -1.
128*0Sstevel@tonic-gate  * 'length' is the size of the array pointed to by 'comp_dn'.
129*0Sstevel@tonic-gate  * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
130*0Sstevel@tonic-gate  * is a pointer to the beginning of the message. The list ends with NULL.
131*0Sstevel@tonic-gate  * 'lastdnptr' is a pointer to the end of the arrary pointed to
132*0Sstevel@tonic-gate  * by 'dnptrs'. Side effect is to update the list of pointers for
133*0Sstevel@tonic-gate  * labels inserted into the message as we compress the name.
134*0Sstevel@tonic-gate  * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
135*0Sstevel@tonic-gate  * is NULL, we don't update the list.
136*0Sstevel@tonic-gate  */
137*0Sstevel@tonic-gate dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
138*0Sstevel@tonic-gate 	u_char *exp_dn, *comp_dn;
139*0Sstevel@tonic-gate 	int length;
140*0Sstevel@tonic-gate 	u_char **dnptrs, **lastdnptr;
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate 	register u_char *cp, *dn;
143*0Sstevel@tonic-gate 	register int c, l;
144*0Sstevel@tonic-gate 	u_char **cpp, **lpp, *sp, *eob;
145*0Sstevel@tonic-gate 	u_char *msg;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	dn = exp_dn;
148*0Sstevel@tonic-gate 	cp = comp_dn;
149*0Sstevel@tonic-gate 	eob = cp + length;
150*0Sstevel@tonic-gate 	if (dnptrs != NULL) {
151*0Sstevel@tonic-gate 		if ((msg = *dnptrs++) != NULL) {
152*0Sstevel@tonic-gate 			for (cpp = dnptrs; *cpp != NULL; cpp++)
153*0Sstevel@tonic-gate 				;
154*0Sstevel@tonic-gate 			lpp = cpp;	/* end of list to search */
155*0Sstevel@tonic-gate 		}
156*0Sstevel@tonic-gate 	} else
157*0Sstevel@tonic-gate 		msg = NULL;
158*0Sstevel@tonic-gate 	for (c = *dn++; c != '\0'; /*EMPTY*/) {
159*0Sstevel@tonic-gate 		/* look to see if we can use pointers */
160*0Sstevel@tonic-gate 		if (msg != NULL) {
161*0Sstevel@tonic-gate 			if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
162*0Sstevel@tonic-gate 				if (cp+1 >= eob)
163*0Sstevel@tonic-gate 					return (-1);
164*0Sstevel@tonic-gate 				*cp++ = (l >> 8) | INDIR_MASK;
165*0Sstevel@tonic-gate 				*cp++ = l % 256;
166*0Sstevel@tonic-gate 				return (cp - comp_dn);
167*0Sstevel@tonic-gate 			}
168*0Sstevel@tonic-gate 			/* not found, save it */
169*0Sstevel@tonic-gate 			if (lastdnptr != NULL && cpp < lastdnptr-1) {
170*0Sstevel@tonic-gate 				*cpp++ = cp;
171*0Sstevel@tonic-gate 				*cpp = NULL;
172*0Sstevel@tonic-gate 			}
173*0Sstevel@tonic-gate 		}
174*0Sstevel@tonic-gate 		sp = cp++;	/* save ptr to length byte */
175*0Sstevel@tonic-gate 		do {
176*0Sstevel@tonic-gate 			if (c == '.') {
177*0Sstevel@tonic-gate 				c = *dn++;
178*0Sstevel@tonic-gate 				break;
179*0Sstevel@tonic-gate 			}
180*0Sstevel@tonic-gate 			if (c == '\\') {
181*0Sstevel@tonic-gate 				if ((c = *dn++) == '\0')
182*0Sstevel@tonic-gate 					break;
183*0Sstevel@tonic-gate 			}
184*0Sstevel@tonic-gate 			if (cp >= eob) {
185*0Sstevel@tonic-gate 				if (msg != NULL)
186*0Sstevel@tonic-gate 					*lpp = NULL;
187*0Sstevel@tonic-gate 				return (-1);
188*0Sstevel@tonic-gate 			}
189*0Sstevel@tonic-gate 			*cp++ = c;
190*0Sstevel@tonic-gate 		} while ((c = *dn++) != '\0');
191*0Sstevel@tonic-gate 		/* catch trailing '.'s but not '..' */
192*0Sstevel@tonic-gate 		if ((l = cp - sp - 1) == 0 && c == '\0') {
193*0Sstevel@tonic-gate 			cp--;
194*0Sstevel@tonic-gate 			break;
195*0Sstevel@tonic-gate 		}
196*0Sstevel@tonic-gate 		if (l <= 0 || l > MAXLABEL) {
197*0Sstevel@tonic-gate 			if (msg != NULL)
198*0Sstevel@tonic-gate 				*lpp = NULL;
199*0Sstevel@tonic-gate 			return (-1);
200*0Sstevel@tonic-gate 		}
201*0Sstevel@tonic-gate 		*sp = l;
202*0Sstevel@tonic-gate 	}
203*0Sstevel@tonic-gate 	if (cp >= eob) {
204*0Sstevel@tonic-gate 		if (msg != NULL)
205*0Sstevel@tonic-gate 			*lpp = NULL;
206*0Sstevel@tonic-gate 		return (-1);
207*0Sstevel@tonic-gate 	}
208*0Sstevel@tonic-gate 	*cp++ = '\0';
209*0Sstevel@tonic-gate 	return (cp - comp_dn);
210*0Sstevel@tonic-gate }
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate /*
213*0Sstevel@tonic-gate  * Skip over a compressed domain name. Return the size or -1.
214*0Sstevel@tonic-gate  */
215*0Sstevel@tonic-gate dn_skipname(comp_dn, eom)
216*0Sstevel@tonic-gate 	u_char *comp_dn, *eom;
217*0Sstevel@tonic-gate {
218*0Sstevel@tonic-gate 	register u_char *cp;
219*0Sstevel@tonic-gate 	register int n;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	cp = comp_dn;
222*0Sstevel@tonic-gate 	while (cp < eom && (n = *cp++)) {
223*0Sstevel@tonic-gate 		/*
224*0Sstevel@tonic-gate 		 * check for indirection
225*0Sstevel@tonic-gate 		 */
226*0Sstevel@tonic-gate 		switch (n & INDIR_MASK) {
227*0Sstevel@tonic-gate 		case 0:		/* normal case, n == len */
228*0Sstevel@tonic-gate 			cp += n;
229*0Sstevel@tonic-gate 			continue;
230*0Sstevel@tonic-gate 		default:	/* illegal type */
231*0Sstevel@tonic-gate 			return (-1);
232*0Sstevel@tonic-gate 		case INDIR_MASK:	/* indirection */
233*0Sstevel@tonic-gate 			cp++;
234*0Sstevel@tonic-gate 		}
235*0Sstevel@tonic-gate 		break;
236*0Sstevel@tonic-gate 	}
237*0Sstevel@tonic-gate 	return (cp - comp_dn);
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate /*
241*0Sstevel@tonic-gate  * Search for expanded name from a list of previously compressed names.
242*0Sstevel@tonic-gate  * Return the offset from msg if found or -1.
243*0Sstevel@tonic-gate  * dnptrs is the pointer to the first name on the list,
244*0Sstevel@tonic-gate  * not the pointer to the start of the message.
245*0Sstevel@tonic-gate  */
246*0Sstevel@tonic-gate static
247*0Sstevel@tonic-gate dn_find(exp_dn, msg, dnptrs, lastdnptr)
248*0Sstevel@tonic-gate 	u_char *exp_dn, *msg;
249*0Sstevel@tonic-gate 	u_char **dnptrs, **lastdnptr;
250*0Sstevel@tonic-gate {
251*0Sstevel@tonic-gate 	register u_char *dn, *cp, **cpp;
252*0Sstevel@tonic-gate 	register int n;
253*0Sstevel@tonic-gate 	u_char *sp;
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
256*0Sstevel@tonic-gate 		dn = exp_dn;
257*0Sstevel@tonic-gate 		sp = cp = *cpp;
258*0Sstevel@tonic-gate 		while (n = *cp++) {
259*0Sstevel@tonic-gate 			/*
260*0Sstevel@tonic-gate 			 * check for indirection
261*0Sstevel@tonic-gate 			 */
262*0Sstevel@tonic-gate 			switch (n & INDIR_MASK) {
263*0Sstevel@tonic-gate 			case 0:		/* normal case, n == len */
264*0Sstevel@tonic-gate 				while (--n >= 0) {
265*0Sstevel@tonic-gate 					if (*dn == '.')
266*0Sstevel@tonic-gate 						goto next;
267*0Sstevel@tonic-gate 					if (*dn == '\\')
268*0Sstevel@tonic-gate 						dn++;
269*0Sstevel@tonic-gate 					if (*dn++ != *cp++)
270*0Sstevel@tonic-gate 						goto next;
271*0Sstevel@tonic-gate 				}
272*0Sstevel@tonic-gate 				if ((n = *dn++) == '\0' && *cp == '\0')
273*0Sstevel@tonic-gate 					return (sp - msg);
274*0Sstevel@tonic-gate 				if (n == '.')
275*0Sstevel@tonic-gate 					continue;
276*0Sstevel@tonic-gate 				goto next;
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 			default:	/* illegal type */
279*0Sstevel@tonic-gate 				return (-1);
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 			case INDIR_MASK:	/* indirection */
282*0Sstevel@tonic-gate 				cp = msg + (((n & 0x3f) << 8) | *cp);
283*0Sstevel@tonic-gate 			}
284*0Sstevel@tonic-gate 		}
285*0Sstevel@tonic-gate 		if (*dn == '\0')
286*0Sstevel@tonic-gate 			return (sp - msg);
287*0Sstevel@tonic-gate 	next:	/*EMPTY*/;
288*0Sstevel@tonic-gate 	}
289*0Sstevel@tonic-gate 	return (-1);
290*0Sstevel@tonic-gate }
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate /*
293*0Sstevel@tonic-gate  * Routines to insert/extract short/long's. Must account for byte
294*0Sstevel@tonic-gate  * order and non-alignment problems. This code at least has the
295*0Sstevel@tonic-gate  * advantage of being portable.
296*0Sstevel@tonic-gate  *
297*0Sstevel@tonic-gate  * used by sendmail.
298*0Sstevel@tonic-gate  */
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate u_short
301*0Sstevel@tonic-gate _getshort(msgp)
302*0Sstevel@tonic-gate 	u_char *msgp;
303*0Sstevel@tonic-gate {
304*0Sstevel@tonic-gate 	register u_char *p = (u_char *) msgp;
305*0Sstevel@tonic-gate #ifdef vax
306*0Sstevel@tonic-gate 	/*
307*0Sstevel@tonic-gate 	 * vax compiler doesn't put shorts in registers
308*0Sstevel@tonic-gate 	 */
309*0Sstevel@tonic-gate 	register u_long u;
310*0Sstevel@tonic-gate #else
311*0Sstevel@tonic-gate 	register u_short u;
312*0Sstevel@tonic-gate #endif
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 	u = *p++ << 8;
315*0Sstevel@tonic-gate 	return ((u_short)(u | *p));
316*0Sstevel@tonic-gate }
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate u_long
319*0Sstevel@tonic-gate _getlong(msgp)
320*0Sstevel@tonic-gate 	u_char *msgp;
321*0Sstevel@tonic-gate {
322*0Sstevel@tonic-gate 	register u_char *p = (u_char *) msgp;
323*0Sstevel@tonic-gate 	register u_long u;
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	u = *p++; u <<= 8;
326*0Sstevel@tonic-gate 	u |= *p++; u <<= 8;
327*0Sstevel@tonic-gate 	u |= *p++; u <<= 8;
328*0Sstevel@tonic-gate 	return (u | *p);
329*0Sstevel@tonic-gate }
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate putshort(s, msgp)
333*0Sstevel@tonic-gate 	register u_short s;
334*0Sstevel@tonic-gate 	register u_char *msgp;
335*0Sstevel@tonic-gate {
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	msgp[1] = s;
338*0Sstevel@tonic-gate 	msgp[0] = s >> 8;
339*0Sstevel@tonic-gate }
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate putlong(l, msgp)
342*0Sstevel@tonic-gate 	register u_long l;
343*0Sstevel@tonic-gate 	register u_char *msgp;
344*0Sstevel@tonic-gate {
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	msgp[3] = l;
347*0Sstevel@tonic-gate 	msgp[2] = (l >>= 8);
348*0Sstevel@tonic-gate 	msgp[1] = (l >>= 8);
349*0Sstevel@tonic-gate 	msgp[0] = l >> 8;
350*0Sstevel@tonic-gate }
351