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