xref: /titanic_41/usr/src/lib/libldap5/sources/ldap/common/getdxbyname.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
2*7c478bd9Sstevel@tonic-gate 
3*7c478bd9Sstevel@tonic-gate /*
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
5*7c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
6*7c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
7*7c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
10*7c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11*7c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
12*7c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
15*7c478bd9Sstevel@tonic-gate  * March 31, 1998.
16*7c478bd9Sstevel@tonic-gate  *
17*7c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
18*7c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
19*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
20*7c478bd9Sstevel@tonic-gate  * Rights Reserved.
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * Contributor(s):
23*7c478bd9Sstevel@tonic-gate  */
24*7c478bd9Sstevel@tonic-gate /*
25*7c478bd9Sstevel@tonic-gate  *  Copyright (c) 1995 Regents of the University of Michigan.
26*7c478bd9Sstevel@tonic-gate  *  All rights reserved.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * nsldapi_getdxbyname - retrieve DX records from the DNS (from
30*7c478bd9Sstevel@tonic-gate  * TXT records for now)
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DNS
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate XXX not MT-safe XXX
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <string.h>
40*7c478bd9Sstevel@tonic-gate #include <ctype.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #ifdef macintosh
43*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
44*7c478bd9Sstevel@tonic-gate #include "macos.h"
45*7c478bd9Sstevel@tonic-gate #endif /* macintosh */
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #ifdef _WINDOWS
48*7c478bd9Sstevel@tonic-gate #include <windows.h>
49*7c478bd9Sstevel@tonic-gate #endif
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #if !defined(macintosh) && !defined(DOS) && !defined( _WINDOWS )
52*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
53*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
54*7c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
58*7c478bd9Sstevel@tonic-gate #include <netdb.h>
59*7c478bd9Sstevel@tonic-gate #include <resolv.h>
60*7c478bd9Sstevel@tonic-gate #endif
61*7c478bd9Sstevel@tonic-gate #include "ldap-int.h"
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate #if defined( DOS )
64*7c478bd9Sstevel@tonic-gate #include "msdos.h"
65*7c478bd9Sstevel@tonic-gate #endif /* DOS */
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate #ifdef NEEDPROTOS
69*7c478bd9Sstevel@tonic-gate static char ** decode_answer( unsigned char *answer, int len );
70*7c478bd9Sstevel@tonic-gate #else /* NEEDPROTOS */
71*7c478bd9Sstevel@tonic-gate static char **decode_answer();
72*7c478bd9Sstevel@tonic-gate #endif /* NEEDPROTOS */
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate extern int h_errno;
75*7c478bd9Sstevel@tonic-gate extern char *h_errlist[];
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate #define MAX_TO_SORT	32
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate /*
82*7c478bd9Sstevel@tonic-gate  * nsldapi_getdxbyname - lookup DNS DX records for domain and return an ordered
83*7c478bd9Sstevel@tonic-gate  *	array.
84*7c478bd9Sstevel@tonic-gate  */
85*7c478bd9Sstevel@tonic-gate char **
nsldapi_getdxbyname(char * domain)86*7c478bd9Sstevel@tonic-gate nsldapi_getdxbyname( char *domain )
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate     unsigned char	buf[ PACKETSZ ];
89*7c478bd9Sstevel@tonic-gate     char		**dxs;
90*7c478bd9Sstevel@tonic-gate     int			rc;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate     LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_getdxbyname( %s )\n", domain, 0, 0 );
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate     memset( buf, 0, sizeof( buf ));
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate     /* XXX not MT safe XXX */
97*7c478bd9Sstevel@tonic-gate     if (( rc = res_search( domain, C_IN, T_TXT, buf, sizeof( buf ))) < 0
98*7c478bd9Sstevel@tonic-gate 		|| ( dxs = decode_answer( buf, rc )) == NULL ) {
99*7c478bd9Sstevel@tonic-gate 	/*
100*7c478bd9Sstevel@tonic-gate 	 * punt:  return list conisting of the original domain name only
101*7c478bd9Sstevel@tonic-gate 	 */
102*7c478bd9Sstevel@tonic-gate 	if (( dxs = (char **)NSLDAPI_MALLOC( 2 * sizeof( char * ))) == NULL ||
103*7c478bd9Sstevel@tonic-gate 		( dxs[ 0 ] = nsldapi_strdup( domain )) == NULL ) {
104*7c478bd9Sstevel@tonic-gate 	    if ( dxs != NULL ) {
105*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( dxs );
106*7c478bd9Sstevel@tonic-gate 	    }
107*7c478bd9Sstevel@tonic-gate 	    dxs = NULL;
108*7c478bd9Sstevel@tonic-gate 	} else {
109*7c478bd9Sstevel@tonic-gate 	    dxs[ 1 ] = NULL;
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate     }
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate     return( dxs );
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate static char **
decode_answer(unsigned char * answer,int len)118*7c478bd9Sstevel@tonic-gate decode_answer( unsigned char *answer, int len )
119*7c478bd9Sstevel@tonic-gate {
120*7c478bd9Sstevel@tonic-gate     HEADER		*hp;
121*7c478bd9Sstevel@tonic-gate     char		buf[ 256 ], **dxs;
122*7c478bd9Sstevel@tonic-gate     unsigned char	*eom, *p;
123*7c478bd9Sstevel@tonic-gate     int			ancount, err, rc, type, class, dx_count, rr_len;
124*7c478bd9Sstevel@tonic-gate     int			dx_pref[ MAX_TO_SORT ];
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG
127*7c478bd9Sstevel@tonic-gate     if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
128*7c478bd9Sstevel@tonic-gate /*	__p_query( answer );	*/
129*7c478bd9Sstevel@tonic-gate     }
130*7c478bd9Sstevel@tonic-gate #endif /* LDAP_DEBUG */
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate     dxs = NULL;
133*7c478bd9Sstevel@tonic-gate     hp = (HEADER *)answer;
134*7c478bd9Sstevel@tonic-gate     eom = answer + len;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate     if ( ntohs( hp->qdcount ) != 1 ) {
137*7c478bd9Sstevel@tonic-gate 	h_errno = NO_RECOVERY;
138*7c478bd9Sstevel@tonic-gate 	return( NULL );
139*7c478bd9Sstevel@tonic-gate     }
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate     ancount = ntohs( hp->ancount );
142*7c478bd9Sstevel@tonic-gate     if ( ancount < 1 ) {
143*7c478bd9Sstevel@tonic-gate 	h_errno = NO_DATA;
144*7c478bd9Sstevel@tonic-gate 	return( NULL );
145*7c478bd9Sstevel@tonic-gate     }
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate     /*
148*7c478bd9Sstevel@tonic-gate      * skip over the query
149*7c478bd9Sstevel@tonic-gate      */
150*7c478bd9Sstevel@tonic-gate     p = answer + HFIXEDSZ;
151*7c478bd9Sstevel@tonic-gate     if (( rc = dn_expand( answer, eom, p, buf, sizeof( buf ))) < 0 ) {
152*7c478bd9Sstevel@tonic-gate 	h_errno = NO_RECOVERY;
153*7c478bd9Sstevel@tonic-gate 	return( NULL );
154*7c478bd9Sstevel@tonic-gate     }
155*7c478bd9Sstevel@tonic-gate     p += ( rc + QFIXEDSZ );
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate     /*
158*7c478bd9Sstevel@tonic-gate      * pull out the answers we are interested in
159*7c478bd9Sstevel@tonic-gate      */
160*7c478bd9Sstevel@tonic-gate     err = dx_count = 0;
161*7c478bd9Sstevel@tonic-gate     while ( ancount > 0 && err == 0 && p < eom ) {
162*7c478bd9Sstevel@tonic-gate 	if (( rc = dn_expand( answer, eom, p, buf, sizeof( buf ))) < 0 ) {
163*7c478bd9Sstevel@tonic-gate 	    err = NO_RECOVERY;
164*7c478bd9Sstevel@tonic-gate 	    continue;
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate 	p += rc;	/* skip over name */
167*7c478bd9Sstevel@tonic-gate 	type = _getshort( p );
168*7c478bd9Sstevel@tonic-gate 	p += INT16SZ;
169*7c478bd9Sstevel@tonic-gate 	class = _getshort( p );
170*7c478bd9Sstevel@tonic-gate 	p += INT16SZ;
171*7c478bd9Sstevel@tonic-gate 	p += INT32SZ;		/* skip over TTL */
172*7c478bd9Sstevel@tonic-gate 	rr_len = _getshort( p );
173*7c478bd9Sstevel@tonic-gate 	p += INT16SZ;
174*7c478bd9Sstevel@tonic-gate 	if ( class == C_IN && type == T_TXT ) {
175*7c478bd9Sstevel@tonic-gate 	    int 	i, n, pref, txt_len;
176*7c478bd9Sstevel@tonic-gate 	    char	*q, *r;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	    q = (char *)p;
179*7c478bd9Sstevel@tonic-gate 	    while ( q < (char *)p + rr_len && err == 0 ) {
180*7c478bd9Sstevel@tonic-gate 		if ( *q >= 3 && strncasecmp( q + 1, "dx:", 3 ) == 0 ) {
181*7c478bd9Sstevel@tonic-gate 		    txt_len = *q - 3;
182*7c478bd9Sstevel@tonic-gate 		    r = q + 4;
183*7c478bd9Sstevel@tonic-gate 		    while ( isspace( *r )) {
184*7c478bd9Sstevel@tonic-gate 			++r;
185*7c478bd9Sstevel@tonic-gate 			--txt_len;
186*7c478bd9Sstevel@tonic-gate 		    }
187*7c478bd9Sstevel@tonic-gate 		    pref = 0;
188*7c478bd9Sstevel@tonic-gate 		    while ( isdigit( *r )) {
189*7c478bd9Sstevel@tonic-gate 			pref *= 10;
190*7c478bd9Sstevel@tonic-gate 			pref += ( *r - '0' );
191*7c478bd9Sstevel@tonic-gate 			++r;
192*7c478bd9Sstevel@tonic-gate 			--txt_len;
193*7c478bd9Sstevel@tonic-gate 		    }
194*7c478bd9Sstevel@tonic-gate 		    if ( dx_count < MAX_TO_SORT - 1 ) {
195*7c478bd9Sstevel@tonic-gate 			dx_pref[ dx_count ] = pref;
196*7c478bd9Sstevel@tonic-gate 		    }
197*7c478bd9Sstevel@tonic-gate 		    while ( isspace( *r )) {
198*7c478bd9Sstevel@tonic-gate 			++r;
199*7c478bd9Sstevel@tonic-gate 			--txt_len;
200*7c478bd9Sstevel@tonic-gate 		    }
201*7c478bd9Sstevel@tonic-gate 		    if ( dx_count == 0 ) {
202*7c478bd9Sstevel@tonic-gate 			dxs = (char **)NSLDAPI_MALLOC( 2 * sizeof( char * ));
203*7c478bd9Sstevel@tonic-gate 		    } else {
204*7c478bd9Sstevel@tonic-gate 			dxs = (char **)NSLDAPI_REALLOC( dxs,
205*7c478bd9Sstevel@tonic-gate 				( dx_count + 2 ) * sizeof( char * ));
206*7c478bd9Sstevel@tonic-gate 		    }
207*7c478bd9Sstevel@tonic-gate 		    if ( dxs == NULL || ( dxs[ dx_count ] =
208*7c478bd9Sstevel@tonic-gate 			    (char *)NSLDAPI_CALLOC( 1, txt_len + 1 ))
209*7c478bd9Sstevel@tonic-gate 			    == NULL ) {
210*7c478bd9Sstevel@tonic-gate 			err = NO_RECOVERY;
211*7c478bd9Sstevel@tonic-gate 			continue;
212*7c478bd9Sstevel@tonic-gate 		    }
213*7c478bd9Sstevel@tonic-gate 		    SAFEMEMCPY( dxs[ dx_count ], r, txt_len );
214*7c478bd9Sstevel@tonic-gate 		    dxs[ ++dx_count ] = NULL;
215*7c478bd9Sstevel@tonic-gate 		}
216*7c478bd9Sstevel@tonic-gate 		q += ( *q + 1 );	/* move past last TXT record */
217*7c478bd9Sstevel@tonic-gate 	    }
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 	p += rr_len;
220*7c478bd9Sstevel@tonic-gate     }
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate     if ( err == 0 ) {
223*7c478bd9Sstevel@tonic-gate 	if ( dx_count == 0 ) {
224*7c478bd9Sstevel@tonic-gate 	    err = NO_DATA;
225*7c478bd9Sstevel@tonic-gate 	} else {
226*7c478bd9Sstevel@tonic-gate 	    /*
227*7c478bd9Sstevel@tonic-gate 	     * sort records based on associated preference value
228*7c478bd9Sstevel@tonic-gate 	     */
229*7c478bd9Sstevel@tonic-gate 	    int		i, j, sort_count, tmp_pref;
230*7c478bd9Sstevel@tonic-gate 	    char	*tmp_dx;
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	    sort_count = ( dx_count < MAX_TO_SORT ) ? dx_count : MAX_TO_SORT;
233*7c478bd9Sstevel@tonic-gate 	    for ( i = 0; i < sort_count; ++i ) {
234*7c478bd9Sstevel@tonic-gate 		for ( j = i + 1; j < sort_count; ++j ) {
235*7c478bd9Sstevel@tonic-gate 		    if ( dx_pref[ i ] > dx_pref[ j ] ) {
236*7c478bd9Sstevel@tonic-gate 			tmp_pref = dx_pref[ i ];
237*7c478bd9Sstevel@tonic-gate 			dx_pref[ i ] = dx_pref[ j ];
238*7c478bd9Sstevel@tonic-gate 			dx_pref[ j ] = tmp_pref;
239*7c478bd9Sstevel@tonic-gate 			tmp_dx = dxs[ i ];
240*7c478bd9Sstevel@tonic-gate 			dxs[ i ] = dxs[ j ];
241*7c478bd9Sstevel@tonic-gate 			dxs[ j ] = tmp_dx;
242*7c478bd9Sstevel@tonic-gate 		    }
243*7c478bd9Sstevel@tonic-gate 		}
244*7c478bd9Sstevel@tonic-gate 	    }
245*7c478bd9Sstevel@tonic-gate 	}
246*7c478bd9Sstevel@tonic-gate     }
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate     h_errno = err;
249*7c478bd9Sstevel@tonic-gate     return( dxs );
250*7c478bd9Sstevel@tonic-gate }
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate #endif /* LDAP_DNS */
253