]> git.sur5r.net Git - openldap/blob - servers/slapd/back-dnssrv/search.c
use BER_BVNULL
[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-2004 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 "external.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         manageDSAit = get_manageDSAit( op );
51         /*
52          * FIXME: we may return a referral if manageDSAit is not set
53          */
54         if ( ! manageDSAit ) {
55                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
56                                 "manageDSAit must be set" );
57                 goto done;
58         }
59
60         if( ldap_dn2domain( op->o_req_dn.bv_val, &domain ) || domain == NULL ) {
61                 rs->sr_err = LDAP_REFERRAL;
62                 rs->sr_ref = default_referral;
63                 send_ldap_result( op, rs );
64                 rs->sr_ref = NULL;
65                 goto done;
66         }
67
68         Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
69                 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "", domain, 0 );
70
71         if( ( rc = ldap_domain2hostlist( domain, &hostlist ) ) ) {
72                 Debug( LDAP_DEBUG_TRACE, "DNSSRV: domain2hostlist returned %d\n",
73                         rc, 0, 0 );
74                 send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT,
75                         "no DNS SRV RR available for DN" );
76                 goto done;
77         }
78
79         hosts = ldap_str2charray( hostlist, " " );
80
81         if( hosts == NULL ) {
82                 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
83                 send_ldap_error( op, rs, LDAP_OTHER,
84                         "problem processing DNS SRV records for DN" );
85                 goto done;
86         }
87
88         for( i=0; hosts[i] != NULL; i++) {
89                 struct berval url;
90
91                 url.bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
92                 url.bv_val = ch_malloc( url.bv_len + 1 );
93
94                 strcpy( url.bv_val, "ldap://" );
95                 strcpy( &url.bv_val[sizeof("ldap://")-1], hosts[i] );
96
97                 if( ber_bvarray_add( &urls, &url ) < 0 ) {
98                         free( url.bv_val );
99                         send_ldap_error( op, rs, LDAP_OTHER,
100                         "problem processing DNS SRV records for DN" );
101                         goto done;
102                 }
103         }
104
105         Statslog( LDAP_DEBUG_STATS,
106             "conn=%lu op=%lu DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
107             op->o_connid, op->o_opid, op->o_protocol,
108                 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "", urls[0].bv_val );
109
110         Debug( LDAP_DEBUG_TRACE,
111                 "DNSSRV: ManageDSAit scope=%d dn=\"%s\" -> url=\"%s\"\n",
112                 op->oq_search.rs_scope,
113                 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
114                 urls[0].bv_val );
115
116         rc = ldap_domain2dn(domain, &refdn);
117
118         if( rc != LDAP_SUCCESS ) {
119                 send_ldap_error( op, rs, LDAP_OTHER,
120                         "DNS SRV problem processing manageDSAit control" );
121                 goto done;
122
123         } else {
124                 struct berval bv;
125                 bv.bv_val = refdn;
126                 bv.bv_len = strlen( refdn );
127
128                 rc = dnNormalize( 0, NULL, NULL, &bv, &nrefdn, op->o_tmpmemctx );
129                 if( rc != LDAP_SUCCESS ) {
130                         send_ldap_error( op, rs, LDAP_OTHER,
131                                 "DNS SRV problem processing manageDSAit control" );
132                         goto done;
133                 }
134         }
135
136         if( !dn_match( &nrefdn, &op->o_req_ndn ) ) {
137                 /* requested dn is subordinate */
138
139                 Debug( LDAP_DEBUG_TRACE,
140                         "DNSSRV: dn=\"%s\" subordinate to refdn=\"%s\"\n",
141                         op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
142                         refdn == NULL ? "" : refdn,
143                         NULL );
144
145                 rs->sr_matched = refdn;
146                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
147                 send_ldap_result( op, rs );
148                 rs->sr_matched = NULL;
149
150         } else if ( op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
151                 send_ldap_error( op, rs, LDAP_SUCCESS, NULL );
152
153         } else {
154                 struct berval   vals[2];
155                 Entry *e = ch_calloc( 1, sizeof(Entry) );
156                 AttributeDescription *ad_objectClass
157                         = slap_schema.si_ad_objectClass;
158                 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
159                 e->e_name.bv_val = strdup( op->o_req_dn.bv_val );
160                 e->e_name.bv_len = op->o_req_dn.bv_len;
161                 e->e_nname.bv_val = strdup( op->o_req_ndn.bv_val );
162                 e->e_nname.bv_len = op->o_req_ndn.bv_len;
163
164                 e->e_attrs = NULL;
165                 e->e_private = NULL;
166
167                 vals[1].bv_val = NULL;
168
169                 vals[0].bv_val = "top";
170                 vals[0].bv_len = sizeof("top")-1;
171                 attr_mergeit( e, ad_objectClass, vals );
172
173                 vals[0].bv_val = "referral";
174                 vals[0].bv_len = sizeof("referral")-1;
175                 attr_mergeit( e, ad_objectClass, vals );
176
177                 vals[0].bv_val = "extensibleObject";
178                 vals[0].bv_len = sizeof("extensibleObject")-1;
179                 attr_mergeit( e, ad_objectClass, vals );
180
181                 {
182                         AttributeDescription *ad = NULL;
183                         const char *text;
184
185                         rc = slap_str2ad( "dc", &ad, &text );
186
187                         if( rc == LDAP_SUCCESS ) {
188                                 char *p;
189                                 vals[0].bv_val = ch_strdup( domain );
190
191                                 p = strchr( vals[0].bv_val, '.' );
192                                         
193                                 if( p == vals[0].bv_val ) {
194                                         vals[0].bv_val[1] = '\0';
195                                 } else if ( p != NULL ) {
196                                         *p = '\0';
197                                 }
198
199                                 vals[0].bv_len = strlen(vals[0].bv_val);
200                                 attr_mergeit( e, ad, vals );
201                         }
202                 }
203
204                 {
205                         AttributeDescription *ad = NULL;
206                         const char *text;
207
208                         rc = slap_str2ad( "associatedDomain", &ad, &text );
209
210                         if( rc == LDAP_SUCCESS ) {
211                                 vals[0].bv_val = domain;
212                                 vals[0].bv_len = strlen(domain);
213                                 attr_mergeit( e, ad, vals );
214                         }
215                 }
216
217                 attr_mergeit( e, ad_ref, urls );
218
219                 rc = test_filter( op, e, op->oq_search.rs_filter ); 
220
221                 if( rc == LDAP_COMPARE_TRUE ) {
222                         rs->sr_entry = e;
223                         rs->sr_attrs = op->oq_search.rs_attrs;
224                         rs->sr_flags = REP_ENTRY_MODIFIABLE;
225                         send_search_entry( op, rs );
226                         rs->sr_entry = NULL;
227                         rs->sr_attrs = NULL;
228                 }
229
230                 entry_free( e );
231
232                 rs->sr_err = LDAP_SUCCESS;
233                 send_ldap_result( op, rs );
234         }
235
236         if ( refdn ) 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