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