1 /* referral.c - DNS SRV backend referral handler */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 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( op->o_req_dn.bv_len == 0 ) {
45 rs->sr_text = "DNS SRV operation upon null (empty) DN disallowed";
46 return LDAP_UNWILLING_TO_PERFORM;
49 if( get_manageDSAit( op ) ) {
50 if( op->o_tag == LDAP_REQ_SEARCH ) {
54 rs->sr_text = "DNS SRV problem processing manageDSAit control";
58 if( ldap_dn2domain( op->o_req_dn.bv_val, &domain ) || domain == NULL ) {
59 rs->sr_err = LDAP_REFERRAL;
60 rs->sr_ref = default_referral;
61 send_ldap_result( op, rs );
65 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
66 op->o_req_dn.bv_val, domain, 0 );
68 if( ( rc = ldap_domain2hostlist( domain, &hostlist ) ) ) {
69 Debug( LDAP_DEBUG_TRACE,
70 "DNSSRV: domain2hostlist(%s) returned %d\n",
72 rs->sr_text = "no DNS SRV RR available for DN";
73 rc = LDAP_NO_SUCH_OBJECT;
77 hosts = ldap_str2charray( hostlist, " " );
80 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
81 rs->sr_text = "problem processing DNS SRV records for DN";
85 for( i=0; hosts[i] != NULL; i++) {
88 url.bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
89 url.bv_val = ch_malloc( url.bv_len + 1 );
91 strcpy( url.bv_val, "ldap://" );
92 strcpy( &url.bv_val[sizeof("ldap://")-1], hosts[i] );
94 if ( ber_bvarray_add( &urls, &url ) < 0 ) {
96 rs->sr_text = "problem processing DNS SRV records for DN";
101 Statslog( LDAP_DEBUG_STATS,
102 "%s DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
103 op->o_log_prefix, op->o_protocol,
104 op->o_req_dn.bv_val, urls[0].bv_val, 0 );
106 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> url=\"%s\"\n",
107 op->o_req_dn.bv_val, urls[0].bv_val, 0 );
110 send_ldap_error( op, rs, LDAP_REFERRAL,
111 "DNS SRV generated referrals" );
114 if( domain != NULL ) ch_free( domain );
115 if( hostlist != NULL ) ch_free( hostlist );
116 if( hosts != NULL ) ldap_charray_free( hosts );
117 ber_bvarray_free( urls );