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