Back to index

openldap  2.4.31
slapi_overlay.c
Go to the documentation of this file.
00001 /* slapi_overlay.c - SLAPI overlay */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2001-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENTS:
00017  * This work was initially developed by Luke Howard for inclusion
00018  * in OpenLDAP Software.
00019  */
00020 
00021 #include "portable.h"
00022 
00023 #include <stdio.h>
00024 
00025 #include <ac/string.h>
00026 #include <ac/socket.h>
00027 
00028 #include "slap.h"
00029 #include "slapi.h"
00030 #include "config.h"
00031 
00032 #ifdef LDAP_SLAPI
00033 
00034 static slap_overinst slapi;
00035 static int slapi_over_initialized = 0;
00036 
00037 static int slapi_over_response( Operation *op, SlapReply *rs );
00038 static int slapi_over_cleanup( Operation *op, SlapReply *rs );
00039 
00040 static Slapi_PBlock *
00041 slapi_over_pblock_new( Operation *op, SlapReply *rs )
00042 {
00043        Slapi_PBlock         *pb;
00044 
00045        pb = slapi_pblock_new();
00046        pb->pb_op = op;
00047        pb->pb_conn = op->o_conn;
00048        pb->pb_rs = rs;
00049        pb->pb_intop = 0;
00050 
00051        PBLOCK_ASSERT_OP( pb, op->o_tag );
00052 
00053        return pb;
00054 }
00055 
00056 static int
00057 slapi_op_internal_p( Operation *op, SlapReply *rs, slap_callback *cb )
00058 {
00059        int                  internal_op = 0;
00060        Slapi_PBlock         *pb = NULL;
00061        slap_callback        *pcb;
00062 
00063        /*
00064         * Abstraction violating check for SLAPI internal operations
00065         * allows pblock to remain consistent when invoking internal
00066         * op plugins
00067         */
00068        for ( pcb = op->o_callback; pcb != NULL; pcb = pcb->sc_next ) {
00069               if ( pcb->sc_response == slapi_int_response ) {
00070                      pb = (Slapi_PBlock *)pcb->sc_private;
00071                      PBLOCK_ASSERT_INTOP( pb, 0 );
00072                      internal_op = 1;
00073                      break;
00074               }
00075        }
00076 
00077        if ( cb != NULL ) {
00078               if ( pb == NULL ) {
00079                      pb = slapi_over_pblock_new( op, rs );
00080               }
00081 
00082               cb->sc_response = slapi_over_response;
00083               cb->sc_cleanup = slapi_over_cleanup;
00084               cb->sc_private = pb;
00085               cb->sc_next = op->o_callback;
00086               op->o_callback = cb;
00087        }
00088 
00089        return internal_op;
00090 }
00091 
00092 static int
00093 slapi_over_compute_output(
00094        computed_attr_context *c,
00095        Slapi_Attr *attribute,
00096        Slapi_Entry *entry
00097 )
00098 {
00099        Attribute            **a;
00100        AttributeDescription *desc;
00101        SlapReply            *rs;
00102 
00103        if ( c == NULL || attribute == NULL || entry == NULL ) {
00104               return 0;
00105        }
00106 
00107        rs = (SlapReply *)c->cac_private;
00108 
00109        assert( rs->sr_entry == entry );
00110 
00111        desc = attribute->a_desc;
00112 
00113        if ( rs->sr_attrs == NULL ) {
00114               /* All attrs request, skip operational attributes */
00115               if ( is_at_operational( desc->ad_type ) ) {
00116                      return 0;
00117               }
00118        } else {
00119               /* Specific attributes requested */
00120               if ( is_at_operational( desc->ad_type ) ) {
00121                      if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
00122                           !ad_inlist( desc, rs->sr_attrs ) ) {
00123                             return 0;
00124                      }
00125               } else {
00126                      if ( !SLAP_USERATTRS( rs->sr_attr_flags ) &&
00127                           !ad_inlist( desc, rs->sr_attrs ) ) {
00128                             return 0;
00129                      }
00130               }
00131        }
00132 
00133        /* XXX perhaps we should check for existing attributes and merge */
00134        for ( a = &rs->sr_operational_attrs; *a != NULL; a = &(*a)->a_next )
00135               ;
00136 
00137        *a = slapi_attr_dup( attribute );
00138 
00139        return 0;
00140 }
00141 
00142 static int
00143 slapi_over_aux_operational( Operation *op, SlapReply *rs )
00144 {
00145        /* Support for computed attribute plugins */
00146        computed_attr_context    ctx;
00147        AttributeName        *anp;
00148 
00149        if ( slapi_op_internal_p( op, rs, NULL ) ) {
00150               return SLAP_CB_CONTINUE;
00151        }
00152 
00153        ctx.cac_pb = slapi_over_pblock_new( op, rs );
00154        ctx.cac_op = op;
00155        ctx.cac_private = rs;
00156 
00157        if ( rs->sr_entry != NULL ) {
00158               /*
00159                * For each client requested attribute, call the plugins.
00160                */
00161               if ( rs->sr_attrs != NULL ) {
00162                      for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
00163                             if ( compute_evaluator( &ctx, anp->an_name.bv_val,
00164                                    rs->sr_entry, slapi_over_compute_output ) == 1 ) {
00165                                    break;
00166                             }
00167                      }
00168               } else {
00169                      /*
00170                       * Technically we shouldn't be returning operational attributes
00171                       * when the user requested only user attributes. We'll let the
00172                       * plugin decide whether to be naughty or not.
00173                       */
00174                      compute_evaluator( &ctx, "*", rs->sr_entry, slapi_over_compute_output );
00175               }
00176        }
00177 
00178        slapi_pblock_destroy( ctx.cac_pb );
00179 
00180        return SLAP_CB_CONTINUE;
00181 }
00182 
00183 /*
00184  * We need this function to call frontendDB (global) plugins before
00185  * database plugins, if we are invoked by a slap_callback.
00186  */
00187 static int
00188 slapi_over_call_plugins( Slapi_PBlock *pb, int type )
00189 {
00190        int                  rc = 1; /* means no plugins called */
00191        Operation            *op;
00192 
00193        PBLOCK_ASSERT_OP( pb, 0 );
00194        op = pb->pb_op;
00195 
00196        if ( !be_match( op->o_bd, frontendDB ) ) {
00197               rc = slapi_int_call_plugins( frontendDB, type, pb );
00198        }
00199        if ( rc >= 0 ) {
00200               rc = slapi_int_call_plugins( op->o_bd, type, pb );
00201        }
00202 
00203        return rc;
00204 }
00205 
00206 static int
00207 slapi_over_search( Operation *op, SlapReply *rs, int type )
00208 {
00209        int                  rc;
00210        Slapi_PBlock         *pb;
00211 
00212        assert( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF );
00213 
00214        /* create a new pblock to not trample on result controls */
00215        pb = slapi_over_pblock_new( op, rs );
00216 
00217        rc = slapi_over_call_plugins( pb, type );
00218        if ( rc >= 0 ) /* 1 means no plugins called */
00219               rc = SLAP_CB_CONTINUE;
00220        else
00221               rc = LDAP_SUCCESS; /* confusing: don't abort, but don't send */
00222 
00223        slapi_pblock_destroy(pb);
00224 
00225        return rc;
00226 }
00227 
00228 /*
00229  * Call pre- and post-result plugins
00230  */
00231 static int
00232 slapi_over_result( Operation *op, SlapReply *rs, int type )
00233 {
00234        Slapi_PBlock         *pb = SLAPI_OPERATION_PBLOCK( op );
00235 
00236        assert( rs->sr_type == REP_RESULT || rs->sr_type == REP_SASL || rs->sr_type == REP_EXTENDED );
00237 
00238        slapi_over_call_plugins( pb, type );
00239 
00240        return SLAP_CB_CONTINUE;
00241 }
00242 
00243 
00244 static int
00245 slapi_op_bind_callback( Operation *op, SlapReply *rs, int prc )
00246 {
00247        switch ( prc ) {
00248        case SLAPI_BIND_SUCCESS:
00249               /* Continue with backend processing */
00250               break;
00251        case SLAPI_BIND_FAIL:
00252               /* Failure, frontend (that's us) sends result */
00253               rs->sr_err = LDAP_INVALID_CREDENTIALS;
00254               send_ldap_result( op, rs );
00255               return rs->sr_err;
00256               break;
00257        case SLAPI_BIND_ANONYMOUS: /* undocumented */
00258        default: /* plugin sent result or no plugins called */
00259               BER_BVZERO( &op->orb_edn );
00260 
00261               if ( rs->sr_err == LDAP_SUCCESS ) {
00262                      /*
00263                       * Plugin will have called slapi_pblock_set(LDAP_CONN_DN) which
00264                       * will have set conn->c_dn and conn->c_ndn
00265                       */
00266                      if ( BER_BVISNULL( &op->o_conn->c_ndn ) && prc == 1 ) {
00267                             /* No plugins were called; continue processing */
00268                             return LDAP_SUCCESS;
00269                      }
00270                      ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
00271                      if ( !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
00272                             ber_len_t max = sockbuf_max_incoming_auth;
00273                             ber_sockbuf_ctrl( op->o_conn->c_sb,
00274                                    LBER_SB_OPT_SET_MAX_INCOMING, &max );
00275                      }
00276                      ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
00277 
00278                      /* log authorization identity */
00279                      Statslog( LDAP_DEBUG_STATS,
00280                             "%s BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
00281                             op->o_log_prefix,
00282                             BER_BVISNULL( &op->o_conn->c_dn )
00283                                    ? "<empty>" : op->o_conn->c_dn.bv_val,
00284                             BER_BVISNULL( &op->orb_mech )
00285                                    ? "<empty>" : op->orb_mech.bv_val, 0, 0 );
00286 
00287                      return -1;
00288               }
00289               break;
00290        }
00291 
00292        return rs->sr_err;
00293 }
00294 
00295 static int
00296 slapi_op_search_callback( Operation *op, SlapReply *rs, int prc )
00297 {
00298        Slapi_PBlock         *pb = SLAPI_OPERATION_PBLOCK( op );
00299 
00300        /* check preoperation result code */
00301        if ( prc < 0 ) {
00302               return rs->sr_err;
00303        }
00304 
00305        rs->sr_err = LDAP_SUCCESS;
00306 
00307        if ( pb->pb_intop == 0 && 
00308             slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb ) == 0 ) {
00309               /*
00310                * The plugin can set the SLAPI_SEARCH_FILTER.
00311                * SLAPI_SEARCH_STRFILER is not normative.
00312                */
00313               op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
00314               filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
00315        }
00316 
00317        return LDAP_SUCCESS;
00318 }
00319 
00320 struct slapi_op_info {
00321        int soi_preop;                     /* preoperation plugin parameter */
00322        int soi_postop;                    /* postoperation plugin parameter */
00323        int soi_internal_preop;            /* internal preoperation plugin parameter */
00324        int soi_internal_postop;    /* internal postoperation plugin parameter */
00325        int (*soi_callback)(Operation *, SlapReply *, int); /* preoperation result handler */
00326 } slapi_op_dispatch_table[] = {
00327        {
00328               SLAPI_PLUGIN_PRE_BIND_FN,
00329               SLAPI_PLUGIN_POST_BIND_FN,
00330               SLAPI_PLUGIN_INTERNAL_PRE_BIND_FN,
00331               SLAPI_PLUGIN_INTERNAL_POST_BIND_FN,
00332               slapi_op_bind_callback
00333        },
00334        {
00335               SLAPI_PLUGIN_PRE_UNBIND_FN,
00336               SLAPI_PLUGIN_POST_UNBIND_FN,
00337               SLAPI_PLUGIN_INTERNAL_PRE_UNBIND_FN,
00338               SLAPI_PLUGIN_INTERNAL_POST_UNBIND_FN,
00339               NULL
00340        },
00341        {
00342               SLAPI_PLUGIN_PRE_SEARCH_FN,
00343               SLAPI_PLUGIN_POST_SEARCH_FN,
00344               SLAPI_PLUGIN_INTERNAL_PRE_SEARCH_FN,
00345               SLAPI_PLUGIN_INTERNAL_POST_SEARCH_FN,
00346               slapi_op_search_callback
00347        },
00348        {
00349               SLAPI_PLUGIN_PRE_COMPARE_FN,
00350               SLAPI_PLUGIN_POST_COMPARE_FN,
00351               SLAPI_PLUGIN_INTERNAL_PRE_COMPARE_FN,
00352               SLAPI_PLUGIN_INTERNAL_POST_COMPARE_FN,
00353               NULL
00354        },
00355        {
00356               SLAPI_PLUGIN_PRE_MODIFY_FN,
00357               SLAPI_PLUGIN_POST_MODIFY_FN,
00358               SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN,
00359               SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
00360               NULL
00361        },
00362        {
00363               SLAPI_PLUGIN_PRE_MODRDN_FN,
00364               SLAPI_PLUGIN_POST_MODRDN_FN,
00365               SLAPI_PLUGIN_INTERNAL_PRE_MODRDN_FN,
00366               SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
00367               NULL
00368        },
00369        {
00370               SLAPI_PLUGIN_PRE_ADD_FN,
00371               SLAPI_PLUGIN_POST_ADD_FN,
00372               SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN,
00373               SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
00374               NULL
00375        },
00376        {
00377               SLAPI_PLUGIN_PRE_DELETE_FN,
00378               SLAPI_PLUGIN_POST_DELETE_FN,
00379               SLAPI_PLUGIN_INTERNAL_PRE_DELETE_FN,
00380               SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
00381               NULL
00382        },
00383        {
00384               SLAPI_PLUGIN_PRE_ABANDON_FN,
00385               SLAPI_PLUGIN_POST_ABANDON_FN,
00386               SLAPI_PLUGIN_INTERNAL_PRE_ABANDON_FN,
00387               SLAPI_PLUGIN_INTERNAL_POST_ABANDON_FN,
00388               NULL
00389        },
00390        {
00391               0,
00392               0,
00393               0,
00394               0,
00395               NULL
00396        }
00397 };
00398 
00399 slap_operation_t
00400 slapi_tag2op( ber_tag_t tag )
00401 {
00402        slap_operation_t op;
00403 
00404        switch ( tag ) {
00405        case LDAP_REQ_BIND:
00406               op = op_bind;
00407               break;
00408        case LDAP_REQ_ADD:
00409               op = op_add;
00410               break;
00411        case LDAP_REQ_DELETE:
00412               op = op_delete;
00413               break;
00414        case LDAP_REQ_MODRDN:
00415               op = op_modrdn;
00416               break;
00417        case LDAP_REQ_MODIFY:
00418               op = op_modify;
00419               break;
00420        case LDAP_REQ_COMPARE:
00421               op = op_compare;
00422               break;
00423        case LDAP_REQ_SEARCH:
00424               op = op_search;
00425               break;
00426        case LDAP_REQ_UNBIND:
00427               op = op_unbind;
00428               break;
00429        default:
00430               op = op_last;
00431               break;
00432        }
00433 
00434        return op;
00435 }
00436 
00437 /* Add SLAPI_RESCONTROLS to rs->sr_ctrls, with care, because
00438  * rs->sr_ctrls could be allocated on the stack */
00439 static int
00440 slapi_over_merge_controls( Operation *op, SlapReply *rs )
00441 {
00442        Slapi_PBlock         *pb = SLAPI_OPERATION_PBLOCK( op );
00443        LDAPControl          **ctrls = NULL;
00444        LDAPControl          **slapi_ctrls = NULL;
00445        size_t               n_slapi_ctrls = 0;
00446        size_t               n_rs_ctrls = 0;
00447        size_t               i;
00448 
00449        slapi_pblock_get( pb, SLAPI_RESCONTROLS, (void **)&slapi_ctrls );
00450 
00451        n_slapi_ctrls = slapi_int_count_controls( slapi_ctrls );
00452        n_rs_ctrls = slapi_int_count_controls( rs->sr_ctrls );
00453 
00454        slapi_pblock_set( pb, SLAPI_X_OLD_RESCONTROLS, (void *)rs->sr_ctrls );
00455 
00456        if ( n_slapi_ctrls == 0 )
00457               return LDAP_SUCCESS; /* no SLAPI controls */
00458 
00459        ctrls = (LDAPControl **) op->o_tmpalloc(
00460               ( n_slapi_ctrls + n_rs_ctrls + 1 ) * sizeof(LDAPControl *),
00461               op->o_tmpmemctx );
00462 
00463        for ( i = 0; i < n_slapi_ctrls; i++ ) {
00464               ctrls[i] = slapi_ctrls[i];
00465        }
00466        if ( rs->sr_ctrls != NULL ) {
00467               for ( i = 0; i < n_rs_ctrls; i++ ) {
00468                      ctrls[n_slapi_ctrls + i] = rs->sr_ctrls[i];
00469               }
00470        }
00471        ctrls[n_slapi_ctrls + n_rs_ctrls] = NULL;
00472 
00473        rs->sr_ctrls = ctrls;
00474 
00475        return LDAP_SUCCESS;
00476 }
00477 
00478 static int
00479 slapi_over_unmerge_controls( Operation *op, SlapReply *rs )
00480 {
00481        Slapi_PBlock         *pb = SLAPI_OPERATION_PBLOCK( op );
00482        LDAPControl          **rs_ctrls = NULL;
00483 
00484        slapi_pblock_get( pb, SLAPI_X_OLD_RESCONTROLS, (void **)&rs_ctrls );
00485 
00486        if ( rs_ctrls == NULL || rs->sr_ctrls == rs_ctrls ) {
00487               /* no copying done */
00488               return LDAP_SUCCESS;
00489        }
00490 
00491        op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
00492        rs->sr_ctrls = rs_ctrls;
00493 
00494        return LDAP_SUCCESS;
00495 }
00496 
00497 static int
00498 slapi_over_response( Operation *op, SlapReply *rs )
00499 {
00500        Slapi_PBlock         *pb = SLAPI_OPERATION_PBLOCK( op );
00501        int                  rc = SLAP_CB_CONTINUE;
00502 
00503        if ( pb->pb_intop == 0 ) {
00504               switch ( rs->sr_type ) {
00505               case REP_RESULT:
00506               case REP_SASL:
00507               case REP_EXTENDED:
00508                      rc = slapi_over_result( op, rs, SLAPI_PLUGIN_PRE_RESULT_FN );
00509                      break;
00510               case REP_SEARCH:
00511                      rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_ENTRY_FN );
00512                      break;
00513               case REP_SEARCHREF:
00514                      rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_REFERRAL_FN );
00515                      break;
00516               default:
00517                      break;
00518               }
00519        }
00520 
00521        slapi_over_merge_controls( op, rs );
00522 
00523        return rc;
00524 }
00525 
00526 static int
00527 slapi_over_cleanup( Operation *op, SlapReply *rs )
00528 {
00529        Slapi_PBlock         *pb = SLAPI_OPERATION_PBLOCK( op );
00530        int                  rc = SLAP_CB_CONTINUE;
00531 
00532        slapi_over_unmerge_controls( op, rs );
00533 
00534        if ( pb->pb_intop == 0 ) {
00535               switch ( rs->sr_type ) {
00536               case REP_RESULT:
00537               case REP_SASL:
00538               case REP_EXTENDED:
00539                      rc = slapi_over_result( op, rs, SLAPI_PLUGIN_POST_RESULT_FN );
00540                      break;
00541               case REP_SEARCH:
00542                      rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_ENTRY_FN );
00543                      break;
00544               case REP_SEARCHREF:
00545                      rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_REFERRAL_FN );
00546                      break;
00547               default:
00548                      break;
00549               }
00550        }
00551 
00552        return rc;
00553 }
00554 
00555 static int
00556 slapi_op_func( Operation *op, SlapReply *rs )
00557 {
00558        Slapi_PBlock         *pb;
00559        slap_operation_t     which;
00560        struct slapi_op_info *opinfo;
00561        int                  rc;
00562        slap_overinfo        *oi;
00563        slap_overinst        *on;
00564        slap_callback        cb;
00565        int                  internal_op;
00566        int                  preop_type, postop_type;
00567        BackendDB            *be;
00568 
00569        if ( !slapi_plugins_used )
00570               return SLAP_CB_CONTINUE;
00571 
00572        /*
00573         * Find the SLAPI operation information for this LDAP
00574         * operation; this will contain the preop and postop
00575         * plugin types, as well as optional callbacks for
00576         * setting up the SLAPI environment.
00577         */
00578        which = slapi_tag2op( op->o_tag );
00579        if ( which >= op_last ) {
00580               /* invalid operation, but let someone else deal with it */
00581               return SLAP_CB_CONTINUE;
00582        }
00583 
00584        opinfo = &slapi_op_dispatch_table[which];
00585        if ( opinfo == NULL ) {
00586               /* no SLAPI plugin types for this operation */
00587               return SLAP_CB_CONTINUE;
00588        }
00589 
00590        internal_op = slapi_op_internal_p( op, rs, &cb );
00591 
00592        if ( internal_op ) {
00593               preop_type = opinfo->soi_internal_preop;
00594               postop_type = opinfo->soi_internal_postop;
00595        } else {
00596               preop_type = opinfo->soi_preop;
00597               postop_type = opinfo->soi_postop;
00598        }
00599 
00600        if ( preop_type == 0 ) {
00601               /* no SLAPI plugin types for this operation */
00602               pb = NULL;
00603               rc = SLAP_CB_CONTINUE;
00604               goto cleanup;
00605        }
00606 
00607        pb = SLAPI_OPERATION_PBLOCK( op );
00608 
00609        /* cache backend so we call correct postop plugins */
00610        be = pb->pb_op->o_bd;
00611 
00612        rc = slapi_int_call_plugins( be, preop_type, pb );
00613 
00614        /*
00615         * soi_callback is responsible for examining the result code
00616         * of the preoperation plugin and determining whether to
00617         * abort. This is needed because of special SLAPI behaviour
00618         e with bind preoperation plugins.
00619         *
00620         * The soi_callback function is also used to reset any values
00621         * returned from the preoperation plugin before calling the
00622         * backend (for the success case).
00623         */
00624        if ( opinfo->soi_callback == NULL ) {
00625               /* default behaviour is preop plugin can abort operation */
00626               if ( rc < 0 ) {
00627                      rc = rs->sr_err;
00628                      goto cleanup;
00629               }
00630        } else {
00631               rc = (opinfo->soi_callback)( op, rs, rc );
00632               if ( rc )
00633                      goto cleanup;
00634        }
00635 
00636        /*
00637         * Call actual backend (or next overlay in stack). We need to
00638         * do this rather than returning SLAP_CB_CONTINUE and calling
00639         * postoperation plugins in a response handler to match the
00640         * behaviour of SLAPI in OpenLDAP 2.2, where postoperation
00641         * plugins are called after the backend has completely
00642         * finished processing the operation.
00643         */
00644        on = (slap_overinst *)op->o_bd->bd_info;
00645        oi = on->on_info;
00646 
00647        rc = overlay_op_walk( op, rs, which, oi, on->on_next );
00648 
00649        /*
00650         * Call postoperation plugins
00651         */
00652        slapi_int_call_plugins( be, postop_type, pb );
00653 
00654 cleanup:
00655        if ( !internal_op ) {
00656               slapi_pblock_destroy(pb);
00657               cb.sc_private = NULL;
00658        }
00659 
00660        op->o_callback = cb.sc_next;
00661 
00662        return rc;
00663 }
00664 
00665 static int
00666 slapi_over_extended( Operation *op, SlapReply *rs )
00667 {
00668        Slapi_PBlock  *pb;
00669        SLAPI_FUNC    callback;
00670        int           rc;
00671        int           internal_op;
00672        slap_callback cb;
00673 
00674        slapi_int_get_extop_plugin( &op->ore_reqoid, &callback );
00675        if ( callback == NULL ) {
00676               return SLAP_CB_CONTINUE;
00677        }
00678 
00679        internal_op = slapi_op_internal_p( op, rs, &cb );
00680        if ( internal_op ) {
00681               return SLAP_CB_CONTINUE;
00682        }
00683 
00684        pb = SLAPI_OPERATION_PBLOCK( op );
00685 
00686        rc = (*callback)( pb );
00687        if ( rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
00688               goto cleanup;
00689        } else if ( rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
00690               rc = SLAP_CB_CONTINUE;
00691               goto cleanup;
00692        }
00693 
00694        assert( rs->sr_rspoid != NULL );
00695 
00696        send_ldap_extended( op, rs );
00697 
00698 #if 0
00699        slapi_ch_free_string( (char **)&rs->sr_rspoid );
00700 #endif
00701 
00702        if ( rs->sr_rspdata != NULL )
00703               ber_bvfree( rs->sr_rspdata );
00704 
00705        rc = rs->sr_err;
00706 
00707 cleanup:
00708        slapi_pblock_destroy( pb );
00709        op->o_callback = cb.sc_next;
00710 
00711        return rc;
00712 }
00713 
00714 static int
00715 slapi_over_access_allowed(
00716        Operation            *op,
00717        Entry                *e,
00718        AttributeDescription *desc,
00719        struct berval        *val,
00720        slap_access_t        access,
00721        AccessControlState   *state,
00722        slap_mask_t          *maskp )
00723 {
00724        int                  rc;
00725        Slapi_PBlock         *pb;
00726        slap_callback        cb;
00727        int                  internal_op;
00728        SlapReply            rs = { REP_RESULT };
00729 
00730        internal_op = slapi_op_internal_p( op, &rs, &cb );
00731 
00732        cb.sc_response = NULL;
00733        cb.sc_cleanup = NULL;
00734 
00735        pb = SLAPI_OPERATION_PBLOCK( op );
00736 
00737        rc = slapi_int_access_allowed( op, e, desc, val, access, state );
00738        if ( rc ) {
00739               rc = SLAP_CB_CONTINUE;
00740        }
00741 
00742        if ( !internal_op ) {
00743               slapi_pblock_destroy( pb );
00744        }
00745 
00746        op->o_callback = cb.sc_next;
00747 
00748        return rc;
00749 }
00750 
00751 static int
00752 slapi_over_acl_group(
00753        Operation            *op,
00754        Entry                *target,
00755        struct berval        *gr_ndn,
00756        struct berval        *op_ndn,
00757        ObjectClass          *group_oc,
00758        AttributeDescription *group_at )
00759 {
00760        Slapi_Entry          *e;
00761        int                  rc;
00762        Slapi_PBlock         *pb;
00763        BackendDB            *be = op->o_bd;
00764        GroupAssertion              *g;
00765        SlapReply            rs = { REP_RESULT };
00766 
00767        op->o_bd = select_backend( gr_ndn, 0 );
00768 
00769        for ( g = op->o_groups; g; g = g->ga_next ) {
00770               if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
00771                      g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
00772               {
00773                      continue;
00774               }
00775               if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
00776                      break;
00777               }
00778        }
00779        if ( g != NULL ) {
00780               rc = g->ga_res;
00781               goto done;
00782        }
00783 
00784        if ( target != NULL && dn_match( &target->e_nname, gr_ndn ) ) {
00785               e = target;
00786               rc = 0;
00787        } else {
00788               rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
00789        }
00790        if ( e != NULL ) {
00791               int                  internal_op;
00792               slap_callback        cb;
00793 
00794               internal_op = slapi_op_internal_p( op, &rs, &cb );
00795 
00796               cb.sc_response = NULL;
00797               cb.sc_cleanup = NULL;
00798 
00799               pb = SLAPI_OPERATION_PBLOCK( op );
00800 
00801               slapi_pblock_set( pb, SLAPI_X_GROUP_ENTRY,        (void *)e );
00802               slapi_pblock_set( pb, SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
00803               slapi_pblock_set( pb, SLAPI_X_GROUP_ATTRIBUTE,    (void *)group_at->ad_cname.bv_val );
00804               slapi_pblock_set( pb, SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
00805 
00806               rc = slapi_over_call_plugins( pb, SLAPI_X_PLUGIN_PRE_GROUP_FN );
00807               if ( rc >= 0 ) /* 1 means no plugins called */
00808                      rc = SLAP_CB_CONTINUE;
00809               else
00810                      rc = pb->pb_rs->sr_err;
00811 
00812               slapi_pblock_delete_param( pb, SLAPI_X_GROUP_ENTRY );
00813               slapi_pblock_delete_param( pb, SLAPI_X_GROUP_OPERATION_DN );
00814               slapi_pblock_delete_param( pb, SLAPI_X_GROUP_ATTRIBUTE );
00815               slapi_pblock_delete_param( pb, SLAPI_X_GROUP_TARGET_ENTRY );
00816 
00817               if ( !internal_op )
00818                      slapi_pblock_destroy( pb );
00819 
00820               if ( e != target ) {
00821                      be_entry_release_r( op, e );
00822               }
00823 
00824               op->o_callback = cb.sc_next;
00825        } else {
00826               rc = LDAP_NO_SUCH_OBJECT; /* return SLAP_CB_CONTINUE for correctness? */
00827        }
00828 
00829        if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache &&
00830             rc != SLAP_CB_CONTINUE ) {
00831               g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
00832                      op->o_tmpmemctx );
00833               g->ga_be = op->o_bd;
00834               g->ga_oc = group_oc;
00835               g->ga_at = group_at;
00836               g->ga_res = rc;
00837               g->ga_len = gr_ndn->bv_len;
00838               strcpy( g->ga_ndn, gr_ndn->bv_val );
00839               g->ga_next = op->o_groups;
00840               op->o_groups = g;
00841        }
00842        /*
00843         * XXX don't call POST_GROUP_FN, I have no idea what the point of
00844         * that plugin function was anyway
00845         */
00846 done:
00847        op->o_bd = be;
00848        return rc;
00849 }
00850 
00851 static int
00852 slapi_over_db_open(
00853        BackendDB     *be,
00854        ConfigReply   *cr )
00855 {
00856        Slapi_PBlock         *pb;
00857        int                  rc;
00858 
00859        pb = slapi_pblock_new();
00860 
00861        rc = slapi_int_call_plugins( be, SLAPI_PLUGIN_START_FN, pb );
00862 
00863        slapi_pblock_destroy( pb );
00864 
00865        return rc;
00866 }
00867 
00868 static int
00869 slapi_over_db_close(
00870        BackendDB     *be,
00871        ConfigReply   *cr )
00872 {
00873        Slapi_PBlock         *pb;
00874        int                  rc;
00875 
00876        pb = slapi_pblock_new();
00877 
00878        rc = slapi_int_call_plugins( be, SLAPI_PLUGIN_CLOSE_FN, pb );
00879 
00880        slapi_pblock_destroy( pb );
00881 
00882        return rc;
00883 }
00884 
00885 static int
00886 slapi_over_init()
00887 {
00888        memset( &slapi, 0, sizeof(slapi) );
00889 
00890        slapi.on_bi.bi_type = SLAPI_OVERLAY_NAME;
00891 
00892        slapi.on_bi.bi_op_bind             = slapi_op_func;
00893        slapi.on_bi.bi_op_unbind    = slapi_op_func;
00894        slapi.on_bi.bi_op_search    = slapi_op_func;
00895        slapi.on_bi.bi_op_compare   = slapi_op_func;
00896        slapi.on_bi.bi_op_modify    = slapi_op_func;
00897        slapi.on_bi.bi_op_modrdn    = slapi_op_func;
00898        slapi.on_bi.bi_op_add              = slapi_op_func;
00899        slapi.on_bi.bi_op_delete    = slapi_op_func;
00900        slapi.on_bi.bi_op_abandon   = slapi_op_func;
00901        slapi.on_bi.bi_op_cancel    = slapi_op_func;
00902 
00903        slapi.on_bi.bi_db_open             = slapi_over_db_open;
00904        slapi.on_bi.bi_db_close            = slapi_over_db_close;
00905 
00906        slapi.on_bi.bi_extended            = slapi_over_extended;
00907        slapi.on_bi.bi_access_allowed      = slapi_over_access_allowed;
00908        slapi.on_bi.bi_operational  = slapi_over_aux_operational;
00909        slapi.on_bi.bi_acl_group    = slapi_over_acl_group;
00910 
00911        return overlay_register( &slapi );
00912 }
00913 
00914 int slapi_over_is_inst( BackendDB *be )
00915 {
00916        return overlay_is_inst( be, SLAPI_OVERLAY_NAME );
00917 }
00918 
00919 int slapi_over_config( BackendDB *be, ConfigReply *cr )
00920 {
00921        if ( slapi_over_initialized == 0 ) {
00922               int rc;
00923 
00924               /* do global initializaiton */
00925               ldap_pvt_thread_mutex_init( &slapi_hn_mutex );
00926               ldap_pvt_thread_mutex_init( &slapi_time_mutex );
00927               ldap_pvt_thread_mutex_init( &slapi_printmessage_mutex );
00928 
00929               if ( slapi_log_file == NULL )
00930                      slapi_log_file = slapi_ch_strdup( LDAP_RUNDIR LDAP_DIRSEP "errors" );
00931 
00932               rc = slapi_int_init_object_extensions();
00933               if ( rc != 0 )
00934                      return rc;
00935 
00936               rc = slapi_over_init();
00937               if ( rc != 0 )
00938                      return rc;
00939 
00940               slapi_over_initialized = 1;
00941        }
00942 
00943        return overlay_config( be, SLAPI_OVERLAY_NAME, -1, NULL, cr );
00944 }
00945 
00946 #endif /* LDAP_SLAPI */