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,
33 struct berval **attrs,
39 char *hostlist = NULL;
42 struct berval **urls = NULL;
44 assert( get_manageDSAit( op ) );
46 if( ldap_dn2domain( dn->bv_val, &domain ) ) {
47 send_ldap_result( conn, op, LDAP_REFERRAL,
48 NULL, NULL, default_referral, NULL );
52 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
53 dn->bv_len ? dn->bv_val : "",
54 domain == NULL ? "" : domain,
57 if( rc = ldap_domain2hostlist( domain, &hostlist ) ) {
58 Debug( LDAP_DEBUG_TRACE, "DNSSRV: domain2hostlist returned %d\n",
60 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
61 NULL, "no DNS SRV RR available for DN", NULL, NULL );
65 hosts = str2charray( hostlist, " " );
68 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
69 send_ldap_result( conn, op, LDAP_OTHER,
70 NULL, "problem processing DNS SRV records for DN", NULL, NULL );
74 for( i=0; hosts[i] != NULL; i++) {
75 struct berval *url = ch_malloc( sizeof( struct berval ) );
77 url->bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
78 url->bv_val = ch_malloc( url->bv_len + 1 );
80 strcpy( url->bv_val, "ldap://" );
81 strcpy( &url->bv_val[sizeof("ldap://")-1], hosts[i] );
83 if( ber_bvecadd( &urls, url ) < 0 ) {
85 send_ldap_result( conn, op, LDAP_OTHER,
86 NULL, "problem processing DNS SRV records for DN",
92 Statslog( LDAP_DEBUG_STATS,
93 "conn=%ld op=%d DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
94 op->o_connid, op->o_opid, op->o_protocol,
95 dn->bv_len ? dn->bv_val : "", urls[0]->bv_val );
97 Debug( LDAP_DEBUG_TRACE,
98 "DNSSRV: ManageDSAit scope=%d dn=\"%s\" -> url=\"%s\"\n",
100 dn->bv_len ? dn->bv_val : "",
103 rc = ldap_domain2dn(domain, &refdn);
105 if( rc != LDAP_SUCCESS ) {
106 send_ldap_result( conn, op, LDAP_OTHER,
107 NULL, "DNS SRV problem processing manageDSAit control",
112 nrefdn = ch_strdup( refdn );
113 dn_normalize(nrefdn);
115 if( strcmp( nrefdn, ndn->bv_val ) != 0 ) {
116 /* requested dn is subordinate */
118 Debug( LDAP_DEBUG_TRACE,
119 "DNSSRV: dn=\"%s\" subordindate to refdn=\"%s\"\n",
120 dn->bv_len ? dn->bv_val : "",
121 refdn == NULL ? "" : refdn,
124 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
128 } else if ( scope == LDAP_SCOPE_ONELEVEL ) {
129 send_ldap_result( conn, op, LDAP_SUCCESS,
130 NULL, NULL, NULL, NULL );
134 struct berval *vals[2];
135 Entry *e = ch_calloc( 1, sizeof(Entry) );
136 AttributeDescription *ad_objectClass
137 = slap_schema.si_ad_objectClass;
138 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
139 e->e_dn = strdup( dn->bv_val );
140 e->e_name.bv_len = dn->bv_len;
141 e->e_ndn = strdup( ndn->bv_val );
142 e->e_nname.bv_len = ndn->bv_len;
151 val.bv_len = sizeof("top")-1;
152 attr_merge( e, ad_objectClass, vals );
154 val.bv_val = "referral";
155 val.bv_len = sizeof("referral")-1;
156 attr_merge( e, ad_objectClass, vals );
158 val.bv_val = "extensibleObject";
159 val.bv_len = sizeof("extensibleObject")-1;
160 attr_merge( e, ad_objectClass, vals );
163 AttributeDescription *ad = NULL;
166 rc = slap_str2ad( "dc", &ad, &text );
168 if( rc == LDAP_SUCCESS ) {
170 val.bv_val = ch_strdup( domain );
172 p = strchr( val.bv_val, '.' );
174 if( p == val.bv_val ) {
175 val.bv_val[1] = '\0';
176 } else if ( p != NULL ) {
180 val.bv_len = strlen(val.bv_val);
181 attr_merge( e, ad, vals );
186 AttributeDescription *ad = NULL;
189 rc = slap_str2ad( "associatedDomain", &ad, &text );
191 if( rc == LDAP_SUCCESS ) {
193 val.bv_len = strlen(domain);
194 attr_merge( e, ad, vals );
198 attr_merge( e, ad_ref, urls );
200 rc = test_filter( be, conn, op, e, filter );
202 if( rc == LDAP_COMPARE_TRUE ) {
203 send_search_entry( be, conn, op,
204 e, attrs, attrsonly, NULL );
209 send_ldap_result( conn, op, LDAP_SUCCESS,
210 NULL, NULL, NULL, NULL );
217 if( domain != NULL ) ch_free( domain );
218 if( hostlist != NULL ) ch_free( hostlist );
219 if( hosts != NULL ) charray_free( hosts );
220 if( urls != NULL ) ber_bvecfree( urls );