1 /* referral.c - DNS SRV backend referral handler */
4 * Copyright 2000-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
19 dnssrv_back_referrals(
30 char *hostlist = NULL;
32 BerVarray urls = NULL;
34 if( ndn->bv_len == 0 ) {
35 *text = "DNS SRV operation upon null (empty) DN disallowed";
36 return LDAP_UNWILLING_TO_PERFORM;
39 if( get_manageDSAit( op ) ) {
40 if( op->o_tag == LDAP_REQ_SEARCH ) {
44 *text = "DNS SRV problem processing manageDSAit control";
48 if( ldap_dn2domain( dn->bv_val, &domain ) ) {
49 send_ldap_result( conn, op, LDAP_REFERRAL,
50 NULL, NULL, default_referral, NULL );
54 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
56 domain == NULL ? "" : domain,
59 if( ( rc = ldap_domain2hostlist( domain, &hostlist ) ) ) {
60 Debug( LDAP_DEBUG_TRACE,
61 "DNSSRV: domain2hostlist(%s) returned %d\n",
63 *text = "no DNS SRV RR available for DN";
64 rc = LDAP_NO_SUCH_OBJECT;
68 hosts = ldap_str2charray( hostlist, " " );
71 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
72 *text = "problem processing DNS SRV records for DN";
76 for( i=0; hosts[i] != NULL; i++) {
79 url.bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
80 url.bv_val = ch_malloc( url.bv_len + 1 );
82 strcpy( url.bv_val, "ldap://" );
83 strcpy( &url.bv_val[sizeof("ldap://")-1], hosts[i] );
85 if ( ber_bvarray_add( &urls, &url ) < 0 ) {
87 *text = "problem processing DNS SRV records for DN";
92 Statslog( LDAP_DEBUG_STATS,
93 "conn=%lu op=%lu DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
94 op->o_connid, op->o_opid, op->o_protocol,
95 dn->bv_val, urls[0].bv_val );
97 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> url=\"%s\"\n",
98 dn->bv_val, urls[0].bv_val, 0 );
100 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
101 NULL, "DNS SRV generated referrals", urls, NULL );
104 if( domain != NULL ) ch_free( domain );
105 if( hostlist != NULL ) ch_free( hostlist );
106 if( hosts != NULL ) ldap_charray_free( hosts );
107 ber_bvarray_free( urls );