1 /* referral.c - DNS SRV backend referral handler */
4 * Copyright 2000 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 struct berval **urls = NULL;
34 if( ndn == NULL || *ndn == '\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, &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, "DNSSRV: domain2hostlist returned %d\n",
62 *text = "no DNS SRV RR available for DN";
63 rc = LDAP_NO_SUCH_OBJECT;
67 hosts = str2charray( hostlist, " " );
70 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
71 *text = "problem processing DNS SRV records for DN";
75 for( i=0; hosts[i] != NULL; i++) {
76 struct berval *url = ch_malloc( sizeof( struct berval ) );
78 url->bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
79 url->bv_val = ch_malloc( url->bv_len + 1 );
81 strcpy( url->bv_val, "ldap://" );
82 strcpy( &url->bv_val[sizeof("ldap://")-1], hosts[i] );
84 if( ber_bvecadd( &urls, url ) < 0 ) {
86 *text = "problem processing DNS SRV records for DN";
91 Statslog( LDAP_DEBUG_STATS,
92 "conn=%ld op=%d DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
93 op->o_connid, op->o_opid, op->o_protocol, dn, urls[0]->bv_val );
95 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> url=\"%s\"\n",
99 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
100 NULL, "DNS SRV generated referrals", urls, NULL );
103 if( domain != NULL ) ch_free( domain );
104 if( hostlist != NULL ) ch_free( hostlist );
105 if( hosts != NULL ) charray_free( hosts );
106 ber_bvecfree( urls );