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