search.c 6.11 KB
Newer Older
1
2
/* search.c - DNS SRV backend search function */
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
 * Copyright 2000-2005 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 * Portions Copyright 2000-2003 Kurt D. Zeilenga.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was originally developed by Kurt D. Zeilenga for inclusion
 * in OpenLDAP Software.
20
21
22
23
24
25
26
27
28
29
30
 */

#include "portable.h"

#include <stdio.h>

#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>

#include "slap.h"
31
#include "proto-dnssrv.h"
32
33
34
35

int
dnssrv_back_search(
    Operation	*op,
36
    SlapReply	*rs )
37
{
38
39
40
41
42
	int i;
	int rc;
	char *domain = NULL;
	char *hostlist = NULL;
	char **hosts = NULL;
43
	char *refdn;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
44
	struct berval nrefdn = BER_BVNULL;
45
	BerVarray urls = NULL;
46
	int manageDSAit;
47

48
49
	rs->sr_ref = NULL;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
	if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
#ifdef LDAP_DEVEL
		/* FIXME: need some means to determine whether the database
		 * is a glue instance; if we got here with empty DN, then
		 * we passed this same test in dnssrv_back_referrals() */
		if ( !SLAP_GLUE_INSTANCE( op->o_bd ) ) {
			rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
			rs->sr_text = "DNS SRV operation upon null (empty) DN disallowed";

		} else {
			rs->sr_err = LDAP_SUCCESS;
		}
		goto done;
#endif /* LDAP_DEVEL */
	}

66
67
68
69
	manageDSAit = get_manageDSAit( op );
	/*
	 * FIXME: we may return a referral if manageDSAit is not set
	 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
70
	if ( !manageDSAit ) {
71
72
73
74
		send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
				"manageDSAit must be set" );
		goto done;
	}
75

76
77
	if( ldap_dn2domain( op->o_req_dn.bv_val, &domain ) || domain == NULL ) {
		rs->sr_err = LDAP_REFERRAL;
78
		rs->sr_ref = default_referral;
79
		send_ldap_result( op, rs );
80
		rs->sr_ref = NULL;
81
82
83
84
		goto done;
	}

	Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
85
		op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "", domain, 0 );
86

87
	if( ( rc = ldap_domain2hostlist( domain, &hostlist ) ) ) {
88
89
		Debug( LDAP_DEBUG_TRACE, "DNSSRV: domain2hostlist returned %d\n",
			rc, 0, 0 );
90
91
		send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT,
			"no DNS SRV RR available for DN" );
92
93
94
		goto done;
	}

95
	hosts = ldap_str2charray( hostlist, " " );
96
97
98

	if( hosts == NULL ) {
		Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
99
100
		send_ldap_error( op, rs, LDAP_OTHER,
			"problem processing DNS SRV records for DN" );
101
102
103
104
		goto done;
	}

	for( i=0; hosts[i] != NULL; i++) {
105
		struct berval url;
106

107
108
		url.bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
		url.bv_val = ch_malloc( url.bv_len + 1 );
109

110
111
		strcpy( url.bv_val, "ldap://" );
		strcpy( &url.bv_val[sizeof("ldap://")-1], hosts[i] );
112

113
		if( ber_bvarray_add( &urls, &url ) < 0 ) {
114
			free( url.bv_val );
115
116
			send_ldap_error( op, rs, LDAP_OTHER,
			"problem processing DNS SRV records for DN" );
117
118
119
120
121
			goto done;
		}
	}

	Statslog( LDAP_DEBUG_STATS,
122
123
124
	    "%s DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
	    op->o_log_prefix, op->o_protocol,
		op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "", urls[0].bv_val, 0 );
125

Kurt Zeilenga's avatar
Kurt Zeilenga committed
126
127
	Debug( LDAP_DEBUG_TRACE,
		"DNSSRV: ManageDSAit scope=%d dn=\"%s\" -> url=\"%s\"\n",
128
129
		op->oq_search.rs_scope,
		op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
130
		urls[0].bv_val );
131
132
133
134

	rc = ldap_domain2dn(domain, &refdn);

	if( rc != LDAP_SUCCESS ) {
135
136
		send_ldap_error( op, rs, LDAP_OTHER,
			"DNS SRV problem processing manageDSAit control" );
137
138
		goto done;

139
140
141
142
143
	} else {
		struct berval bv;
		bv.bv_val = refdn;
		bv.bv_len = strlen( refdn );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
144
		rc = dnNormalize( 0, NULL, NULL, &bv, &nrefdn, op->o_tmpmemctx );
145
		if( rc != LDAP_SUCCESS ) {
146
147
			send_ldap_error( op, rs, LDAP_OTHER,
				"DNS SRV problem processing manageDSAit control" );
148
149
150
			goto done;
		}
	}
151

152
	if( !dn_match( &nrefdn, &op->o_req_ndn ) ) {
153
154
155
		/* requested dn is subordinate */

		Debug( LDAP_DEBUG_TRACE,
156
157
			"DNSSRV: dn=\"%s\" subordinate to refdn=\"%s\"\n",
			op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
158
159
			refdn == NULL ? "" : refdn,
			NULL );
160

161
162
163
		rs->sr_matched = refdn;
		rs->sr_err = LDAP_NO_SUCH_OBJECT;
		send_ldap_result( op, rs );
164
		rs->sr_matched = NULL;
165

166
167
	} else if ( op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
		send_ldap_error( op, rs, LDAP_SUCCESS, NULL );
168
169
170
171
172
173

	} else {
		Entry *e = ch_calloc( 1, sizeof(Entry) );
		AttributeDescription *ad_objectClass
			= slap_schema.si_ad_objectClass;
		AttributeDescription *ad_ref = slap_schema.si_ad_ref;
174
175
176
177
		e->e_name.bv_val = strdup( op->o_req_dn.bv_val );
		e->e_name.bv_len = op->o_req_dn.bv_len;
		e->e_nname.bv_val = strdup( op->o_req_ndn.bv_val );
		e->e_nname.bv_len = op->o_req_ndn.bv_len;
178
179
180
181

		e->e_attrs = NULL;
		e->e_private = NULL;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
182
183
184
		attr_mergeit_one( e, ad_objectClass, &slap_schema.si_oc_top->soc_cname );
		attr_mergeit_one( e, ad_objectClass, &slap_schema.si_oc_referral->soc_cname );
		attr_mergeit_one( e, ad_objectClass, &slap_schema.si_oc_extensibleObject->soc_cname );
185

Kurt Zeilenga's avatar
Kurt Zeilenga committed
186
187
188
		if ( ad_dc ) {
			char		*p;
			struct berval	bv;
189

Kurt Zeilenga's avatar
Kurt Zeilenga committed
190
			bv.bv_val = domain;
191

Kurt Zeilenga's avatar
Kurt Zeilenga committed
192
193
194
195
			p = strchr( bv.bv_val, '.' );
					
			if ( p == bv.bv_val ) {
				bv.bv_len = 1;
196

Kurt Zeilenga's avatar
Kurt Zeilenga committed
197
198
			} else if ( p != NULL ) {
				bv.bv_len = p - bv.bv_val;
199

Kurt Zeilenga's avatar
Kurt Zeilenga committed
200
201
			} else {
				bv.bv_len = strlen( bv.bv_val );
202
203
			}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
204
205
			attr_mergeit_one( e, ad_dc, &bv );
		}
206

Kurt Zeilenga's avatar
Kurt Zeilenga committed
207
208
		if ( ad_associatedDomain ) {
			struct berval	bv;
209

Kurt Zeilenga's avatar
Kurt Zeilenga committed
210
211
			ber_str2bv( domain, 0, 0, &bv );
			attr_mergeit_one( e, ad_associatedDomain, &bv );
212
213
		}

214
		attr_mergeit( e, ad_ref, urls );
215

216
		rc = test_filter( op, e, op->oq_search.rs_filter ); 
217
218

		if( rc == LDAP_COMPARE_TRUE ) {
219
220
			rs->sr_entry = e;
			rs->sr_attrs = op->oq_search.rs_attrs;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
221
			rs->sr_flags = REP_ENTRY_MODIFIABLE;
222
			send_search_entry( op, rs );
223
224
			rs->sr_entry = NULL;
			rs->sr_attrs = NULL;
225
226
227
		}

		entry_free( e );
228
229
230

		rs->sr_err = LDAP_SUCCESS;
		send_ldap_result( op, rs );
231
232
	}

233
234
	if ( refdn ) free( refdn );
	if ( nrefdn.bv_val ) free( nrefdn.bv_val );
235
236
237
238

done:
	if( domain != NULL ) ch_free( domain );
	if( hostlist != NULL ) ch_free( hostlist );
239
	if( hosts != NULL ) ldap_charray_free( hosts );
240
	if( urls != NULL ) ber_bvarray_free( urls );
241
	return 0;
242
}
243