1 /* referral.c - DNS SRV backend referral handler */
4 * Copyright 2000-2003 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(
26 char *hostlist = NULL;
28 BerVarray urls = NULL;
30 if( op->o_req_dn.bv_len == 0 ) {
31 rs->sr_text = "DNS SRV operation upon null (empty) DN disallowed";
32 return LDAP_UNWILLING_TO_PERFORM;
35 if( get_manageDSAit( op ) ) {
36 if( op->o_tag == LDAP_REQ_SEARCH ) {
40 rs->sr_text = "DNS SRV problem processing manageDSAit control";
44 if( ldap_dn2domain( op->o_req_dn.bv_val, &domain ) || domain == NULL ) {
45 rs->sr_err = LDAP_REFERRAL;
46 rs->sr_ref = default_referral;
47 send_ldap_result( op, rs );
51 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
52 op->o_req_dn.bv_val, domain, 0 );
54 if( ( rc = ldap_domain2hostlist( domain, &hostlist ) ) ) {
55 Debug( LDAP_DEBUG_TRACE,
56 "DNSSRV: domain2hostlist(%s) returned %d\n",
58 rs->sr_text = "no DNS SRV RR available for DN";
59 rc = LDAP_NO_SUCH_OBJECT;
63 hosts = ldap_str2charray( hostlist, " " );
66 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
67 rs->sr_text = "problem processing DNS SRV records for DN";
71 for( i=0; hosts[i] != NULL; i++) {
74 url.bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
75 url.bv_val = ch_malloc( url.bv_len + 1 );
77 strcpy( url.bv_val, "ldap://" );
78 strcpy( &url.bv_val[sizeof("ldap://")-1], hosts[i] );
80 if ( ber_bvarray_add( &urls, &url ) < 0 ) {
82 rs->sr_text = "problem processing DNS SRV records for DN";
87 Statslog( LDAP_DEBUG_STATS,
88 "conn=%lu op=%lu DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
89 op->o_connid, op->o_opid, op->o_protocol,
90 op->o_req_dn.bv_val, urls[0].bv_val );
92 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> url=\"%s\"\n",
93 op->o_req_dn.bv_val, urls[0].bv_val, 0 );
96 send_ldap_error( op, rs, LDAP_REFERRAL,
97 "DNS SRV generated referrals" );
100 if( domain != NULL ) ch_free( domain );
101 if( hostlist != NULL ) ch_free( hostlist );
102 if( hosts != NULL ) ldap_charray_free( hosts );
103 ber_bvarray_free( urls );