]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
1b0e3a20ee28a785f69dd546ce1b850fbcf560cb
[openldap] / servers / slapd / back-ldap / search.c
1 /* search.c - ldap backend search function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7 /* This is an altered version */
8 /*
9  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
10  * 
11  * Permission is granted to anyone to use this software for any purpose
12  * on any computer system, and to alter it and redistribute it, subject
13  * to the following restrictions:
14  * 
15  * 1. The author is not responsible for the consequences of use of this
16  *    software, no matter how awful, even if they arise from flaws in it.
17  * 
18  * 2. The origin of this software must not be misrepresented, either by
19  *    explicit claim or by omission.  Since few users ever read sources,
20  *    credits should appear in the documentation.
21  * 
22  * 3. Altered versions must be plainly marked as such, and must not be
23  *    misrepresented as being the original software.  Since few users
24  *    ever read sources, credits should appear in the documentation.
25  * 
26  * 4. This notice may not be removed or altered.
27  *
28  *
29  *
30  * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
31  * 
32  * This software is being modified by Pierangelo Masarati.
33  * The previously reported conditions apply to the modified code as well.
34  * Changes in the original code are highlighted where required.
35  * Credits for the original code go to the author, Howard Chu.
36  */
37
38 #include "portable.h"
39
40 #include <stdio.h>
41
42 #include <ac/socket.h>
43 #include <ac/string.h>
44 #include <ac/time.h>
45
46 #include "slap.h"
47 #include "back-ldap.h"
48
49 static void ldap_send_entry( Backend *be, Operation *op, struct ldapconn *lc,
50                              LDAPMessage *e, char **attrs, int attrsonly );
51
52 int
53 ldap_back_search(
54     Backend     *be,
55     Connection  *conn,
56     Operation   *op,
57     const char  *base,
58     const char  *nbase,
59     int         scope,
60     int         deref,
61     int         size,
62     int         time,
63     Filter      *filter,
64     const char  *filterstr,
65     char        **attrs,
66     int         attrsonly
67 )
68 {
69         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
70         struct ldapconn *lc;
71         struct timeval  tv;
72         LDAPMessage             *res, *e;
73         int     count, rc = 0, msgid, sres = LDAP_SUCCESS; 
74         char *match = NULL, *err = NULL;
75         char *mbase = NULL, *mapped_filter = NULL, **mapped_attrs = NULL;
76 #ifdef ENABLE_REWRITE
77         char *mfilter = NULL, *mmatch = NULL;
78 #endif /* ENABLE_REWRITE */
79
80         lc = ldap_back_getconn(li, conn, op);
81         if ( !lc ) {
82                 return( -1 );
83         }
84
85         if (deref != -1)
86                 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
87         if (time != -1)
88                 ldap_set_option( lc->ld, LDAP_OPT_TIMELIMIT, (void *)&time);
89         if (size != -1)
90                 ldap_set_option( lc->ld, LDAP_OPT_SIZELIMIT, (void *)&size);
91         
92         if ( !ldap_back_dobind( lc, op ) ) {
93                 return( -1 );
94         }
95
96         /*
97          * Rewrite the search base, if required
98          */
99 #ifdef ENABLE_REWRITE
100         switch ( rewrite_session( li->rwinfo, "searchBase",
101                                 base, conn, &mbase ) ) {
102         case REWRITE_REGEXEC_OK:
103                 if ( mbase == NULL ) {
104                         mbase = ( char * )base;
105                 }
106                 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
107                                 base, mbase, "" );
108                 break;
109                 
110         case REWRITE_REGEXEC_UNWILLING:
111                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
112                                 NULL, "Unwilling to perform", NULL, NULL );
113
114         case REWRITE_REGEXEC_ERR:
115                 rc = -1;
116                 goto finish;
117         }
118         
119         /*
120          * Rewrite the search filter, if required
121          */
122         switch ( rewrite_session( li->rwinfo, "searchFilter",
123                                 filterstr, conn, &mfilter ) ) {
124         case REWRITE_REGEXEC_OK:
125                 if ( mfilter == NULL || mfilter[0] == '\0') {
126                         if ( mfilter != NULL ) {
127                                 free( mfilter );
128                         }
129                         mfilter = ( char * )filterstr;
130                 }
131                 Debug( LDAP_DEBUG_ARGS,
132                                 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
133                                 filterstr, mfilter, "" );
134                 break;
135                 
136         case REWRITE_REGEXEC_UNWILLING:
137                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
138                                 NULL, "Unwilling to perform", NULL, NULL );
139         case REWRITE_REGEXEC_ERR:
140                 rc = -1;
141                 goto finish;
142         }
143 #else /* !ENABLE_REWRITE */
144         mbase = ldap_back_dn_massage( li, ch_strdup( base ), 0 );
145 #endif /* !ENABLE_REWRITE */
146
147         mapped_filter = ldap_back_map_filter(&li->at_map, &li->oc_map,
148 #ifdef ENABLE_REWRITE
149                         (char *)mfilter,
150 #else /* !ENABLE_REWRITE */
151                         (char *)filterstr,
152 #endif /* !ENABLE_REWRITE */
153                         0);
154         if ( mapped_filter == NULL ) {
155 #ifdef ENABLE_REWRITE
156                 mapped_filter = (char *)mfilter;
157 #else /* !ENABLE_REWRITE */
158                 mapped_filter = (char *)filterstr;
159 #endif /* !ENABLE_REWRITE */
160         }
161
162         mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, 0);
163         if ( mapped_attrs == NULL ) {
164                 mapped_attrs = attrs;
165         }
166
167         if ((msgid = ldap_search(lc->ld, mbase, scope, mapped_filter, mapped_attrs,
168                 attrsonly)) == -1)
169         {
170 fail:
171                 rc = ldap_back_op_result(lc, op);
172                 goto finish;
173         }
174
175         /* We pull apart the ber result, stuff it into a slapd entry, and
176          * let send_search_entry stuff it back into ber format. Slow & ugly,
177          * but this is necessary for version matching, and for ACL processing.
178          */
179         
180         for (   count=0, rc=0;
181                         rc != -1;
182                         rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res))
183         {
184                 int ab;
185
186                 /* check for abandon */
187                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
188                 ab = op->o_abandon;
189                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
190
191                 if (ab) {
192                         ldap_abandon(lc->ld, msgid);
193                         rc = 0;
194                         goto finish;
195                 }
196                 if (rc == 0) {
197                         tv.tv_sec = 0;
198                         tv.tv_usec = 100000;
199                         ldap_pvt_thread_yield();
200                 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
201                         e = ldap_first_entry(lc->ld,res);
202                         ldap_send_entry(be, op, lc, e, attrs, attrsonly);
203                         count++;
204                         ldap_msgfree(res);
205                 } else {
206                         sres = ldap_result2error(lc->ld, res, 1);
207                         sres = ldap_back_map_result(sres);
208                         ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
209                         ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
210                         rc = 0;
211                         break;
212                 }
213         }
214
215         if (rc == -1)
216                 goto fail;
217
218 #ifdef ENABLE_REWRITE
219         /*
220          * Rewrite the matched portion of the search base, if required
221          */
222         if ( match != NULL ) {
223                 switch ( rewrite_session( li->rwinfo, "matchedDn",
224                                 match, conn, &mmatch ) ) {
225                 case REWRITE_REGEXEC_OK:
226                         if ( mmatch == NULL ) {
227                                 mmatch = ( char * )match;
228                         }
229                         Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
230                                        " \"%s\" -> \"%s\"\n%s",
231                                        match, mmatch, "" );
232                         break;
233                         
234                 case REWRITE_REGEXEC_UNWILLING:
235                         send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
236                                         NULL, "Unwilling to perform",
237                                         NULL, NULL );
238                         
239                 case REWRITE_REGEXEC_ERR:
240                         rc = -1;
241                         goto finish;
242                 }
243         }
244
245         send_search_result( conn, op, sres,
246                 mmatch, err, NULL, NULL, count );
247
248 #else /* !ENABLE_REWRITE */
249         send_search_result( conn, op, sres,
250                 match, err, NULL, NULL, count );
251 #endif /* !ENABLE_REWRITE */
252
253 finish:
254         if ( match ) {
255 #ifdef ENABLE_REWRITE
256                 if ( mmatch != match ) {
257                         free( mmatch );
258                 }
259 #endif /* ENABLE_REWRITE */
260                 free(match);
261         }
262         if ( err ) {
263                 free( err );
264         }
265         if ( mapped_attrs != attrs ) {
266                 charray_free( mapped_attrs );
267         }
268 #ifdef ENABLE_REWRITE
269         if ( mapped_filter != mfilter ) {
270                 free( mapped_filter );
271         }
272         if ( mfilter != filterstr ) {
273                 free( mfilter );
274         }
275 #else /* !ENABLE_REWRITE */
276         if ( mapped_filter != filterstr ) {
277                 free( mapped_filter );
278         }
279 #endif /* !ENABLE_REWRITE */
280         
281 #ifdef ENABLE_REWRITE
282         if ( mbase != base ) {
283 #endif /* ENABLE_REWRITE */
284                 free( mbase );
285 #ifdef ENABLE_REWRITE
286         }
287 #endif /* ENABLE_REWRITE */
288         
289         return rc;
290 }
291
292 static void
293 ldap_send_entry(
294         Backend *be,
295         Operation *op,
296         struct ldapconn *lc,
297         LDAPMessage *e,
298         char **attrs,
299         int attrsonly
300 )
301 {
302         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
303         char *a, *mapped;
304         Entry ent;
305         BerElement *ber = NULL;
306         Attribute *attr, **attrp;
307         struct berval *dummy = NULL;
308         struct berval *bv;
309         const char *text;
310
311 #ifdef ENABLE_REWRITE
312         char *dn;
313
314         dn = ldap_get_dn(lc->ld, e);
315         if ( dn == NULL ) {
316                 return;
317         }
318
319         /*
320          * Rewrite the dn of the result, if needed
321          */
322         switch ( rewrite_session( li->rwinfo, "searchResult",
323                                 dn, lc->conn, &ent.e_dn ) ) {
324         case REWRITE_REGEXEC_OK:
325                 if ( ent.e_dn == NULL ) {
326                         ent.e_dn = dn;
327                 } else {
328                         Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
329                                         " -> \"%s\"\n%s", dn, ent.e_dn, "" );
330                         free( dn );
331                         dn = NULL;
332                 }
333                 break;
334                 
335         case REWRITE_REGEXEC_ERR:
336         case REWRITE_REGEXEC_UNWILLING:
337                 free( dn );
338                 return;
339         }
340 #else /* !ENABLE_REWRITE */
341         ent.e_dn = ldap_back_dn_restore( li, ldap_get_dn(lc->ld, e), 0 );
342 #endif /* !ENABLE_REWRITE */
343
344         ent.e_ndn = ch_strdup( ent.e_dn );
345         (void) dn_normalize( ent.e_ndn );
346         ent.e_id = 0;
347         ent.e_attrs = 0;
348         ent.e_private = 0;
349         attrp = &ent.e_attrs;
350
351         for (   a = ldap_first_attribute(lc->ld, e, &ber);
352                         a != NULL;
353                         a = ldap_next_attribute(lc->ld, e, ber))
354         {
355                 mapped = ldap_back_map(&li->at_map, a, 1);
356                 if (mapped == NULL)
357                         continue;
358                 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
359                 if (attr == NULL)
360                         continue;
361                 attr->a_next = 0;
362                 attr->a_desc = NULL;
363                 if (slap_str2ad(mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
364                         ch_free(attr);
365                         continue;
366                 }
367                 attr->a_vals = ldap_get_values_len(lc->ld, e, a);
368                 if (!attr->a_vals) {
369                         attr->a_vals = &dummy;
370                 } else if ( strcasecmp( mapped, "objectclass" ) == 0 ) {
371                         int i, last;
372                         for ( last = 0; attr->a_vals[last]; last++ ) ;
373                         for ( i = 0; ( bv = attr->a_vals[i] ); i++ ) {
374                                 mapped = ldap_back_map(&li->oc_map, bv->bv_val, 1);
375                                 if (mapped == NULL) {
376                                         ber_bvfree(attr->a_vals[i]);
377                                         attr->a_vals[i] = NULL;
378                                         if (--last < 0)
379                                                 break;
380                                         attr->a_vals[i] = attr->a_vals[last];
381                                         attr->a_vals[last] = NULL;
382                                         i--;
383                                 } else if ( mapped != bv->bv_val ) {
384                                         ch_free(bv->bv_val);
385                                         bv->bv_val = ch_strdup( mapped );
386                                         bv->bv_len = strlen( mapped );
387                                 }
388                         }
389
390 #ifdef ENABLE_REWRITE
391                 /*
392                  * It is necessary to try to rewrite attributes with
393                  * dn syntax because they might be used in ACLs as
394                  * members of groups; since ACLs are applied to the
395                  * rewritten stuff, no dn-based subecj clause could
396                  * be used at the ldap backend side (see
397                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
398                  * The problem can be overcome by moving the dn-based
399                  * ACLs to the target directory server, and letting
400                  * everything pass thru the ldap backend.
401                  */
402                 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
403                                         SLAPD_DN_SYNTAX ) == 0 ) {
404                         int i;
405                         for ( i = 0; ( bv = attr->a_vals[ i ] ); i++ ) {
406                                 char *newval;
407                                 
408                                 switch ( rewrite_session( li->rwinfo,
409                                                         "searchResult",
410                                                         bv->bv_val,
411                                                         lc->conn, &newval )) {
412                                 case REWRITE_REGEXEC_OK:
413                                         /* left as is */
414                                         if ( newval == NULL ) {
415                                                 break;
416                                         }
417                                         Debug( LDAP_DEBUG_ARGS,
418                 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
419                                                 attr->a_desc->ad_type->sat_cname,
420                                                 bv->bv_val, newval );
421                                         
422                                         free( bv->bv_val );
423                                         bv->bv_val = newval;
424                                         bv->bv_len = strlen( newval );
425                                         
426                                         break;
427                                         
428                                 case REWRITE_REGEXEC_UNWILLING:
429                                         
430                                 case REWRITE_REGEXEC_ERR:
431                                         /*
432                                          * FIXME: better give up,
433                                          * skip the attribute
434                                          * or leave it untouched?
435                                          */
436                                         break;
437                                 }
438                         }
439 #endif /* ENABLE_REWRITE */
440                 }
441
442                 *attrp = attr;
443                 attrp = &attr->a_next;
444         }
445         send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
446         while (ent.e_attrs) {
447                 attr = ent.e_attrs;
448                 ent.e_attrs = attr->a_next;
449                 ad_free(attr->a_desc, 1);
450                 if (attr->a_vals != &dummy)
451                         ber_bvecfree(attr->a_vals);
452                 free(attr);
453         }
454         if (ber)
455                 ber_free(ber,0);
456         
457         if ( ent.e_dn )
458                 free( ent.e_dn );
459         if ( ent.e_ndn )
460                 free( ent.e_ndn );
461 }
462