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