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