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