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