search.c 5.48 KB
Newer Older
1
2
3
/* search.c - DNS SRV backend search function */
/* $OpenLDAP$ */
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 2000-2003 The OpenLDAP Foundation, All Rights Reserved.
5
6
7
8
9
10
11
12
13
14
15
16
17
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */


#include "portable.h"

#include <stdio.h>

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

#include "slap.h"
18
#include "external.h"
19
20
21
22

int
dnssrv_back_search(
    Operation	*op,
23
    SlapReply	*rs )
24
{
25
26
27
28
29
	int i;
	int rc;
	char *domain = NULL;
	char *hostlist = NULL;
	char **hosts = NULL;
30
	char *refdn;
31
	struct berval nrefdn = { 0, NULL };
32
	BerVarray urls = NULL;
33
	int manageDSAit;
34

35
36
37
38
39
40
41
42
43
44
45
	rs->sr_ref = NULL;

	manageDSAit = get_manageDSAit( op );
	/*
	 * FIXME: we may return a referral if manageDSAit is not set
	 */
	if ( ! manageDSAit ) {
		send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
				"manageDSAit must be set" );
		goto done;
	}
46

47
48
49
50
	if( ldap_dn2domain( op->o_req_dn.bv_val, &domain ) || domain == NULL ) {
		rs->sr_err = LDAP_REFERRAL;
		rs->sr_ref = default_referral;
		send_ldap_result( op, rs );
51
		rs->sr_ref = NULL;
52
53
54
55
		goto done;
	}

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

58
	if( ( rc = ldap_domain2hostlist( domain, &hostlist ) ) ) {
59
60
		Debug( LDAP_DEBUG_TRACE, "DNSSRV: domain2hostlist returned %d\n",
			rc, 0, 0 );
61
62
		send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT,
			"no DNS SRV RR available for DN" );
63
64
65
		goto done;
	}

66
	hosts = ldap_str2charray( hostlist, " " );
67
68
69

	if( hosts == NULL ) {
		Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
70
71
		send_ldap_error( op, rs, LDAP_OTHER,
			"problem processing DNS SRV records for DN" );
72
73
74
75
		goto done;
	}

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

78
79
		url.bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
		url.bv_val = ch_malloc( url.bv_len + 1 );
80

81
82
		strcpy( url.bv_val, "ldap://" );
		strcpy( &url.bv_val[sizeof("ldap://")-1], hosts[i] );
83

84
		if( ber_bvarray_add( &urls, &url ) < 0 ) {
85
			free( url.bv_val );
86
87
			send_ldap_error( op, rs, LDAP_OTHER,
			"problem processing DNS SRV records for DN" );
88
89
90
91
92
			goto done;
		}
	}

	Statslog( LDAP_DEBUG_STATS,
93
	    "conn=%lu op=%lu DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
94
	    op->o_connid, op->o_opid, op->o_protocol,
95
		op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "", urls[0].bv_val );
96

Kurt Zeilenga's avatar
Kurt Zeilenga committed
97
98
	Debug( LDAP_DEBUG_TRACE,
		"DNSSRV: ManageDSAit scope=%d dn=\"%s\" -> url=\"%s\"\n",
99
100
		op->oq_search.rs_scope,
		op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
101
		urls[0].bv_val );
102
103
104
105

	rc = ldap_domain2dn(domain, &refdn);

	if( rc != LDAP_SUCCESS ) {
106
107
		send_ldap_error( op, rs, LDAP_OTHER,
			"DNS SRV problem processing manageDSAit control" );
108
109
		goto done;

110
111
112
113
114
	} else {
		struct berval bv;
		bv.bv_val = refdn;
		bv.bv_len = strlen( refdn );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
115
		rc = dnNormalize( 0, NULL, NULL, &bv, &nrefdn, op->o_tmpmemctx );
116
		if( rc != LDAP_SUCCESS ) {
117
118
			send_ldap_error( op, rs, LDAP_OTHER,
				"DNS SRV problem processing manageDSAit control" );
119
120
121
			goto done;
		}
	}
122

123
	if( !dn_match( &nrefdn, &op->o_req_ndn ) ) {
124
125
126
		/* requested dn is subordinate */

		Debug( LDAP_DEBUG_TRACE,
127
128
			"DNSSRV: dn=\"%s\" subordinate to refdn=\"%s\"\n",
			op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
129
130
			refdn == NULL ? "" : refdn,
			NULL );
131

132
133
134
		rs->sr_matched = refdn;
		rs->sr_err = LDAP_NO_SUCH_OBJECT;
		send_ldap_result( op, rs );
135
		rs->sr_matched = NULL;
136

137
138
	} else if ( op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
		send_ldap_error( op, rs, LDAP_SUCCESS, NULL );
139
140

	} else {
141
		struct berval	vals[2];
142
143
144
145
		Entry *e = ch_calloc( 1, sizeof(Entry) );
		AttributeDescription *ad_objectClass
			= slap_schema.si_ad_objectClass;
		AttributeDescription *ad_ref = slap_schema.si_ad_ref;
146
147
148
149
		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;
150
151
152
153

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

154
		vals[1].bv_val = NULL;
155

156
157
		vals[0].bv_val = "top";
		vals[0].bv_len = sizeof("top")-1;
158
		attr_mergeit( e, ad_objectClass, vals );
159

160
161
		vals[0].bv_val = "referral";
		vals[0].bv_len = sizeof("referral")-1;
162
		attr_mergeit( e, ad_objectClass, vals );
163

164
165
		vals[0].bv_val = "extensibleObject";
		vals[0].bv_len = sizeof("extensibleObject")-1;
166
		attr_mergeit( e, ad_objectClass, vals );
167
168
169
170
171
172
173
174
175

		{
			AttributeDescription *ad = NULL;
			const char *text;

			rc = slap_str2ad( "dc", &ad, &text );

			if( rc == LDAP_SUCCESS ) {
				char *p;
176
				vals[0].bv_val = ch_strdup( domain );
177

178
				p = strchr( vals[0].bv_val, '.' );
179
					
180
181
				if( p == vals[0].bv_val ) {
					vals[0].bv_val[1] = '\0';
182
183
184
185
				} else if ( p != NULL ) {
					*p = '\0';
				}

186
				vals[0].bv_len = strlen(vals[0].bv_val);
187
				attr_mergeit( e, ad, vals );
188
189
190
191
192
193
194
195
196
197
			}
		}

		{
			AttributeDescription *ad = NULL;
			const char *text;

			rc = slap_str2ad( "associatedDomain", &ad, &text );

			if( rc == LDAP_SUCCESS ) {
198
199
				vals[0].bv_val = domain;
				vals[0].bv_len = strlen(domain);
200
				attr_mergeit( e, ad, vals );
201
202
203
			}
		}

204
		attr_mergeit( e, ad_ref, urls );
205

206
		rc = test_filter( op, e, op->oq_search.rs_filter ); 
207
208

		if( rc == LDAP_COMPARE_TRUE ) {
209
210
211
			rs->sr_entry = e;
			rs->sr_attrs = op->oq_search.rs_attrs;
			send_search_entry( op, rs );
212
213
			rs->sr_entry = NULL;
			rs->sr_attrs = NULL;
214
215
216
		}

		entry_free( e );
217
218
219

		rs->sr_err = LDAP_SUCCESS;
		send_ldap_result( op, rs );
220
221
	}

222
223
	if ( refdn ) free( refdn );
	if ( nrefdn.bv_val ) free( nrefdn.bv_val );
224
225
226
227

done:
	if( domain != NULL ) ch_free( domain );
	if( hostlist != NULL ) ch_free( hostlist );
228
	if( hosts != NULL ) ldap_charray_free( hosts );
229
	if( urls != NULL ) ber_bvarray_free( urls );
230
	return 0;
231
}
232