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