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