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 **urls = NULL;
44 assert( get_manageDSAit( op ) );
46 if( ldap_dn2domain( dn, &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",
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, dn, urls[0]->bv_val );
96 Debug( LDAP_DEBUG_TRACE,
97 "DNSSRV: ManageDSAit scope=%d dn=\"%s\" -> url=\"%s\"\n",
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",
111 nrefdn = ch_strdup( refdn );
112 dn_normalize(nrefdn);
114 if( strcmp( nrefdn, ndn ) != 0 ) {
115 /* requested dn is subordinate */
117 Debug( LDAP_DEBUG_TRACE,
118 "DNSSRV: dn=\"%s\" subordindate to refdn=\"%s\"\n",
119 dn == NULL ? "" : dn,
120 refdn == NULL ? "" : refdn,
123 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
127 } else if ( scope == LDAP_SCOPE_ONELEVEL ) {
128 send_ldap_result( conn, op, LDAP_SUCCESS,
129 NULL, NULL, NULL, NULL );
133 struct berval *vals[2];
134 Entry *e = ch_calloc( 1, sizeof(Entry) );
135 AttributeDescription *ad_objectClass
136 = slap_schema.si_ad_objectClass;
137 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
138 e->e_dn = strdup( dn );
139 e->e_ndn = strdup( ndn );
148 val.bv_len = sizeof("top")-1;
149 attr_merge( e, ad_objectClass, vals );
151 val.bv_val = "referral";
152 val.bv_len = sizeof("referral")-1;
153 attr_merge( e, ad_objectClass, vals );
155 val.bv_val = "extensibleObject";
156 val.bv_len = sizeof("extensibleObject")-1;
157 attr_merge( e, ad_objectClass, vals );
160 AttributeDescription *ad = NULL;
163 rc = slap_str2ad( "dc", &ad, &text );
165 if( rc == LDAP_SUCCESS ) {
167 val.bv_val = ch_strdup( domain );
169 p = strchr( val.bv_val, '.' );
171 if( p == val.bv_val ) {
172 val.bv_val[1] = '\0';
173 } else if ( p != NULL ) {
177 val.bv_len = strlen(val.bv_val);
178 attr_merge( e, ad, vals );
183 AttributeDescription *ad = NULL;
186 rc = slap_str2ad( "associatedDomain", &ad, &text );
188 if( rc == LDAP_SUCCESS ) {
190 val.bv_len = strlen(domain);
191 attr_merge( e, ad, vals );
195 attr_merge( e, ad_ref, urls );
197 rc = test_filter( be, conn, op, e, filter );
199 if( rc == LDAP_COMPARE_TRUE ) {
200 send_search_entry( be, conn, op,
201 e, attrs, attrsonly, NULL );
206 send_ldap_result( conn, op, LDAP_SUCCESS,
207 NULL, NULL, NULL, NULL );
214 if( domain != NULL ) ch_free( domain );
215 if( hostlist != NULL ) ch_free( hostlist );
216 if( hosts != NULL ) charray_free( hosts );
217 if( urls != NULL ) ber_bvecfree( urls );