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