1 /* referral.c - DNS SRV backend referral handler */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2006 The OpenLDAP Foundation.
6 * Portions Copyright 2000-2003 Kurt D. Zeilenga.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was originally developed by Kurt D. Zeilenga for inclusion
19 * in OpenLDAP Software.
26 #include <ac/string.h>
27 #include <ac/socket.h>
30 #include "proto-dnssrv.h"
33 dnssrv_back_referrals(
40 char *hostlist = NULL;
42 BerVarray urls = NULL;
44 if ( BER_BVISEMPTY( &op->o_req_dn ) ) {
46 /* FIXME: need some means to determine whether the database
47 * is a glue instance */
48 if ( SLAP_GLUE_INSTANCE( op->o_bd ) ) {
51 #endif /* LDAP_DEVEL */
53 rs->sr_text = "DNS SRV operation upon null (empty) DN disallowed";
54 return LDAP_UNWILLING_TO_PERFORM;
57 if( get_manageDSAit( op ) ) {
58 if( op->o_tag == LDAP_REQ_SEARCH ) {
62 rs->sr_text = "DNS SRV problem processing manageDSAit control";
66 if( ldap_dn2domain( op->o_req_dn.bv_val, &domain ) || domain == NULL ) {
67 rs->sr_err = LDAP_REFERRAL;
68 rs->sr_ref = default_referral;
69 send_ldap_result( op, rs );
74 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
75 op->o_req_dn.bv_val, domain, 0 );
77 i = ldap_domain2hostlist( domain, &hostlist );
79 Debug( LDAP_DEBUG_TRACE,
80 "DNSSRV: domain2hostlist(%s) returned %d\n",
82 rs->sr_text = "no DNS SRV RR available for DN";
83 rc = LDAP_NO_SUCH_OBJECT;
87 hosts = ldap_str2charray( hostlist, " " );
90 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
91 rs->sr_text = "problem processing DNS SRV records for DN";
95 for( i=0; hosts[i] != NULL; i++) {
98 url.bv_len = STRLENOF( "ldap://" ) + strlen( hosts[i] );
99 url.bv_val = ch_malloc( url.bv_len + 1 );
101 strcpy( url.bv_val, "ldap://" );
102 strcpy( &url.bv_val[STRLENOF( "ldap://" )], hosts[i] );
104 if ( ber_bvarray_add( &urls, &url ) < 0 ) {
106 rs->sr_text = "problem processing DNS SRV records for DN";
111 Statslog( LDAP_DEBUG_STATS,
112 "%s DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
113 op->o_log_prefix, op->o_protocol,
114 op->o_req_dn.bv_val, urls[0].bv_val, 0 );
116 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> url=\"%s\"\n",
117 op->o_req_dn.bv_val, urls[0].bv_val, 0 );
120 send_ldap_error( op, rs, LDAP_REFERRAL,
121 "DNS SRV generated referrals" );
126 if( domain != NULL ) ch_free( domain );
127 if( hostlist != NULL ) ch_free( hostlist );
128 if( hosts != NULL ) ldap_charray_free( hosts );
129 ber_bvarray_free( urls );