1 /* search.c - DNS SRV backend search function */
4 * Copyright 2000-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
28 char *hostlist = NULL;
31 struct berval nrefdn = { 0, NULL };
32 BerVarray urls = NULL;
34 assert( get_manageDSAit( op ) );
36 if( ldap_dn2domain( op->o_req_dn.bv_val, &domain ) || domain == NULL ) {
37 rs->sr_err = LDAP_REFERRAL;
38 rs->sr_ref = default_referral;
39 send_ldap_result( op, rs );
43 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
44 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "", domain, 0 );
46 if( ( rc = ldap_domain2hostlist( domain, &hostlist ) ) ) {
47 Debug( LDAP_DEBUG_TRACE, "DNSSRV: domain2hostlist returned %d\n",
49 send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT,
50 "no DNS SRV RR available for DN" );
54 hosts = ldap_str2charray( hostlist, " " );
57 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
58 send_ldap_error( op, rs, LDAP_OTHER,
59 "problem processing DNS SRV records for DN" );
63 for( i=0; hosts[i] != NULL; i++) {
66 url.bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
67 url.bv_val = ch_malloc( url.bv_len + 1 );
69 strcpy( url.bv_val, "ldap://" );
70 strcpy( &url.bv_val[sizeof("ldap://")-1], hosts[i] );
72 if( ber_bvarray_add( &urls, &url ) < 0 ) {
74 send_ldap_error( op, rs, LDAP_OTHER,
75 "problem processing DNS SRV records for DN" );
80 Statslog( LDAP_DEBUG_STATS,
81 "conn=%lu op=%lu DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
82 op->o_connid, op->o_opid, op->o_protocol,
83 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "", urls[0].bv_val );
85 Debug( LDAP_DEBUG_TRACE,
86 "DNSSRV: ManageDSAit scope=%d dn=\"%s\" -> url=\"%s\"\n",
87 op->oq_search.rs_scope,
88 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
91 rc = ldap_domain2dn(domain, &refdn);
93 if( rc != LDAP_SUCCESS ) {
94 send_ldap_error( op, rs, LDAP_OTHER,
95 "DNS SRV problem processing manageDSAit control" );
101 bv.bv_len = strlen( refdn );
103 rc = dnNormalize2( NULL, &bv, &nrefdn );
104 if( rc != LDAP_SUCCESS ) {
105 send_ldap_error( op, rs, LDAP_OTHER,
106 "DNS SRV problem processing manageDSAit control" );
111 if( !dn_match( &nrefdn, &op->o_req_ndn ) ) {
112 /* requested dn is subordinate */
114 Debug( LDAP_DEBUG_TRACE,
115 "DNSSRV: dn=\"%s\" subordinate to refdn=\"%s\"\n",
116 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
117 refdn == NULL ? "" : refdn,
120 rs->sr_matched = refdn;
121 rs->sr_err = LDAP_NO_SUCH_OBJECT;
122 send_ldap_result( op, rs );
124 } else if ( op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
125 send_ldap_error( op, rs, LDAP_SUCCESS, NULL );
128 struct berval vals[2];
129 Entry *e = ch_calloc( 1, sizeof(Entry) );
130 AttributeDescription *ad_objectClass
131 = slap_schema.si_ad_objectClass;
132 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
133 e->e_name.bv_val = strdup( op->o_req_dn.bv_val );
134 e->e_name.bv_len = op->o_req_dn.bv_len;
135 e->e_nname.bv_val = strdup( op->o_req_ndn.bv_val );
136 e->e_nname.bv_len = op->o_req_ndn.bv_len;
141 vals[1].bv_val = NULL;
143 vals[0].bv_val = "top";
144 vals[0].bv_len = sizeof("top")-1;
145 attr_mergeit( e, ad_objectClass, vals );
147 vals[0].bv_val = "referral";
148 vals[0].bv_len = sizeof("referral")-1;
149 attr_mergeit( e, ad_objectClass, vals );
151 vals[0].bv_val = "extensibleObject";
152 vals[0].bv_len = sizeof("extensibleObject")-1;
153 attr_mergeit( e, ad_objectClass, vals );
156 AttributeDescription *ad = NULL;
159 rc = slap_str2ad( "dc", &ad, &text );
161 if( rc == LDAP_SUCCESS ) {
163 vals[0].bv_val = ch_strdup( domain );
165 p = strchr( vals[0].bv_val, '.' );
167 if( p == vals[0].bv_val ) {
168 vals[0].bv_val[1] = '\0';
169 } else if ( p != NULL ) {
173 vals[0].bv_len = strlen(vals[0].bv_val);
174 attr_mergeit( e, ad, vals );
179 AttributeDescription *ad = NULL;
182 rc = slap_str2ad( "associatedDomain", &ad, &text );
184 if( rc == LDAP_SUCCESS ) {
185 vals[0].bv_val = domain;
186 vals[0].bv_len = strlen(domain);
187 attr_mergeit( e, ad, vals );
191 attr_mergeit( e, ad_ref, urls );
193 rc = test_filter( op, e, op->oq_search.rs_filter );
195 if( rc == LDAP_COMPARE_TRUE ) {
197 rs->sr_attrs = op->oq_search.rs_attrs;
198 send_search_entry( op, rs );
203 rs->sr_err = LDAP_SUCCESS;
204 send_ldap_result( op, rs );
207 if ( refdn ) free( refdn );
208 if ( nrefdn.bv_val ) free( nrefdn.bv_val );
211 if( domain != NULL ) ch_free( domain );
212 if( hostlist != NULL ) ch_free( hostlist );
213 if( hosts != NULL ) ldap_charray_free( hosts );
214 if( urls != NULL ) ber_bvarray_free( urls );