]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
Add entryDN MRA support. This is intended to be used:
[openldap] / servers / slapd / search.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29
30 #include <ac/string.h>
31 #include <ac/socket.h>
32
33 #include "ldap_pvt.h"
34 #include "lutil.h"
35 #include "slap.h"
36
37 #ifdef LDAP_SLAPI
38 #include "slapi/slapi.h"
39
40 static char **anlist2charray( Operation *op, AttributeName *an );
41 static void init_search_pblock( Operation *op, char **attrs, int managedsait );
42 static int call_search_preop_plugins( Operation *op );
43 static int call_search_rewrite_plugins( Operation *op );
44 static void call_search_postop_plugins( Operation *op );
45 #endif /* LDAPI_SLAPI */
46
47 int
48 do_search(
49     Operation   *op,    /* info about the op to which we're responding */
50     SlapReply   *rs     /* all the response data we'll send */ )
51 {
52         struct berval base = BER_BVNULL;
53         ber_len_t       siz, off, i;
54
55         Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
56
57         /*
58          * Parse the search request.  It looks like this:
59          *
60          *      SearchRequest := [APPLICATION 3] SEQUENCE {
61          *              baseObject      DistinguishedName,
62          *              scope           ENUMERATED {
63          *                      baseObject      (0),
64          *                      singleLevel     (1),
65          *                      wholeSubtree (2),
66          *          subordinate (3)  -- OpenLDAP extension
67          *              },
68          *              derefAliases    ENUMERATED {
69          *                      neverDerefaliases       (0),
70          *                      derefInSearching        (1),
71          *                      derefFindingBaseObj     (2),
72          *                      alwaysDerefAliases      (3)
73          *              },
74          *              sizelimit       INTEGER (0 .. 65535),
75          *              timelimit       INTEGER (0 .. 65535),
76          *              attrsOnly       BOOLEAN,
77          *              filter          Filter,
78          *              attributes      SEQUENCE OF AttributeType
79          *      }
80          */
81
82         /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
83         if ( ber_scanf( op->o_ber, "{miiiib" /*}*/,
84                 &base, &op->ors_scope, &op->ors_deref, &op->ors_slimit,
85             &op->ors_tlimit, &op->ors_attrsonly ) == LBER_ERROR )
86         {
87                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
88                 rs->sr_err = SLAPD_DISCONNECT;
89                 goto return_results;
90         }
91
92         if ( op->ors_tlimit < 0 || op->ors_tlimit > SLAP_MAX_LIMIT ) {
93                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid time limit" );
94                 goto return_results;
95         }
96
97         if ( op->ors_slimit < 0 || op->ors_slimit > SLAP_MAX_LIMIT ) {
98                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid size limit" );
99                 goto return_results;
100         }
101
102         switch( op->ors_scope ) {
103         case LDAP_SCOPE_BASE:
104         case LDAP_SCOPE_ONELEVEL:
105         case LDAP_SCOPE_SUBTREE:
106 #ifdef LDAP_SCOPE_SUBORDINATE
107         case LDAP_SCOPE_SUBORDINATE:
108 #endif
109                 break;
110         default:
111                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid scope" );
112                 goto return_results;
113         }
114
115         switch( op->ors_deref ) {
116         case LDAP_DEREF_NEVER:
117         case LDAP_DEREF_FINDING:
118         case LDAP_DEREF_SEARCHING:
119         case LDAP_DEREF_ALWAYS:
120                 break;
121         default:
122                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid deref" );
123                 goto return_results;
124         }
125
126         rs->sr_err = dnPrettyNormal( NULL, &base, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
127         if( rs->sr_err != LDAP_SUCCESS ) {
128                 Debug( LDAP_DEBUG_ANY,
129                         "do_search: invalid dn (%s)\n", base.bv_val, 0, 0 );
130                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
131                 goto return_results;
132         }
133
134         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d",
135                 base.bv_val, op->ors_scope, op->ors_deref );
136         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n",
137                 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
138
139         /* filter - returns a "normalized" version */
140         rs->sr_err = get_filter( op, op->o_ber, &op->ors_filter, &rs->sr_text );
141         if( rs->sr_err != LDAP_SUCCESS ) {
142                 if( rs->sr_err == SLAPD_DISCONNECT ) {
143                         rs->sr_err = LDAP_PROTOCOL_ERROR;
144                         send_ldap_disconnect( op, rs );
145                         rs->sr_err = SLAPD_DISCONNECT;
146                 } else {
147                         send_ldap_result( op, rs );
148                 }
149                 goto return_results;
150         }
151         filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
152
153         Debug( LDAP_DEBUG_ARGS, "    filter: %s\n",
154                 !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 );
155
156         /* attributes */
157         siz = sizeof(AttributeName);
158         off = offsetof(AttributeName,an_name);
159         if ( ber_scanf( op->o_ber, "{M}}", &op->ors_attrs, &siz, off ) == LBER_ERROR ) {
160                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding attrs error" );
161                 rs->sr_err = SLAPD_DISCONNECT;
162                 goto return_results;
163         }
164         for ( i=0; i<siz; i++ ) {
165                 const char *dummy;      /* ignore msgs from bv2ad */
166                 op->ors_attrs[i].an_desc = NULL;
167                 op->ors_attrs[i].an_oc = NULL;
168                 op->ors_attrs[i].an_oc_exclude = 0;
169                 slap_bv2ad(&op->ors_attrs[i].an_name, &op->ors_attrs[i].an_desc, &dummy);
170         }
171
172         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
173                 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
174
175                 goto return_results;
176         }
177
178         Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
179
180         if ( siz != 0 ) {
181                 for ( i = 0; i<siz; i++ ) {
182                         Debug( LDAP_DEBUG_ARGS, " %s", op->ors_attrs[i].an_name.bv_val, 0, 0 );
183                 }
184         }
185
186         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
187
188         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
189                 char abuf[BUFSIZ/2], *ptr = abuf;
190                 int len = 0, alen;
191
192                 sprintf(abuf, "scope=%d deref=%d", op->ors_scope, op->ors_deref);
193                 Statslog( LDAP_DEBUG_STATS,
194                         "conn=%lu op=%lu SRCH base=\"%s\" %s filter=\"%s\"\n",
195                         op->o_connid, op->o_opid, op->o_req_dn.bv_val, abuf,
196                         op->ors_filterstr.bv_val );
197
198                 for ( i = 0; i<siz; i++ ) {
199                         alen = op->ors_attrs[i].an_name.bv_len;
200                         if (alen >= sizeof(abuf)) {
201                                 alen = sizeof(abuf)-1;
202                         }
203                         if (len && (len + 1 + alen >= sizeof(abuf))) {
204                                 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
205                                     op->o_connid, op->o_opid, abuf, 0, 0 );
206                                 len = 0;
207                                 ptr = abuf;
208                         }
209                         if (len) {
210                                 *ptr++ = ' ';
211                                 len++;
212                         }
213                         ptr = lutil_strncopy(ptr, op->ors_attrs[i].an_name.bv_val, alen);
214                         len += alen;
215                         *ptr = '\0';
216                 }
217                 if (len) {
218                         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
219                                 op->o_connid, op->o_opid, abuf, 0, 0 );
220                 }
221         }
222
223         op->o_bd = frontendDB;
224         rs->sr_err = frontendDB->be_search( op, rs );
225
226 return_results:;
227         if ( ( op->o_sync_mode & SLAP_SYNC_PERSIST ) ) {
228                 return rs->sr_err;
229         }
230         if ( ( op->o_sync_slog_size != -1 ) ) {
231                 return rs->sr_err;
232         }
233         if ( !BER_BVISNULL( &op->o_req_dn ) ) {
234                 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
235         }
236         if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
237                 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
238         }
239         if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
240                 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
241         }
242         if ( op->ors_filter != NULL) {
243                 filter_free_x( op, op->ors_filter );
244         }
245         if ( op->ors_attrs != NULL ) {
246                 op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
247         }
248
249         return rs->sr_err;
250 }
251
252 int
253 fe_op_search( Operation *op, SlapReply *rs )
254 {
255         int                     manageDSAit;
256         int                     be_manageDSAit;
257 #ifdef LDAP_SLAPI
258         char                    **attrs = NULL;
259 #endif
260
261         manageDSAit = get_manageDSAit( op );
262
263         /* fake while loop to allow breaking out */
264         while ( op->ors_scope == LDAP_SCOPE_BASE ) {
265                 Entry *entry = NULL;
266
267                 if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
268 #ifdef LDAP_CONNECTIONLESS
269                         /* Ignore LDAPv2 CLDAP Root DSE queries */
270                         if (op->o_protocol == LDAP_VERSION2 && op->o_conn->c_is_udp) {
271                                 goto return_results;
272                         }
273 #endif
274                         /* check restrictions */
275                         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
276                                 send_ldap_result( op, rs );
277                                 goto return_results;
278                         }
279
280 #ifdef LDAP_SLAPI
281                         if ( op->o_pb ) {
282                                 attrs = anlist2charray( op, op->ors_attrs );
283                                 init_search_pblock( op, attrs, manageDSAit );
284                                 rs->sr_err = call_search_preop_plugins( op );
285                                 if ( rs->sr_err ) break;
286                                 call_search_rewrite_plugins( op );
287                         }
288 #endif /* LDAP_SLAPI */
289                         rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
290
291                 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
292                         /* check restrictions */
293                         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
294                                 send_ldap_result( op, rs );
295                                 goto return_results;
296                         }
297
298 #ifdef LDAP_SLAPI
299                         if ( op->o_pb ) {
300                                 attrs = anlist2charray( op, op->ors_attrs );
301                                 init_search_pblock( op, attrs, manageDSAit );
302                                 rs->sr_err = call_search_preop_plugins( op );
303                                 if ( rs->sr_err ) break;
304                                 call_search_rewrite_plugins( op );
305                         }
306 #endif /* LDAP_SLAPI */
307                         rs->sr_err = schema_info( &entry, &rs->sr_text );
308                 }
309
310                 if( rs->sr_err != LDAP_SUCCESS ) {
311                         send_ldap_result( op, rs );
312 #ifdef LDAP_SLAPI
313                         if ( op->o_pb ) call_search_postop_plugins( op );
314 #endif /* LDAP_SLAPI */
315                         goto return_results;
316
317                 } else if ( entry != NULL ) {
318                         rs->sr_err = test_filter( op, entry, op->ors_filter );
319
320                         if( rs->sr_err == LDAP_COMPARE_TRUE ) {
321                                 rs->sr_entry = entry;
322                                 rs->sr_attrs = op->ors_attrs;
323                                 rs->sr_operational_attrs = NULL;
324                                 send_search_entry( op, rs );
325                                 rs->sr_entry = NULL;
326                                 rs->sr_operational_attrs = NULL;
327                         }
328                         entry_free( entry );
329
330                         rs->sr_err = LDAP_SUCCESS;
331                         send_ldap_result( op, rs );
332 #ifdef LDAP_SLAPI
333                         if ( op->o_pb ) call_search_postop_plugins( op );
334 #endif /* LDAP_SLAPI */
335                         goto return_results;
336                 }
337                 break;
338         }
339
340         if( BER_BVISEMPTY( &op->o_req_ndn ) && !BER_BVISEMPTY( &default_search_nbase ) ) {
341                 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
342                 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
343
344                 ber_dupbv_x( &op->o_req_dn, &default_search_base, op->o_tmpmemctx );
345                 ber_dupbv_x( &op->o_req_ndn, &default_search_nbase, op->o_tmpmemctx );
346         }
347
348         /*
349          * We could be serving multiple database backends.  Select the
350          * appropriate one, or send a referral to our "referral server"
351          * if we don't hold it.
352          */
353
354         /* Sync control overrides manageDSAit */
355
356         if ( manageDSAit != SLAP_NO_CONTROL ) {
357                 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
358                         be_manageDSAit = SLAP_NO_CONTROL;
359                 } else {
360                         be_manageDSAit = manageDSAit;
361                 }
362         } else {
363                 be_manageDSAit = manageDSAit;
364         }
365
366         op->o_bd = select_backend( &op->o_req_ndn, be_manageDSAit, 1 );
367         if ( op->o_bd == NULL ) {
368                 rs->sr_ref = referral_rewrite( default_referral,
369                         NULL, &op->o_req_dn, op->ors_scope );
370
371                 if (!rs->sr_ref) rs->sr_ref = default_referral;
372                 rs->sr_err = LDAP_REFERRAL;
373                 send_ldap_result( op, rs );
374
375                 if (rs->sr_ref != default_referral)
376                 ber_bvarray_free( rs->sr_ref );
377                 rs->sr_ref = NULL;
378                 goto return_results;
379         }
380
381         /* check restrictions */
382         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
383                 send_ldap_result( op, rs );
384                 goto return_results;
385         }
386
387         /* check for referrals */
388         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
389                 goto return_results;
390         }
391
392 #ifdef LDAP_SLAPI
393         if ( op->o_pb ) {
394                 attrs = anlist2charray( op, op->ors_attrs );
395                 init_search_pblock( op, attrs, manageDSAit );
396                 rs->sr_err = call_search_preop_plugins( op );
397                 if ( rs->sr_err != LDAP_SUCCESS ) {
398                         goto return_results;
399                 }
400
401                 call_search_rewrite_plugins( op );
402         }
403 #endif /* LDAP_SLAPI */
404
405         /* actually do the search and send the result(s) */
406         if ( op->o_bd->be_search ) {
407                 if ( limits_check( op, rs ) == 0 ) {
408                         (op->o_bd->be_search)( op, rs );
409                 }
410                 /* else limits_check() sends error */
411
412         } else {
413                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
414                         "operation not supported within namingContext" );
415         }
416
417 #ifdef LDAP_SLAPI
418         if ( op->o_pb ) call_search_postop_plugins( op );
419 #endif /* LDAP_SLAPI */
420
421 #ifdef LDAP_SLAPI
422         if( attrs != NULL) op->o_tmpfree( attrs, op->o_tmpmemctx );
423 #endif /* LDAP_SLAPI */
424
425 return_results:;
426         return rs->sr_err;
427 }
428
429 #ifdef LDAP_SLAPI
430
431 static char **anlist2charray( Operation *op, AttributeName *an )
432 {
433         char **attrs;
434         int i;
435
436         if ( an != NULL ) {
437                 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
438                         ;
439                 attrs = (char **)op->o_tmpalloc( (i + 1) * sizeof(char *), op->o_tmpmemctx );
440                 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
441                         attrs[i] = an[i].an_name.bv_val;
442                 }
443                 attrs[i] = NULL;
444         } else {
445                 attrs = NULL;
446         }
447
448         return attrs;
449 }
450
451 static void init_search_pblock( Operation *op,
452         char **attrs, int managedsait )
453 {
454         slapi_int_pblock_set_operation( op->o_pb, op );
455         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_TARGET, (void *)op->o_req_dn.bv_val );
456         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_SCOPE, (void *)op->ors_scope );
457         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_DEREF, (void *)op->ors_deref );
458         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_SIZELIMIT, (void *)op->ors_slimit );
459         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_TIMELIMIT, (void *)op->ors_tlimit );
460         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_FILTER, (void *)op->ors_filter );
461         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_STRFILTER, (void *)op->ors_filterstr.bv_val );
462         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_ATTRS, (void *)attrs );
463         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_ATTRSONLY, (void *)op->ors_attrsonly );
464         slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)managedsait );
465 }
466
467 static int call_search_preop_plugins( Operation *op )
468 {
469         int rc;
470
471         rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_SEARCH_FN, op->o_pb );
472         if ( rc < 0 ) {
473                 /*
474                  * A preoperation plugin failure will abort the
475                  * entire operation.
476                  */
477                 Debug(LDAP_DEBUG_TRACE, "call_search_preop_plugins: search preoperation plugin "
478                                 "returned %d.\n", rc, 0, 0);
479                 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 ) ||
480                      rc == LDAP_SUCCESS ) {
481                         rc = LDAP_OTHER;
482                 }
483         } else {
484                 rc = LDAP_SUCCESS;
485         }
486
487         return rc;
488 }
489
490 static int call_search_rewrite_plugins( Operation *op )
491 {
492         if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, op->o_pb ) == 0 ) {
493                 int rc;
494
495                 /*
496                  * The plugin can set the SLAPI_SEARCH_FILTER.
497                  * SLAPI_SEARCH_STRFILER is not normative.
498                  */
499                 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_FILTER, (void *)&op->ors_filter );
500                 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
501                 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
502
503                 /*
504                  * Also permit other search parameters to be reset. One thing
505                  * this doesn't (yet) deal with is plugins that change a root
506                  * DSE search to a non-root DSE search...
507                  */
508                 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_TARGET, (void **)&op->o_req_dn.bv_val );
509                 op->o_req_dn.bv_len = strlen( op->o_req_dn.bv_val );
510
511                 if( !BER_BVISNULL( &op->o_req_ndn ) ) {
512                         slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
513                 }
514                 rc = dnNormalize( 0, NULL, NULL, &op->o_req_dn, &op->o_req_ndn,
515                         op->o_tmpmemctx );
516                 if ( rc != LDAP_SUCCESS ) {
517                         return rc;
518                 }
519
520                 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_SCOPE, (void **)&op->ors_scope );
521                 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_DEREF, (void **)&op->ors_deref );
522
523                 Debug( LDAP_DEBUG_ARGS, "    after compute_rewrite_search filter: %s\n",
524                         !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 );
525         }
526
527         return LDAP_SUCCESS;
528 }
529
530 static void call_search_postop_plugins( Operation *op )
531 {
532         if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_SEARCH_FN, op->o_pb ) < 0 ) {
533                 Debug(LDAP_DEBUG_TRACE, "call_search_postop_plugins: search postoperation plugins "
534                                 "failed.\n", 0, 0, 0);
535         }
536 }
537
538 void slapi_int_dummy(void)
539 {
540         /*
541          * XXX slapi_search_internal() was no getting pulled
542          * in; all manner of linker flags failed to link it.
543          * FIXME
544          */
545         slapi_search_internal( NULL, 0, NULL, NULL, NULL, 0 );
546 }
547 #endif /* LDAP_SLAPI */
548