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