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