]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
added new log; minor cleanup of rewrite stuff
[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 #ifdef NEW_LOGGING
107                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
108                                 "[rw] searchBase: \"%s\" -> \"%s\"\n%",
109                                 base, mbase ));
110 #else /* !NEW_LOGGING */
111                 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
112                                 base, mbase, "" );
113 #endif /* !NEW_LOGGING */
114                 break;
115                 
116         case REWRITE_REGEXEC_UNWILLING:
117                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
118                                 NULL, "Unwilling to perform", NULL, NULL );
119                 rc = -1;
120                 goto finish;
121
122         case REWRITE_REGEXEC_ERR:
123                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
124                                 NULL, "Operations error", NULL, NULL );
125                 rc = -1;
126                 goto finish;
127         }
128         
129         /*
130          * Rewrite the search filter, if required
131          */
132         switch ( rewrite_session( li->rwinfo, "searchFilter",
133                                 filterstr, conn, &mfilter ) ) {
134         case REWRITE_REGEXEC_OK:
135                 if ( mfilter == NULL || mfilter[0] == '\0') {
136                         if ( mfilter != NULL ) {
137                                 free( mfilter );
138                         }
139                         mfilter = ( char * )filterstr;
140                 }
141 #ifdef NEW_LOGGING
142                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
143                                 "[rw] searchFilter: \"%s\" -> \"%s\"\n",
144                                 filterstr, mfilter ));
145 #else /* !NEW_LOGGING */
146                 Debug( LDAP_DEBUG_ARGS,
147                                 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
148                                 filterstr, mfilter, "" );
149 #endif /* !NEW_LOGGING */
150                 break;
151                 
152         case REWRITE_REGEXEC_UNWILLING:
153                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
154                                 NULL, "Unwilling to perform", NULL, NULL );
155         case REWRITE_REGEXEC_ERR:
156                 rc = -1;
157                 goto finish;
158         }
159 #else /* !ENABLE_REWRITE */
160         mbase = ldap_back_dn_massage( li, ch_strdup( base ), 0 );
161 #endif /* !ENABLE_REWRITE */
162
163         mapped_filter = ldap_back_map_filter(&li->at_map, &li->oc_map,
164 #ifdef ENABLE_REWRITE
165                         (char *)mfilter,
166 #else /* !ENABLE_REWRITE */
167                         (char *)filterstr,
168 #endif /* !ENABLE_REWRITE */
169                         0);
170         if ( mapped_filter == NULL ) {
171 #ifdef ENABLE_REWRITE
172                 mapped_filter = (char *)mfilter;
173 #else /* !ENABLE_REWRITE */
174                 mapped_filter = (char *)filterstr;
175 #endif /* !ENABLE_REWRITE */
176         }
177
178         mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, 0);
179         if ( mapped_attrs == NULL ) {
180                 mapped_attrs = attrs;
181         }
182
183         if ((msgid = ldap_search(lc->ld, mbase, scope, mapped_filter, mapped_attrs,
184                 attrsonly)) == -1)
185         {
186 fail:
187                 rc = ldap_back_op_result(lc, op);
188                 goto finish;
189         }
190
191         /* We pull apart the ber result, stuff it into a slapd entry, and
192          * let send_search_entry stuff it back into ber format. Slow & ugly,
193          * but this is necessary for version matching, and for ACL processing.
194          */
195         
196         for (   count=0, rc=0;
197                         rc != -1;
198                         rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res))
199         {
200                 int ab;
201
202                 /* check for abandon */
203                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
204                 ab = op->o_abandon;
205                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
206
207                 if (ab) {
208                         ldap_abandon(lc->ld, msgid);
209                         rc = 0;
210                         goto finish;
211                 }
212                 if (rc == 0) {
213                         tv.tv_sec = 0;
214                         tv.tv_usec = 100000;
215                         ldap_pvt_thread_yield();
216                 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
217                         e = ldap_first_entry(lc->ld,res);
218                         ldap_send_entry(be, op, lc, e, attrs, attrsonly);
219                         count++;
220                         ldap_msgfree(res);
221                 } else {
222                         sres = ldap_result2error(lc->ld, res, 1);
223                         sres = ldap_back_map_result(sres);
224                         ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
225                         ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
226                         rc = 0;
227                         break;
228                 }
229         }
230
231         if (rc == -1)
232                 goto fail;
233
234 #ifdef ENABLE_REWRITE
235         /*
236          * Rewrite the matched portion of the search base, if required
237          */
238         if ( match != NULL ) {
239                 switch ( rewrite_session( li->rwinfo, "matchedDn",
240                                 match, conn, &mmatch ) ) {
241                 case REWRITE_REGEXEC_OK:
242                         if ( mmatch == NULL ) {
243                                 mmatch = ( char * )match;
244                         }
245 #ifdef NEW_LOGGING
246                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
247                                         "[rw]  matchedDn:"
248                                         " \"%s\" -> \"%s\"\n",
249                                         match, mmatch ));
250 #else /* !NEW_LOGGING */
251                         Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
252                                         " \"%s\" -> \"%s\"\n%s",
253                                         match, mmatch, "" );
254 #endif /* !NEW_LOGGING */
255                         break;
256                         
257                 case REWRITE_REGEXEC_UNWILLING:
258                         send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
259                                         NULL, "Unwilling to perform",
260                                         NULL, NULL );
261                         
262                 case REWRITE_REGEXEC_ERR:
263                         rc = -1;
264                         goto finish;
265                 }
266         }
267
268         send_search_result( conn, op, sres,
269                 mmatch, err, NULL, NULL, count );
270
271 #else /* !ENABLE_REWRITE */
272         send_search_result( conn, op, sres,
273                 match, err, NULL, NULL, count );
274 #endif /* !ENABLE_REWRITE */
275
276 finish:
277         if ( match ) {
278 #ifdef ENABLE_REWRITE
279                 if ( mmatch != match ) {
280                         free( mmatch );
281                 }
282 #endif /* ENABLE_REWRITE */
283                 free(match);
284         }
285         if ( err ) {
286                 free( err );
287         }
288         if ( mapped_attrs != attrs ) {
289                 charray_free( mapped_attrs );
290         }
291 #ifdef ENABLE_REWRITE
292         if ( mapped_filter != mfilter ) {
293                 free( mapped_filter );
294         }
295         if ( mfilter != filterstr ) {
296                 free( mfilter );
297         }
298 #else /* !ENABLE_REWRITE */
299         if ( mapped_filter != filterstr ) {
300                 free( mapped_filter );
301         }
302 #endif /* !ENABLE_REWRITE */
303         
304 #ifdef ENABLE_REWRITE
305         if ( mbase != base ) {
306 #endif /* ENABLE_REWRITE */
307                 free( mbase );
308 #ifdef ENABLE_REWRITE
309         }
310 #endif /* ENABLE_REWRITE */
311         
312         return rc;
313 }
314
315 static void
316 ldap_send_entry(
317         Backend *be,
318         Operation *op,
319         struct ldapconn *lc,
320         LDAPMessage *e,
321         char **attrs,
322         int attrsonly
323 )
324 {
325         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
326         char *a, *mapped;
327         Entry ent;
328         BerElement *ber = NULL;
329         Attribute *attr, **attrp;
330         struct berval *dummy = NULL;
331         struct berval *bv;
332         const char *text;
333
334 #ifdef ENABLE_REWRITE
335         char *dn;
336
337         dn = ldap_get_dn(lc->ld, e);
338         if ( dn == NULL ) {
339                 return;
340         }
341
342         /*
343          * Rewrite the dn of the result, if needed
344          */
345         switch ( rewrite_session( li->rwinfo, "searchResult",
346                                 dn, lc->conn, &ent.e_dn ) ) {
347         case REWRITE_REGEXEC_OK:
348                 if ( ent.e_dn == NULL ) {
349                         ent.e_dn = dn;
350                 } else {
351 #ifdef NEW_LOGGING
352                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
353                                         "[rw] searchResult: \"%s\""
354                                         " -> \"%s\"\n", dn, ent.e_dn ));
355 #else /* !NEW_LOGGING */
356                         Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
357                                         " -> \"%s\"\n%s", dn, ent.e_dn, "" );
358 #endif /* !NEW_LOGGING */
359                         free( dn );
360                         dn = NULL;
361                 }
362                 break;
363                 
364         case REWRITE_REGEXEC_ERR:
365         case REWRITE_REGEXEC_UNWILLING:
366                 free( dn );
367                 return;
368         }
369 #else /* !ENABLE_REWRITE */
370         ent.e_dn = ldap_back_dn_restore( li, ldap_get_dn(lc->ld, e), 0 );
371 #endif /* !ENABLE_REWRITE */
372
373         ent.e_ndn = ch_strdup( ent.e_dn );
374         (void) dn_normalize( ent.e_ndn );
375         ent.e_id = 0;
376         ent.e_attrs = 0;
377         ent.e_private = 0;
378         attrp = &ent.e_attrs;
379
380         for (   a = ldap_first_attribute(lc->ld, e, &ber);
381                         a != NULL;
382                         a = ldap_next_attribute(lc->ld, e, ber))
383         {
384                 mapped = ldap_back_map(&li->at_map, a, 1);
385                 if (mapped == NULL)
386                         continue;
387                 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
388                 if (attr == NULL)
389                         continue;
390                 attr->a_next = 0;
391                 attr->a_desc = NULL;
392                 if (slap_str2ad(mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
393                         ch_free(attr);
394                         continue;
395                 }
396                 attr->a_vals = ldap_get_values_len(lc->ld, e, a);
397                 if (!attr->a_vals) {
398                         attr->a_vals = &dummy;
399                 } else if ( strcasecmp( mapped, "objectclass" ) == 0 ) {
400                         int i, last;
401                         for ( last = 0; attr->a_vals[last]; last++ ) ;
402                         for ( i = 0; ( bv = attr->a_vals[i] ); i++ ) {
403                                 mapped = ldap_back_map(&li->oc_map, bv->bv_val, 1);
404                                 if (mapped == NULL) {
405                                         ber_bvfree(attr->a_vals[i]);
406                                         attr->a_vals[i] = NULL;
407                                         if (--last < 0)
408                                                 break;
409                                         attr->a_vals[i] = attr->a_vals[last];
410                                         attr->a_vals[last] = NULL;
411                                         i--;
412                                 } else if ( mapped != bv->bv_val ) {
413                                         ch_free(bv->bv_val);
414                                         bv->bv_val = ch_strdup( mapped );
415                                         bv->bv_len = strlen( mapped );
416                                 }
417                         }
418
419 #ifdef ENABLE_REWRITE
420                 /*
421                  * It is necessary to try to rewrite attributes with
422                  * dn syntax because they might be used in ACLs as
423                  * members of groups; since ACLs are applied to the
424                  * rewritten stuff, no dn-based subecj clause could
425                  * be used at the ldap backend side (see
426                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
427                  * The problem can be overcome by moving the dn-based
428                  * ACLs to the target directory server, and letting
429                  * everything pass thru the ldap backend.
430                  */
431                 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
432                                         SLAPD_DN_SYNTAX ) == 0 ) {
433                         int i;
434                         for ( i = 0; ( bv = attr->a_vals[ i ] ); i++ ) {
435                                 char *newval;
436                                 
437                                 switch ( rewrite_session( li->rwinfo,
438                                                         "searchResult",
439                                                         bv->bv_val,
440                                                         lc->conn, &newval )) {
441                                 case REWRITE_REGEXEC_OK:
442                                         /* left as is */
443                                         if ( newval == NULL ) {
444                                                 break;
445                                         }
446 #ifdef NEW_LOGGING
447                                         LDAP_LOG(( "backend",
448                                                         LDAP_LEVEL_DETAIL1,
449                                                         "[rw] searchResult on"
450                                                         " attr=%s:"
451                                                         " \"%s\" -> \"%s\"\n",
452                                                         attr->a_desc->ad_type->sat_cname,
453                                                         bv->bv_val, newval ));
454 #else /* !NEW_LOGGING */
455                                         Debug( LDAP_DEBUG_ARGS,
456                 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
457                                                 attr->a_desc->ad_type->sat_cname,
458                                                 bv->bv_val, newval );
459 #endif /* !NEW_LOGGING */
460                                         
461                                         free( bv->bv_val );
462                                         bv->bv_val = newval;
463                                         bv->bv_len = strlen( newval );
464                                         
465                                         break;
466                                         
467                                 case REWRITE_REGEXEC_UNWILLING:
468                                         
469                                 case REWRITE_REGEXEC_ERR:
470                                         /*
471                                          * FIXME: better give up,
472                                          * skip the attribute
473                                          * or leave it untouched?
474                                          */
475                                         break;
476                                 }
477                         }
478 #endif /* ENABLE_REWRITE */
479                 }
480
481                 *attrp = attr;
482                 attrp = &attr->a_next;
483         }
484         send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
485         while (ent.e_attrs) {
486                 attr = ent.e_attrs;
487                 ent.e_attrs = attr->a_next;
488                 ad_free(attr->a_desc, 1);
489                 if (attr->a_vals != &dummy)
490                         ber_bvecfree(attr->a_vals);
491                 free(attr);
492         }
493         if (ber)
494                 ber_free(ber,0);
495         
496         if ( ent.e_dn )
497                 free( ent.e_dn );
498         if ( ent.e_ndn )
499                 free( ent.e_ndn );
500 }
501