]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/slapi_overlay.c
s/orb_tmp_mech/orb_mech/
[openldap] / servers / slapd / slapi / slapi_overlay.c
1 /* slapi_overlay.c - SLAPI overlay */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2007 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Luke Howard for inclusion
18  * in OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24
25 #include <ac/string.h>
26 #include <ac/socket.h>
27
28 #include "slap.h"
29 #include "slapi.h"
30
31 #ifdef LDAP_SLAPI
32
33 static slap_overinst slapi;
34 static int slapi_over_initialized = 0;
35
36 static int slapi_over_response( Operation *op, SlapReply *rs );
37 static int slapi_over_cleanup( Operation *op, SlapReply *rs );
38
39 static Slapi_PBlock *
40 slapi_over_pblock_new( Operation *op, SlapReply *rs )
41 {
42         Slapi_PBlock            *pb;
43
44         pb = slapi_pblock_new();
45         pb->pb_op = op;
46         pb->pb_conn = op->o_conn;
47         pb->pb_rs = rs;
48         pb->pb_intop = 0;
49
50         PBLOCK_ASSERT_OP( pb, op->o_tag );
51
52         return pb;
53 }
54
55 static int
56 slapi_op_internal_p( Operation *op, SlapReply *rs, slap_callback *cb )
57 {
58         int                     internal_op = 0;
59         Slapi_PBlock            *pb = NULL;
60         slap_callback           *pcb;
61
62         /*
63          * Abstraction violating check for SLAPI internal operations
64          * allows pblock to remain consistent when invoking internal
65          * op plugins
66          */
67         for ( pcb = op->o_callback; pcb != NULL; pcb = pcb->sc_next ) {
68                 if ( pcb->sc_response == slapi_int_response ) {
69                         pb = (Slapi_PBlock *)pcb->sc_private;
70                         PBLOCK_ASSERT_INTOP( pb, 0 );
71                         internal_op = 1;
72                         break;
73                 }
74         }
75
76         if ( cb != NULL ) {
77                 if ( pb == NULL ) {
78                         pb = slapi_over_pblock_new( op, rs );
79                 }
80
81                 cb->sc_response = slapi_over_response;
82                 cb->sc_cleanup = slapi_over_cleanup;
83                 cb->sc_private = pb;
84                 cb->sc_next = op->o_callback;
85                 op->o_callback = cb;
86         }
87
88         return internal_op;
89 }
90
91 static int
92 slapi_over_compute_output(
93         computed_attr_context *c,
94         Slapi_Attr *attribute,
95         Slapi_Entry *entry
96 )
97 {
98         Attribute               **a;
99         AttributeDescription    *desc;
100         SlapReply               *rs;
101
102         if ( c == NULL || attribute == NULL || entry == NULL ) {
103                 return 0;
104         }
105
106         rs = (SlapReply *)c->cac_private;
107
108         assert( rs->sr_entry == entry );
109
110         desc = attribute->a_desc;
111
112         if ( rs->sr_attrs == NULL ) {
113                 /* All attrs request, skip operational attributes */
114                 if ( is_at_operational( desc->ad_type ) ) {
115                         return 0;
116                 }
117         } else {
118                 /* Specific attributes requested */
119                 if ( is_at_operational( desc->ad_type ) ) {
120                         if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
121                              !ad_inlist( desc, rs->sr_attrs ) ) {
122                                 return 0;
123                         }
124                 } else {
125                         if ( !SLAP_USERATTRS( rs->sr_attr_flags ) &&
126                              !ad_inlist( desc, rs->sr_attrs ) ) {
127                                 return 0;
128                         }
129                 }
130         }
131
132         /* XXX perhaps we should check for existing attributes and merge */
133         for ( a = &rs->sr_operational_attrs; *a != NULL; a = &(*a)->a_next )
134                 ;
135
136         *a = slapi_attr_dup( attribute );
137
138         return 0;
139 }
140
141 static int
142 slapi_over_aux_operational( Operation *op, SlapReply *rs )
143 {
144         /* Support for computed attribute plugins */
145         computed_attr_context    ctx;
146         AttributeName           *anp;
147
148         if ( slapi_op_internal_p( op, rs, NULL ) ) {
149                 return SLAP_CB_CONTINUE;
150         }
151
152         ctx.cac_pb = slapi_over_pblock_new( op, rs );
153         ctx.cac_op = op;
154         ctx.cac_private = rs;
155
156         if ( rs->sr_entry != NULL ) {
157                 /*
158                  * For each client requested attribute, call the plugins.
159                  */
160                 if ( rs->sr_attrs != NULL ) {
161                         for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
162                                 if ( compute_evaluator( &ctx, anp->an_name.bv_val,
163                                         rs->sr_entry, slapi_over_compute_output ) == 1 ) {
164                                         break;
165                                 }
166                         }
167                 } else {
168                         /*
169                          * Technically we shouldn't be returning operational attributes
170                          * when the user requested only user attributes. We'll let the
171                          * plugin decide whether to be naughty or not.
172                          */
173                         compute_evaluator( &ctx, "*", rs->sr_entry, slapi_over_compute_output );
174                 }
175         }
176
177         slapi_pblock_destroy( ctx.cac_pb );
178
179         return SLAP_CB_CONTINUE;
180 }
181
182 /*
183  * We need this function to call frontendDB (global) plugins before
184  * database plugins, if we are invoked by a slap_callback.
185  */
186 static int
187 slapi_over_call_plugins( Slapi_PBlock *pb, int type )
188 {
189         int                     rc = 1; /* means no plugins called */
190         Operation               *op;
191
192         PBLOCK_ASSERT_OP( pb, 0 );
193         op = pb->pb_op;
194
195         if ( !be_match( op->o_bd, frontendDB ) ) {
196                 rc = slapi_int_call_plugins( frontendDB, type, pb );
197         }
198         if ( rc >= 0 ) {
199                 rc = slapi_int_call_plugins( op->o_bd, type, pb );
200         }
201
202         return rc;
203 }
204
205 static int
206 slapi_over_search( Operation *op, SlapReply *rs, int type )
207 {
208         int                     rc;
209         Slapi_PBlock            *pb;
210
211         assert( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF );
212
213         /* create a new pblock to not trample on result controls */
214         pb = slapi_over_pblock_new( op, rs );
215
216         rc = slapi_over_call_plugins( pb, type );
217         if ( rc >= 0 ) /* 1 means no plugins called */
218                 rc = SLAP_CB_CONTINUE;
219         else
220                 rc = LDAP_SUCCESS; /* confusing: don't abort, but don't send */
221
222         slapi_pblock_destroy(pb);
223
224         return rc;
225 }
226
227 /*
228  * Call pre- and post-result plugins
229  */
230 static int
231 slapi_over_result( Operation *op, SlapReply *rs, int type )
232 {
233         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
234
235         assert( rs->sr_type == REP_RESULT || rs->sr_type == REP_SASL || rs->sr_type == REP_EXTENDED );
236
237         slapi_over_call_plugins( pb, type );
238
239         return SLAP_CB_CONTINUE;
240 }
241
242
243 static int
244 slapi_op_bind_callback( Operation *op, SlapReply *rs, int prc )
245 {
246         switch ( prc ) {
247         case SLAPI_BIND_SUCCESS:
248                 /* Continue with backend processing */
249                 break;
250         case SLAPI_BIND_FAIL:
251                 /* Failure, frontend (that's us) sends result */
252                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
253                 send_ldap_result( op, rs );
254                 return rs->sr_err;
255                 break;
256         case SLAPI_BIND_ANONYMOUS: /* undocumented */
257         default: /* plugin sent result or no plugins called */
258                 BER_BVZERO( &op->orb_edn );
259
260                 if ( rs->sr_err == LDAP_SUCCESS ) {
261                         /*
262                          * Plugin will have called slapi_pblock_set(LDAP_CONN_DN) which
263                          * will have set conn->c_dn and conn->c_ndn
264                          */
265                         if ( BER_BVISNULL( &op->o_conn->c_ndn ) && prc == 1 ) {
266                                 /* No plugins were called; continue processing */
267                                 return LDAP_SUCCESS;
268                         }
269                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
270                         if ( !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
271                                 ber_len_t max = sockbuf_max_incoming_auth;
272                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
273                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
274                         }
275                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
276
277                         /* log authorization identity */
278                         Statslog( LDAP_DEBUG_STATS,
279                                 "%s BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
280                                 op->o_log_prefix,
281                                 BER_BVISNULL( &op->o_conn->c_dn )
282                                         ? "<empty>" : op->o_conn->c_dn.bv_val,
283                                 BER_BVISNULL( &op->orb_mech )
284                                         ? "<empty>" : op->orb_mech.bv_val, 0, 0 );
285
286                         return -1;
287                 }
288                 break;
289         }
290
291         return rs->sr_err;
292 }
293
294 static int
295 slapi_op_search_callback( Operation *op, SlapReply *rs, int prc )
296 {
297         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
298
299         /* check preoperation result code */
300         if ( prc < 0 ) {
301                 return rs->sr_err;
302         }
303
304         rs->sr_err = LDAP_SUCCESS;
305
306         if ( pb->pb_intop == 0 && 
307              slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb ) == 0 ) {
308                 /*
309                  * The plugin can set the SLAPI_SEARCH_FILTER.
310                  * SLAPI_SEARCH_STRFILER is not normative.
311                  */
312                 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
313                 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
314         }
315
316         return LDAP_SUCCESS;
317 }
318
319 struct slapi_op_info {
320         int soi_preop;                  /* preoperation plugin parameter */
321         int soi_postop;                 /* postoperation plugin parameter */
322         int soi_internal_preop;         /* internal preoperation plugin parameter */
323         int soi_internal_postop;        /* internal postoperation plugin parameter */
324         int (*soi_callback)(Operation *, SlapReply *, int); /* preoperation result handler */
325 } slapi_op_dispatch_table[] = {
326         {
327                 SLAPI_PLUGIN_PRE_BIND_FN,
328                 SLAPI_PLUGIN_POST_BIND_FN,
329                 SLAPI_PLUGIN_INTERNAL_PRE_BIND_FN,
330                 SLAPI_PLUGIN_INTERNAL_POST_BIND_FN,
331                 slapi_op_bind_callback
332         },
333         {
334                 SLAPI_PLUGIN_PRE_UNBIND_FN,
335                 SLAPI_PLUGIN_POST_UNBIND_FN,
336                 SLAPI_PLUGIN_INTERNAL_PRE_UNBIND_FN,
337                 SLAPI_PLUGIN_INTERNAL_POST_UNBIND_FN,
338                 NULL
339         },
340         {
341                 SLAPI_PLUGIN_PRE_SEARCH_FN,
342                 SLAPI_PLUGIN_POST_SEARCH_FN,
343                 SLAPI_PLUGIN_INTERNAL_PRE_SEARCH_FN,
344                 SLAPI_PLUGIN_INTERNAL_POST_SEARCH_FN,
345                 slapi_op_search_callback
346         },
347         {
348                 SLAPI_PLUGIN_PRE_COMPARE_FN,
349                 SLAPI_PLUGIN_POST_COMPARE_FN,
350                 SLAPI_PLUGIN_INTERNAL_PRE_COMPARE_FN,
351                 SLAPI_PLUGIN_INTERNAL_POST_COMPARE_FN,
352                 NULL
353         },
354         {
355                 SLAPI_PLUGIN_PRE_MODIFY_FN,
356                 SLAPI_PLUGIN_POST_MODIFY_FN,
357                 SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN,
358                 SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
359                 NULL
360         },
361         {
362                 SLAPI_PLUGIN_PRE_MODRDN_FN,
363                 SLAPI_PLUGIN_POST_MODRDN_FN,
364                 SLAPI_PLUGIN_INTERNAL_PRE_MODRDN_FN,
365                 SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
366                 NULL
367         },
368         {
369                 SLAPI_PLUGIN_PRE_ADD_FN,
370                 SLAPI_PLUGIN_POST_ADD_FN,
371                 SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN,
372                 SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
373                 NULL
374         },
375         {
376                 SLAPI_PLUGIN_PRE_DELETE_FN,
377                 SLAPI_PLUGIN_POST_DELETE_FN,
378                 SLAPI_PLUGIN_INTERNAL_PRE_DELETE_FN,
379                 SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
380                 NULL
381         },
382         {
383                 SLAPI_PLUGIN_PRE_ABANDON_FN,
384                 SLAPI_PLUGIN_POST_ABANDON_FN,
385                 SLAPI_PLUGIN_INTERNAL_PRE_ABANDON_FN,
386                 SLAPI_PLUGIN_INTERNAL_POST_ABANDON_FN,
387                 NULL
388         },
389         {
390                 0,
391                 0,
392                 0,
393                 0,
394                 NULL
395         }
396 };
397
398 slap_operation_t
399 slapi_tag2op( ber_tag_t tag )
400 {
401         slap_operation_t op;
402
403         switch ( tag ) {
404         case LDAP_REQ_BIND:
405                 op = op_bind;
406                 break;
407         case LDAP_REQ_ADD:
408                 op = op_add;
409                 break;
410         case LDAP_REQ_DELETE:
411                 op = op_delete;
412                 break;
413         case LDAP_REQ_MODRDN:
414                 op = op_modrdn;
415                 break;
416         case LDAP_REQ_MODIFY:
417                 op = op_modify;
418                 break;
419         case LDAP_REQ_COMPARE:
420                 op = op_compare;
421                 break;
422         case LDAP_REQ_SEARCH:
423                 op = op_search;
424                 break;
425         case LDAP_REQ_UNBIND:
426                 op = op_unbind;
427                 break;
428         default:
429                 op = op_last;
430                 break;
431         }
432
433         return op;
434 }
435
436 /* Add SLAPI_RESCONTROLS to rs->sr_ctrls, with care, because
437  * rs->sr_ctrls could be allocated on the stack */
438 static int
439 slapi_over_merge_controls( Operation *op, SlapReply *rs )
440 {
441         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
442         LDAPControl             **ctrls = NULL;
443         LDAPControl             **slapi_ctrls = NULL;
444         size_t                  n_slapi_ctrls = 0;
445         size_t                  n_rs_ctrls = 0;
446         size_t                  i;
447
448         slapi_pblock_get( pb, SLAPI_RESCONTROLS, (void **)&slapi_ctrls );
449
450         n_slapi_ctrls = slapi_int_count_controls( slapi_ctrls );
451         n_rs_ctrls = slapi_int_count_controls( rs->sr_ctrls );
452
453         slapi_pblock_set( pb, SLAPI_X_OLD_RESCONTROLS, (void *)rs->sr_ctrls );
454
455         if ( n_slapi_ctrls == 0 )
456                 return LDAP_SUCCESS; /* no SLAPI controls */
457
458         ctrls = (LDAPControl **) op->o_tmpalloc(
459                 ( n_slapi_ctrls + n_rs_ctrls + 1 ) * sizeof(LDAPControl *),
460                 op->o_tmpmemctx );
461
462         for ( i = 0; i < n_slapi_ctrls; i++ ) {
463                 ctrls[i] = slapi_ctrls[i];
464         }
465         if ( rs->sr_ctrls != NULL ) {
466                 for ( i = 0; i < n_rs_ctrls; i++ ) {
467                         ctrls[n_slapi_ctrls + i] = rs->sr_ctrls[i];
468                 }
469         }
470         ctrls[n_slapi_ctrls + n_rs_ctrls] = NULL;
471
472         rs->sr_ctrls = ctrls;
473
474         return LDAP_SUCCESS;
475 }
476
477 static int
478 slapi_over_unmerge_controls( Operation *op, SlapReply *rs )
479 {
480         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
481         LDAPControl             **rs_ctrls = NULL;
482
483         slapi_pblock_get( pb, SLAPI_X_OLD_RESCONTROLS, (void **)&rs_ctrls );
484
485         if ( rs_ctrls == NULL || rs->sr_ctrls == rs_ctrls ) {
486                 /* no copying done */
487                 return LDAP_SUCCESS;
488         }
489
490         op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
491         rs->sr_ctrls = rs_ctrls;
492
493         return LDAP_SUCCESS;
494 }
495
496 static int
497 slapi_over_response( Operation *op, SlapReply *rs )
498 {
499         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
500         int                     rc = SLAP_CB_CONTINUE;
501
502         if ( pb->pb_intop == 0 ) {
503                 switch ( rs->sr_type ) {
504                 case REP_RESULT:
505                 case REP_SASL:
506                 case REP_EXTENDED:
507                         rc = slapi_over_result( op, rs, SLAPI_PLUGIN_PRE_RESULT_FN );
508                         break;
509                 case REP_SEARCH:
510                         rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_ENTRY_FN );
511                         break;
512                 case REP_SEARCHREF:
513                         rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_REFERRAL_FN );
514                         break;
515                 default:
516                         break;
517                 }
518         }
519
520         slapi_over_merge_controls( op, rs );
521
522         return rc;
523 }
524
525 static int
526 slapi_over_cleanup( Operation *op, SlapReply *rs )
527 {
528         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
529         int                     rc = SLAP_CB_CONTINUE;
530
531         slapi_over_unmerge_controls( op, rs );
532
533         if ( pb->pb_intop == 0 ) {
534                 switch ( rs->sr_type ) {
535                 case REP_RESULT:
536                 case REP_SASL:
537                 case REP_EXTENDED:
538                         rc = slapi_over_result( op, rs, SLAPI_PLUGIN_POST_RESULT_FN );
539                         break;
540                 case REP_SEARCH:
541                         rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_ENTRY_FN );
542                         break;
543                 case REP_SEARCHREF:
544                         rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_REFERRAL_FN );
545                         break;
546                 default:
547                         break;
548                 }
549         }
550
551         return rc;
552 }
553
554 static int
555 slapi_op_func( Operation *op, SlapReply *rs )
556 {
557         Slapi_PBlock            *pb;
558         slap_operation_t        which;
559         struct slapi_op_info    *opinfo;
560         int                     rc;
561         slap_overinfo           *oi;
562         slap_overinst           *on;
563         slap_callback           cb;
564         int                     internal_op;
565         int                     preop_type, postop_type;
566         BackendDB               *be;
567
568         if ( !slapi_plugins_used )
569                 return SLAP_CB_CONTINUE;
570
571         /*
572          * Find the SLAPI operation information for this LDAP
573          * operation; this will contain the preop and postop
574          * plugin types, as well as optional callbacks for
575          * setting up the SLAPI environment.
576          */
577         which = slapi_tag2op( op->o_tag );
578         if ( which >= op_last ) {
579                 /* invalid operation, but let someone else deal with it */
580                 return SLAP_CB_CONTINUE;
581         }
582
583         opinfo = &slapi_op_dispatch_table[which];
584         if ( opinfo == NULL ) {
585                 /* no SLAPI plugin types for this operation */
586                 return SLAP_CB_CONTINUE;
587         }
588
589         internal_op = slapi_op_internal_p( op, rs, &cb );
590
591         if ( internal_op ) {
592                 preop_type = opinfo->soi_internal_preop;
593                 postop_type = opinfo->soi_internal_postop;
594         } else {
595                 preop_type = opinfo->soi_preop;
596                 postop_type = opinfo->soi_postop;
597         }
598
599         if ( preop_type == 0 ) {
600                 /* no SLAPI plugin types for this operation */
601                 pb = NULL;
602                 rc = SLAP_CB_CONTINUE;
603                 goto cleanup;
604         }
605
606         pb = SLAPI_OPERATION_PBLOCK( op );
607
608         /* cache backend so we call correct postop plugins */
609         be = pb->pb_op->o_bd;
610
611         rc = slapi_int_call_plugins( be, preop_type, pb );
612
613         /*
614          * soi_callback is responsible for examining the result code
615          * of the preoperation plugin and determining whether to
616          * abort. This is needed because of special SLAPI behaviour
617          e with bind preoperation plugins.
618          *
619          * The soi_callback function is also used to reset any values
620          * returned from the preoperation plugin before calling the
621          * backend (for the success case).
622          */
623         if ( opinfo->soi_callback == NULL ) {
624                 /* default behaviour is preop plugin can abort operation */
625                 if ( rc < 0 ) {
626                         rc = rs->sr_err;
627                         goto cleanup;
628                 }
629         } else {
630                 rc = (opinfo->soi_callback)( op, rs, rc );
631                 if ( rc )
632                         goto cleanup;
633         }
634
635         /*
636          * Call actual backend (or next overlay in stack). We need to
637          * do this rather than returning SLAP_CB_CONTINUE and calling
638          * postoperation plugins in a response handler to match the
639          * behaviour of SLAPI in OpenLDAP 2.2, where postoperation
640          * plugins are called after the backend has completely
641          * finished processing the operation.
642          */
643         on = (slap_overinst *)op->o_bd->bd_info;
644         oi = on->on_info;
645
646         rc = overlay_op_walk( op, rs, which, oi, on->on_next );
647
648         /*
649          * Call postoperation plugins
650          */
651         slapi_int_call_plugins( be, postop_type, pb );
652
653 cleanup:
654         if ( !internal_op ) {
655                 slapi_pblock_destroy(pb);
656                 cb.sc_private = NULL;
657         }
658
659         op->o_callback = cb.sc_next;
660
661         return rc;
662 }
663
664 static int
665 slapi_over_extended( Operation *op, SlapReply *rs )
666 {
667         Slapi_PBlock    *pb;
668         SLAPI_FUNC      callback;
669         int             rc;
670         int             internal_op;
671         slap_callback   cb;
672
673         slapi_int_get_extop_plugin( &op->ore_reqoid, &callback );
674         if ( callback == NULL ) {
675                 return SLAP_CB_CONTINUE;
676         }
677
678         internal_op = slapi_op_internal_p( op, rs, &cb );
679         if ( internal_op ) {
680                 return SLAP_CB_CONTINUE;
681         }
682
683         pb = SLAPI_OPERATION_PBLOCK( op );
684
685         rc = (*callback)( pb );
686         if ( rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
687                 goto cleanup;
688         } else if ( rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
689                 rc = SLAP_CB_CONTINUE;
690                 goto cleanup;
691         }
692
693         assert( rs->sr_rspoid != NULL );
694
695         send_ldap_extended( op, rs );
696
697 #if 0
698         slapi_ch_free_string( (char **)&rs->sr_rspoid );
699 #endif
700
701         if ( rs->sr_rspdata != NULL )
702                 ber_bvfree( rs->sr_rspdata );
703
704         rc = rs->sr_err;
705
706 cleanup:
707         slapi_pblock_destroy( pb );
708         op->o_callback = cb.sc_next;
709
710         return rc;
711 }
712
713 static int
714 slapi_over_access_allowed(
715         Operation               *op,
716         Entry                   *e,
717         AttributeDescription    *desc,
718         struct berval           *val,
719         slap_access_t           access,
720         AccessControlState      *state,
721         slap_mask_t             *maskp )
722 {
723         int                     rc;
724         Slapi_PBlock            *pb;
725         slap_callback           cb;
726         int                     internal_op;
727         SlapReply               rs = { REP_RESULT };
728
729         internal_op = slapi_op_internal_p( op, &rs, &cb );
730
731         cb.sc_response = NULL;
732         cb.sc_cleanup = NULL;
733
734         pb = SLAPI_OPERATION_PBLOCK( op );
735
736         rc = slapi_int_access_allowed( op, e, desc, val, access, state );
737         if ( rc ) {
738                 rc = SLAP_CB_CONTINUE;
739         }
740
741         if ( !internal_op ) {
742                 slapi_pblock_destroy( pb );
743         }
744
745         op->o_callback = cb.sc_next;
746
747         return rc;
748 }
749
750 static int
751 slapi_over_acl_group(
752         Operation               *op,
753         Entry                   *target,
754         struct berval           *gr_ndn,
755         struct berval           *op_ndn,
756         ObjectClass             *group_oc,
757         AttributeDescription    *group_at )
758 {
759         Slapi_Entry             *e;
760         int                     rc;
761         Slapi_PBlock            *pb;
762         BackendDB               *be = op->o_bd;
763         GroupAssertion          *g;
764         SlapReply               rs = { REP_RESULT };
765
766         op->o_bd = select_backend( gr_ndn, 0 );
767
768         for ( g = op->o_groups; g; g = g->ga_next ) {
769                 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
770                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
771                 {
772                         continue;
773                 }
774                 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
775                         break;
776                 }
777         }
778         if ( g != NULL ) {
779                 rc = g->ga_res;
780                 goto done;
781         }
782
783         if ( target != NULL && dn_match( &target->e_nname, gr_ndn ) ) {
784                 e = target;
785                 rc = 0;
786         } else {
787                 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
788         }
789         if ( e != NULL ) {
790                 int                     internal_op;
791                 slap_callback           cb;
792
793                 internal_op = slapi_op_internal_p( op, &rs, &cb );
794
795                 cb.sc_response = NULL;
796                 cb.sc_cleanup = NULL;
797
798                 pb = SLAPI_OPERATION_PBLOCK( op );
799
800                 slapi_pblock_set( pb, SLAPI_X_GROUP_ENTRY,        (void *)e );
801                 slapi_pblock_set( pb, SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
802                 slapi_pblock_set( pb, SLAPI_X_GROUP_ATTRIBUTE,    (void *)group_at->ad_cname.bv_val );
803                 slapi_pblock_set( pb, SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
804
805                 rc = slapi_over_call_plugins( pb, SLAPI_X_PLUGIN_PRE_GROUP_FN );
806                 if ( rc >= 0 ) /* 1 means no plugins called */
807                         rc = SLAP_CB_CONTINUE;
808                 else
809                         rc = pb->pb_rs->sr_err;
810
811                 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_ENTRY );
812                 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_OPERATION_DN );
813                 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_ATTRIBUTE );
814                 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_TARGET_ENTRY );
815
816                 if ( !internal_op )
817                         slapi_pblock_destroy( pb );
818
819                 if ( e != target ) {
820                         be_entry_release_r( op, e );
821                 }
822
823                 op->o_callback = cb.sc_next;
824         } else {
825                 rc = LDAP_NO_SUCH_OBJECT; /* return SLAP_CB_CONTINUE for correctness? */
826         }
827
828         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache &&
829              rc != SLAP_CB_CONTINUE ) {
830                 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
831                         op->o_tmpmemctx );
832                 g->ga_be = op->o_bd;
833                 g->ga_oc = group_oc;
834                 g->ga_at = group_at;
835                 g->ga_res = rc;
836                 g->ga_len = gr_ndn->bv_len;
837                 strcpy( g->ga_ndn, gr_ndn->bv_val );
838                 g->ga_next = op->o_groups;
839                 op->o_groups = g;
840         }
841         /*
842          * XXX don't call POST_GROUP_FN, I have no idea what the point of
843          * that plugin function was anyway
844          */
845 done:
846         op->o_bd = be;
847         return rc;
848 }
849
850 static int
851 slapi_over_db_open( BackendDB *be )
852 {
853         Slapi_PBlock            *pb;
854         int                     rc;
855
856         pb = slapi_pblock_new();
857
858         rc = slapi_int_call_plugins( be, SLAPI_PLUGIN_START_FN, pb );
859
860         slapi_pblock_destroy( pb );
861
862         return rc;
863 }
864
865 static int
866 slapi_over_db_close( BackendDB *be )
867 {
868         Slapi_PBlock            *pb;
869         int                     rc;
870
871         pb = slapi_pblock_new();
872
873         rc = slapi_int_call_plugins( be, SLAPI_PLUGIN_CLOSE_FN, pb );
874
875         slapi_pblock_destroy( pb );
876
877         return rc;
878 }
879
880 static int
881 slapi_over_init()
882 {
883         memset( &slapi, 0, sizeof(slapi) );
884
885         slapi.on_bi.bi_type = SLAPI_OVERLAY_NAME;
886
887         slapi.on_bi.bi_op_bind          = slapi_op_func;
888         slapi.on_bi.bi_op_unbind        = slapi_op_func;
889         slapi.on_bi.bi_op_search        = slapi_op_func;
890         slapi.on_bi.bi_op_compare       = slapi_op_func;
891         slapi.on_bi.bi_op_modify        = slapi_op_func;
892         slapi.on_bi.bi_op_modrdn        = slapi_op_func;
893         slapi.on_bi.bi_op_add           = slapi_op_func;
894         slapi.on_bi.bi_op_delete        = slapi_op_func;
895         slapi.on_bi.bi_op_abandon       = slapi_op_func;
896         slapi.on_bi.bi_op_cancel        = slapi_op_func;
897
898         slapi.on_bi.bi_db_open          = slapi_over_db_open;
899         slapi.on_bi.bi_db_close         = slapi_over_db_close;
900
901         slapi.on_bi.bi_extended         = slapi_over_extended;
902         slapi.on_bi.bi_access_allowed   = slapi_over_access_allowed;
903         slapi.on_bi.bi_operational      = slapi_over_aux_operational;
904         slapi.on_bi.bi_acl_group        = slapi_over_acl_group;
905
906         return overlay_register( &slapi );
907 }
908
909 int slapi_over_is_inst( BackendDB *be )
910 {
911         return overlay_is_inst( be, SLAPI_OVERLAY_NAME );
912 }
913
914 int slapi_over_config( BackendDB *be )
915 {
916         if ( slapi_over_initialized == 0 ) {
917                 int rc;
918
919                 /* do global initializaiton */
920                 ldap_pvt_thread_mutex_init( &slapi_hn_mutex );
921                 ldap_pvt_thread_mutex_init( &slapi_time_mutex );
922                 ldap_pvt_thread_mutex_init( &slapi_printmessage_mutex );
923
924                 if ( slapi_log_file == NULL )
925                         slapi_log_file = slapi_ch_strdup( LDAP_RUNDIR LDAP_DIRSEP "errors" );
926
927                 rc = slapi_int_init_object_extensions();
928                 if ( rc != 0 )
929                         return rc;
930
931                 rc = slapi_over_init();
932                 if ( rc != 0 )
933                         return rc;
934
935                 slapi_over_initialized = 1;
936         }
937
938         return overlay_config( be, SLAPI_OVERLAY_NAME, -1, NULL );
939 }
940
941 #endif /* LDAP_SLAPI */