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