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