Back to index

openldap  2.4.31
backglue.c
Go to the documentation of this file.
00001 /* backglue.c - backend glue */
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 
00017 /*
00018  * Functions to glue a bunch of other backends into a single tree.
00019  * All of the glued backends must share a common suffix. E.g., you
00020  * can glue o=foo and ou=bar,o=foo but you can't glue o=foo and o=bar.
00021  *
00022  * The purpose of these functions is to allow you to split a single database
00023  * into pieces (for load balancing purposes, whatever) but still be able
00024  * to treat it as a single database after it's been split. As such, each
00025  * of the glued backends should have identical rootdn.
00026  *  -- Howard Chu
00027  */
00028 
00029 #include "portable.h"
00030 
00031 #include <stdio.h>
00032 
00033 #include <ac/string.h>
00034 #include <ac/socket.h>
00035 
00036 #define SLAPD_TOOLS
00037 #include "slap.h"
00038 #include "lutil.h"
00039 #include "config.h"
00040 
00041 typedef struct gluenode {
00042        BackendDB *gn_be;
00043        struct berval gn_pdn;
00044 } gluenode;
00045 
00046 typedef struct glueinfo {
00047        int gi_nodes;
00048        struct berval gi_pdn;
00049        gluenode gi_n[1];
00050 } glueinfo;
00051 
00052 static slap_overinst glue;
00053 
00054 static int glueMode;
00055 static BackendDB *glueBack;
00056 static BackendDB glueBackDone;
00057 #define GLUEBACK_DONE (&glueBackDone)
00058 
00059 static slap_overinst * glue_tool_inst( BackendInfo *bi);
00060 
00061 static slap_response glue_op_response;
00062 
00063 /* Just like select_backend, but only for our backends */
00064 static BackendDB *
00065 glue_back_select (
00066        BackendDB *be,
00067        struct berval *dn
00068 )
00069 {
00070        slap_overinst *on = (slap_overinst *)be->bd_info;
00071        glueinfo             *gi = (glueinfo *)on->on_bi.bi_private;
00072        int i;
00073 
00074        for (i = gi->gi_nodes-1; i >= 0; i--) {
00075               assert( gi->gi_n[i].gn_be->be_nsuffix != NULL );
00076 
00077               if (dnIsSuffix(dn, &gi->gi_n[i].gn_be->be_nsuffix[0])) {
00078                      return gi->gi_n[i].gn_be;
00079               }
00080        }
00081        be->bd_info = on->on_info->oi_orig;
00082        return be;
00083 }
00084 
00085 
00086 typedef struct glue_state {
00087        char *matched;
00088        BerVarray refs;
00089        LDAPControl **ctrls;
00090        int err;
00091        int matchlen;
00092        int nrefs;
00093        int nctrls;
00094 } glue_state;
00095 
00096 static int
00097 glue_op_cleanup( Operation *op, SlapReply *rs )
00098 {
00099        /* This is not a final result */
00100        if (rs->sr_type == REP_RESULT )
00101               rs->sr_type = REP_GLUE_RESULT;
00102        return SLAP_CB_CONTINUE;
00103 }
00104 
00105 static int
00106 glue_op_response ( Operation *op, SlapReply *rs )
00107 {
00108        glue_state *gs = op->o_callback->sc_private;
00109 
00110        switch(rs->sr_type) {
00111        case REP_SEARCH:
00112        case REP_SEARCHREF:
00113        case REP_INTERMEDIATE:
00114               return SLAP_CB_CONTINUE;
00115 
00116        default:
00117               if (rs->sr_err == LDAP_SUCCESS ||
00118                      rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
00119                      rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
00120                      rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
00121                      rs->sr_err == LDAP_NO_SUCH_OBJECT ||
00122                      gs->err != LDAP_SUCCESS)
00123                      gs->err = rs->sr_err;
00124               if (gs->err == LDAP_SUCCESS && gs->matched) {
00125                      ch_free (gs->matched);
00126                      gs->matched = NULL;
00127                      gs->matchlen = 0;
00128               }
00129               if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
00130                      int len;
00131                      len = strlen (rs->sr_matched);
00132                      if (len > gs->matchlen) {
00133                             if (gs->matched)
00134                                    ch_free (gs->matched);
00135                             gs->matched = ch_strdup (rs->sr_matched);
00136                             gs->matchlen = len;
00137                      }
00138               }
00139               if (rs->sr_ref) {
00140                      int i, j, k;
00141                      BerVarray new;
00142 
00143                      for (i=0; rs->sr_ref[i].bv_val; i++);
00144 
00145                      j = gs->nrefs;
00146                      if (!j) {
00147                             new = ch_malloc ((i+1)*sizeof(struct berval));
00148                      } else {
00149                             new = ch_realloc(gs->refs,
00150                                    (j+i+1)*sizeof(struct berval));
00151                      }
00152                      for (k=0; k<i; j++,k++) {
00153                             ber_dupbv( &new[j], &rs->sr_ref[k] );
00154                      }
00155                      new[j].bv_val = NULL;
00156                      gs->nrefs = j;
00157                      gs->refs = new;
00158               }
00159               if (rs->sr_ctrls) {
00160                      int i, j, k;
00161                      LDAPControl **newctrls;
00162 
00163                      for (i=0; rs->sr_ctrls[i]; i++);
00164 
00165                      j = gs->nctrls;
00166                      if (!j) {
00167                             newctrls = op->o_tmpalloc((i+1)*sizeof(LDAPControl *),
00168                                    op->o_tmpmemctx);
00169                      } else {
00170                             /* Forget old pagedResults response if we're sending
00171                              * a new one now
00172                              */
00173                             if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
00174                                    int newpage = 0;
00175                                    for ( k=0; k<i; k++ ) {
00176                                           if ( !strcmp(rs->sr_ctrls[k]->ldctl_oid,
00177                                                  LDAP_CONTROL_PAGEDRESULTS )) {
00178                                                  newpage = 1;
00179                                                  break;
00180                                           }
00181                                    }
00182                                    if ( newpage ) {
00183                                           for ( k=0; k<j; k++ ) {
00184                                                  if ( !strcmp(gs->ctrls[k]->ldctl_oid,
00185                                                         LDAP_CONTROL_PAGEDRESULTS ))
00186                                                  {
00187                                                         op->o_tmpfree(gs->ctrls[k], op->o_tmpmemctx);
00188                                                         gs->ctrls[k] = gs->ctrls[--j];
00189                                                         gs->ctrls[j] = NULL;
00190                                                         break;
00191                                                  }
00192                                           }
00193                                    }
00194                             }
00195                             newctrls = op->o_tmprealloc(gs->ctrls,
00196                                    (j+i+1)*sizeof(LDAPControl *), op->o_tmpmemctx);
00197                      }
00198                      for (k=0; k<i; j++,k++) {
00199                             ber_len_t oidlen = strlen( rs->sr_ctrls[k]->ldctl_oid );
00200                             newctrls[j] = op->o_tmpalloc(sizeof(LDAPControl) + oidlen + 1 + rs->sr_ctrls[k]->ldctl_value.bv_len + 1,
00201                                    op->o_tmpmemctx);
00202                             newctrls[j]->ldctl_iscritical = rs->sr_ctrls[k]->ldctl_iscritical;
00203                             newctrls[j]->ldctl_oid = (char *)&newctrls[j][1];
00204                             lutil_strcopy( newctrls[j]->ldctl_oid, rs->sr_ctrls[k]->ldctl_oid );
00205                             if ( !BER_BVISNULL( &rs->sr_ctrls[k]->ldctl_value ) ) {
00206                                    newctrls[j]->ldctl_value.bv_val = &newctrls[j]->ldctl_oid[oidlen + 1];
00207                                    newctrls[j]->ldctl_value.bv_len = rs->sr_ctrls[k]->ldctl_value.bv_len;
00208                                    lutil_memcopy( newctrls[j]->ldctl_value.bv_val,
00209                                           rs->sr_ctrls[k]->ldctl_value.bv_val,
00210                                           rs->sr_ctrls[k]->ldctl_value.bv_len + 1 );
00211                             } else {
00212                                    BER_BVZERO( &newctrls[j]->ldctl_value );
00213                             }
00214                      }
00215                      newctrls[j] = NULL;
00216                      gs->nctrls = j;
00217                      gs->ctrls = newctrls;
00218               }
00219        }
00220        return 0;
00221 }
00222 
00223 static int
00224 glue_op_func ( Operation *op, SlapReply *rs )
00225 {
00226        slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
00227        BackendDB *b0 = op->o_bd;
00228        BackendInfo *bi0 = op->o_bd->bd_info;
00229        BI_op_modify **func;
00230        slap_operation_t which = op_bind;
00231        int rc;
00232 
00233        op->o_bd = glue_back_select (b0, &op->o_req_ndn);
00234 
00235        /* If we're on the master backend, let overlay framework handle it */
00236        if ( op->o_bd == b0 )
00237               return SLAP_CB_CONTINUE;
00238 
00239        b0->bd_info = on->on_info->oi_orig;
00240 
00241        switch(op->o_tag) {
00242        case LDAP_REQ_ADD: which = op_add; break;
00243        case LDAP_REQ_DELETE: which = op_delete; break;
00244        case LDAP_REQ_MODIFY: which = op_modify; break;
00245        case LDAP_REQ_MODRDN: which = op_modrdn; break;
00246        case LDAP_REQ_EXTENDED: which = op_extended; break;
00247        default: assert( 0 ); break;
00248        }
00249 
00250        func = &op->o_bd->bd_info->bi_op_bind;
00251        if ( func[which] )
00252               rc = func[which]( op, rs );
00253        else
00254               rc = SLAP_CB_BYPASS;
00255 
00256        op->o_bd = b0;
00257        op->o_bd->bd_info = bi0;
00258        return rc;
00259 }
00260 
00261 static int
00262 glue_op_abandon( Operation *op, SlapReply *rs )
00263 {
00264        slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
00265        glueinfo             *gi = (glueinfo *)on->on_bi.bi_private;
00266        BackendDB *b0 = op->o_bd;
00267        BackendInfo *bi0 = op->o_bd->bd_info;
00268        int i;
00269 
00270        b0->bd_info = on->on_info->oi_orig;
00271 
00272        for (i = gi->gi_nodes-1; i >= 0; i--) {
00273               assert( gi->gi_n[i].gn_be->be_nsuffix != NULL );
00274               op->o_bd = gi->gi_n[i].gn_be;
00275               if ( op->o_bd == b0 )
00276                      continue;
00277               if ( op->o_bd->bd_info->bi_op_abandon )
00278                      op->o_bd->bd_info->bi_op_abandon( op, rs );
00279        }
00280        op->o_bd = b0;
00281        op->o_bd->bd_info = bi0;
00282        return SLAP_CB_CONTINUE;
00283 }
00284 
00285 static int
00286 glue_response ( Operation *op, SlapReply *rs )
00287 {
00288        BackendDB *be = op->o_bd;
00289        be = glue_back_select (op->o_bd, &op->o_req_ndn);
00290 
00291        /* If we're on the master backend, let overlay framework handle it.
00292         * Otherwise, bail out.
00293         */
00294        return ( op->o_bd == be ) ? SLAP_CB_CONTINUE : SLAP_CB_BYPASS;
00295 }
00296 
00297 static int
00298 glue_chk_referrals ( Operation *op, SlapReply *rs )
00299 {
00300        slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
00301        BackendDB *b0 = op->o_bd;
00302        BackendInfo *bi0 = op->o_bd->bd_info;
00303        int rc;
00304 
00305        op->o_bd = glue_back_select (b0, &op->o_req_ndn);
00306        if ( op->o_bd == b0 )
00307               return SLAP_CB_CONTINUE;
00308 
00309        b0->bd_info = on->on_info->oi_orig;
00310 
00311        if ( op->o_bd->bd_info->bi_chk_referrals )
00312               rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
00313        else
00314               rc = SLAP_CB_CONTINUE;
00315 
00316        op->o_bd = b0;
00317        op->o_bd->bd_info = bi0;
00318        return rc;
00319 }
00320 
00321 static int
00322 glue_chk_controls ( Operation *op, SlapReply *rs )
00323 {
00324        slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
00325        BackendDB *b0 = op->o_bd;
00326        BackendInfo *bi0 = op->o_bd->bd_info;
00327        int rc = SLAP_CB_CONTINUE;
00328 
00329        op->o_bd = glue_back_select (b0, &op->o_req_ndn);
00330        if ( op->o_bd == b0 )
00331               return SLAP_CB_CONTINUE;
00332 
00333        b0->bd_info = on->on_info->oi_orig;
00334 
00335        /* if the subordinate database has overlays, the bi_chk_controls()
00336         * hook is actually over_aux_chk_controls(); in case it actually
00337         * wraps a missing hok, we need to mimic the behavior
00338         * of the frontend applied to that database */
00339        if ( op->o_bd->bd_info->bi_chk_controls ) {
00340               rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
00341        }
00342 
00343        
00344        if ( rc == SLAP_CB_CONTINUE ) {
00345               rc = backend_check_controls( op, rs );
00346        }
00347 
00348        op->o_bd = b0;
00349        op->o_bd->bd_info = bi0;
00350        return rc;
00351 }
00352 
00353 /* ITS#4615 - overlays configured above the glue overlay should be
00354  * invoked for the entire glued tree. Overlays configured below the
00355  * glue overlay should only be invoked on the master backend.
00356  * So, if we're searching on any subordinates, we need to force the
00357  * current overlay chain to stop processing, without stopping the
00358  * overall callback flow.
00359  */
00360 static int
00361 glue_sub_search( Operation *op, SlapReply *rs, BackendDB *b0,
00362        slap_overinst *on )
00363 {
00364        /* Process any overlays on the master backend */
00365        if ( op->o_bd == b0 && on->on_next ) {
00366               BackendInfo *bi = op->o_bd->bd_info;
00367               int rc = SLAP_CB_CONTINUE;
00368               for ( on=on->on_next; on; on=on->on_next ) {
00369                      op->o_bd->bd_info = (BackendInfo *)on;
00370                      if ( on->on_bi.bi_op_search ) {
00371                             rc = on->on_bi.bi_op_search( op, rs );
00372                             if ( rc != SLAP_CB_CONTINUE )
00373                                    break;
00374                      }
00375               }
00376               op->o_bd->bd_info = bi;
00377               if ( rc != SLAP_CB_CONTINUE )
00378                      return rc;
00379        }
00380        return op->o_bd->be_search( op, rs );
00381 }
00382 
00383 static const ID glueID = NOID;
00384 static const struct berval gluecookie = { sizeof( glueID ), (char *)&glueID };
00385 
00386 static int
00387 glue_op_search ( Operation *op, SlapReply *rs )
00388 {
00389        slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
00390        glueinfo             *gi = (glueinfo *)on->on_bi.bi_private;
00391        BackendDB *b0 = op->o_bd;
00392        BackendDB *b1 = NULL, *btmp;
00393        BackendInfo *bi0 = op->o_bd->bd_info;
00394        int i;
00395        long stoptime = 0, starttime;
00396        glue_state gs = {NULL, NULL, NULL, 0, 0, 0, 0};
00397        slap_callback cb = { NULL, glue_op_response, glue_op_cleanup, NULL };
00398        int scope0, tlimit0;
00399        struct berval dn, ndn, *pdn;
00400 
00401        cb.sc_private = &gs;
00402 
00403        cb.sc_next = op->o_callback;
00404 
00405        starttime = op->o_time;
00406        stoptime = slap_get_time () + op->ors_tlimit;
00407 
00408        /* reset dummy cookie used to keep paged results going across databases */
00409        if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED
00410               && bvmatch( &((PagedResultsState *)op->o_pagedresults_state)->ps_cookieval, &gluecookie ) )
00411        {
00412               PagedResultsState *ps = op->o_pagedresults_state;
00413               BerElementBuffer berbuf;
00414               BerElement *ber = (BerElement *)&berbuf;
00415               struct berval cookie = BER_BVC(""), value;
00416               int c;
00417 
00418               for (c = 0; op->o_ctrls[c] != NULL; c++) {
00419                      if (strcmp(op->o_ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0)
00420                             break;
00421               }
00422 
00423               assert( op->o_ctrls[c] != NULL );
00424 
00425               ber_init2( ber, NULL, LBER_USE_DER );
00426               ber_printf( ber, "{iO}", ps->ps_size, &cookie );
00427               ber_flatten2( ber, &value, 0 );
00428               assert( op->o_ctrls[c]->ldctl_value.bv_len >= value.bv_len );
00429               op->o_ctrls[c]->ldctl_value.bv_len = value.bv_len;
00430               lutil_memcopy( op->o_ctrls[c]->ldctl_value.bv_val,
00431                      value.bv_val, value.bv_len );
00432               ber_free_buf( ber );
00433 
00434               ps->ps_cookie = (PagedResultsCookie)0;
00435               BER_BVZERO( &ps->ps_cookieval );
00436        }
00437 
00438        op->o_bd = glue_back_select (b0, &op->o_req_ndn);
00439        b0->bd_info = on->on_info->oi_orig;
00440 
00441        switch (op->ors_scope) {
00442        case LDAP_SCOPE_BASE:
00443               if ( op->o_bd == b0 )
00444                      return SLAP_CB_CONTINUE;
00445 
00446               if (op->o_bd && op->o_bd->be_search) {
00447                      rs->sr_err = op->o_bd->be_search( op, rs );
00448               } else {
00449                      rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
00450               }
00451               return rs->sr_err;
00452 
00453        case LDAP_SCOPE_ONELEVEL:
00454        case LDAP_SCOPE_SUBTREE:
00455        case LDAP_SCOPE_SUBORDINATE: /* FIXME */
00456               op->o_callback = &cb;
00457               rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
00458               scope0 = op->ors_scope;
00459               tlimit0 = op->ors_tlimit;
00460               dn = op->o_req_dn;
00461               ndn = op->o_req_ndn;
00462               b1 = op->o_bd;
00463 
00464               /*
00465                * Execute in reverse order, most specific first 
00466                */
00467               for (i = gi->gi_nodes; i >= 0; i--) {
00468                      if ( i == gi->gi_nodes ) {
00469                             btmp = b0;
00470                             pdn = &gi->gi_pdn;
00471                      } else {
00472                             btmp = gi->gi_n[i].gn_be;
00473                             pdn = &gi->gi_n[i].gn_pdn;
00474                      }
00475                      if (!btmp || !btmp->be_search)
00476                             continue;
00477                      if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
00478                             continue;
00479                      if (get_no_subordinate_glue(op) && btmp != b1)
00480                             continue;
00481                      /* If we remembered which backend we were on before,
00482                       * skip down to it now
00483                       */
00484                      if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED &&
00485                             op->o_conn->c_pagedresults_state.ps_be &&
00486                             op->o_conn->c_pagedresults_state.ps_be != btmp )
00487                             continue;
00488 
00489                      if (tlimit0 != SLAP_NO_LIMIT) {
00490                             op->o_time = slap_get_time();
00491                             op->ors_tlimit = stoptime - op->o_time;
00492                             if (op->ors_tlimit <= 0) {
00493                                    rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
00494                                    break;
00495                             }
00496                      }
00497                      rs->sr_err = 0;
00498                      /*
00499                       * check for abandon 
00500                       */
00501                      if (op->o_abandon) {
00502                             goto end_of_loop;
00503                      }
00504                      op->o_bd = btmp;
00505 
00506                      assert( op->o_bd->be_suffix != NULL );
00507                      assert( op->o_bd->be_nsuffix != NULL );
00508                      
00509                      if (scope0 == LDAP_SCOPE_ONELEVEL && 
00510                             dn_match(pdn, &ndn))
00511                      {
00512                             struct berval mdn, mndn;
00513                             op->ors_scope = LDAP_SCOPE_BASE;
00514                             mdn = op->o_req_dn = op->o_bd->be_suffix[0];
00515                             mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
00516                             rs->sr_err = op->o_bd->be_search(op, rs);
00517                             if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
00518                                    gs.err = LDAP_SUCCESS;
00519                             }
00520                             op->ors_scope = LDAP_SCOPE_ONELEVEL;
00521                             if ( op->o_req_dn.bv_val == mdn.bv_val )
00522                                    op->o_req_dn = dn;
00523                             if ( op->o_req_ndn.bv_val == mndn.bv_val )
00524                                    op->o_req_ndn = ndn;
00525 
00526                      } else if (scope0 == LDAP_SCOPE_SUBTREE &&
00527                             dn_match(&op->o_bd->be_nsuffix[0], &ndn))
00528                      {
00529                             rs->sr_err = glue_sub_search( op, rs, b0, on );
00530 
00531                      } else if (scope0 == LDAP_SCOPE_SUBTREE &&
00532                             dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
00533                      {
00534                             struct berval mdn, mndn;
00535                             mdn = op->o_req_dn = op->o_bd->be_suffix[0];
00536                             mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
00537                             rs->sr_err = glue_sub_search( op, rs, b0, on );
00538                             if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
00539                                    gs.err = LDAP_SUCCESS;
00540                             }
00541                             if ( op->o_req_dn.bv_val == mdn.bv_val )
00542                                    op->o_req_dn = dn;
00543                             if ( op->o_req_ndn.bv_val == mndn.bv_val )
00544                                    op->o_req_ndn = ndn;
00545 
00546                      } else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
00547                             rs->sr_err = glue_sub_search( op, rs, b0, on );
00548                      }
00549 
00550                      switch ( gs.err ) {
00551 
00552                      /*
00553                       * Add errors that should result in dropping
00554                       * the search
00555                       */
00556                      case LDAP_SIZELIMIT_EXCEEDED:
00557                      case LDAP_TIMELIMIT_EXCEEDED:
00558                      case LDAP_ADMINLIMIT_EXCEEDED:
00559                      case LDAP_NO_SUCH_OBJECT:
00560 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
00561                      case LDAP_X_CANNOT_CHAIN:
00562 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
00563                             goto end_of_loop;
00564 
00565                      case LDAP_SUCCESS:
00566                             if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
00567                                    PagedResultsState *ps = op->o_pagedresults_state;
00568 
00569                                    /* Assume this backend can be forgotten now */
00570                                    op->o_conn->c_pagedresults_state.ps_be = NULL;
00571 
00572                                    /* If we have a full page, exit the loop. We may
00573                                     * need to remember this backend so we can continue
00574                                     * from here on a subsequent request.
00575                                     */
00576                                    if ( rs->sr_nentries >= ps->ps_size ) {
00577                                           PagedResultsState *cps = &op->o_conn->c_pagedresults_state;
00578                                           
00579                                           /* Don't bother to remember the first backend.
00580                                            * Only remember the last one if there's more state left.
00581                                            */
00582                                           if ( op->o_bd != b0 &&
00583                                                  ( cps->ps_cookie != NOID
00584                                                         || !BER_BVISNULL( &cps->ps_cookieval )
00585                                                         || op->o_bd != gi->gi_n[0].gn_be ) )
00586                                           {
00587                                                  op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
00588                                           }
00589 
00590                                           /* Check whether the cookie is empty,
00591                                            * and give remaining databases a chance
00592                                            */
00593                                           if ( op->o_bd != gi->gi_n[0].gn_be || cps->ps_cookie == NOID ) {
00594                                                  int           c;
00595 
00596                                                  for ( c = 0; gs.ctrls[c] != NULL; c++ ) {
00597                                                         if ( strcmp( gs.ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 ) {
00598                                                                break;
00599                                                         }
00600                                                  }
00601 
00602                                                  if ( gs.ctrls[c] != NULL ) {
00603                                                         BerElementBuffer berbuf;
00604                                                         BerElement    *ber = (BerElement *)&berbuf;
00605                                                         ber_tag_t     tag;
00606                                                         ber_int_t     size;
00607                                                         struct berval cookie, value;
00608                                                  
00609                                                         ber_init2( ber, &gs.ctrls[c]->ldctl_value, LBER_USE_DER );
00610 
00611                                                         tag = ber_scanf( ber, "{im}", &size, &cookie );
00612                                                         assert( tag != LBER_ERROR );
00613 
00614                                                         if ( BER_BVISEMPTY( &cookie ) && op->o_bd != gi->gi_n[0].gn_be ) {
00615                                                                /* delete old, create new cookie with NOID */
00616                                                                PagedResultsCookie respcookie = (PagedResultsCookie)NOID;
00617                                                                ber_len_t oidlen = strlen( gs.ctrls[c]->ldctl_oid );
00618                                                                LDAPControl *newctrl;
00619 
00620                                                                /* it's next database's turn */
00621                                                                if ( btmp == b0 ) {
00622                                                                       op->o_conn->c_pagedresults_state.ps_be = gi->gi_n[gi->gi_nodes - 1].gn_be;
00623 
00624                                                                } else {
00625                                                                       op->o_conn->c_pagedresults_state.ps_be = gi->gi_n[(i > 0 ? i - 1: 0)].gn_be;
00626                                                                }
00627 
00628                                                                cookie.bv_val = (char *)&respcookie;
00629                                                                cookie.bv_len = sizeof( PagedResultsCookie );
00630 
00631                                                                ber_init2( ber, NULL, LBER_USE_DER );
00632                                                                ber_printf( ber, "{iO}", 0, &cookie );
00633                                                                ber_flatten2( ber, &value, 0 );
00634 
00635                                                                newctrl = op->o_tmprealloc( gs.ctrls[c],
00636                                                                       sizeof(LDAPControl) + oidlen + 1 + value.bv_len + 1,
00637                                                                       op->o_tmpmemctx);
00638                                                                newctrl->ldctl_iscritical = gs.ctrls[c]->ldctl_iscritical;
00639                                                                newctrl->ldctl_oid = (char *)&newctrl[1];
00640                                                                lutil_strcopy( newctrl->ldctl_oid, gs.ctrls[c]->ldctl_oid );
00641                                                                newctrl->ldctl_value.bv_len = value.bv_len;
00642                                                                lutil_memcopy( newctrl->ldctl_value.bv_val,
00643                                                                       value.bv_val, value.bv_len );
00644 
00645                                                                gs.ctrls[c] = newctrl;
00646 
00647                                                                ber_free_buf( ber );
00648 
00649                                                         } else if ( !BER_BVISEMPTY( &cookie ) && op->o_bd != b0 ) {
00650                                                                /* if cookie not empty, it's again this database's turn */
00651                                                                op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
00652                                                         }
00653                                                  }
00654                                           }
00655 
00656                                           goto end_of_loop;
00657                                    }
00658 
00659                                    /* This backend has run out of entries, but more responses
00660                                     * can fit in the page. Fake a reset of the state so the
00661                                     * next backend will start up properly. Only back-[bh]db
00662                                     * and back-sql look at this state info.
00663                                     */
00664                                    ps->ps_cookie = (PagedResultsCookie)0;
00665                                    BER_BVZERO( &ps->ps_cookieval );
00666 
00667                                    {
00668                                           /* change the size of the page in the request
00669                                            * that will be propagated, and reset the cookie */
00670                                           BerElementBuffer berbuf;
00671                                           BerElement *ber = (BerElement *)&berbuf;
00672                                           int size = ps->ps_size - rs->sr_nentries;
00673                                           struct berval cookie = BER_BVC(""), value;
00674                                           int c;
00675 
00676                                           for (c = 0; op->o_ctrls[c] != NULL; c++) {
00677                                                  if (strcmp(op->o_ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0)
00678                                                         break;
00679                                           }
00680 
00681                                           assert( op->o_ctrls[c] != NULL );
00682 
00683                                           ber_init2( ber, NULL, LBER_USE_DER );
00684                                           ber_printf( ber, "{iO}", size, &cookie );
00685                                           ber_flatten2( ber, &value, 0 );
00686                                           assert( op->o_ctrls[c]->ldctl_value.bv_len >= value.bv_len );
00687                                           op->o_ctrls[c]->ldctl_value.bv_len = value.bv_len;
00688                                           lutil_memcopy( op->o_ctrls[c]->ldctl_value.bv_val,
00689                                                  value.bv_val, value.bv_len );
00690                                           ber_free_buf( ber );
00691                                    }
00692                             }
00693                             
00694                      default:
00695                             break;
00696                      }
00697               }
00698 end_of_loop:;
00699               op->ors_scope = scope0;
00700               op->ors_tlimit = tlimit0;
00701               op->o_time = starttime;
00702 
00703               break;
00704        }
00705 
00706        op->o_callback = cb.sc_next;
00707        if ( op->o_abandon ) {
00708               rs->sr_err = SLAPD_ABANDON;
00709        } else {
00710               rs->sr_err = gs.err;
00711               rs->sr_matched = gs.matched;
00712               rs->sr_ref = gs.refs;
00713        }
00714        rs->sr_ctrls = gs.ctrls;
00715 
00716        send_ldap_result( op, rs );
00717 
00718        op->o_bd = b0;
00719        op->o_bd->bd_info = bi0;
00720        if (gs.matched)
00721               free (gs.matched);
00722        if (gs.refs)
00723               ber_bvarray_free(gs.refs);
00724        if (gs.ctrls) {
00725               for (i = gs.nctrls; --i >= 0; ) {
00726                      op->o_tmpfree(gs.ctrls[i], op->o_tmpmemctx);
00727               }
00728               op->o_tmpfree(gs.ctrls, op->o_tmpmemctx);
00729        }
00730        return rs->sr_err;
00731 }
00732 
00733 static BackendDB toolDB;
00734 
00735 static int
00736 glue_tool_entry_open (
00737        BackendDB *b0,
00738        int mode
00739 )
00740 {
00741        slap_overinfo *oi = (slap_overinfo *)b0->bd_info;
00742 
00743        /* We don't know which backend to talk to yet, so just
00744         * remember the mode and move on...
00745         */
00746 
00747        glueMode = mode;
00748        glueBack = NULL;
00749        toolDB = *b0;
00750        toolDB.bd_info = oi->oi_orig;
00751 
00752        /* Sanity checks */
00753        {
00754               slap_overinst *on = glue_tool_inst( b0->bd_info );
00755               glueinfo      *gi = on->on_bi.bi_private;
00756 
00757               int i;
00758               for (i = 0; i < gi->gi_nodes; i++) {
00759                      BackendDB *bd;
00760                      struct berval pdn;
00761        
00762                      dnParent( &gi->gi_n[i].gn_be->be_nsuffix[0], &pdn );
00763                      bd = select_backend( &pdn, 0 );
00764                      if ( bd ) {
00765                             ID id;
00766                             BackendDB db;
00767        
00768                             if ( overlay_is_over( bd ) ) {
00769                                    slap_overinfo *oi = (slap_overinfo *)bd->bd_info;
00770                                    db = *bd;
00771                                    db.bd_info = oi->oi_orig;
00772                                    bd = &db;
00773                             }
00774        
00775                             if ( !bd->bd_info->bi_tool_dn2id_get
00776                                    || !bd->bd_info->bi_tool_entry_open
00777                                    || !bd->bd_info->bi_tool_entry_close )
00778                             {
00779                                    continue;
00780                             }
00781        
00782                             bd->bd_info->bi_tool_entry_open( bd, 0 );
00783                             id = bd->bd_info->bi_tool_dn2id_get( bd, &gi->gi_n[i].gn_be->be_nsuffix[0] );
00784                             bd->bd_info->bi_tool_entry_close( bd );
00785                             if ( id != NOID ) {
00786                                    Debug( LDAP_DEBUG_ANY,
00787                                           "glue_tool_entry_open: subordinate database suffix entry DN=\"%s\" also present in superior database rooted at DN=\"%s\"\n",
00788                                           gi->gi_n[i].gn_be->be_suffix[0].bv_val, bd->be_suffix[0].bv_val, 0 );
00789                                    return LDAP_OTHER;
00790                             }
00791                      }
00792               }
00793        }
00794        
00795        return 0;
00796 }
00797 
00798 static int
00799 glue_tool_entry_close (
00800        BackendDB *b0
00801 )
00802 {
00803        int rc = 0;
00804 
00805        if (glueBack && glueBack != GLUEBACK_DONE) {
00806               if (!glueBack->be_entry_close)
00807                      return 0;
00808               rc = glueBack->be_entry_close (glueBack);
00809        }
00810        return rc;
00811 }
00812 
00813 static slap_overinst *
00814 glue_tool_inst(
00815        BackendInfo *bi
00816 )
00817 {
00818        slap_overinfo *oi = (slap_overinfo *)bi;
00819        slap_overinst *on;
00820 
00821        for ( on = oi->oi_list; on; on=on->on_next ) {
00822               if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
00823                      return on;
00824        }
00825        return NULL;
00826 }
00827 
00828 /* This function will only be called in tool mode */
00829 static int
00830 glue_open (
00831        BackendInfo *bi
00832 )
00833 {
00834        slap_overinst *on = glue_tool_inst( bi );
00835        glueinfo             *gi = on->on_bi.bi_private;
00836        static int glueOpened = 0;
00837        int i, j, same, bsame = 0, rc = 0;
00838        ConfigReply cr = {0};
00839 
00840        if (glueOpened) return 0;
00841 
00842        glueOpened = 1;
00843 
00844        /* If we were invoked in tool mode, open all the underlying backends */
00845        if (slapMode & SLAP_TOOL_MODE) {
00846               for (i = 0; i<gi->gi_nodes; i++) {
00847                      same = 0;
00848                      /* Same bi_open as our main backend? */
00849                      if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
00850                             on->on_info->oi_orig->bi_open )
00851                             bsame = 1;
00852 
00853                      /* Loop thru the bd_info's and make sure we only
00854                       * invoke their bi_open functions once each.
00855                       */
00856                      for ( j = 0; j<i; j++ ) {
00857                             if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
00858                                    gi->gi_n[j].gn_be->bd_info->bi_open ) {
00859                                    same = 1;
00860                                    break;
00861                             }
00862                      }
00863                      /* OK, it's unique and non-NULL, call it. */
00864                      if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
00865                             rc = gi->gi_n[i].gn_be->bd_info->bi_open(
00866                                    gi->gi_n[i].gn_be->bd_info );
00867                      /* Let backend.c take care of the rest of startup */
00868                      if ( !rc )
00869                             rc = backend_startup_one( gi->gi_n[i].gn_be, &cr );
00870                      if ( rc ) break;
00871               }
00872               if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
00873                      rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
00874 
00875        } /* other case is impossible */
00876        return rc;
00877 }
00878 
00879 /* This function will only be called in tool mode */
00880 static int
00881 glue_close (
00882        BackendInfo *bi
00883 )
00884 {
00885        static int glueClosed = 0;
00886        int rc = 0;
00887 
00888        if (glueClosed) return 0;
00889 
00890        glueClosed = 1;
00891 
00892        if (slapMode & SLAP_TOOL_MODE) {
00893               rc = backend_shutdown( NULL );
00894        }
00895        return rc;
00896 }
00897 
00898 static int
00899 glue_entry_get_rw (
00900        Operation            *op,
00901        struct berval *dn,
00902        ObjectClass          *oc,
00903        AttributeDescription *ad,
00904        int    rw,
00905        Entry  **e )
00906 {
00907        int rc;
00908        BackendDB *b0 = op->o_bd;
00909        op->o_bd = glue_back_select( b0, dn );
00910 
00911        if ( op->o_bd->be_fetch ) {
00912               rc = op->o_bd->be_fetch( op, dn, oc, ad, rw, e );
00913        } else {
00914               rc = LDAP_UNWILLING_TO_PERFORM;
00915        }
00916        op->o_bd =b0;
00917        return rc;
00918 }
00919 
00920 static int
00921 glue_entry_release_rw (
00922        Operation *op,
00923        Entry *e,
00924        int rw
00925 )
00926 {
00927        BackendDB *b0 = op->o_bd;
00928        int rc = -1;
00929 
00930        op->o_bd = glue_back_select (b0, &e->e_nname);
00931 
00932        if ( op->o_bd->be_release ) {
00933               rc = op->o_bd->be_release( op, e, rw );
00934 
00935        } else {
00936               /* FIXME: mimic be_entry_release_rw
00937                * when no be_release() available */
00938               /* free entry */
00939               entry_free( e );
00940               rc = 0;
00941        }
00942        op->o_bd = b0;
00943        return rc;
00944 }
00945 
00946 static struct berval *glue_base;
00947 static int glue_scope;
00948 static Filter *glue_filter;
00949 
00950 static ID
00951 glue_tool_entry_first (
00952        BackendDB *b0
00953 )
00954 {
00955        slap_overinst *on = glue_tool_inst( b0->bd_info );
00956        glueinfo             *gi = on->on_bi.bi_private;
00957        int i;
00958        ID rc;
00959 
00960        /* If we're starting from scratch, start at the most general */
00961        if (!glueBack) {
00962               if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
00963                      glueBack = &toolDB;
00964               } else {
00965                      for (i = gi->gi_nodes-1; i >= 0; i--) {
00966                             if (gi->gi_n[i].gn_be->be_entry_open &&
00967                                    gi->gi_n[i].gn_be->be_entry_first) {
00968                                           glueBack = gi->gi_n[i].gn_be;
00969                                    break;
00970                             }
00971                      }
00972               }
00973        }
00974        if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
00975               glueBack->be_entry_open (glueBack, glueMode) != 0)
00976               return NOID;
00977 
00978        rc = glueBack->be_entry_first (glueBack);
00979        while ( rc == NOID ) {
00980               if ( glueBack && glueBack->be_entry_close )
00981                      glueBack->be_entry_close (glueBack);
00982               for (i=0; i<gi->gi_nodes; i++) {
00983                      if (gi->gi_n[i].gn_be == glueBack)
00984                             break;
00985               }
00986               if (i == 0) {
00987                      glueBack = GLUEBACK_DONE;
00988                      break;
00989               } else {
00990                      glueBack = gi->gi_n[i-1].gn_be;
00991                      rc = glue_tool_entry_first (b0);
00992                      if ( glueBack == GLUEBACK_DONE ) {
00993                             break;
00994                      }
00995               }
00996        }
00997        return rc;
00998 }
00999 
01000 static ID
01001 glue_tool_entry_first_x (
01002        BackendDB *b0,
01003        struct berval *base,
01004        int scope,
01005        Filter *f
01006 )
01007 {
01008        slap_overinst *on = glue_tool_inst( b0->bd_info );
01009        glueinfo             *gi = on->on_bi.bi_private;
01010        int i;
01011        ID rc;
01012 
01013        glue_base = base;
01014        glue_scope = scope;
01015        glue_filter = f;
01016 
01017        /* If we're starting from scratch, start at the most general */
01018        if (!glueBack) {
01019               if ( toolDB.be_entry_open && toolDB.be_entry_first_x ) {
01020                      glueBack = &toolDB;
01021               } else {
01022                      for (i = gi->gi_nodes-1; i >= 0; i--) {
01023                             if (gi->gi_n[i].gn_be->be_entry_open &&
01024                                    gi->gi_n[i].gn_be->be_entry_first_x)
01025                             {
01026                                    glueBack = gi->gi_n[i].gn_be;
01027                                    break;
01028                             }
01029                      }
01030               }
01031        }
01032        if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first_x ||
01033               glueBack->be_entry_open (glueBack, glueMode) != 0)
01034               return NOID;
01035 
01036        rc = glueBack->be_entry_first_x (glueBack,
01037               glue_base, glue_scope, glue_filter);
01038        while ( rc == NOID ) {
01039               if ( glueBack && glueBack->be_entry_close )
01040                      glueBack->be_entry_close (glueBack);
01041               for (i=0; i<gi->gi_nodes; i++) {
01042                      if (gi->gi_n[i].gn_be == glueBack)
01043                             break;
01044               }
01045               if (i == 0) {
01046                      glueBack = GLUEBACK_DONE;
01047                      break;
01048               } else {
01049                      glueBack = gi->gi_n[i-1].gn_be;
01050                      rc = glue_tool_entry_first_x (b0,
01051                             glue_base, glue_scope, glue_filter);
01052                      if ( glueBack == GLUEBACK_DONE ) {
01053                             break;
01054                      }
01055               }
01056        }
01057        return rc;
01058 }
01059 
01060 static ID
01061 glue_tool_entry_next (
01062        BackendDB *b0
01063 )
01064 {
01065        slap_overinst *on = glue_tool_inst( b0->bd_info );
01066        glueinfo             *gi = on->on_bi.bi_private;
01067        int i;
01068        ID rc;
01069 
01070        if (!glueBack || !glueBack->be_entry_next)
01071               return NOID;
01072 
01073        rc = glueBack->be_entry_next (glueBack);
01074 
01075        /* If we ran out of entries in one database, move on to the next */
01076        while (rc == NOID) {
01077               if ( glueBack && glueBack->be_entry_close )
01078                      glueBack->be_entry_close (glueBack);
01079               for (i=0; i<gi->gi_nodes; i++) {
01080                      if (gi->gi_n[i].gn_be == glueBack)
01081                             break;
01082               }
01083               if (i == 0) {
01084                      glueBack = GLUEBACK_DONE;
01085                      break;
01086               } else {
01087                      glueBack = gi->gi_n[i-1].gn_be;
01088                      if ( glue_base || glue_filter ) {
01089                             /* using entry_first_x() */
01090                             rc = glue_tool_entry_first_x (b0,
01091                                    glue_base, glue_scope, glue_filter);
01092 
01093                      } else {
01094                             /* using entry_first() */
01095                             rc = glue_tool_entry_first (b0);
01096                      }
01097                      if ( glueBack == GLUEBACK_DONE ) {
01098                             break;
01099                      }
01100               }
01101        }
01102        return rc;
01103 }
01104 
01105 static ID
01106 glue_tool_dn2id_get (
01107        BackendDB *b0,
01108        struct berval *dn
01109 )
01110 {
01111        BackendDB *be, b2;
01112        int rc = -1;
01113 
01114        b2 = *b0;
01115        b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
01116        be = glue_back_select (&b2, dn);
01117        if ( be == &b2 ) be = &toolDB;
01118 
01119        if (!be->be_dn2id_get)
01120               return NOID;
01121 
01122        if (!glueBack) {
01123               if ( be->be_entry_open ) {
01124                      rc = be->be_entry_open (be, glueMode);
01125               }
01126               if (rc != 0) {
01127                      return NOID;
01128               }
01129        } else if (be != glueBack) {
01130               /* If this entry belongs in a different branch than the
01131                * previous one, close the current database and open the
01132                * new one.
01133                */
01134               if ( glueBack->be_entry_close ) {
01135                      glueBack->be_entry_close (glueBack);
01136               }
01137               if ( be->be_entry_open ) {
01138                      rc = be->be_entry_open (be, glueMode);
01139               }
01140               if (rc != 0) {
01141                      return NOID;
01142               }
01143        }
01144        glueBack = be;
01145        return be->be_dn2id_get (be, dn);
01146 }
01147 
01148 static Entry *
01149 glue_tool_entry_get (
01150        BackendDB *b0,
01151        ID id
01152 )
01153 {
01154        if (!glueBack || !glueBack->be_entry_get)
01155               return NULL;
01156 
01157        return glueBack->be_entry_get (glueBack, id);
01158 }
01159 
01160 static ID
01161 glue_tool_entry_put (
01162        BackendDB *b0,
01163        Entry *e,
01164        struct berval *text
01165 )
01166 {
01167        BackendDB *be, b2;
01168        int rc = -1;
01169 
01170        b2 = *b0;
01171        b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
01172        be = glue_back_select (&b2, &e->e_nname);
01173        if ( be == &b2 ) be = &toolDB;
01174 
01175        if (!be->be_entry_put)
01176               return NOID;
01177 
01178        if (!glueBack) {
01179               if ( be->be_entry_open ) {
01180                      rc = be->be_entry_open (be, glueMode);
01181               }
01182               if (rc != 0) {
01183                      return NOID;
01184               }
01185        } else if (be != glueBack) {
01186               /* If this entry belongs in a different branch than the
01187                * previous one, close the current database and open the
01188                * new one.
01189                */
01190               if ( glueBack->be_entry_close ) {
01191                      glueBack->be_entry_close (glueBack);
01192               }
01193               if ( be->be_entry_open ) {
01194                      rc = be->be_entry_open (be, glueMode);
01195               }
01196               if (rc != 0) {
01197                      return NOID;
01198               }
01199        }
01200        glueBack = be;
01201        return be->be_entry_put (be, e, text);
01202 }
01203 
01204 static ID
01205 glue_tool_entry_modify (
01206        BackendDB *b0,
01207        Entry *e,
01208        struct berval *text
01209 )
01210 {
01211        if (!glueBack || !glueBack->be_entry_modify)
01212               return NOID;
01213 
01214        return glueBack->be_entry_modify (glueBack, e, text);
01215 }
01216 
01217 static int
01218 glue_tool_entry_reindex (
01219        BackendDB *b0,
01220        ID id,
01221        AttributeDescription **adv
01222 )
01223 {
01224        if (!glueBack || !glueBack->be_entry_reindex)
01225               return -1;
01226 
01227        return glueBack->be_entry_reindex (glueBack, id, adv);
01228 }
01229 
01230 static int
01231 glue_tool_sync (
01232        BackendDB *b0
01233 )
01234 {
01235        slap_overinst *on = glue_tool_inst( b0->bd_info );
01236        glueinfo             *gi = on->on_bi.bi_private;
01237        BackendInfo          *bi = b0->bd_info;
01238        int i;
01239 
01240        /* just sync everyone */
01241        for (i = 0; i<gi->gi_nodes; i++)
01242               if (gi->gi_n[i].gn_be->be_sync)
01243                      gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
01244        b0->bd_info = on->on_info->oi_orig;
01245        if ( b0->be_sync )
01246               b0->be_sync( b0 );
01247        b0->bd_info = bi;
01248        return 0;
01249 }
01250 
01251 typedef struct glue_Addrec {
01252        struct glue_Addrec *ga_next;
01253        BackendDB *ga_be;
01254 } glue_Addrec;
01255 
01256 /* List of added subordinates */
01257 static glue_Addrec *ga_list;
01258 static int ga_adding;
01259 
01260 static int
01261 glue_db_init(
01262        BackendDB *be,
01263        ConfigReply *cr
01264 )
01265 {
01266        slap_overinst *on = (slap_overinst *)be->bd_info;
01267        slap_overinfo *oi = on->on_info;
01268        BackendInfo   *bi = oi->oi_orig;
01269        glueinfo *gi;
01270 
01271        if ( SLAP_GLUE_SUBORDINATE( be )) {
01272               Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
01273                      "cannot have glue overlay!\n",
01274                      be->be_suffix[0].bv_val, 0, 0 );
01275               return LDAP_OTHER;
01276        }
01277 
01278        gi = ch_calloc( 1, sizeof(glueinfo));
01279        on->on_bi.bi_private = gi;
01280        dnParent( be->be_nsuffix, &gi->gi_pdn );
01281 
01282        /* Currently the overlay framework doesn't handle these entry points
01283         * but we need them....
01284         */
01285        oi->oi_bi.bi_open = glue_open;
01286        oi->oi_bi.bi_close = glue_close;
01287 
01288        /* Only advertise these if the root DB supports them */
01289        if ( bi->bi_tool_entry_open )
01290               oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
01291        if ( bi->bi_tool_entry_close )
01292               oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
01293        if ( bi->bi_tool_entry_first )
01294               oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
01295        /* FIXME: check whether all support bi_tool_entry_first_x() ? */
01296        if ( bi->bi_tool_entry_first_x )
01297               oi->oi_bi.bi_tool_entry_first_x = glue_tool_entry_first_x;
01298        if ( bi->bi_tool_entry_next )
01299               oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
01300        if ( bi->bi_tool_entry_get )
01301               oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
01302        if ( bi->bi_tool_dn2id_get )
01303               oi->oi_bi.bi_tool_dn2id_get = glue_tool_dn2id_get;
01304        if ( bi->bi_tool_entry_put )
01305               oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
01306        if ( bi->bi_tool_entry_reindex )
01307               oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
01308        if ( bi->bi_tool_entry_modify )
01309               oi->oi_bi.bi_tool_entry_modify = glue_tool_entry_modify;
01310        if ( bi->bi_tool_sync )
01311               oi->oi_bi.bi_tool_sync = glue_tool_sync;
01312 
01313        SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
01314 
01315        if ( ga_list ) {
01316               be->bd_info = (BackendInfo *)oi;
01317               glue_sub_attach( 1 );
01318        }
01319 
01320        return 0;
01321 }
01322 
01323 static int
01324 glue_db_destroy (
01325        BackendDB *be,
01326        ConfigReply *cr
01327 )
01328 {
01329        slap_overinst *on = (slap_overinst *)be->bd_info;
01330        glueinfo             *gi = (glueinfo *)on->on_bi.bi_private;
01331 
01332        free (gi);
01333        return SLAP_CB_CONTINUE;
01334 }
01335 
01336 static int
01337 glue_db_close( 
01338        BackendDB *be,
01339        ConfigReply *cr
01340 )
01341 {
01342        slap_overinst *on = (slap_overinst *)be->bd_info;
01343 
01344        on->on_info->oi_bi.bi_db_close = 0;
01345        return 0;
01346 }
01347 
01348 int
01349 glue_sub_del( BackendDB *b0 )
01350 {
01351        BackendDB *be;
01352        int rc = 0;
01353 
01354        /* Find the top backend for this subordinate */
01355        be = b0;
01356        while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
01357               slap_overinfo *oi;
01358               slap_overinst *on;
01359               glueinfo *gi;
01360               int i;
01361 
01362               if ( SLAP_GLUE_SUBORDINATE( be ))
01363                      continue;
01364               if ( !SLAP_GLUE_INSTANCE( be ))
01365                      continue;
01366               if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
01367                      continue;
01368 
01369               /* OK, got the right backend, find the overlay */
01370               oi = (slap_overinfo *)be->bd_info;
01371               for ( on=oi->oi_list; on; on=on->on_next ) {
01372                      if ( on->on_bi.bi_type == glue.on_bi.bi_type )
01373                             break;
01374               }
01375               assert( on != NULL );
01376               gi = on->on_bi.bi_private;
01377               for ( i=0; i < gi->gi_nodes; i++ ) {
01378                      if ( gi->gi_n[i].gn_be == b0 ) {
01379                             int j;
01380 
01381                             for (j=i+1; j < gi->gi_nodes; j++)
01382                                    gi->gi_n[j-1] = gi->gi_n[j];
01383 
01384                             gi->gi_nodes--;
01385                      }
01386               }
01387        }
01388        if ( be == NULL )
01389               rc = LDAP_NO_SUCH_OBJECT;
01390 
01391        return rc;
01392 }
01393 
01394 
01395 /* Attach all the subordinate backends to their superior */
01396 int
01397 glue_sub_attach( int online )
01398 {
01399        glue_Addrec *ga, *gnext = NULL;
01400        int rc = 0;
01401 
01402        if ( ga_adding )
01403               return 0;
01404 
01405        ga_adding = 1;
01406 
01407        /* For all the subordinate backends */
01408        for ( ga=ga_list; ga != NULL; ga = gnext ) {
01409               BackendDB *be;
01410 
01411               gnext = ga->ga_next;
01412 
01413               /* Find the top backend for this subordinate */
01414               be = ga->ga_be;
01415               while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
01416                      slap_overinfo *oi;
01417                      slap_overinst *on;
01418                      glueinfo *gi;
01419 
01420                      if ( SLAP_GLUE_SUBORDINATE( be ))
01421                             continue;
01422                      if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
01423                             continue;
01424 
01425                      /* If it's not already configured, set up the overlay */
01426                      if ( !SLAP_GLUE_INSTANCE( be )) {
01427                             rc = overlay_config( be, glue.on_bi.bi_type, -1, NULL, NULL);
01428                             if ( rc )
01429                                    break;
01430                      }
01431                      /* Find the overlay instance */
01432                      oi = (slap_overinfo *)be->bd_info;
01433                      for ( on=oi->oi_list; on; on=on->on_next ) {
01434                             if ( on->on_bi.bi_type == glue.on_bi.bi_type )
01435                                    break;
01436                      }
01437                      assert( on != NULL );
01438                      gi = on->on_bi.bi_private;
01439                      gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
01440                             gi->gi_nodes * sizeof(gluenode));
01441                      gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
01442                      dnParent( &ga->ga_be->be_nsuffix[0],
01443                             &gi->gi_n[gi->gi_nodes].gn_pdn );
01444                      gi->gi_nodes++;
01445                      on->on_bi.bi_private = gi;
01446                      ga->ga_be->be_flags |= SLAP_DBFLAG_GLUE_LINKED;
01447                      break;
01448               }
01449               if ( !be ) {
01450                      Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
01451                             ga->ga_be->be_suffix[0].bv_val, 0, 0 );
01452                      /* allow this for now, assume a superior will
01453                       * be added later
01454                       */
01455                      if ( online ) {
01456                             rc = 0;
01457                             gnext = ga_list;
01458                             break;
01459                      }
01460                      rc = LDAP_NO_SUCH_OBJECT;
01461               }
01462               ch_free( ga );
01463               if ( rc ) break;
01464        }
01465 
01466        ga_list = gnext;
01467 
01468        ga_adding = 0;
01469 
01470        return rc;
01471 }
01472 
01473 int
01474 glue_sub_add( BackendDB *be, int advert, int online )
01475 {
01476        glue_Addrec *ga;
01477        int rc = 0;
01478 
01479        if ( overlay_is_inst( be, "glue" )) {
01480               Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
01481                      "cannot be a subordinate!\n",
01482                      be->be_suffix[0].bv_val, 0, 0 );
01483               return LDAP_OTHER;
01484        }
01485        SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
01486        if ( advert )
01487               SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
01488 
01489        ga = ch_malloc( sizeof( glue_Addrec ));
01490        ga->ga_next = ga_list;
01491        ga->ga_be = be;
01492        ga_list = ga;
01493 
01494        if ( online )
01495               rc = glue_sub_attach( online );
01496 
01497        return rc;
01498 }
01499 
01500 static int
01501 glue_access_allowed(
01502        Operation            *op,
01503        Entry                *e,
01504        AttributeDescription *desc,
01505        struct berval        *val,
01506        slap_access_t        access,
01507        AccessControlState   *state,
01508        slap_mask_t          *maskp )
01509 {
01510        BackendDB *b0, *be = glue_back_select( op->o_bd, &e->e_nname );
01511        int rc;
01512 
01513        if ( be == NULL || be == op->o_bd || be->bd_info->bi_access_allowed == NULL )
01514               return SLAP_CB_CONTINUE;
01515 
01516        b0 = op->o_bd;
01517        op->o_bd = be;
01518        rc = be->bd_info->bi_access_allowed ( op, e, desc, val, access, state, maskp );
01519        op->o_bd = b0;
01520        return rc;
01521 }
01522 
01523 int
01524 glue_sub_init()
01525 {
01526        glue.on_bi.bi_type = "glue";
01527 
01528        glue.on_bi.bi_db_init = glue_db_init;
01529        glue.on_bi.bi_db_close = glue_db_close;
01530        glue.on_bi.bi_db_destroy = glue_db_destroy;
01531 
01532        glue.on_bi.bi_op_search = glue_op_search;
01533        glue.on_bi.bi_op_modify = glue_op_func;
01534        glue.on_bi.bi_op_modrdn = glue_op_func;
01535        glue.on_bi.bi_op_add = glue_op_func;
01536        glue.on_bi.bi_op_delete = glue_op_func;
01537        glue.on_bi.bi_op_abandon = glue_op_abandon;
01538        glue.on_bi.bi_extended = glue_op_func;
01539 
01540        glue.on_bi.bi_chk_referrals = glue_chk_referrals;
01541        glue.on_bi.bi_chk_controls = glue_chk_controls;
01542        glue.on_bi.bi_entry_get_rw = glue_entry_get_rw;
01543        glue.on_bi.bi_entry_release_rw = glue_entry_release_rw;
01544        glue.on_bi.bi_access_allowed = glue_access_allowed;
01545 
01546        glue.on_response = glue_response;
01547 
01548        return overlay_register( &glue );
01549 }