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