1 /* search.c - DNS SRV backend search function */
4 * Copyright 2000-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
32 struct berval *filterstr,
39 char *hostlist = NULL;
42 struct berval nrefdn = { 0, NULL };
43 BerVarray urls = NULL;
45 assert( get_manageDSAit( op ) );
47 if( ldap_dn2domain( dn->bv_val, &domain ) ) {
48 send_ldap_result( conn, op, LDAP_REFERRAL,
49 NULL, NULL, default_referral, NULL );
53 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
54 dn->bv_len ? dn->bv_val : "",
55 domain == NULL ? "" : domain,
58 if( ( rc = ldap_domain2hostlist( domain, &hostlist ) ) ) {
59 Debug( LDAP_DEBUG_TRACE, "DNSSRV: domain2hostlist returned %d\n",
61 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
62 NULL, "no DNS SRV RR available for DN", NULL, NULL );
66 hosts = ldap_str2charray( hostlist, " " );
69 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
70 send_ldap_result( conn, op, LDAP_OTHER,
71 NULL, "problem processing DNS SRV records for DN", NULL, NULL );
75 for( i=0; hosts[i] != NULL; i++) {
78 url.bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
79 url.bv_val = ch_malloc( url.bv_len + 1 );
81 strcpy( url.bv_val, "ldap://" );
82 strcpy( &url.bv_val[sizeof("ldap://")-1], hosts[i] );
84 if( ber_bvarray_add( &urls, &url ) < 0 ) {
86 send_ldap_result( conn, op, LDAP_OTHER,
87 NULL, "problem processing DNS SRV records for DN",
93 Statslog( LDAP_DEBUG_STATS,
94 "conn=%lu op=%lu DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
95 op->o_connid, op->o_opid, op->o_protocol,
96 dn->bv_len ? dn->bv_val : "", urls[0].bv_val );
98 Debug( LDAP_DEBUG_TRACE,
99 "DNSSRV: ManageDSAit scope=%d dn=\"%s\" -> url=\"%s\"\n",
101 dn->bv_len ? dn->bv_val : "",
104 rc = ldap_domain2dn(domain, &refdn);
106 if( rc != LDAP_SUCCESS ) {
107 send_ldap_result( conn, op, LDAP_OTHER,
108 NULL, "DNS SRV problem processing manageDSAit control",
115 bv.bv_len = strlen( refdn );
117 rc = dnNormalize2( NULL, &bv, &nrefdn );
118 if( rc != LDAP_SUCCESS ) {
119 send_ldap_result( conn, op, LDAP_OTHER,
120 NULL, "DNS SRV problem processing manageDSAit control",
126 if( !dn_match( &nrefdn, ndn ) ) {
127 /* requested dn is subordinate */
129 Debug( LDAP_DEBUG_TRACE,
130 "DNSSRV: dn=\"%s\" subordindate to refdn=\"%s\"\n",
131 dn->bv_len ? dn->bv_val : "",
132 refdn == NULL ? "" : refdn,
135 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
139 } else if ( scope == LDAP_SCOPE_ONELEVEL ) {
140 send_ldap_result( conn, op, LDAP_SUCCESS,
141 NULL, NULL, NULL, NULL );
144 struct berval vals[2];
145 Entry *e = ch_calloc( 1, sizeof(Entry) );
146 AttributeDescription *ad_objectClass
147 = slap_schema.si_ad_objectClass;
148 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
149 e->e_dn = strdup( dn->bv_val );
150 e->e_name.bv_len = dn->bv_len;
151 e->e_ndn = strdup( ndn->bv_val );
152 e->e_nname.bv_len = ndn->bv_len;
157 vals[1].bv_val = NULL;
159 vals[0].bv_val = "top";
160 vals[0].bv_len = sizeof("top")-1;
161 attr_merge( e, ad_objectClass, vals );
163 vals[0].bv_val = "referral";
164 vals[0].bv_len = sizeof("referral")-1;
165 attr_merge( e, ad_objectClass, vals );
167 vals[0].bv_val = "extensibleObject";
168 vals[0].bv_len = sizeof("extensibleObject")-1;
169 attr_merge( e, ad_objectClass, vals );
172 AttributeDescription *ad = NULL;
175 rc = slap_str2ad( "dc", &ad, &text );
177 if( rc == LDAP_SUCCESS ) {
179 vals[0].bv_val = ch_strdup( domain );
181 p = strchr( vals[0].bv_val, '.' );
183 if( p == vals[0].bv_val ) {
184 vals[0].bv_val[1] = '\0';
185 } else if ( p != NULL ) {
189 vals[0].bv_len = strlen(vals[0].bv_val);
190 attr_merge( e, ad, vals );
195 AttributeDescription *ad = NULL;
198 rc = slap_str2ad( "associatedDomain", &ad, &text );
200 if( rc == LDAP_SUCCESS ) {
201 vals[0].bv_val = domain;
202 vals[0].bv_len = strlen(domain);
203 attr_merge( e, ad, vals );
207 attr_merge( e, ad_ref, urls );
209 rc = test_filter( be, conn, op, e, filter );
211 if( rc == LDAP_COMPARE_TRUE ) {
212 send_search_entry( be, conn, op,
213 e, attrs, attrsonly, NULL );
218 send_ldap_result( conn, op, LDAP_SUCCESS,
219 NULL, NULL, NULL, NULL );
222 if ( refdn ) free( refdn );
223 if ( nrefdn.bv_val ) free( nrefdn.bv_val );
226 if( domain != NULL ) ch_free( domain );
227 if( hostlist != NULL ) ch_free( hostlist );
228 if( hosts != NULL ) ldap_charray_free( hosts );
229 if( urls != NULL ) ber_bvarray_free( urls );