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