]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/slapi_overlay.c
Add internal bind/unbind/search/compare/abandon plugin types
[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-2006 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_tmp_mech )
284                                         ? "<empty>" : op->orb_tmp_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 ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb ) == 0 ) {
307                 /*
308                  * The plugin can set the SLAPI_SEARCH_FILTER.
309                  * SLAPI_SEARCH_STRFILER is not normative.
310                  */
311                 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
312                 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
313         }
314
315         return LDAP_SUCCESS;
316 }
317
318 struct slapi_op_info {
319         int soi_preop;                  /* preoperation plugin parameter */
320         int soi_postop;                 /* postoperation plugin parameter */
321         int soi_internal_preop;         /* internal preoperation plugin parameter */
322         int soi_internal_postop;        /* internal postoperation plugin parameter */
323         int (*soi_callback)(Operation *, SlapReply *, int); /* preoperation result handler */
324 } slapi_op_dispatch_table[] = {
325         {
326                 SLAPI_PLUGIN_PRE_BIND_FN,
327                 SLAPI_PLUGIN_POST_BIND_FN,
328                 SLAPI_PLUGIN_INTERNAL_PRE_BIND_FN,
329                 SLAPI_PLUGIN_INTERNAL_POST_BIND_FN,
330                 slapi_op_bind_callback
331         },
332         {
333                 SLAPI_PLUGIN_PRE_UNBIND_FN,
334                 SLAPI_PLUGIN_POST_UNBIND_FN,
335                 SLAPI_PLUGIN_INTERNAL_PRE_UNBIND_FN,
336                 SLAPI_PLUGIN_INTERNAL_POST_UNBIND_FN,
337                 NULL
338         },
339         {
340                 SLAPI_PLUGIN_PRE_SEARCH_FN,
341                 SLAPI_PLUGIN_POST_SEARCH_FN,
342                 SLAPI_PLUGIN_INTERNAL_PRE_SEARCH_FN,
343                 SLAPI_PLUGIN_INTERNAL_POST_SEARCH_FN,
344                 slapi_op_search_callback
345         },
346         {
347                 SLAPI_PLUGIN_PRE_COMPARE_FN,
348                 SLAPI_PLUGIN_POST_COMPARE_FN,
349                 SLAPI_PLUGIN_INTERNAL_PRE_COMPARE_FN,
350                 SLAPI_PLUGIN_INTERNAL_POST_COMPARE_FN,
351                 NULL
352         },
353         {
354                 SLAPI_PLUGIN_PRE_MODIFY_FN,
355                 SLAPI_PLUGIN_POST_MODIFY_FN,
356                 SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN,
357                 SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
358                 NULL
359         },
360         {
361                 SLAPI_PLUGIN_PRE_MODRDN_FN,
362                 SLAPI_PLUGIN_POST_MODRDN_FN,
363                 SLAPI_PLUGIN_INTERNAL_PRE_MODRDN_FN,
364                 SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
365                 NULL
366         },
367         {
368                 SLAPI_PLUGIN_PRE_ADD_FN,
369                 SLAPI_PLUGIN_POST_ADD_FN,
370                 SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN,
371                 SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
372                 NULL
373         },
374         {
375                 SLAPI_PLUGIN_PRE_DELETE_FN,
376                 SLAPI_PLUGIN_POST_DELETE_FN,
377                 SLAPI_PLUGIN_INTERNAL_PRE_DELETE_FN,
378                 SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
379                 NULL
380         },
381         {
382                 SLAPI_PLUGIN_PRE_ABANDON_FN,
383                 SLAPI_PLUGIN_POST_ABANDON_FN,
384                 SLAPI_PLUGIN_INTERNAL_PRE_ABANDON_FN,
385                 SLAPI_PLUGIN_INTERNAL_POST_ABANDON_FN,
386                 NULL
387         },
388         {
389                 0,
390                 0,
391                 0,
392                 0,
393                 NULL
394         }
395 };
396
397 slap_operation_t
398 slapi_tag2op( ber_tag_t tag )
399 {
400         slap_operation_t op;
401
402         switch ( tag ) {
403         case LDAP_REQ_BIND:
404                 op = op_bind;
405                 break;
406         case LDAP_REQ_ADD:
407                 op = op_add;
408                 break;
409         case LDAP_REQ_DELETE:
410                 op = op_delete;
411                 break;
412         case LDAP_REQ_MODRDN:
413                 op = op_modrdn;
414                 break;
415         case LDAP_REQ_MODIFY:
416                 op = op_modify;
417                 break;
418         case LDAP_REQ_COMPARE:
419                 op = op_compare;
420                 break;
421         case LDAP_REQ_SEARCH:
422                 op = op_search;
423                 break;
424         case LDAP_REQ_UNBIND:
425                 op = op_unbind;
426                 break;
427         default:
428                 op = op_last;
429                 break;
430         }
431
432         return op;
433 }
434
435 /* Add SLAPI_RESCONTROLS to rs->sr_ctrls, with care, because
436  * rs->sr_ctrls could be allocated on the stack */
437 static int
438 slapi_over_merge_controls( Operation *op, SlapReply *rs )
439 {
440         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
441         LDAPControl             **ctrls = NULL;
442         LDAPControl             **slapi_ctrls = NULL;
443         size_t                  n_slapi_ctrls = 0;
444         size_t                  n_rs_ctrls = 0;
445         size_t                  i;
446
447         slapi_pblock_get( pb, SLAPI_RESCONTROLS, (void **)&slapi_ctrls );
448
449         n_slapi_ctrls = slapi_int_count_controls( slapi_ctrls );
450         n_rs_ctrls = slapi_int_count_controls( rs->sr_ctrls );
451
452         slapi_pblock_set( pb, SLAPI_X_OLD_RESCONTROLS, (void *)rs->sr_ctrls );
453
454         if ( n_slapi_ctrls == 0 )
455                 return LDAP_SUCCESS; /* no SLAPI controls */
456
457         ctrls = (LDAPControl **) op->o_tmpalloc(
458                 ( n_slapi_ctrls + n_rs_ctrls + 1 ) * sizeof(LDAPControl *),
459                 op->o_tmpmemctx );
460
461         for ( i = 0; i < n_slapi_ctrls; i++ ) {
462                 ctrls[i] = slapi_ctrls[i];
463         }
464         if ( rs->sr_ctrls != NULL ) {
465                 for ( i = 0; i < n_rs_ctrls; i++ ) {
466                         ctrls[n_slapi_ctrls + i] = rs->sr_ctrls[i];
467                 }
468         }
469         ctrls[n_slapi_ctrls + n_rs_ctrls] = NULL;
470
471         rs->sr_ctrls = ctrls;
472
473         return LDAP_SUCCESS;
474 }
475
476 static int
477 slapi_over_unmerge_controls( Operation *op, SlapReply *rs )
478 {
479         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
480         LDAPControl             **rs_ctrls = NULL;
481
482         slapi_pblock_get( pb, SLAPI_X_OLD_RESCONTROLS, (void **)&rs_ctrls );
483
484         if ( rs_ctrls == NULL || rs->sr_ctrls == rs_ctrls ) {
485                 /* no copying done */
486                 return LDAP_SUCCESS;
487         }
488
489         op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
490         rs->sr_ctrls = rs_ctrls;
491
492         return LDAP_SUCCESS;
493 }
494
495 static int
496 slapi_over_response( Operation *op, SlapReply *rs )
497 {
498         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
499         int                     rc = SLAP_CB_CONTINUE;
500
501         if ( pb->pb_intop == 0 ) {
502                 switch ( rs->sr_type ) {
503                 case REP_RESULT:
504                 case REP_SASL:
505                 case REP_EXTENDED:
506                         rc = slapi_over_result( op, rs, SLAPI_PLUGIN_PRE_RESULT_FN );
507                         break;
508                 case REP_SEARCH:
509                         rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_ENTRY_FN );
510                         break;
511                 case REP_SEARCHREF:
512                         rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_REFERRAL_FN );
513                         break;
514                 default:
515                         break;
516                 }
517         }
518
519         slapi_over_merge_controls( op, rs );
520
521         return rc;
522 }
523
524 static int
525 slapi_over_cleanup( Operation *op, SlapReply *rs )
526 {
527         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
528         int                     rc = SLAP_CB_CONTINUE;
529
530         slapi_over_unmerge_controls( op, rs );
531
532         if ( pb->pb_intop == 0 ) {
533                 switch ( rs->sr_type ) {
534                 case REP_RESULT:
535                 case REP_SASL:
536                 case REP_EXTENDED:
537                         rc = slapi_over_result( op, rs, SLAPI_PLUGIN_POST_RESULT_FN );
538                         break;
539                 case REP_SEARCH:
540                         rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_ENTRY_FN );
541                         break;
542                 case REP_SEARCHREF:
543                         rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_REFERRAL_FN );
544                         break;
545                 default:
546                         break;
547                 }
548         }
549
550         return rc;
551 }
552
553 static int
554 slapi_op_func( Operation *op, SlapReply *rs )
555 {
556         Slapi_PBlock            *pb;
557         slap_operation_t        which;
558         struct slapi_op_info    *opinfo;
559         int                     rc;
560         slap_overinfo           *oi;
561         slap_overinst           *on;
562         slap_callback           cb;
563         int                     internal_op;
564         int                     preop_type, postop_type;
565         BackendDB               *be;
566
567         if ( !slapi_plugins_used )
568                 return SLAP_CB_CONTINUE;
569
570         /*
571          * Find the SLAPI operation information for this LDAP
572          * operation; this will contain the preop and postop
573          * plugin types, as well as optional callbacks for
574          * setting up the SLAPI environment.
575          */
576         which = slapi_tag2op( op->o_tag );
577         if ( which >= op_last ) {
578                 /* invalid operation, but let someone else deal with it */
579                 return SLAP_CB_CONTINUE;
580         }
581
582         opinfo = &slapi_op_dispatch_table[which];
583         if ( opinfo == NULL ) {
584                 /* no SLAPI plugin types for this operation */
585                 return SLAP_CB_CONTINUE;
586         }
587
588         internal_op = slapi_op_internal_p( op, rs, &cb );
589
590         if ( internal_op ) {
591                 preop_type = opinfo->soi_internal_preop;
592                 postop_type = opinfo->soi_internal_postop;
593         } else {
594                 preop_type = opinfo->soi_preop;
595                 postop_type = opinfo->soi_postop;
596         }
597
598         if ( preop_type == 0 ) {
599                 /* no SLAPI plugin types for this operation */
600                 pb = NULL;
601                 rc = SLAP_CB_CONTINUE;
602                 goto cleanup;
603         }
604
605         pb = SLAPI_OPERATION_PBLOCK( op );
606
607         /* cache backend so we call correct postop plugins */
608         be = pb->pb_op->o_bd;
609
610         rc = slapi_int_call_plugins( be, preop_type, pb );
611
612         /*
613          * soi_callback is responsible for examining the result code
614          * of the preoperation plugin and determining whether to
615          * abort. This is needed because of special SLAPI behaviour
616          e with bind preoperation plugins.
617          *
618          * The soi_callback function is also used to reset any values
619          * returned from the preoperation plugin before calling the
620          * backend (for the success case).
621          */
622         if ( opinfo->soi_callback == NULL ) {
623                 /* default behaviour is preop plugin can abort operation */
624                 if ( rc < 0 ) {
625                         rc = rs->sr_err;
626                         goto cleanup;
627                 }
628         } else {
629                 rc = (opinfo->soi_callback)( op, rs, rc );
630                 if ( rc )
631                         goto cleanup;
632         }
633
634         /*
635          * Call actual backend (or next overlay in stack). We need to
636          * do this rather than returning SLAP_CB_CONTINUE and calling
637          * postoperation plugins in a response handler to match the
638          * behaviour of SLAPI in OpenLDAP 2.2, where postoperation
639          * plugins are called after the backend has completely
640          * finished processing the operation.
641          */
642         on = (slap_overinst *)op->o_bd->bd_info;
643         oi = on->on_info;
644
645         rc = overlay_op_walk( op, rs, which, oi, on->on_next );
646
647         /*
648          * Call postoperation plugins
649          */
650         slapi_int_call_plugins( be, postop_type, pb );
651
652 cleanup:
653         if ( !internal_op ) {
654                 slapi_pblock_destroy(pb);
655                 cb.sc_private = NULL;
656         }
657
658         op->o_callback = cb.sc_next;
659
660         return rc;
661 }
662
663 static int
664 slapi_over_extended( Operation *op, SlapReply *rs )
665 {
666         Slapi_PBlock    *pb;
667         SLAPI_FUNC      callback;
668         int             rc;
669         int             internal_op;
670         slap_callback   cb;
671
672         slapi_int_get_extop_plugin( &op->ore_reqoid, &callback );
673         if ( callback == NULL ) {
674                 return SLAP_CB_CONTINUE;
675         }
676
677         internal_op = slapi_op_internal_p( op, rs, &cb );
678         if ( internal_op ) {
679                 return SLAP_CB_CONTINUE;
680         }
681
682         pb = SLAPI_OPERATION_PBLOCK( op );
683
684         rc = (*callback)( pb );
685         if ( rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
686                 goto cleanup;
687         } else if ( rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
688                 rc = SLAP_CB_CONTINUE;
689                 goto cleanup;
690         }
691
692         assert( rs->sr_rspoid != NULL );
693
694         send_ldap_extended( op, rs );
695
696 #if 0
697         slapi_ch_free_string( (char **)&rs->sr_rspoid );
698 #endif
699
700         if ( rs->sr_rspdata != NULL )
701                 ber_bvfree( rs->sr_rspdata );
702
703         rc = rs->sr_err;
704
705 cleanup:
706         slapi_pblock_destroy( pb );
707         op->o_callback = cb.sc_next;
708
709         return rc;
710 }
711
712 static int
713 slapi_over_access_allowed(
714         Operation               *op,
715         Entry                   *e,
716         AttributeDescription    *desc,
717         struct berval           *val,
718         slap_access_t           access,
719         AccessControlState      *state,
720         slap_mask_t             *maskp )
721 {
722         int                     rc;
723         Slapi_PBlock            *pb;
724         slap_callback           cb;
725         int                     internal_op;
726         SlapReply               rs = { REP_RESULT };
727
728         internal_op = slapi_op_internal_p( op, &rs, &cb );
729
730         cb.sc_response = NULL;
731         cb.sc_cleanup = NULL;
732
733         pb = SLAPI_OPERATION_PBLOCK( op );
734
735         rc = slapi_int_access_allowed( op, e, desc, val, access, state );
736         if ( rc ) {
737                 rc = SLAP_CB_CONTINUE;
738         }
739
740         if ( !internal_op ) {
741                 slapi_pblock_destroy( pb );
742         }
743
744         op->o_callback = cb.sc_next;
745
746         return rc;
747 }
748
749 static int
750 slapi_over_acl_group(
751         Operation               *op,
752         Entry                   *target,
753         struct berval           *gr_ndn,
754         struct berval           *op_ndn,
755         ObjectClass             *group_oc,
756         AttributeDescription    *group_at )
757 {
758         Slapi_Entry             *e;
759         int                     rc;
760         Slapi_PBlock            *pb;
761         BackendDB               *be = op->o_bd;
762         GroupAssertion          *g;
763         SlapReply               rs = { REP_RESULT };
764
765         op->o_bd = select_backend( gr_ndn, 0, 0 );
766
767         for ( g = op->o_groups; g; g = g->ga_next ) {
768                 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
769                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
770                 {
771                         continue;
772                 }
773                 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
774                         break;
775                 }
776         }
777         if ( g != NULL ) {
778                 rc = g->ga_res;
779                 goto done;
780         }
781
782         if ( target != NULL && dn_match( &target->e_nname, gr_ndn ) ) {
783                 e = target;
784                 rc = 0;
785         } else {
786                 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
787         }
788         if ( e != NULL ) {
789                 int                     internal_op;
790                 slap_callback           cb;
791
792                 internal_op = slapi_op_internal_p( op, &rs, &cb );
793
794                 cb.sc_response = NULL;
795                 cb.sc_cleanup = NULL;
796
797                 pb = SLAPI_OPERATION_PBLOCK( op );
798
799                 slapi_pblock_set( pb, SLAPI_X_GROUP_ENTRY,        (void *)e );
800                 slapi_pblock_set( pb, SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
801                 slapi_pblock_set( pb, SLAPI_X_GROUP_ATTRIBUTE,    (void *)group_at->ad_cname.bv_val );
802                 slapi_pblock_set( pb, SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
803
804                 rc = slapi_over_call_plugins( pb, SLAPI_X_PLUGIN_PRE_GROUP_FN );
805                 if ( rc >= 0 ) /* 1 means no plugins called */
806                         rc = SLAP_CB_CONTINUE;
807                 else
808                         rc = pb->pb_rs->sr_err;
809
810                 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_ENTRY );
811                 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_OPERATION_DN );
812                 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_ATTRIBUTE );
813                 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_TARGET_ENTRY );
814
815                 if ( !internal_op )
816                         slapi_pblock_destroy( pb );
817
818                 if ( e != target ) {
819                         be_entry_release_r( op, e );
820                 }
821
822                 op->o_callback = cb.sc_next;
823         } else {
824                 rc = LDAP_NO_SUCH_OBJECT; /* return SLAP_CB_CONTINUE for correctness? */
825         }
826
827         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache &&
828              rc != SLAP_CB_CONTINUE ) {
829                 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
830                         op->o_tmpmemctx );
831                 g->ga_be = op->o_bd;
832                 g->ga_oc = group_oc;
833                 g->ga_at = group_at;
834                 g->ga_res = rc;
835                 g->ga_len = gr_ndn->bv_len;
836                 strcpy( g->ga_ndn, gr_ndn->bv_val );
837                 g->ga_next = op->o_groups;
838                 op->o_groups = g;
839         }
840         /*
841          * XXX don't call POST_GROUP_FN, I have no idea what the point of
842          * that plugin function was anyway
843          */
844 done:
845         op->o_bd = be;
846         return rc;
847 }
848
849 static int
850 slapi_over_db_open( BackendDB *be )
851 {
852         Slapi_PBlock            *pb;
853         int                     rc;
854
855         pb = slapi_pblock_new();
856
857         rc = slapi_int_call_plugins( be, SLAPI_PLUGIN_START_FN, pb );
858
859         slapi_pblock_destroy( pb );
860
861         return rc;
862 }
863
864 static int
865 slapi_over_db_close( BackendDB *be )
866 {
867         Slapi_PBlock            *pb;
868         int                     rc;
869
870         pb = slapi_pblock_new();
871
872         rc = slapi_int_call_plugins( be, SLAPI_PLUGIN_CLOSE_FN, pb );
873
874         slapi_pblock_destroy( pb );
875
876         return rc;
877 }
878
879 static int
880 slapi_over_init()
881 {
882         memset( &slapi, 0, sizeof(slapi) );
883
884         slapi.on_bi.bi_type = SLAPI_OVERLAY_NAME;
885
886         slapi.on_bi.bi_op_bind          = slapi_op_func;
887         slapi.on_bi.bi_op_unbind        = slapi_op_func;
888         slapi.on_bi.bi_op_search        = slapi_op_func;
889         slapi.on_bi.bi_op_compare       = slapi_op_func;
890         slapi.on_bi.bi_op_modify        = slapi_op_func;
891         slapi.on_bi.bi_op_modrdn        = slapi_op_func;
892         slapi.on_bi.bi_op_add           = slapi_op_func;
893         slapi.on_bi.bi_op_delete        = slapi_op_func;
894         slapi.on_bi.bi_op_abandon       = slapi_op_func;
895         slapi.on_bi.bi_op_cancel        = slapi_op_func;
896
897         slapi.on_bi.bi_db_open          = slapi_over_db_open;
898         slapi.on_bi.bi_db_close         = slapi_over_db_close;
899
900         slapi.on_bi.bi_extended         = slapi_over_extended;
901         slapi.on_bi.bi_access_allowed   = slapi_over_access_allowed;
902         slapi.on_bi.bi_operational      = slapi_over_aux_operational;
903         slapi.on_bi.bi_acl_group        = slapi_over_acl_group;
904
905         return overlay_register( &slapi );
906 }
907
908 int slapi_over_is_inst( BackendDB *be )
909 {
910         return overlay_is_inst( be, SLAPI_OVERLAY_NAME );
911 }
912
913 int slapi_over_config( BackendDB *be )
914 {
915         if ( slapi_over_initialized == 0 ) {
916                 int rc;
917
918                 /* do global initializaiton */
919                 ldap_pvt_thread_mutex_init( &slapi_hn_mutex );
920                 ldap_pvt_thread_mutex_init( &slapi_time_mutex );
921                 ldap_pvt_thread_mutex_init( &slapi_printmessage_mutex );
922
923                 if ( slapi_log_file == NULL )
924                         slapi_log_file = slapi_ch_strdup( LDAP_RUNDIR LDAP_DIRSEP "errors" );
925
926                 rc = slapi_int_init_object_extensions();
927                 if ( rc != 0 )
928                         return rc;
929
930                 rc = slapi_over_init();
931                 if ( rc != 0 )
932                         return rc;
933
934                 slapi_over_initialized = 1;
935         }
936
937         return overlay_config( be, SLAPI_OVERLAY_NAME );
938 }
939
940 #endif /* LDAP_SLAPI */