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