]> git.sur5r.net Git - openldap/blob - servers/slapd/back-dnssrv/search.c
Changed search attrs to struct 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     const char  *dn,
26     const char  *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, &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 == NULL ? "" : dn,
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, dn, urls[0]->bv_val );
95
96         Debug( LDAP_DEBUG_TRACE,
97                 "DNSSRV: ManageDSAit scope=%d dn=\"%s\" -> url=\"%s\"\n",
98                 scope,
99                 dn == NULL ? "" : dn,
100                 urls[0]->bv_val );
101
102         rc = ldap_domain2dn(domain, &refdn);
103
104         if( rc != LDAP_SUCCESS ) {
105                 send_ldap_result( conn, op, LDAP_OTHER,
106                         NULL, "DNS SRV problem processing manageDSAit control",
107                         NULL, NULL );
108                 goto done;
109         }
110
111         nrefdn = ch_strdup( refdn );
112         dn_normalize(nrefdn);
113
114         if( strcmp( nrefdn, ndn ) != 0 ) {
115                 /* requested dn is subordinate */
116
117                 Debug( LDAP_DEBUG_TRACE,
118                                 "DNSSRV: dn=\"%s\" subordindate to refdn=\"%s\"\n",
119                                 dn == NULL ? "" : dn,
120                                 refdn == NULL ? "" : refdn,
121                                 NULL );
122
123                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
124                         refdn, NULL,
125                         NULL, NULL );
126
127         } else if ( scope == LDAP_SCOPE_ONELEVEL ) {
128                 send_ldap_result( conn, op, LDAP_SUCCESS,
129                         NULL, NULL, NULL, NULL );
130
131         } else {
132                 struct berval   val;
133                 struct berval   *vals[2];
134                 Entry *e = ch_calloc( 1, sizeof(Entry) );
135                 AttributeDescription *ad_objectClass
136                         = slap_schema.si_ad_objectClass;
137                 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
138                 e->e_dn = strdup( dn );
139                 e->e_ndn = strdup( ndn );
140
141                 e->e_attrs = NULL;
142                 e->e_private = NULL;
143
144                 vals[0] = &val;
145                 vals[1] = NULL;
146
147                 val.bv_val = "top";
148                 val.bv_len = sizeof("top")-1;
149                 attr_merge( e, ad_objectClass, vals );
150
151                 val.bv_val = "referral";
152                 val.bv_len = sizeof("referral")-1;
153                 attr_merge( e, ad_objectClass, vals );
154
155                 val.bv_val = "extensibleObject";
156                 val.bv_len = sizeof("extensibleObject")-1;
157                 attr_merge( e, ad_objectClass, vals );
158
159                 {
160                         AttributeDescription *ad = NULL;
161                         const char *text;
162
163                         rc = slap_str2ad( "dc", &ad, &text );
164
165                         if( rc == LDAP_SUCCESS ) {
166                                 char *p;
167                                 val.bv_val = ch_strdup( domain );
168
169                                 p = strchr( val.bv_val, '.' );
170                                         
171                                 if( p == val.bv_val ) {
172                                         val.bv_val[1] = '\0';
173                                 } else if ( p != NULL ) {
174                                         *p = '\0';
175                                 }
176
177                                 val.bv_len = strlen(val.bv_val);
178                                 attr_merge( e, ad, vals );
179                         }
180                 }
181
182                 {
183                         AttributeDescription *ad = NULL;
184                         const char *text;
185
186                         rc = slap_str2ad( "associatedDomain", &ad, &text );
187
188                         if( rc == LDAP_SUCCESS ) {
189                                 val.bv_val = domain;
190                                 val.bv_len = strlen(domain);
191                                 attr_merge( e, ad, vals );
192                         }
193                 }
194
195                 attr_merge( e, ad_ref, urls );
196
197                 rc = test_filter( be, conn, op, e, filter ); 
198
199                 if( rc == LDAP_COMPARE_TRUE ) {
200                         send_search_entry( be, conn, op,
201                                 e, attrs, attrsonly, NULL );
202                 }
203
204                 entry_free( e );
205                         
206                 send_ldap_result( conn, op, LDAP_SUCCESS,
207                         NULL, NULL, NULL, NULL );
208         }
209
210         free( refdn );
211         free( nrefdn );
212
213 done:
214         if( domain != NULL ) ch_free( domain );
215         if( hostlist != NULL ) ch_free( hostlist );
216         if( hosts != NULL ) charray_free( hosts );
217         if( urls != NULL ) ber_bvecfree( urls );
218         return 0;
219 }