]> git.sur5r.net Git - openldap/blob - servers/slapd/back-dnssrv/search.c
Added dnPretty2/dnNormalize2 using preallocated destination berval
[openldap] / servers / slapd / back-dnssrv / search.c
1 /* search.c - DNS SRV backend search function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8
9 #include "portable.h"
10
11 #include <stdio.h>
12
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16
17 #include "slap.h"
18 #include "external.h"
19
20 int
21 dnssrv_back_search(
22     Backend     *be,
23     Connection  *conn,
24     Operation   *op,
25     struct berval       *dn,
26     struct berval       *ndn,
27     int         scope,
28     int         deref,
29     int         size,
30     int         time,
31     Filter      *filter,
32     const char  *filterstr,
33     struct berval       **attrs,
34     int         attrsonly )
35 {
36         int i;
37         int rc;
38         char *domain = NULL;
39         char *hostlist = NULL;
40         char **hosts = NULL;
41         char *refdn;
42         struct berval nrefdn = { 0, NULL };
43         struct berval **urls = NULL;
44
45         assert( get_manageDSAit( op ) );
46
47         if( ldap_dn2domain( dn->bv_val, &domain ) ) {
48                 send_ldap_result( conn, op, LDAP_REFERRAL,
49                         NULL, NULL, default_referral, NULL );
50                 goto done;
51         }
52
53         Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
54                 dn->bv_len ? dn->bv_val : "",
55                 domain == NULL ? "" : domain,
56                 0 );
57
58         if( rc = ldap_domain2hostlist( domain, &hostlist ) ) {
59                 Debug( LDAP_DEBUG_TRACE, "DNSSRV: domain2hostlist returned %d\n",
60                         rc, 0, 0 );
61                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
62                         NULL, "no DNS SRV RR available for DN", NULL, NULL );
63                 goto done;
64         }
65
66         hosts = str2charray( hostlist, " " );
67
68         if( hosts == NULL ) {
69                 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
70                 send_ldap_result( conn, op, LDAP_OTHER,
71                         NULL, "problem processing DNS SRV records for DN", NULL, NULL );
72                 goto done;
73         }
74
75         for( i=0; hosts[i] != NULL; i++) {
76                 struct berval *url = ch_malloc( sizeof( struct berval ) ); 
77
78                 url->bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
79                 url->bv_val = ch_malloc( url->bv_len + 1 );
80
81                 strcpy( url->bv_val, "ldap://" );
82                 strcpy( &url->bv_val[sizeof("ldap://")-1], hosts[i] );
83
84                 if( ber_bvecadd( &urls, url ) < 0 ) {
85                         ber_bvfree( url );
86                         send_ldap_result( conn, op, LDAP_OTHER,
87                                 NULL, "problem processing DNS SRV records for DN",
88                                 NULL, NULL );
89                         goto done;
90                 }
91         }
92
93         Statslog( LDAP_DEBUG_STATS,
94             "conn=%ld op=%d DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
95             op->o_connid, op->o_opid, op->o_protocol,
96                 dn->bv_len ? dn->bv_val : "", urls[0]->bv_val );
97
98         Debug( LDAP_DEBUG_TRACE,
99                 "DNSSRV: ManageDSAit scope=%d dn=\"%s\" -> url=\"%s\"\n",
100                 scope,
101                 dn->bv_len ? dn->bv_val : "",
102                 urls[0]->bv_val );
103
104         rc = ldap_domain2dn(domain, &refdn);
105
106         if( rc != LDAP_SUCCESS ) {
107                 send_ldap_result( conn, op, LDAP_OTHER,
108                         NULL, "DNS SRV problem processing manageDSAit control",
109                         NULL, NULL );
110                 goto done;
111
112         } else {
113                 struct berval bv;
114                 bv.bv_val = refdn;
115                 bv.bv_len = strlen( refdn );
116
117                 rc = dnNormalize2( NULL, &bv, &nrefdn );
118                 if( rc != LDAP_SUCCESS ) {
119                         send_ldap_result( conn, op, LDAP_OTHER,
120                                 NULL, "DNS SRV problem processing manageDSAit control",
121                                 NULL, NULL );
122                         goto done;
123                 }
124         }
125
126         if( strcmp( nrefdn.bv_val, ndn->bv_val ) != 0 ) {
127                 /* requested dn is subordinate */
128
129                 Debug( LDAP_DEBUG_TRACE,
130                         "DNSSRV: dn=\"%s\" subordindate to refdn=\"%s\"\n",
131                         dn->bv_len ? dn->bv_val : "",
132                         refdn == NULL ? "" : refdn,
133                         NULL );
134
135                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
136                         refdn, NULL,
137                         NULL, NULL );
138
139         } else if ( scope == LDAP_SCOPE_ONELEVEL ) {
140                 send_ldap_result( conn, op, LDAP_SUCCESS,
141                         NULL, NULL, NULL, NULL );
142
143         } else {
144                 struct berval   val;
145                 struct berval   *vals[2];
146                 Entry *e = ch_calloc( 1, sizeof(Entry) );
147                 AttributeDescription *ad_objectClass
148                         = slap_schema.si_ad_objectClass;
149                 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
150                 e->e_dn = strdup( dn->bv_val );
151                 e->e_name.bv_len = dn->bv_len;
152                 e->e_ndn = strdup( ndn->bv_val );
153                 e->e_nname.bv_len = ndn->bv_len;
154
155                 e->e_attrs = NULL;
156                 e->e_private = NULL;
157
158                 vals[0] = &val;
159                 vals[1] = NULL;
160
161                 val.bv_val = "top";
162                 val.bv_len = sizeof("top")-1;
163                 attr_merge( e, ad_objectClass, vals );
164
165                 val.bv_val = "referral";
166                 val.bv_len = sizeof("referral")-1;
167                 attr_merge( e, ad_objectClass, vals );
168
169                 val.bv_val = "extensibleObject";
170                 val.bv_len = sizeof("extensibleObject")-1;
171                 attr_merge( e, ad_objectClass, vals );
172
173                 {
174                         AttributeDescription *ad = NULL;
175                         const char *text;
176
177                         rc = slap_str2ad( "dc", &ad, &text );
178
179                         if( rc == LDAP_SUCCESS ) {
180                                 char *p;
181                                 val.bv_val = ch_strdup( domain );
182
183                                 p = strchr( val.bv_val, '.' );
184                                         
185                                 if( p == val.bv_val ) {
186                                         val.bv_val[1] = '\0';
187                                 } else if ( p != NULL ) {
188                                         *p = '\0';
189                                 }
190
191                                 val.bv_len = strlen(val.bv_val);
192                                 attr_merge( e, ad, vals );
193                         }
194                 }
195
196                 {
197                         AttributeDescription *ad = NULL;
198                         const char *text;
199
200                         rc = slap_str2ad( "associatedDomain", &ad, &text );
201
202                         if( rc == LDAP_SUCCESS ) {
203                                 val.bv_val = domain;
204                                 val.bv_len = strlen(domain);
205                                 attr_merge( e, ad, vals );
206                         }
207                 }
208
209                 attr_merge( e, ad_ref, urls );
210
211                 rc = test_filter( be, conn, op, e, filter ); 
212
213                 if( rc == LDAP_COMPARE_TRUE ) {
214                         send_search_entry( be, conn, op,
215                                 e, attrs, attrsonly, NULL );
216                 }
217
218                 entry_free( e );
219                         
220                 send_ldap_result( conn, op, LDAP_SUCCESS,
221                         NULL, NULL, NULL, NULL );
222         }
223
224         if ( refdn ) free( refdn );
225         if ( nrefdn.bv_val ) free( nrefdn.bv_val );
226
227 done:
228         if( domain != NULL ) ch_free( domain );
229         if( hostlist != NULL ) ch_free( hostlist );
230         if( hosts != NULL ) charray_free( hosts );
231         if( urls != NULL ) ber_bvecfree( urls );
232         return 0;
233 }