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