1 /* search.c - DNS SRV backend search function */
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/socket.h>
27 #include <ac/string.h>
31 #include "proto-dnssrv.h"
41 char *hostlist = NULL;
44 struct berval nrefdn = BER_BVNULL;
45 BerVarray urls = NULL;
50 manageDSAit = get_manageDSAit( op );
52 * FIXME: we may return a referral if manageDSAit is not set
54 if ( ! manageDSAit ) {
55 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
56 "manageDSAit must be set" );
60 if( ldap_dn2domain( op->o_req_dn.bv_val, &domain ) || domain == NULL ) {
61 rs->sr_err = LDAP_REFERRAL;
62 rs->sr_ref = default_referral;
63 send_ldap_result( op, rs );
68 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
69 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "", domain, 0 );
71 if( ( rc = ldap_domain2hostlist( domain, &hostlist ) ) ) {
72 Debug( LDAP_DEBUG_TRACE, "DNSSRV: domain2hostlist returned %d\n",
74 send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT,
75 "no DNS SRV RR available for DN" );
79 hosts = ldap_str2charray( hostlist, " " );
82 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
83 send_ldap_error( op, rs, LDAP_OTHER,
84 "problem processing DNS SRV records for DN" );
88 for( i=0; hosts[i] != NULL; i++) {
91 url.bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
92 url.bv_val = ch_malloc( url.bv_len + 1 );
94 strcpy( url.bv_val, "ldap://" );
95 strcpy( &url.bv_val[sizeof("ldap://")-1], hosts[i] );
97 if( ber_bvarray_add( &urls, &url ) < 0 ) {
99 send_ldap_error( op, rs, LDAP_OTHER,
100 "problem processing DNS SRV records for DN" );
105 Statslog( LDAP_DEBUG_STATS,
106 "%s DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
107 op->o_log_prefix, op->o_protocol,
108 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "", urls[0].bv_val, 0 );
110 Debug( LDAP_DEBUG_TRACE,
111 "DNSSRV: ManageDSAit scope=%d dn=\"%s\" -> url=\"%s\"\n",
112 op->oq_search.rs_scope,
113 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
116 rc = ldap_domain2dn(domain, &refdn);
118 if( rc != LDAP_SUCCESS ) {
119 send_ldap_error( op, rs, LDAP_OTHER,
120 "DNS SRV problem processing manageDSAit control" );
126 bv.bv_len = strlen( refdn );
128 rc = dnNormalize( 0, NULL, NULL, &bv, &nrefdn, op->o_tmpmemctx );
129 if( rc != LDAP_SUCCESS ) {
130 send_ldap_error( op, rs, LDAP_OTHER,
131 "DNS SRV problem processing manageDSAit control" );
136 if( !dn_match( &nrefdn, &op->o_req_ndn ) ) {
137 /* requested dn is subordinate */
139 Debug( LDAP_DEBUG_TRACE,
140 "DNSSRV: dn=\"%s\" subordinate to refdn=\"%s\"\n",
141 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
142 refdn == NULL ? "" : refdn,
145 rs->sr_matched = refdn;
146 rs->sr_err = LDAP_NO_SUCH_OBJECT;
147 send_ldap_result( op, rs );
148 rs->sr_matched = NULL;
150 } else if ( op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
151 send_ldap_error( op, rs, LDAP_SUCCESS, NULL );
154 struct berval vals[2];
155 Entry *e = ch_calloc( 1, sizeof(Entry) );
156 AttributeDescription *ad_objectClass
157 = slap_schema.si_ad_objectClass;
158 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
159 e->e_name.bv_val = strdup( op->o_req_dn.bv_val );
160 e->e_name.bv_len = op->o_req_dn.bv_len;
161 e->e_nname.bv_val = strdup( op->o_req_ndn.bv_val );
162 e->e_nname.bv_len = op->o_req_ndn.bv_len;
167 vals[1].bv_val = NULL;
169 vals[0].bv_val = "top";
170 vals[0].bv_len = sizeof("top")-1;
171 attr_mergeit( e, ad_objectClass, vals );
173 vals[0].bv_val = "referral";
174 vals[0].bv_len = sizeof("referral")-1;
175 attr_mergeit( e, ad_objectClass, vals );
177 vals[0].bv_val = "extensibleObject";
178 vals[0].bv_len = sizeof("extensibleObject")-1;
179 attr_mergeit( e, ad_objectClass, vals );
182 AttributeDescription *ad = NULL;
185 rc = slap_str2ad( "dc", &ad, &text );
187 if( rc == LDAP_SUCCESS ) {
189 vals[0].bv_val = ch_strdup( domain );
191 p = strchr( vals[0].bv_val, '.' );
193 if( p == vals[0].bv_val ) {
194 vals[0].bv_val[1] = '\0';
195 } else if ( p != NULL ) {
199 vals[0].bv_len = strlen(vals[0].bv_val);
200 attr_mergeit( e, ad, vals );
205 AttributeDescription *ad = NULL;
208 rc = slap_str2ad( "associatedDomain", &ad, &text );
210 if( rc == LDAP_SUCCESS ) {
211 vals[0].bv_val = domain;
212 vals[0].bv_len = strlen(domain);
213 attr_mergeit( e, ad, vals );
217 attr_mergeit( e, ad_ref, urls );
219 rc = test_filter( op, e, op->oq_search.rs_filter );
221 if( rc == LDAP_COMPARE_TRUE ) {
223 rs->sr_attrs = op->oq_search.rs_attrs;
224 rs->sr_flags = REP_ENTRY_MODIFIABLE;
225 send_search_entry( op, rs );
232 rs->sr_err = LDAP_SUCCESS;
233 send_ldap_result( op, rs );
236 if ( refdn ) free( refdn );
237 if ( nrefdn.bv_val ) free( nrefdn.bv_val );
240 if( domain != NULL ) ch_free( domain );
241 if( hostlist != NULL ) ch_free( hostlist );
242 if( hosts != NULL ) ldap_charray_free( hosts );
243 if( urls != NULL ) ber_bvarray_free( urls );