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