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