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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
135 AttributeDescription *ad_objectClass
136 = slap_schema.si_ad_objectClass;
137 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
139 const char ad_objectClass = "objectClass";
140 const char ad_ref = "ref";
142 e->e_dn = strdup( dn );
143 e->e_ndn = strdup( ndn );
152 val.bv_len = sizeof("top")-1;
153 attr_merge( e, ad_objectClass, vals );
155 val.bv_val = "referral";
156 val.bv_len = sizeof("referral")-1;
157 attr_merge( e, ad_objectClass, vals );
159 val.bv_val = "extensibleObject";
160 val.bv_len = sizeof("extensibleObject")-1;
161 attr_merge( e, ad_objectClass, vals );
164 #ifdef SLAPD_SCHEMA_NOT_COMPAT
165 AttributeDescription *ad = NULL;
168 rc = slap_str2ad( "dc", &ad, &text );
171 const char *ad = "dc";
174 if( rc == LDAP_SUCCESS ) {
176 val.bv_val = ch_strdup( domain );
178 p = strchr( val.bv_val, '.' );
180 if( p == val.bv_val ) {
181 val.bv_val[1] = '\0';
182 } else if ( p != NULL ) {
186 val.bv_len = strlen(val.bv_val);
187 attr_merge( e, ad, vals );
194 #ifdef SLAPD_SCHEMA_NOT_COMPAT
195 AttributeDescription *ad = NULL;
198 rc = slap_str2ad( "associatedDomain", &ad, &text );
201 const char *ad = "associatedDomain";
204 if( rc == LDAP_SUCCESS ) {
206 val.bv_len = strlen(domain);
207 attr_merge( e, ad, vals );
213 attr_merge( e, ad_ref, urls );
215 rc = test_filter( be, conn, op, e, filter );
217 if( rc == LDAP_COMPARE_TRUE ) {
218 send_search_entry( be, conn, op,
219 e, attrs, attrsonly, NULL );
224 send_ldap_result( conn, op, LDAP_SUCCESS,
225 NULL, NULL, NULL, NULL );
228 send_ldap_result( conn, op, LDAP_SUCCESS,
229 NULL, NULL, NULL, NULL );
236 send_ldap_result( conn, op, LDAP_REFERRAL,
237 NULL, "DNS SRV generated referrals", urls, NULL );
241 if( domain != NULL ) ch_free( domain );
242 if( hostlist != NULL ) ch_free( hostlist );
243 if( hosts != NULL ) charray_free( hosts );
244 if( urls != NULL ) ber_bvecfree( urls );