]> git.sur5r.net Git - openldap/blob - libraries/libldap/getdxbyname.c
Fix ldap_send_initial_request() to open connection if not already
[openldap] / libraries / libldap / getdxbyname.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*  Portions
6  *  Copyright (c) 1995 Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  * ldap_getdxbyname - retrieve DX records from the DNS (from TXT records for now)
10  */
11
12 #include "portable.h"
13
14 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include <ac/ctype.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #include <ac/time.h>
23
24 #include "ldap-int.h"
25
26 static char ** decode_answer LDAP_P(( unsigned char *answer, int len ));
27
28 #define MAX_TO_SORT     32
29
30
31 /*
32  * ldap_getdxbyname - lookup DNS DX records for domain and return an ordered
33  *      array.
34  */
35 char **
36 ldap_getdxbyname( char *domain )
37 {
38     unsigned char       buf[ PACKETSZ ];
39     char                **dxs;
40     int                 rc;
41
42     Debug( LDAP_DEBUG_TRACE, "ldap_getdxbyname( %s )\n", domain, 0, 0 );
43
44     memset( buf, 0, sizeof( buf ));
45
46     if (( rc = res_search( domain, C_IN, T_TXT, buf, sizeof( buf ))) < 0
47                 || ( dxs = decode_answer( buf, rc )) == NULL ) {
48         /*
49          * punt:  return list conisting of the original domain name only
50          */
51         if (( dxs = (char **)malloc( 2 * sizeof( char * ))) == NULL ||
52                 ( dxs[ 0 ] = strdup( domain )) == NULL ) {
53             if ( dxs != NULL ) {
54                 free( dxs );
55             }
56             dxs = NULL;
57         } else {
58             dxs[ 1 ] = NULL;
59         }
60     }
61
62     return( dxs );
63 }
64
65
66 static char **
67 decode_answer( unsigned char *answer, int len )
68 {
69     HEADER              *hp;
70     char                buf[ 256 ], **dxs;
71     unsigned char       *eom, *p;
72     int                 ancount, err, rc, type, class, dx_count, rr_len;
73     int                 dx_pref[ MAX_TO_SORT ];
74
75 #ifdef LDAP_DEBUG
76 #ifdef notdef
77     if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
78                 __p_query( answer );
79     }
80 #endif
81 #endif /* LDAP_DEBUG */
82
83     dxs = NULL;
84     hp = (HEADER *)answer;
85     eom = answer + len;
86
87     if ( ntohs( hp->qdcount ) != 1 ) {
88         h_errno = NO_RECOVERY;
89         return( NULL );
90     }
91
92     ancount = ntohs( hp->ancount );
93     if ( ancount < 1 ) {
94         h_errno = NO_DATA;
95         return( NULL );
96     }
97
98     /*
99      * skip over the query
100      */
101     p = answer + HFIXEDSZ;
102     if (( rc = dn_expand( answer, eom, p, buf, sizeof( buf ))) < 0 ) {
103         h_errno = NO_RECOVERY;
104         return( NULL );
105     }
106     p += ( rc + QFIXEDSZ );
107
108     /*
109      * pull out the answers we are interested in
110      */
111     err = dx_count = 0;
112     while ( ancount > 0 && err == 0 && p < eom ) {
113         if (( rc = dn_expand( answer, eom, p, buf, sizeof( buf ))) < 0 ) {
114             err = NO_RECOVERY;
115             continue;
116         }
117         p += rc;        /* skip over name */
118         type = _getshort( p );
119         p += INT16SZ;
120         class = _getshort( p );
121         p += INT16SZ;
122         p += INT32SZ;           /* skip over TTL */
123         rr_len = _getshort( p );
124         p += INT16SZ;
125         if ( class == C_IN && type == T_TXT ) {
126             int         i, n, pref, txt_len;
127             char        *q, *r;
128
129             q = (char *)p;
130             while ( q < (char *)p + rr_len && err == 0 ) {
131                 if ( *q >= 3 && strncasecmp( q + 1, "dx:", 3 ) == 0 ) {
132                     txt_len = *q - 3;
133                     r = q + 4;
134                     while ( isspace( *r )) { 
135                         ++r;
136                         --txt_len;
137                     }
138                     pref = 0;
139                     while ( isdigit( *r )) {
140                         pref *= 10;
141                         pref += ( *r - '0' );
142                         ++r;
143                         --txt_len;
144                     }
145                     if ( dx_count < MAX_TO_SORT - 1 ) {
146                         dx_pref[ dx_count ] = pref;
147                     }
148                     while ( isspace( *r )) { 
149                         ++r;
150                         --txt_len;
151                     }
152                     if ( dx_count == 0 ) {
153                         dxs = (char **)malloc( 2 * sizeof( char * ));
154                     } else {
155                         dxs = (char **)realloc( dxs,
156                                 ( dx_count + 2 ) * sizeof( char * ));
157                     }
158                     if ( dxs == NULL || ( dxs[ dx_count ] =
159                                 (char *)calloc( 1, txt_len + 1 )) == NULL ) {
160                         err = NO_RECOVERY;
161                         continue;
162                     }
163                     memcpy( dxs[ dx_count ], r, txt_len );
164                     dxs[ ++dx_count ] = NULL;
165                 }
166                 q += ( *q + 1 );        /* move past last TXT record */
167             }
168         }
169         p += rr_len;
170     }
171
172     if ( err == 0 ) {
173         if ( dx_count == 0 ) {
174             err = NO_DATA;
175         } else {
176             /*
177              * sort records based on associated preference value
178              */
179             int         i, j, sort_count, tmp_pref;
180             char        *tmp_dx;
181
182             sort_count = ( dx_count < MAX_TO_SORT ) ? dx_count : MAX_TO_SORT;
183             for ( i = 0; i < sort_count; ++i ) {
184                 for ( j = i + 1; j < sort_count; ++j ) {
185                     if ( dx_pref[ i ] > dx_pref[ j ] ) {
186                         tmp_pref = dx_pref[ i ];
187                         dx_pref[ i ] = dx_pref[ j ];
188                         dx_pref[ j ] = tmp_pref;
189                         tmp_dx = dxs[ i ];
190                         dxs[ i ] = dxs[ j ];
191                         dxs[ j ] = tmp_dx;
192                     }
193                 }
194             }
195         }
196     }
197
198     h_errno = err;
199     return( dxs );
200 }
201
202 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */