1 /* add.c - DNS SRV backend request handler */
4 * Copyright 2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
16 #include "back-dnssrv.h"
25 int scope, Filter *filter,
26 char **attrs, int attrsonly )
31 char *hostlist = NULL;
33 struct berval **urls = NULL;
34 int manageDSAit = get_manageDSAit( op );
36 if( ndn == NULL || *ndn == '\0' ) {
37 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
38 NULL, "operation upon null (empty) DN disallowed",
43 if( ldap_dn2domain( dn, &domain ) ) {
44 send_ldap_result( conn, op, LDAP_REFERRAL,
45 NULL, NULL, default_referral, NULL );
49 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
51 domain == NULL ? "" : domain,
54 if( rc = ldap_domain2hostlist( domain, &hostlist ) ) {
55 Debug( LDAP_DEBUG_TRACE, "DNSSRV: domain2hostlist returned %d\n",
57 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
58 NULL, "no DNS SRV RR available for DN", NULL, NULL );
62 hosts = str2charray( hostlist, " " );
65 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
66 send_ldap_result( conn, op, LDAP_OTHER,
67 NULL, "problem processing DNS SRV records for DN", NULL, NULL );
71 for( i=0; hosts[i] != NULL; i++) {
72 struct berval *url = ch_malloc( sizeof( struct berval ) );
74 url->bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
75 url->bv_val = ch_malloc( url->bv_len + 1 );
77 strcpy( url->bv_val, "ldap://" );
78 strcpy( &url->bv_val[sizeof("ldap://")-1], hosts[i] );
80 if( ber_bvecadd( &urls, url ) < 0 ) {
82 send_ldap_result( conn, op, LDAP_OTHER,
83 NULL, "problem processing DNS SRV records for DN",
89 Statslog( LDAP_DEBUG_STATS,
90 "conn=%ld op=%d DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
91 op->o_connid, op->o_opid, op->o_protocol, dn, urls[0]->bv_val );
93 Debug( LDAP_DEBUG_TRACE, "DNSSRV: %sdn=\"%s\" -> url=\"%s\"\n",
94 manageDSAit ? "ManageDSAit " : "",
100 rc = ldap_domain2dn(domain, &refdn);
102 if( rc != LDAP_SUCCESS ) {
103 send_ldap_result( conn, op, LDAP_OTHER,
104 NULL, "DNS SRV problem processing manageDSAit control",
109 nrefdn = ch_strdup( refdn );
110 dn_normalize(nrefdn);
112 if( strcmp( nrefdn, ndn ) != 0 ) {
113 /* requested dn is subordinate */
115 Debug( LDAP_DEBUG_TRACE,
116 "DNSSRV: dn=\"%s\" subordindate to refdn=\"%s\"\n",
117 dn == NULL ? "" : dn,
118 refdn == NULL ? "" : refdn,
121 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
125 } else if( op->o_tag != LDAP_REQ_SEARCH ) {
126 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
127 dn, "DNS SRV ManageDSAIT control disallowed",
130 } else if ( scope != LDAP_SCOPE_ONELEVEL ) {
132 struct berval *vals[2];
133 Entry *e = ch_calloc( 1, sizeof(Entry) );
134 AttributeDescription *ad_objectClass
135 = slap_schema.si_ad_objectClass;
136 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
137 e->e_dn = strdup( dn );
138 e->e_ndn = strdup( ndn );
147 val.bv_len = sizeof("top")-1;
148 attr_merge( e, ad_objectClass, vals );
150 val.bv_val = "referral";
151 val.bv_len = sizeof("referral")-1;
152 attr_merge( e, ad_objectClass, vals );
154 val.bv_val = "extensibleObject";
155 val.bv_len = sizeof("extensibleObject")-1;
156 attr_merge( e, ad_objectClass, vals );
159 AttributeDescription *ad = NULL;
162 rc = slap_str2ad( "dc", &ad, &text );
164 if( rc == LDAP_SUCCESS ) {
166 val.bv_val = ch_strdup( domain );
168 p = strchr( val.bv_val, '.' );
170 if( p == val.bv_val ) {
171 val.bv_val[1] = '\0';
172 } else if ( p != NULL ) {
176 val.bv_len = strlen(val.bv_val);
177 attr_merge( e, ad, vals );
184 AttributeDescription *ad = NULL;
187 rc = slap_str2ad( "associatedDomain", &ad, &text );
189 if( rc == LDAP_SUCCESS ) {
191 val.bv_len = strlen(domain);
192 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 );
213 send_ldap_result( conn, op, LDAP_SUCCESS,
214 NULL, NULL, NULL, NULL );
221 send_ldap_result( conn, op, LDAP_REFERRAL,
222 NULL, "DNS SRV generated referrals", urls, NULL );
226 if( domain != NULL ) ch_free( domain );
227 if( hostlist != NULL ) ch_free( hostlist );
228 if( hosts != NULL ) charray_free( hosts );
229 if( urls != NULL ) ber_bvecfree( urls );