search.c revision 1.1.1.4.10.1 1 /* $NetBSD: search.c,v 1.1.1.4.10.1 2017/04/21 16:52:29 bouyer Exp $ */
2
3 /* search.c - DNS SRV backend search function */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 2000-2016 The OpenLDAP Foundation.
8 * Portions Copyright 2000-2003 Kurt D. Zeilenga.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
13 * Public License.
14 *
15 * A copy of this license is available in the file LICENSE in the
16 * top-level directory of the distribution or, alternatively, at
17 * <http://www.OpenLDAP.org/license.html>.
18 */
19 /* ACKNOWLEDGEMENTS:
20 * This work was originally developed by Kurt D. Zeilenga for inclusion
21 * in OpenLDAP Software.
22 */
23
24 #include <sys/cdefs.h>
25 __RCSID("$NetBSD: search.c,v 1.1.1.4.10.1 2017/04/21 16:52:29 bouyer Exp $");
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/socket.h>
32 #include <ac/string.h>
33 #include <ac/time.h>
34
35 #include "slap.h"
36 #include "proto-dnssrv.h"
37
38 int
39 dnssrv_back_search(
40 Operation *op,
41 SlapReply *rs )
42 {
43 int i;
44 int rc;
45 char *domain = NULL;
46 char *hostlist = NULL;
47 char **hosts = NULL;
48 char *refdn;
49 struct berval nrefdn = BER_BVNULL;
50 BerVarray urls = NULL;
51 int manageDSAit;
52
53 rs->sr_ref = NULL;
54
55 if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
56 /* FIXME: need some means to determine whether the database
57 * is a glue instance; if we got here with empty DN, then
58 * we passed this same test in dnssrv_back_referrals() */
59 if ( !SLAP_GLUE_INSTANCE( op->o_bd ) ) {
60 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
61 rs->sr_text = "DNS SRV operation upon null (empty) DN disallowed";
62
63 } else {
64 rs->sr_err = LDAP_SUCCESS;
65 }
66 goto done;
67 }
68
69 manageDSAit = get_manageDSAit( op );
70 /*
71 * FIXME: we may return a referral if manageDSAit is not set
72 */
73 if ( !manageDSAit ) {
74 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
75 "manageDSAit must be set" );
76 goto done;
77 }
78
79 if( ldap_dn2domain( op->o_req_dn.bv_val, &domain ) || domain == NULL ) {
80 rs->sr_err = LDAP_REFERRAL;
81 rs->sr_ref = default_referral;
82 send_ldap_result( op, rs );
83 rs->sr_ref = NULL;
84 goto done;
85 }
86
87 Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
88 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "", domain, 0 );
89
90 if( ( rc = ldap_domain2hostlist( domain, &hostlist ) ) ) {
91 Debug( LDAP_DEBUG_TRACE, "DNSSRV: domain2hostlist returned %d\n",
92 rc, 0, 0 );
93 send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT,
94 "no DNS SRV RR available for DN" );
95 goto done;
96 }
97
98 hosts = ldap_str2charray( hostlist, " " );
99
100 if( hosts == NULL ) {
101 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
102 send_ldap_error( op, rs, LDAP_OTHER,
103 "problem processing DNS SRV records for DN" );
104 goto done;
105 }
106
107 for( i=0; hosts[i] != NULL; i++) {
108 struct berval url;
109
110 url.bv_len = STRLENOF( "ldap://" ) + strlen(hosts[i]);
111 url.bv_val = ch_malloc( url.bv_len + 1 );
112
113 strcpy( url.bv_val, "ldap://" );
114 strcpy( &url.bv_val[STRLENOF( "ldap://" )], hosts[i] );
115
116 if( ber_bvarray_add( &urls, &url ) < 0 ) {
117 free( url.bv_val );
118 send_ldap_error( op, rs, LDAP_OTHER,
119 "problem processing DNS SRV records for DN" );
120 goto done;
121 }
122 }
123
124 Statslog( LDAP_DEBUG_STATS,
125 "%s DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
126 op->o_log_prefix, op->o_protocol,
127 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "", urls[0].bv_val, 0 );
128
129 Debug( LDAP_DEBUG_TRACE,
130 "DNSSRV: ManageDSAit scope=%d dn=\"%s\" -> url=\"%s\"\n",
131 op->oq_search.rs_scope,
132 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
133 urls[0].bv_val );
134
135 rc = ldap_domain2dn(domain, &refdn);
136
137 if( rc != LDAP_SUCCESS ) {
138 send_ldap_error( op, rs, LDAP_OTHER,
139 "DNS SRV problem processing manageDSAit control" );
140 goto done;
141
142 } else {
143 struct berval bv;
144 bv.bv_val = refdn;
145 bv.bv_len = strlen( refdn );
146
147 rc = dnNormalize( 0, NULL, NULL, &bv, &nrefdn, op->o_tmpmemctx );
148 if( rc != LDAP_SUCCESS ) {
149 send_ldap_error( op, rs, LDAP_OTHER,
150 "DNS SRV problem processing manageDSAit control" );
151 goto done;
152 }
153 }
154
155 if( !dn_match( &nrefdn, &op->o_req_ndn ) ) {
156 /* requested dn is subordinate */
157
158 Debug( LDAP_DEBUG_TRACE,
159 "DNSSRV: dn=\"%s\" subordinate to refdn=\"%s\"\n",
160 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
161 refdn == NULL ? "" : refdn,
162 NULL );
163
164 rs->sr_matched = refdn;
165 rs->sr_err = LDAP_NO_SUCH_OBJECT;
166 send_ldap_result( op, rs );
167 rs->sr_matched = NULL;
168
169 } else if ( op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
170 send_ldap_error( op, rs, LDAP_SUCCESS, NULL );
171
172 } else {
173 Entry e = { 0 };
174 AttributeDescription *ad_objectClass
175 = slap_schema.si_ad_objectClass;
176 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
177 e.e_name.bv_val = ch_strdup( op->o_req_dn.bv_val );
178 e.e_name.bv_len = op->o_req_dn.bv_len;
179 e.e_nname.bv_val = ch_strdup( op->o_req_ndn.bv_val );
180 e.e_nname.bv_len = op->o_req_ndn.bv_len;
181
182 e.e_attrs = NULL;
183 e.e_private = NULL;
184
185 attr_merge_one( &e, ad_objectClass, &slap_schema.si_oc_referral->soc_cname, NULL );
186 attr_merge_one( &e, ad_objectClass, &slap_schema.si_oc_extensibleObject->soc_cname, NULL );
187
188 if ( ad_dc ) {
189 char *p;
190 struct berval bv;
191
192 bv.bv_val = domain;
193
194 p = strchr( bv.bv_val, '.' );
195
196 if ( p == bv.bv_val ) {
197 bv.bv_len = 1;
198
199 } else if ( p != NULL ) {
200 bv.bv_len = p - bv.bv_val;
201
202 } else {
203 bv.bv_len = strlen( bv.bv_val );
204 }
205
206 attr_merge_normalize_one( &e, ad_dc, &bv, NULL );
207 }
208
209 if ( ad_associatedDomain ) {
210 struct berval bv;
211
212 ber_str2bv( domain, 0, 0, &bv );
213 attr_merge_normalize_one( &e, ad_associatedDomain, &bv, NULL );
214 }
215
216 attr_merge_normalize_one( &e, ad_ref, urls, NULL );
217
218 rc = test_filter( op, &e, op->oq_search.rs_filter );
219
220 if( rc == LDAP_COMPARE_TRUE ) {
221 rs->sr_entry = &e;
222 rs->sr_attrs = op->oq_search.rs_attrs;
223 rs->sr_flags = REP_ENTRY_MODIFIABLE;
224 send_search_entry( op, rs );
225 rs->sr_entry = NULL;
226 rs->sr_attrs = NULL;
227 rs->sr_flags = 0;
228 }
229
230 entry_clean( &e );
231
232 rs->sr_err = LDAP_SUCCESS;
233 send_ldap_result( op, rs );
234 }
235
236 free( refdn );
237 if ( nrefdn.bv_val ) free( nrefdn.bv_val );
238
239 done:
240 if( domain != NULL ) ch_free( domain );
241 if( hostlist != NULL ) ch_free( hostlist );
242 if( hosts != NULL ) ldap_charray_free( hosts );
243 if( urls != NULL ) ber_bvarray_free( urls );
244 return 0;
245 }
246