]> git.sur5r.net Git - openldap/blob - servers/slapd/back-dnssrv/search.c
Fix syntax errors caused by struct berval changes...
[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, *nrefdn;
42         struct berval **urls = NULL;
43
44         assert( get_manageDSAit( op ) );
45
46         if( ldap_dn2domain( dn->bv_val, &domain ) ) {
47                 send_ldap_result( conn, op, LDAP_REFERRAL,
48                         NULL, NULL, default_referral, NULL );
49                 goto done;
50         }
51
52         Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
53                 dn->bv_len ? dn->bv_val : "",
54                 domain == NULL ? "" : domain,
55                 0 );
56
57         if( rc = ldap_domain2hostlist( domain, &hostlist ) ) {
58                 Debug( LDAP_DEBUG_TRACE, "DNSSRV: domain2hostlist returned %d\n",
59                         rc, 0, 0 );
60                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
61                         NULL, "no DNS SRV RR available for DN", NULL, NULL );
62                 goto done;
63         }
64
65         hosts = str2charray( hostlist, " " );
66
67         if( hosts == NULL ) {
68                 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
69                 send_ldap_result( conn, op, LDAP_OTHER,
70                         NULL, "problem processing DNS SRV records for DN", NULL, NULL );
71                 goto done;
72         }
73
74         for( i=0; hosts[i] != NULL; i++) {
75                 struct berval *url = ch_malloc( sizeof( struct berval ) ); 
76
77                 url->bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
78                 url->bv_val = ch_malloc( url->bv_len + 1 );
79
80                 strcpy( url->bv_val, "ldap://" );
81                 strcpy( &url->bv_val[sizeof("ldap://")-1], hosts[i] );
82
83                 if( ber_bvecadd( &urls, url ) < 0 ) {
84                         ber_bvfree( url );
85                         send_ldap_result( conn, op, LDAP_OTHER,
86                                 NULL, "problem processing DNS SRV records for DN",
87                                 NULL, NULL );
88                         goto done;
89                 }
90         }
91
92         Statslog( LDAP_DEBUG_STATS,
93             "conn=%ld op=%d DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
94             op->o_connid, op->o_opid, op->o_protocol,
95                 dn->bv_len ? dn->bv_val : "", urls[0]->bv_val );
96
97         Debug( LDAP_DEBUG_TRACE,
98                 "DNSSRV: ManageDSAit scope=%d dn=\"%s\" -> url=\"%s\"\n",
99                 scope,
100                 dn->bv_len ? dn->bv_val : "",
101                 urls[0]->bv_val );
102
103         rc = ldap_domain2dn(domain, &refdn);
104
105         if( rc != LDAP_SUCCESS ) {
106                 send_ldap_result( conn, op, LDAP_OTHER,
107                         NULL, "DNS SRV problem processing manageDSAit control",
108                         NULL, NULL );
109                 goto done;
110         }
111
112         nrefdn = ch_strdup( refdn );
113         dn_normalize(nrefdn);
114
115         if( strcmp( nrefdn, ndn->bv_val ) != 0 ) {
116                 /* requested dn is subordinate */
117
118                 Debug( LDAP_DEBUG_TRACE,
119                         "DNSSRV: dn=\"%s\" subordindate to refdn=\"%s\"\n",
120                         dn->bv_len ? dn->bv_val : "",
121                         refdn == NULL ? "" : refdn,
122                         NULL );
123
124                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
125                         refdn, NULL,
126                         NULL, NULL );
127
128         } else if ( scope == LDAP_SCOPE_ONELEVEL ) {
129                 send_ldap_result( conn, op, LDAP_SUCCESS,
130                         NULL, NULL, NULL, NULL );
131
132         } else {
133                 struct berval   val;
134                 struct berval   *vals[2];
135                 Entry *e = ch_calloc( 1, sizeof(Entry) );
136                 AttributeDescription *ad_objectClass
137                         = slap_schema.si_ad_objectClass;
138                 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
139                 e->e_dn = strdup( dn->bv_val );
140                 e->e_name.bv_len = dn->bv_len;
141                 e->e_ndn = strdup( ndn->bv_val );
142                 e->e_nname.bv_len = ndn->bv_len;
143
144                 e->e_attrs = NULL;
145                 e->e_private = NULL;
146
147                 vals[0] = &val;
148                 vals[1] = NULL;
149
150                 val.bv_val = "top";
151                 val.bv_len = sizeof("top")-1;
152                 attr_merge( e, ad_objectClass, vals );
153
154                 val.bv_val = "referral";
155                 val.bv_len = sizeof("referral")-1;
156                 attr_merge( e, ad_objectClass, vals );
157
158                 val.bv_val = "extensibleObject";
159                 val.bv_len = sizeof("extensibleObject")-1;
160                 attr_merge( e, ad_objectClass, vals );
161
162                 {
163                         AttributeDescription *ad = NULL;
164                         const char *text;
165
166                         rc = slap_str2ad( "dc", &ad, &text );
167
168                         if( rc == LDAP_SUCCESS ) {
169                                 char *p;
170                                 val.bv_val = ch_strdup( domain );
171
172                                 p = strchr( val.bv_val, '.' );
173                                         
174                                 if( p == val.bv_val ) {
175                                         val.bv_val[1] = '\0';
176                                 } else if ( p != NULL ) {
177                                         *p = '\0';
178                                 }
179
180                                 val.bv_len = strlen(val.bv_val);
181                                 attr_merge( e, ad, vals );
182                         }
183                 }
184
185                 {
186                         AttributeDescription *ad = NULL;
187                         const char *text;
188
189                         rc = slap_str2ad( "associatedDomain", &ad, &text );
190
191                         if( rc == LDAP_SUCCESS ) {
192                                 val.bv_val = domain;
193                                 val.bv_len = strlen(domain);
194                                 attr_merge( e, ad, vals );
195                         }
196                 }
197
198                 attr_merge( e, ad_ref, urls );
199
200                 rc = test_filter( be, conn, op, e, filter ); 
201
202                 if( rc == LDAP_COMPARE_TRUE ) {
203                         send_search_entry( be, conn, op,
204                                 e, attrs, attrsonly, NULL );
205                 }
206
207                 entry_free( e );
208                         
209                 send_ldap_result( conn, op, LDAP_SUCCESS,
210                         NULL, NULL, NULL, NULL );
211         }
212
213         free( refdn );
214         free( nrefdn );
215
216 done:
217         if( domain != NULL ) ch_free( domain );
218         if( hostlist != NULL ) ch_free( hostlist );
219         if( hosts != NULL ) charray_free( hosts );
220         if( urls != NULL ) ber_bvecfree( urls );
221         return 0;
222 }