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 ) || domain == NULL ) {
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 : "", domain, 0 );
56 if( ( rc = ldap_domain2hostlist( domain, &hostlist ) ) ) {
57 Debug( LDAP_DEBUG_TRACE, "DNSSRV: domain2hostlist returned %d\n",
59 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
60 NULL, "no DNS SRV RR available for DN", NULL, NULL );
64 hosts = ldap_str2charray( hostlist, " " );
67 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
68 send_ldap_result( conn, op, LDAP_OTHER,
69 NULL, "problem processing DNS SRV records for DN", NULL, NULL );
73 for( i=0; hosts[i] != NULL; i++) {
76 url.bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
77 url.bv_val = ch_malloc( url.bv_len + 1 );
79 strcpy( url.bv_val, "ldap://" );
80 strcpy( &url.bv_val[sizeof("ldap://")-1], hosts[i] );
82 if( ber_bvarray_add( &urls, &url ) < 0 ) {
84 send_ldap_result( conn, op, LDAP_OTHER,
85 NULL, "problem processing DNS SRV records for DN",
91 Statslog( LDAP_DEBUG_STATS,
92 "conn=%lu op=%lu DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
93 op->o_connid, op->o_opid, op->o_protocol,
94 dn->bv_len ? dn->bv_val : "", urls[0].bv_val );
96 Debug( LDAP_DEBUG_TRACE,
97 "DNSSRV: ManageDSAit scope=%d dn=\"%s\" -> url=\"%s\"\n",
99 dn->bv_len ? dn->bv_val : "",
102 rc = ldap_domain2dn(domain, &refdn);
104 if( rc != LDAP_SUCCESS ) {
105 send_ldap_result( conn, op, LDAP_OTHER,
106 NULL, "DNS SRV problem processing manageDSAit control",
113 bv.bv_len = strlen( refdn );
115 rc = dnNormalize2( NULL, &bv, &nrefdn );
116 if( rc != LDAP_SUCCESS ) {
117 send_ldap_result( conn, op, LDAP_OTHER,
118 NULL, "DNS SRV problem processing manageDSAit control",
124 if( !dn_match( &nrefdn, ndn ) ) {
125 /* requested dn is subordinate */
127 Debug( LDAP_DEBUG_TRACE,
128 "DNSSRV: dn=\"%s\" subordindate to refdn=\"%s\"\n",
129 dn->bv_len ? dn->bv_val : "",
130 refdn == NULL ? "" : refdn,
133 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
137 } else if ( scope == LDAP_SCOPE_ONELEVEL ) {
138 send_ldap_result( conn, op, LDAP_SUCCESS,
139 NULL, NULL, NULL, NULL );
142 struct berval vals[2];
143 Entry *e = ch_calloc( 1, sizeof(Entry) );
144 AttributeDescription *ad_objectClass
145 = slap_schema.si_ad_objectClass;
146 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
147 e->e_dn = strdup( dn->bv_val );
148 e->e_name.bv_len = dn->bv_len;
149 e->e_ndn = strdup( ndn->bv_val );
150 e->e_nname.bv_len = ndn->bv_len;
155 vals[1].bv_val = NULL;
157 vals[0].bv_val = "top";
158 vals[0].bv_len = sizeof("top")-1;
159 attr_merge( e, ad_objectClass, vals );
161 vals[0].bv_val = "referral";
162 vals[0].bv_len = sizeof("referral")-1;
163 attr_merge( e, ad_objectClass, vals );
165 vals[0].bv_val = "extensibleObject";
166 vals[0].bv_len = sizeof("extensibleObject")-1;
167 attr_merge( e, ad_objectClass, vals );
170 AttributeDescription *ad = NULL;
173 rc = slap_str2ad( "dc", &ad, &text );
175 if( rc == LDAP_SUCCESS ) {
177 vals[0].bv_val = ch_strdup( domain );
179 p = strchr( vals[0].bv_val, '.' );
181 if( p == vals[0].bv_val ) {
182 vals[0].bv_val[1] = '\0';
183 } else if ( p != NULL ) {
187 vals[0].bv_len = strlen(vals[0].bv_val);
188 attr_merge( e, ad, vals );
193 AttributeDescription *ad = NULL;
196 rc = slap_str2ad( "associatedDomain", &ad, &text );
198 if( rc == LDAP_SUCCESS ) {
199 vals[0].bv_val = domain;
200 vals[0].bv_len = strlen(domain);
201 attr_merge( e, ad, vals );
205 attr_merge( e, ad_ref, urls );
207 rc = test_filter( be, conn, op, e, filter );
209 if( rc == LDAP_COMPARE_TRUE ) {
210 send_search_entry( be, conn, op,
211 e, attrs, attrsonly, NULL );
216 send_ldap_result( conn, op, LDAP_SUCCESS,
217 NULL, NULL, NULL, NULL );
220 if ( refdn ) free( refdn );
221 if ( nrefdn.bv_val ) free( nrefdn.bv_val );
224 if( domain != NULL ) ch_free( domain );
225 if( hostlist != NULL ) ch_free( hostlist );
226 if( hosts != NULL ) ldap_charray_free( hosts );
227 if( urls != NULL ) ber_bvarray_free( urls );