1 /* search.c - DNS SRV backend search function */
4 * Copyright 2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
32 const char *filterstr,
39 char *hostlist = NULL;
42 struct berval nrefdn = { 0, NULL };
43 struct berval **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 = 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++) {
76 struct berval *url = ch_malloc( sizeof( struct berval ) );
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_bvecadd( &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=%ld op=%d 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( strcmp( nrefdn.bv_val, ndn->bv_val ) != 0 ) {
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 );
145 struct berval *vals[2];
146 Entry *e = ch_calloc( 1, sizeof(Entry) );
147 AttributeDescription *ad_objectClass
148 = slap_schema.si_ad_objectClass;
149 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
150 e->e_dn = strdup( dn->bv_val );
151 e->e_name.bv_len = dn->bv_len;
152 e->e_ndn = strdup( ndn->bv_val );
153 e->e_nname.bv_len = ndn->bv_len;
162 val.bv_len = sizeof("top")-1;
163 attr_merge( e, ad_objectClass, vals );
165 val.bv_val = "referral";
166 val.bv_len = sizeof("referral")-1;
167 attr_merge( e, ad_objectClass, vals );
169 val.bv_val = "extensibleObject";
170 val.bv_len = sizeof("extensibleObject")-1;
171 attr_merge( e, ad_objectClass, vals );
174 AttributeDescription *ad = NULL;
177 rc = slap_str2ad( "dc", &ad, &text );
179 if( rc == LDAP_SUCCESS ) {
181 val.bv_val = ch_strdup( domain );
183 p = strchr( val.bv_val, '.' );
185 if( p == val.bv_val ) {
186 val.bv_val[1] = '\0';
187 } else if ( p != NULL ) {
191 val.bv_len = strlen(val.bv_val);
192 attr_merge( e, ad, vals );
197 AttributeDescription *ad = NULL;
200 rc = slap_str2ad( "associatedDomain", &ad, &text );
202 if( rc == LDAP_SUCCESS ) {
204 val.bv_len = strlen(domain);
205 attr_merge( e, ad, vals );
209 attr_merge( e, ad_ref, urls );
211 rc = test_filter( be, conn, op, e, filter );
213 if( rc == LDAP_COMPARE_TRUE ) {
214 send_search_entry( be, conn, op,
215 e, attrs, attrsonly, NULL );
220 send_ldap_result( conn, op, LDAP_SUCCESS,
221 NULL, NULL, NULL, NULL );
224 if ( refdn ) free( refdn );
225 if ( nrefdn.bv_val ) free( nrefdn.bv_val );
228 if( domain != NULL ) ch_free( domain );
229 if( hostlist != NULL ) ch_free( hostlist );
230 if( hosts != NULL ) charray_free( hosts );
231 if( urls != NULL ) ber_bvecfree( urls );