1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
1998-12-29 04:53:15 +08:00
|
|
|
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
/* Portions
|
1998-08-09 08:43:13 +08:00
|
|
|
* Copyright (c) 1995 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
1998-08-09 11:04:12 +08:00
|
|
|
* ldap_getdxbyname - retrieve DX records from the DNS (from TXT records for now)
|
1998-08-09 08:43:13 +08:00
|
|
|
*/
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
|
1998-10-25 09:41:42 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
#include <ac/stdlib.h>
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include <ac/ctype.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include "ldap-int.h"
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
static char ** decode_answer LDAP_P(( unsigned char *answer, ber_len_t len ));
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#define MAX_TO_SORT 32
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1998-08-09 11:04:12 +08:00
|
|
|
* ldap_getdxbyname - lookup DNS DX records for domain and return an ordered
|
1998-08-09 08:43:13 +08:00
|
|
|
* array.
|
|
|
|
*/
|
|
|
|
char **
|
1999-08-02 15:41:29 +08:00
|
|
|
ldap_getdxbyname( const char *domain )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
unsigned char buf[ PACKETSZ ];
|
|
|
|
char **dxs;
|
|
|
|
int rc;
|
|
|
|
|
1998-08-09 11:04:12 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_getdxbyname( %s )\n", domain, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
memset( buf, 0, sizeof( buf ));
|
|
|
|
|
|
|
|
if (( rc = res_search( domain, C_IN, T_TXT, buf, sizeof( buf ))) < 0
|
|
|
|
|| ( dxs = decode_answer( buf, rc )) == NULL ) {
|
|
|
|
/*
|
|
|
|
* punt: return list conisting of the original domain name only
|
|
|
|
*/
|
1999-05-29 09:19:14 +08:00
|
|
|
if (( dxs = (char **)LDAP_MALLOC( 2 * sizeof( char * ))) == NULL ||
|
1999-06-03 06:28:22 +08:00
|
|
|
( dxs[ 0 ] = LDAP_STRDUP( domain )) == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( dxs != NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( dxs );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
dxs = NULL;
|
|
|
|
} else {
|
|
|
|
dxs[ 1 ] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( dxs );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char **
|
1999-08-02 15:41:29 +08:00
|
|
|
decode_answer( unsigned char *answer, ber_len_t len )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
HEADER *hp;
|
|
|
|
char buf[ 256 ], **dxs;
|
|
|
|
unsigned char *eom, *p;
|
|
|
|
int ancount, err, rc, type, class, dx_count, rr_len;
|
|
|
|
int dx_pref[ MAX_TO_SORT ];
|
|
|
|
|
|
|
|
#ifdef LDAP_DEBUG
|
1998-08-21 03:42:38 +08:00
|
|
|
#ifdef notdef
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
|
1998-08-21 03:42:38 +08:00
|
|
|
__p_query( answer );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1998-08-21 03:42:38 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
#endif /* LDAP_DEBUG */
|
|
|
|
|
|
|
|
dxs = NULL;
|
|
|
|
hp = (HEADER *)answer;
|
|
|
|
eom = answer + len;
|
|
|
|
|
|
|
|
if ( ntohs( hp->qdcount ) != 1 ) {
|
|
|
|
h_errno = NO_RECOVERY;
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
ancount = ntohs( hp->ancount );
|
|
|
|
if ( ancount < 1 ) {
|
|
|
|
h_errno = NO_DATA;
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* skip over the query
|
|
|
|
*/
|
|
|
|
p = answer + HFIXEDSZ;
|
|
|
|
if (( rc = dn_expand( answer, eom, p, buf, sizeof( buf ))) < 0 ) {
|
|
|
|
h_errno = NO_RECOVERY;
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
p += ( rc + QFIXEDSZ );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pull out the answers we are interested in
|
|
|
|
*/
|
|
|
|
err = dx_count = 0;
|
|
|
|
while ( ancount > 0 && err == 0 && p < eom ) {
|
|
|
|
if (( rc = dn_expand( answer, eom, p, buf, sizeof( buf ))) < 0 ) {
|
|
|
|
err = NO_RECOVERY;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
p += rc; /* skip over name */
|
|
|
|
type = _getshort( p );
|
|
|
|
p += INT16SZ;
|
|
|
|
class = _getshort( p );
|
|
|
|
p += INT16SZ;
|
|
|
|
p += INT32SZ; /* skip over TTL */
|
|
|
|
rr_len = _getshort( p );
|
|
|
|
p += INT16SZ;
|
|
|
|
if ( class == C_IN && type == T_TXT ) {
|
1999-09-02 16:05:17 +08:00
|
|
|
int pref, txt_len;
|
1998-08-09 08:43:13 +08:00
|
|
|
char *q, *r;
|
|
|
|
|
|
|
|
q = (char *)p;
|
|
|
|
while ( q < (char *)p + rr_len && err == 0 ) {
|
|
|
|
if ( *q >= 3 && strncasecmp( q + 1, "dx:", 3 ) == 0 ) {
|
|
|
|
txt_len = *q - 3;
|
|
|
|
r = q + 4;
|
1999-02-23 01:57:22 +08:00
|
|
|
while ( isspace( (unsigned char) *r )) {
|
1998-08-09 08:43:13 +08:00
|
|
|
++r;
|
|
|
|
--txt_len;
|
|
|
|
}
|
|
|
|
pref = 0;
|
1999-02-23 01:57:22 +08:00
|
|
|
while ( isdigit( (unsigned char) *r )) {
|
1998-08-09 08:43:13 +08:00
|
|
|
pref *= 10;
|
|
|
|
pref += ( *r - '0' );
|
|
|
|
++r;
|
|
|
|
--txt_len;
|
|
|
|
}
|
|
|
|
if ( dx_count < MAX_TO_SORT - 1 ) {
|
|
|
|
dx_pref[ dx_count ] = pref;
|
|
|
|
}
|
1999-02-23 01:57:22 +08:00
|
|
|
while ( isspace( (unsigned char) *r )) {
|
1998-08-09 08:43:13 +08:00
|
|
|
++r;
|
|
|
|
--txt_len;
|
|
|
|
}
|
1999-05-29 09:19:14 +08:00
|
|
|
dxs = (char **)LDAP_REALLOC( dxs,
|
1998-08-09 08:43:13 +08:00
|
|
|
( dx_count + 2 ) * sizeof( char * ));
|
|
|
|
if ( dxs == NULL || ( dxs[ dx_count ] =
|
1999-05-29 09:19:14 +08:00
|
|
|
(char *)LDAP_CALLOC( 1, txt_len + 1 )) == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
err = NO_RECOVERY;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
memcpy( dxs[ dx_count ], r, txt_len );
|
|
|
|
dxs[ ++dx_count ] = NULL;
|
|
|
|
}
|
|
|
|
q += ( *q + 1 ); /* move past last TXT record */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p += rr_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( err == 0 ) {
|
|
|
|
if ( dx_count == 0 ) {
|
|
|
|
err = NO_DATA;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* sort records based on associated preference value
|
|
|
|
*/
|
|
|
|
int i, j, sort_count, tmp_pref;
|
|
|
|
char *tmp_dx;
|
|
|
|
|
|
|
|
sort_count = ( dx_count < MAX_TO_SORT ) ? dx_count : MAX_TO_SORT;
|
|
|
|
for ( i = 0; i < sort_count; ++i ) {
|
|
|
|
for ( j = i + 1; j < sort_count; ++j ) {
|
|
|
|
if ( dx_pref[ i ] > dx_pref[ j ] ) {
|
|
|
|
tmp_pref = dx_pref[ i ];
|
|
|
|
dx_pref[ i ] = dx_pref[ j ];
|
|
|
|
dx_pref[ j ] = tmp_pref;
|
|
|
|
tmp_dx = dxs[ i ];
|
|
|
|
dxs[ i ] = dxs[ j ];
|
|
|
|
dxs[ j ] = tmp_dx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
h_errno = err;
|
|
|
|
return( dxs );
|
|
|
|
}
|
|
|
|
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
|