]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/slapi_overlay.c
update for new backend types
[openldap] / servers / slapd / slapi / slapi_overlay.c
1 /* slapi_overlay.c - SLAPI overlay */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Luke Howard for inclusion
18  * in OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24
25 #include <ac/string.h>
26 #include <ac/socket.h>
27
28 #include "slap.h"
29 #include "slapi.h"
30
31 #ifdef LDAP_SLAPI
32
33 static slap_overinst slapi;
34
35 static int slapi_over_response( Operation *op, SlapReply *rs );
36 static int slapi_over_cleanup( Operation *op, SlapReply *rs );
37
38 static Slapi_PBlock *
39 slapi_over_pblock_new ( Operation *op )
40 {
41         Slapi_PBlock            *pb;
42
43         pb = slapi_pblock_new();
44         if ( pb == NULL ) {
45                 return NULL;
46         }
47
48         slapi_int_pblock_set_operation( pb, op );
49
50         return pb;
51 }
52
53 static int
54 slapi_op_internal_p( Operation *op, slap_callback *cb )
55 {
56         int                     internal_op = 0;
57         Slapi_PBlock            *pb = NULL;
58         slap_callback           *pcb;
59
60         /* Abstraction violating check for SLAPI internal operations */
61         /* allows pblock to remain consistent when invoking internal op plugins */
62         for ( pcb = op->o_callback; pcb != NULL; pcb = pcb->sc_next ) {
63                 if ( pcb->sc_response == slapi_int_response ) {
64                         pb = (Slapi_PBlock *)pcb->sc_private;
65                         internal_op = 1;
66                         break;
67                 }
68         }
69
70         if ( cb != NULL ) {
71                 if ( !internal_op ) {
72                         cb->sc_response = slapi_over_response;
73                         cb->sc_cleanup = slapi_over_cleanup;
74                 } else {
75                         cb->sc_response = NULL;
76                         cb->sc_cleanup = NULL;
77                 }
78
79                 if ( pb == NULL ) {
80                         pb = slapi_over_pblock_new( op );
81                 }
82
83                 cb->sc_private = pb;
84                 cb->sc_next = op->o_callback;
85                 op->o_callback = cb;
86         }
87
88         return internal_op;
89 }
90
91 static int
92 slapi_over_compute_output(
93         computed_attr_context *c,
94         Slapi_Attr *attribute,
95         Slapi_Entry *entry
96 )
97 {
98         Attribute               **a;
99         AttributeDescription    *desc;
100         SlapReply               *rs = (SlapReply *)c->cac_private;
101
102         if ( c == NULL || attribute == NULL || entry == NULL ) {
103                 return 0;
104         }
105
106         assert( rs->sr_entry == entry );
107
108         desc = attribute->a_desc;
109
110         if ( rs->sr_attrs == NULL ) {
111                 /* All attrs request, skip operational attributes */
112                 if ( is_at_operational( desc->ad_type ) ) {
113                         return 0;
114                 }
115         } else {
116                 /* Specific attributes requested */
117                 if ( is_at_operational( desc->ad_type ) ) {
118                         if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
119                              !ad_inlist( desc, rs->sr_attrs ) ) {
120                                 return 0;
121                         }
122                 } else {
123                         if ( !SLAP_USERATTRS( rs->sr_attr_flags ) &&
124                              !ad_inlist( desc, rs->sr_attrs ) ) {
125                                 return 0;
126                         }
127                 }
128         }
129
130         /* XXX perhaps we should check for existing attributes and merge */
131         for ( a = &rs->sr_operational_attrs; *a != NULL; a = &(*a)->a_next )
132                 ;
133
134         *a = attr_dup( attribute );
135
136         return 0;
137 }
138
139 static int
140 slapi_over_aux_operational( Operation *op, SlapReply *rs )
141 {
142         /* Support for computed attribute plugins */
143         computed_attr_context    ctx;
144         AttributeName           *anp;
145
146         if ( slapi_op_internal_p( op, NULL ) ) {
147                 return SLAP_CB_CONTINUE;
148         }
149
150         ctx.cac_pb = slapi_over_pblock_new( op );
151         ctx.cac_op = op;
152         ctx.cac_private = rs;
153
154         if ( rs->sr_entry != NULL ) {
155                 /*
156                  * For each client requested attribute, call the plugins.
157                  */
158                 if ( rs->sr_attrs != NULL ) {
159                         for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
160                                 if ( compute_evaluator( &ctx, anp->an_name.bv_val,
161                                         rs->sr_entry, slapi_over_compute_output ) == 1 ) {
162                                         break;
163                                 }
164                         }
165                 } else {
166                         /*
167                          * Technically we shouldn't be returning operational attributes
168                          * when the user requested only user attributes. We'll let the
169                          * plugin decide whether to be naughty or not.
170                          */
171                         compute_evaluator( &ctx, "*", rs->sr_entry, slapi_over_compute_output );
172                 }
173         }
174
175         slapi_pblock_destroy( ctx.cac_pb );
176
177         return SLAP_CB_CONTINUE;
178 }
179
180 static int
181 slapi_over_search( Operation *op, SlapReply *rs, int type )
182 {
183         int                     rc;
184         Slapi_PBlock            *pb;
185
186         assert( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF );
187
188         /* create a new pblock to not trample on result controls */
189         pb = slapi_over_pblock_new( op );
190
191         slapi_pblock_set( pb, SLAPI_RESCONTROLS,         (void *)rs->sr_ctrls );
192         slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY, (void *)rs->sr_entry );
193
194         rc = slapi_int_call_plugins( op->o_bd, type, pb );
195         if ( rc >= 0 ) /* 1 means no plugins called */
196                 rc = SLAP_CB_CONTINUE;
197         else
198                 rc = LDAP_SUCCESS; /* confusing: don't abort, but don't send */
199
200         slapi_pblock_set( pb, SLAPI_RESCONTROLS, NULL ); /* don't free */
201         slapi_pblock_destroy(pb);
202
203         return rc;
204 }
205
206 static int
207 slapi_over_count_controls( LDAPControl **controls )
208 {
209         int                     i;
210
211         if ( controls == NULL )
212                 return 0;
213
214         for ( i = 0; controls[i] != NULL; i++ )
215                 ;
216
217         return i;
218 }
219
220 static int
221 slapi_over_merge_controls( Operation *op, SlapReply *rs )
222 {
223         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
224         LDAPControl             **slapiControls = NULL, **mergedControls;
225         int                     nSlapiControls = 0;
226         int                     nResControls = 0;
227         int                     i;
228
229         slapi_pblock_get( pb, SLAPI_RESCONTROLS, (void **)&slapiControls );
230         nSlapiControls = slapi_over_count_controls( slapiControls );
231
232         if ( nSlapiControls == 0 ) {
233                 /* short-circuit */
234                 return LDAP_SUCCESS;
235         }
236
237         nResControls = slapi_over_count_controls( rs->sr_ctrls );
238
239         /* XXX this is a bit tricky, rs->sr_ctrls may have been allocated on stack */
240         mergedControls = (LDAPControl **)op->o_tmpalloc( ( nResControls + nSlapiControls + 1 ) *
241                                                          sizeof( LDAPControl *), op->o_tmpmemctx );
242         if ( mergedControls == NULL ) {
243                 return LDAP_NO_MEMORY;
244         }
245
246         if ( rs->sr_ctrls != NULL ) {
247                 for ( i = 0; i < nResControls; i++ )
248                         mergedControls[i] = rs->sr_ctrls[i];
249         }
250         if ( slapiControls != NULL ) {
251                 for ( i = 0; i < nSlapiControls; i++ )
252                         mergedControls[nResControls + i] = slapiControls[i];
253         }
254         mergedControls[nResControls + nSlapiControls] = NULL;
255
256         if ( slapiControls != NULL ) {
257                 slapi_ch_free( (void **)&slapiControls );
258                 slapi_pblock_set( pb, SLAPI_RESCONTROLS, NULL ); /* don't free */
259         }
260
261         rs->sr_ctrls = mergedControls;
262
263         return LDAP_SUCCESS;
264 }
265
266 /*
267  * Call pre- and post-result plugins
268  */
269 static int
270 slapi_over_result( Operation *op, SlapReply *rs, int type )
271 {
272         int                     rc;
273         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
274
275         assert( rs->sr_type == REP_RESULT );
276
277         slapi_pblock_set( pb, SLAPI_RESULT_CODE,    (void *)rs->sr_err );
278         slapi_pblock_set( pb, SLAPI_RESULT_TEXT,    (void *)rs->sr_text );
279         slapi_pblock_set( pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
280
281         rc = slapi_int_call_plugins( op->o_bd, type, pb );
282         
283         slapi_pblock_get( pb, SLAPI_RESULT_CODE,    (void **)&rs->sr_err );
284         slapi_pblock_get( pb, SLAPI_RESULT_TEXT,    (void **)&rs->sr_text );
285         slapi_pblock_get( pb, SLAPI_RESULT_MATCHED, (void **)&rs->sr_matched );
286
287         if ( type == SLAPI_PLUGIN_PRE_RESULT_FN ) {
288                 rc = slapi_over_merge_controls( op, rs );
289         }
290
291         return SLAP_CB_CONTINUE;
292 }
293
294 static int
295 slapi_op_add_init( Operation *op, SlapReply *rs )
296 {
297         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
298
299         slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)op->ora_e );
300
301         return LDAP_SUCCESS;
302 }
303
304 static int
305 slapi_op_bind_preop_init( Operation *op, SlapReply *rs )
306 {
307         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
308
309         slapi_pblock_set( pb, SLAPI_BIND_TARGET,      (void *)op->o_req_dn.bv_val );
310         slapi_pblock_set( pb, SLAPI_BIND_METHOD,      (void *)op->orb_method );
311         slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, (void *)&op->orb_cred );
312         slapi_pblock_set( pb, SLAPI_CONN_DN,          NULL );
313
314         return LDAP_SUCCESS;
315 }
316
317 static int
318 slapi_op_bind_postop_init( Operation *op, SlapReply *rs )
319 {
320         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
321
322         if ( rs->sr_err == LDAP_SUCCESS ) {
323                 /* fix for ITS#2971 */
324                 slapi_pblock_set( pb, SLAPI_CONN_DN, op->o_conn->c_authz.sai_dn.bv_val );
325         }
326
327         return LDAP_SUCCESS;
328 }
329
330 static int
331 slapi_op_bind_callback( Operation *op, SlapReply *rs )
332 {
333         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
334         int                     rc = rs->sr_err;
335
336         switch ( rc ) {
337         case SLAPI_BIND_SUCCESS:
338                 /* Continue with backend processing */
339                 break;
340         case SLAPI_BIND_FAIL:
341                 /* Failure, frontend (that's us) sends result */
342                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
343                 send_ldap_result( op, rs );
344                 return rs->sr_err;
345                 break;
346         case SLAPI_BIND_ANONYMOUS: /* undocumented */
347         default: /* plugin sent result or no plugins called */
348                 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void **)&rs->sr_err ) != 0 ) {
349                         rs->sr_err = LDAP_OTHER;
350                 }
351
352                 BER_BVZERO( &op->orb_edn );
353
354                 if ( rs->sr_err == LDAP_SUCCESS ) {
355                         slapi_pblock_get( pb, SLAPI_CONN_DN, (void *)&op->orb_edn.bv_val );
356                         if ( BER_BVISNULL( &op->orb_edn ) ) {
357                                 if ( rc == 1 ) {
358                                         /* No plugins were called; continue processing */
359                                         return LDAP_SUCCESS;
360                                 }
361                         } else {
362                                 op->orb_edn.bv_len = strlen( op->orb_edn.bv_val );
363                         }
364                         rs->sr_err = dnPrettyNormal( NULL, &op->orb_edn,
365                                 &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
366
367                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
368                         ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
369                         ber_dupbv(&op->o_conn->c_ndn, &op->o_req_ndn);
370                         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
371                         BER_BVZERO( &op->o_req_dn );
372                         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
373                         BER_BVZERO( &op->o_req_ndn );
374                         if ( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
375                                 ber_len_t max = sockbuf_max_incoming_auth;
376                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
377                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
378                         }
379
380                         /* log authorization identity */
381                         Statslog( LDAP_DEBUG_STATS,
382                                 "%s BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
383                                 op->o_log_prefix,
384                                 BER_BVISNULL( &op->o_conn->c_dn )
385                                         ? "<empty>" : op->o_conn->c_dn.bv_val,
386                                 op->orb_tmp_mech.bv_val, 0, 0 );
387
388                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
389                         return -1;
390                 }
391                 break;
392         }
393
394         return rc;
395 }
396
397 static int
398 slapi_op_compare_init( Operation *op, SlapReply *rs )
399 {
400         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
401
402         slapi_pblock_set( pb, SLAPI_COMPARE_TYPE,  (void *)op->orc_ava->aa_desc->ad_cname.bv_val );
403         slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&op->orc_ava->aa_value );
404
405         return LDAP_SUCCESS;
406 }
407
408 static int
409 slapi_op_modify_init( Operation *op, SlapReply *rs )
410 {
411         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
412         LDAPMod                 **modv = NULL;
413
414         modv = slapi_int_modifications2ldapmods( &op->orm_modlist );
415         slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)modv );
416
417         return LDAP_SUCCESS;
418 }
419
420 static int
421 slapi_op_modify_callback( Operation *op, SlapReply *rs )
422 {
423         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
424         LDAPMod                 **modv = NULL;
425
426         /* check preoperation result code */
427         if ( rs->sr_err < 0 ) {
428                 slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void **)&rs->sr_err );
429                 return rs->sr_err;
430         }
431
432         /*
433          * NB: it is valid for the plugin to return no modifications
434          * (for example, a plugin might store some attributes elsewhere
435          * and remove them from the modification list; if only those
436          * attribute types were included in the modification request,
437          * then slapi_int_ldapmods2modifications() above will return
438          * NULL).
439          *
440          * However, the post-operation plugin should still be
441          * called.
442          */
443
444         slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
445         op->orm_modlist = slapi_int_ldapmods2modifications( modv );
446
447         return LDAP_SUCCESS;
448 }
449
450 static int
451 slapi_op_modify_cleanup( Operation *op, SlapReply *rs )
452 {
453         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
454         LDAPMod                 **modv = NULL;
455
456         slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
457
458         if ( modv != NULL )
459                 slapi_int_free_ldapmods( modv );
460
461         return LDAP_SUCCESS;
462 }
463
464 static int
465 slapi_op_modrdn_init( Operation *op, SlapReply *rs )
466 {
467         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
468
469         slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN,      (void *)op->orr_newrdn.bv_val );
470         slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR, (void *)op->orr_newSup->bv_val );
471         slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN,   (void *)op->orr_deleteoldrdn );
472
473         return LDAP_SUCCESS;
474 }
475
476 static int
477 slapi_op_search_init( Operation *op, SlapReply *rs )
478 {
479         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
480         char                    **attrs;
481
482         attrs = anlist2charray_x( op->ors_attrs, 0, op->o_tmpmemctx );
483
484         slapi_pblock_set( pb, SLAPI_SEARCH_SCOPE,     (void *)op->ors_scope );
485         slapi_pblock_set( pb, SLAPI_SEARCH_DEREF,     (void *)op->ors_deref );
486         slapi_pblock_set( pb, SLAPI_SEARCH_SIZELIMIT, (void *)op->ors_slimit );
487         slapi_pblock_set( pb, SLAPI_SEARCH_TIMELIMIT, (void *)op->ors_tlimit );
488         slapi_pblock_set( pb, SLAPI_SEARCH_FILTER,    (void *)op->ors_filter );
489         slapi_pblock_set( pb, SLAPI_SEARCH_STRFILTER, (void *)op->ors_filterstr.bv_val );
490         slapi_pblock_set( pb, SLAPI_SEARCH_ATTRS,     (void *)attrs );
491         slapi_pblock_set( pb, SLAPI_SEARCH_ATTRSONLY, (void *)op->ors_attrsonly );
492
493         return LDAP_SUCCESS;
494 }
495
496 static int
497 slapi_op_search_callback( Operation *op, SlapReply *rs )
498 {
499         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
500
501         /* check preoperation result code */
502         if ( rs->sr_err < 0 ) {
503                 slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void **)&rs->sr_err );
504                 return rs->sr_err;
505         }
506
507         if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb ) != 0 ) {
508                 return LDAP_SUCCESS;
509         }
510
511         /*
512          * The plugin can set the SLAPI_SEARCH_FILTER.
513          * SLAPI_SEARCH_STRFILER is not normative.
514          */
515         slapi_pblock_get( pb, SLAPI_SEARCH_FILTER, (void *)&op->ors_filter );
516         op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
517         filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
518
519         slapi_pblock_get( pb, SLAPI_SEARCH_TARGET, (void **)&op->o_req_dn.bv_val );
520         op->o_req_dn.bv_len = strlen( op->o_req_dn.bv_val );
521
522         if( !BER_BVISNULL( &op->o_req_ndn ) ) {
523                 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
524         }
525         rs->sr_err = dnNormalize( 0, NULL, NULL, &op->o_req_dn, &op->o_req_ndn,
526                                   op->o_tmpmemctx );
527         if ( rs->sr_err != LDAP_SUCCESS ) {
528                 send_ldap_result( op, rs );
529                 return rs->sr_err;
530         }
531
532         slapi_pblock_get( pb, SLAPI_SEARCH_SCOPE, (void **)&op->ors_scope );
533         slapi_pblock_get( pb, SLAPI_SEARCH_DEREF, (void **)&op->ors_deref );
534
535         return LDAP_SUCCESS;
536 }
537
538 static int
539 slapi_op_search_cleanup( Operation *op, SlapReply *rs )
540 {
541         Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
542         char                    **attrs = NULL;
543
544         slapi_pblock_get( pb, SLAPI_SEARCH_ATTRS,     (void *)&attrs );
545
546         if ( attrs != NULL )
547                 op->o_tmpfree( attrs, op->o_tmpmemctx );
548
549         return LDAP_SUCCESS;
550 }
551
552 struct slapi_op_info {
553         int soi_preop;                  /* preoperation plugin parameter */
554         int soi_postop;                 /* postoperation plugin parameter */
555         int soi_internal_preop;         /* internal preoperation plugin parameter */
556         int soi_internal_postop;        /* internal postoperation plugin parameter */
557         slap_response *soi_preop_init;  /* preoperation pblock init function */
558         slap_response *soi_callback;    /* preoperation result handler */
559         slap_response *soi_postop_init; /* postoperation pblock init function */
560         slap_response *soi_cleanup;     /* cleanup function (not for internal ops) */
561 } slapi_op_dispatch_table[] = {
562         {
563                 SLAPI_PLUGIN_PRE_BIND_FN,
564                 SLAPI_PLUGIN_POST_BIND_FN,
565                 0,
566                 0,
567                 slapi_op_bind_preop_init,
568                 slapi_op_bind_callback,
569                 slapi_op_bind_postop_init,
570                 NULL
571         },
572         {
573                 SLAPI_PLUGIN_PRE_UNBIND_FN,
574                 SLAPI_PLUGIN_POST_UNBIND_FN,
575                 0,
576                 0,
577                 NULL,
578                 NULL,
579                 NULL,
580                 NULL
581         },
582         {
583                 SLAPI_PLUGIN_PRE_SEARCH_FN,
584                 SLAPI_PLUGIN_POST_SEARCH_FN,
585                 0,
586                 0,
587                 slapi_op_search_init,
588                 slapi_op_search_callback,
589                 NULL,
590                 slapi_op_search_cleanup
591         },
592         {
593                 SLAPI_PLUGIN_PRE_COMPARE_FN,
594                 SLAPI_PLUGIN_POST_COMPARE_FN,
595                 0,
596                 0,
597                 slapi_op_compare_init,
598                 NULL,
599                 NULL,
600                 NULL
601         },
602         {
603                 SLAPI_PLUGIN_PRE_MODIFY_FN,
604                 SLAPI_PLUGIN_POST_MODIFY_FN,
605                 SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN,
606                 SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
607                 slapi_op_modify_init,
608                 slapi_op_modify_callback,
609                 NULL,
610                 slapi_op_modify_cleanup
611         },
612         {
613                 SLAPI_PLUGIN_PRE_MODRDN_FN,
614                 SLAPI_PLUGIN_POST_MODRDN_FN,
615                 SLAPI_PLUGIN_INTERNAL_PRE_MODRDN_FN,
616                 SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
617                 slapi_op_modrdn_init,
618                 NULL,
619                 NULL,
620                 NULL
621         },
622         {
623                 SLAPI_PLUGIN_PRE_ADD_FN,
624                 SLAPI_PLUGIN_POST_ADD_FN,
625                 SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN,
626                 SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
627                 slapi_op_add_init,
628                 NULL,
629                 NULL,
630                 NULL
631         },
632         {
633                 SLAPI_PLUGIN_PRE_DELETE_FN,
634                 SLAPI_PLUGIN_POST_DELETE_FN,
635                 SLAPI_PLUGIN_INTERNAL_PRE_DELETE_FN,
636                 SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
637                 NULL,
638                 NULL,
639                 NULL,
640                 NULL
641         },
642         {
643                 SLAPI_PLUGIN_PRE_ABANDON_FN,
644                 SLAPI_PLUGIN_POST_ABANDON_FN,
645                 0,
646                 0,
647                 NULL,
648                 NULL,
649                 NULL,
650                 NULL
651         },
652         {
653                 0,
654                 0,
655                 0,
656                 0,
657                 NULL,
658                 NULL,
659                 NULL,
660                 NULL
661         }
662 };
663
664 slap_operation_t
665 slapi_tag2op( ber_tag_t tag )
666 {
667         slap_operation_t op;
668
669         switch ( tag ) {
670         case LDAP_REQ_BIND:
671                 op = op_bind;
672                 break;
673         case LDAP_REQ_ADD:
674                 op = op_add;
675                 break;
676         case LDAP_REQ_DELETE:
677                 op = op_compare;
678                 break;
679         case LDAP_REQ_MODRDN:
680                 op = op_modrdn;
681                 break;
682         case LDAP_REQ_MODIFY:
683                 op = op_modify;
684                 break;
685         case LDAP_REQ_COMPARE:
686                 op = op_compare;
687                 break;
688         case LDAP_REQ_SEARCH:
689                 op = op_search;
690                 break;
691         case LDAP_REQ_UNBIND:
692                 op = op_unbind;
693                 break;
694         default:
695                 op = op_last;
696                 break;
697         }
698
699         return op;
700 }
701
702 static int
703 slapi_over_response( Operation *op, SlapReply *rs )
704 {
705         int                     rc;
706
707         switch ( rs->sr_type ) {
708         case REP_RESULT:
709                 rc = slapi_over_result( op, rs, SLAPI_PLUGIN_PRE_RESULT_FN );
710                 break;
711         case REP_SEARCH:
712                 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_ENTRY_FN );
713                 break;
714         case REP_SEARCHREF:
715                 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_REFERRAL_FN );
716                 break;
717         default:
718                 rc = SLAP_CB_CONTINUE;
719                 break;
720         }
721
722         return rc;
723 }
724
725 static int
726 slapi_over_cleanup( Operation *op, SlapReply *rs )
727 {
728         int                     rc;
729
730         switch ( rs->sr_type ) {
731         case REP_RESULT:
732                 rc = slapi_over_result( op, rs, SLAPI_PLUGIN_POST_RESULT_FN );
733                 break;
734         case REP_SEARCH:
735                 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_ENTRY_FN );
736                 break;
737         case REP_SEARCHREF:
738                 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_REFERRAL_FN );
739                 break;
740         default:
741                 rc = SLAP_CB_CONTINUE;
742                 break;
743         }
744
745         return rc;
746 }
747
748 static int
749 slapi_op_func( Operation *op, SlapReply *rs )
750 {
751         Slapi_PBlock            *pb;
752         slap_operation_t        which;
753         struct slapi_op_info    *opinfo;
754         int                     rc;
755         slap_overinfo           *oi;
756         slap_overinst           *on;
757         slap_callback           cb;
758         int                     internal_op;
759         int                     preop_type, postop_type;
760
761         if ( !slapi_plugins_used )
762                 return SLAP_CB_CONTINUE;
763
764         /*
765          * Find the SLAPI operation information for this LDAP
766          * operation; this will contain the preop and postop
767          * plugin types, as well as optional callbacks for
768          * setting up the SLAPI environment.
769          */
770         which = slapi_tag2op( op->o_tag );
771         if ( which >= op_last ) {
772                 /* invalid operation, but let someone else deal with it */
773                 return SLAP_CB_CONTINUE;
774         }
775
776         opinfo = &slapi_op_dispatch_table[which];
777         if ( opinfo == NULL ) {
778                 /* no SLAPI plugin types for this operation */
779                 return SLAP_CB_CONTINUE;
780         }
781
782         internal_op = slapi_op_internal_p( op, &cb );
783
784         if ( internal_op ) {
785                 preop_type = opinfo->soi_internal_preop;
786                 postop_type = opinfo->soi_internal_postop;
787         } else {
788                 preop_type = opinfo->soi_preop;
789                 postop_type = opinfo->soi_postop;
790         }
791
792         if ( preop_type == 0 ) {
793                 /* no SLAPI plugin types for this operation */
794                 rs->sr_err = SLAP_CB_CONTINUE;
795                 goto cleanup;
796         }
797
798         pb = SLAPI_OPERATION_PBLOCK( op );
799
800         slapi_pblock_set( pb, SLAPI_TARGET_ADDRESS, NULL );
801         slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, NULL );
802         slapi_pblock_set( pb, SLAPI_TARGET_DN, (void *)op->o_req_dn.bv_val );
803
804         /*
805          * Call preoperation plugins 
806          */
807         if ( opinfo->soi_preop_init != NULL ) {
808                 rs->sr_err = (opinfo->soi_preop_init)( op, rs );
809                 if ( rs->sr_err != LDAP_SUCCESS )
810                         goto cleanup;
811         }
812
813         rs->sr_err = slapi_int_call_plugins( op->o_bd, preop_type, pb );
814
815         /*
816          * soi_callback is responsible for examining the result code
817          * of the preoperation plugin and determining whether to
818          * abort. This is needed because of special SLAPI behaviour
819          * with bind preoperation plugins.
820          *
821          * The soi_callback function is also used to reset any values
822          * returned from the preoperation plugin before calling the
823          * backend (for the success case).
824          */
825         if ( opinfo->soi_callback == NULL ) {
826                 /* default behaviour is preop plugin can abort operation */
827                 if ( rs->sr_err < 0 ) {
828                         slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void **)&rs->sr_err );
829                         goto cleanup;
830                 } else {
831                         rs->sr_err = LDAP_SUCCESS;
832                 }
833         } else {
834                 rc = (opinfo->soi_callback)( op, rs );
835                 if ( rc )
836                         goto cleanup;
837         }
838
839         /*
840          * Call actual backend (or next overlay in stack). We need to
841          * do this rather than returning SLAP_CB_CONTINUE and calling
842          * postoperation plugins in a response handler to match the
843          * behaviour of SLAPI in OpenLDAP 2.2, where postoperation
844          * plugins are called after the backend has completely
845          * finished processing the operation.
846          */
847         on = (slap_overinst *)op->o_bd->bd_info;
848         oi = on->on_info;
849
850         rs->sr_err = overlay_op_walk( op, rs, which, oi, on->on_next );
851
852         /*
853          * Call postoperation plugins
854          */
855         slapi_pblock_set( pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
856
857         if ( opinfo->soi_postop_init != NULL ) {
858                 (opinfo->soi_postop_init)( op, rs );
859         }
860
861         slapi_int_call_plugins( op->o_bd, postop_type, pb );
862
863 cleanup:
864         if ( !internal_op ) {
865                 if ( opinfo->soi_cleanup != NULL ) {
866                         (opinfo->soi_cleanup)( op, rs );
867                 }
868                 slapi_pblock_destroy(pb);
869                 cb.sc_private = NULL;
870         }
871
872         op->o_callback = cb.sc_next;
873
874         return rs->sr_err;
875 }
876
877 static int
878 slapi_over_extended( Operation *op, SlapReply *rs )
879 {
880         Slapi_PBlock    *pb;
881         SLAPI_FUNC      callback;
882         int             rc;
883         struct berval   reqdata = BER_BVNULL;
884         int             internal_op;
885         slap_callback   cb;
886
887         slapi_int_get_extop_plugin( &op->ore_reqoid, &callback );
888         if ( callback == NULL ) {
889                 return SLAP_CB_CONTINUE;
890         }
891
892         internal_op = slapi_op_internal_p( op, &cb );
893         if ( internal_op ) {
894                 return SLAP_CB_CONTINUE;
895         }
896
897         pb = SLAPI_OPERATION_PBLOCK( op );
898
899         if ( op->ore_reqdata != NULL ) {
900                 reqdata = *op->ore_reqdata;
901         }
902
903         slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_OID,   (void *)op->ore_reqoid.bv_val);
904         slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_VALUE, (void *)&reqdata);
905
906         rc = (*callback)( pb );
907         if ( rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
908                 slapi_pblock_destroy( pb );
909                 return rc;
910         } else if ( rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
911                 slapi_pblock_destroy( pb );
912                 return SLAP_CB_CONTINUE;
913         }
914
915         slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID,   (void **)&rs->sr_rspoid );
916         slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, (void **)&rs->sr_rspdata );
917
918         rs->sr_err = rc;
919         send_ldap_extended( op, rs );
920
921         slapi_ch_free_string( (char **)&rs->sr_rspoid );
922
923         if ( rs->sr_rspdata != NULL )
924                 ber_bvfree( rs->sr_rspdata );
925
926         slapi_pblock_destroy( pb );
927
928         return rs->sr_err;
929 }
930
931 static int
932 slapi_over_access_allowed(
933         Operation               *op,
934         Entry                   *e,
935         AttributeDescription    *desc,
936         struct berval           *val,
937         slap_access_t           access,
938         AccessControlState      *state,
939         slap_mask_t             *maskp )
940 {
941         int                     rc;
942         Slapi_PBlock            *pb;
943         slap_callback           cb;
944         int                     internal_op;
945
946         internal_op = slapi_op_internal_p( op, &cb );
947
948         cb.sc_response = NULL;
949         cb.sc_cleanup = NULL;
950
951         pb = SLAPI_OPERATION_PBLOCK( op );
952
953         rc = slapi_int_access_allowed( op, e, desc, val, access, state );
954         if ( rc ) {
955                 rc = SLAP_CB_CONTINUE;
956         }
957
958         op->o_callback = cb.sc_next;
959
960         if ( !internal_op )
961                 slapi_pblock_destroy( pb );
962
963         return rc;
964 }
965
966 static int
967 slapi_over_acl_group(
968         Operation               *op,
969         Entry                   *target,
970         struct berval           *gr_ndn,
971         struct berval           *op_ndn,
972         ObjectClass             *group_oc,
973         AttributeDescription    *group_at )
974 {
975         Slapi_Entry             *e;
976         int                     rc;
977         Slapi_PBlock            *pb;
978         BackendDB               *be = op->o_bd;
979         GroupAssertion          *g;
980
981         op->o_bd = select_backend( gr_ndn, 0, 0 );
982
983         for ( g = op->o_groups; g; g = g->ga_next ) {
984                 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
985                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
986                 {
987                         continue;
988                 }
989                 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
990                         break;
991                 }
992         }
993         if ( g != NULL ) {
994                 rc = g->ga_res;
995                 goto done;
996         }
997
998         if ( target != NULL && dn_match( &target->e_nname, gr_ndn ) ) {
999                 e = target;
1000                 rc = 0;
1001         } else {
1002                 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1003         }
1004         if ( e != NULL ) {
1005                 int                     internal_op;
1006                 slap_callback           cb;
1007
1008                 internal_op = slapi_op_internal_p( op, &cb );
1009
1010                 cb.sc_response = NULL;
1011                 cb.sc_cleanup = NULL;
1012
1013                 pb = SLAPI_OPERATION_PBLOCK( op );
1014
1015                 slapi_pblock_set( pb, SLAPI_X_GROUP_ENTRY,        (void *)e );
1016                 slapi_pblock_set( pb, SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1017                 slapi_pblock_set( pb, SLAPI_X_GROUP_ATTRIBUTE,    (void *)group_at->ad_cname.bv_val );
1018                 slapi_pblock_set( pb, SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1019
1020                 rc = slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_PRE_GROUP_FN, pb );
1021                 if ( rc >= 0 ) /* 1 means no plugins called */
1022                         rc = SLAP_CB_CONTINUE;
1023                 else
1024                         slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void **)&rc );
1025
1026                 if ( !internal_op )
1027                         slapi_pblock_destroy( pb );
1028
1029                 if ( e != target ) {
1030                         be_entry_release_r( op, e );
1031                 }
1032         } else {
1033                 rc = LDAP_NO_SUCH_OBJECT; /* return SLAP_CB_CONTINUE for correctness? */
1034         }
1035
1036         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache &&
1037              rc != SLAP_CB_CONTINUE ) {
1038                 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1039                         op->o_tmpmemctx );
1040                 g->ga_be = op->o_bd;
1041                 g->ga_oc = group_oc;
1042                 g->ga_at = group_at;
1043                 g->ga_res = rc;
1044                 g->ga_len = gr_ndn->bv_len;
1045                 strcpy( g->ga_ndn, gr_ndn->bv_val );
1046                 g->ga_next = op->o_groups;
1047                 op->o_groups = g;
1048         }
1049         /*
1050          * XXX don't call POST_GROUP_FN, I have no idea what the point of
1051          * that plugin function was anyway
1052          */
1053 done:
1054         op->o_bd = be;
1055         return rc;
1056 }
1057
1058 int
1059 slapi_int_overlay_init()
1060 {
1061         memset( &slapi, 0, sizeof(slapi) );
1062
1063         slapi.on_bi.bi_type = SLAPI_OVERLAY_NAME;
1064
1065         slapi.on_bi.bi_op_bind          = slapi_op_func;
1066         slapi.on_bi.bi_op_unbind        = slapi_op_func;
1067         slapi.on_bi.bi_op_search        = slapi_op_func;
1068         slapi.on_bi.bi_op_compare       = slapi_op_func;
1069         slapi.on_bi.bi_op_modify        = slapi_op_func;
1070         slapi.on_bi.bi_op_modrdn        = slapi_op_func;
1071         slapi.on_bi.bi_op_add           = slapi_op_func;
1072         slapi.on_bi.bi_op_delete        = slapi_op_func;
1073         slapi.on_bi.bi_op_abandon       = slapi_op_func;
1074         slapi.on_bi.bi_op_cancel        = slapi_op_func;
1075
1076         slapi.on_bi.bi_extended         = slapi_over_extended;
1077         slapi.on_bi.bi_access_allowed   = slapi_over_access_allowed;
1078         slapi.on_bi.bi_operational      = slapi_over_aux_operational;
1079         slapi.on_bi.bi_acl_group        = slapi_over_acl_group;
1080
1081         return overlay_register( &slapi );
1082 }
1083
1084 #endif /* LDAP_SLAPI */