Back to index

openldap  2.4.31
vernum.c
Go to the documentation of this file.
00001 /* vernum.c - RDN value overlay */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 2008 Pierangelo Masarati.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This work was initially developed by Pierangelo Masarati
00019  * for inclusion in OpenLDAP Software.
00020  */
00021 
00022 #include "portable.h"
00023 
00024 #ifdef SLAPD_OVER_VERNUM
00025 
00026 #include <stdio.h>
00027 
00028 #include "ac/string.h"
00029 #include "ac/socket.h"
00030 
00031 #include "slap.h"
00032 #include "config.h"
00033 
00034 #include "lutil.h"
00035 
00036 /*
00037  * Maintain an attribute (e.g. msDS-KeyVersionNumber) that consists
00038  * in a counter of modifications of another attribute (e.g. unicodePwd).
00039  */
00040 
00041 typedef struct vernum_t {
00042        AttributeDescription *vn_attr;
00043        AttributeDescription *vn_vernum;
00044 } vernum_t;
00045 
00046 static AttributeDescription *ad_msDS_KeyVersionNumber;
00047 
00048 static struct berval        val_init = BER_BVC( "0" );
00049 static slap_overinst               vernum;
00050 
00051 static int
00052 vernum_op_add( Operation *op, SlapReply *rs )
00053 {
00054        slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
00055        vernum_t      *vn = (vernum_t *)on->on_bi.bi_private;
00056 
00057        Attribute *a, **ap;
00058        int rc;
00059 
00060        /* NOTE: should we accept an entry still in mods format? */
00061        assert( op->ora_e != NULL );
00062 
00063        if ( BER_BVISEMPTY( &op->ora_e->e_nname ) ) {
00064               return SLAP_CB_CONTINUE;
00065        }
00066 
00067        a = attr_find( op->ora_e->e_attrs, vn->vn_attr );
00068        if ( a == NULL ) {
00069               return SLAP_CB_CONTINUE;
00070        }
00071 
00072        if ( attr_find( op->ora_e->e_attrs, vn->vn_vernum ) != NULL ) {
00073               /* already present - leave it alone */
00074               return SLAP_CB_CONTINUE;
00075        }
00076 
00077        a = attr_alloc( vn->vn_vernum );
00078 
00079        value_add_one( &a->a_vals, &val_init );
00080        a->a_nvals = a->a_vals;
00081        a->a_numvals = 1;
00082 
00083        for ( ap = &op->ora_e->e_attrs; *ap != NULL; ap = &(*ap)->a_next )
00084               /* goto tail */ ;
00085 
00086        *ap = a;
00087 
00088        return SLAP_CB_CONTINUE;
00089 }
00090 
00091 static int
00092 vernum_op_modify( Operation *op, SlapReply *rs )
00093 {
00094        slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
00095        vernum_t      *vn = (vernum_t *)on->on_bi.bi_private;
00096 
00097        Modifications *ml, **mlp;
00098        struct berval val = BER_BVC( "1" );
00099        int rc;
00100        unsigned got = 0;
00101 
00102        for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
00103               if ( ml->sml_desc == vn->vn_vernum ) {
00104                      /* already present - leave it alone
00105                       * (or should we increment it anyway?) */
00106                      return SLAP_CB_CONTINUE;
00107               }
00108 
00109               if ( ml->sml_desc == vn->vn_attr ) {
00110                      got = 1;
00111               }
00112        }
00113 
00114        if ( !got ) {
00115               return SLAP_CB_CONTINUE;
00116        }
00117 
00118        for ( mlp = &op->orm_modlist; *mlp != NULL; mlp = &(*mlp)->sml_next )
00119               /* goto tail */ ;
00120 
00121        /* ITS#6561 */
00122 #ifdef SLAP_MOD_ADD_IF_NOT_PRESENT
00123        /* the initial value is only added if the vernum attr is not present */
00124        ml = SLAP_CALLOC( sizeof( Modifications ), 1 );
00125        ml->sml_values = SLAP_CALLOC( sizeof( struct berval ) , 2 );
00126        value_add_one( &ml->sml_values, &val_init );
00127        ml->sml_nvalues = NULL;
00128        ml->sml_numvals = 1;
00129        ml->sml_op = SLAP_MOD_ADD_IF_NOT_PRESENT;
00130        ml->sml_flags = SLAP_MOD_INTERNAL;
00131        ml->sml_desc = vn->vn_vernum;
00132        ml->sml_type = vn->vn_vernum->ad_cname;
00133 
00134        *mlp = ml;
00135        mlp = &ml->sml_next;
00136 #endif /* SLAP_MOD_ADD_IF_NOT_PRESENT */
00137 
00138        /* this increments by 1 the vernum attr */
00139        ml = SLAP_CALLOC( sizeof( Modifications ), 1 );
00140        ml->sml_values = SLAP_CALLOC( sizeof( struct berval ) , 2 );
00141        value_add_one( &ml->sml_values, &val );
00142        ml->sml_nvalues = NULL;
00143        ml->sml_numvals = 1;
00144        ml->sml_op = LDAP_MOD_INCREMENT;
00145        ml->sml_flags = SLAP_MOD_INTERNAL;
00146        ml->sml_desc = vn->vn_vernum;
00147        ml->sml_type = vn->vn_vernum->ad_cname;
00148 
00149        *mlp = ml;
00150 
00151        return SLAP_CB_CONTINUE;
00152 }
00153 
00154 static int
00155 vernum_db_init(
00156        BackendDB     *be,
00157        ConfigReply   *cr)
00158 {
00159        slap_overinst *on = (slap_overinst *) be->bd_info;
00160        vernum_t      *vn = NULL;
00161 
00162        if ( SLAP_ISGLOBALOVERLAY( be ) ) {
00163               Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
00164                      "vernum_db_init: vernum cannot be used as global overlay.\n" );
00165               return 1;
00166        }
00167 
00168        if ( be->be_nsuffix == NULL ) {
00169               Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
00170                      "vernum_db_init: database must have suffix\n" );
00171               return 1;
00172        }
00173 
00174        if ( BER_BVISNULL( &be->be_rootndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
00175               Log1( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
00176                      "vernum_db_init: missing rootdn for database DN=\"%s\", YMMV\n",
00177                      be->be_suffix[ 0 ].bv_val );
00178        }
00179 
00180        vn = (vernum_t *)ch_calloc( 1, sizeof( vernum_t ) );
00181 
00182        on->on_bi.bi_private = (void *)vn;
00183 
00184        return 0;
00185 }
00186 
00187 typedef struct vernum_mod_t {
00188        struct berval ndn;
00189        struct vernum_mod_t *next;
00190 } vernum_mod_t;
00191 
00192 typedef struct {
00193        BackendDB *bd;
00194        vernum_mod_t *mods;
00195 } vernum_repair_cb_t;
00196 
00197 static int
00198 vernum_repair_cb( Operation *op, SlapReply *rs )
00199 {
00200        int rc;
00201        vernum_repair_cb_t *rcb = op->o_callback->sc_private;
00202        vernum_mod_t *mod;
00203        ber_len_t len;
00204        BackendDB *save_bd = op->o_bd;
00205 
00206        switch ( rs->sr_type ) {
00207        case REP_SEARCH:
00208               break;
00209 
00210        case REP_SEARCHREF:
00211        case REP_RESULT:
00212               return rs->sr_err;
00213 
00214        default:
00215               assert( 0 );
00216        }
00217 
00218        assert( rs->sr_entry != NULL );
00219 
00220        len = sizeof( vernum_mod_t ) + rs->sr_entry->e_nname.bv_len + 1;
00221        mod = op->o_tmpalloc( len, op->o_tmpmemctx );
00222        mod->ndn.bv_len = rs->sr_entry->e_nname.bv_len;
00223        mod->ndn.bv_val = (char *)&mod[1];
00224        lutil_strncopy( mod->ndn.bv_val, rs->sr_entry->e_nname.bv_val, rs->sr_entry->e_nname.bv_len );
00225 
00226        mod->next = rcb->mods;
00227        rcb->mods = mod;
00228 
00229        Debug( LDAP_DEBUG_TRACE, "%s: vernum_repair_cb: scheduling entry DN=\"%s\" for repair\n",
00230               op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
00231 
00232        return 0;
00233 }
00234 
00235 static int
00236 vernum_repair( BackendDB *be )
00237 {
00238        slap_overinst *on = (slap_overinst *)be->bd_info;
00239        vernum_t *vn = (vernum_t *)on->on_bi.bi_private;
00240        void *ctx = ldap_pvt_thread_pool_context();
00241        Connection conn = { 0 };
00242        OperationBuffer opbuf;
00243        Operation *op;
00244        BackendDB db;
00245        slap_callback sc = { 0 };
00246        vernum_repair_cb_t rcb = { 0 };
00247        SlapReply rs = { REP_RESULT };
00248        vernum_mod_t *rmod;
00249        int nrepaired = 0;
00250 
00251        connection_fake_init2( &conn, &opbuf, ctx, 0 );
00252        op = &opbuf.ob_op;
00253 
00254        op->o_tag = LDAP_REQ_SEARCH;
00255        memset( &op->oq_search, 0, sizeof( op->oq_search ) );
00256 
00257        assert( !BER_BVISNULL( &be->be_nsuffix[ 0 ] ) );
00258 
00259        op->o_bd = select_backend( &be->be_nsuffix[ 0 ], 0 );
00260        assert( op->o_bd != NULL );
00261        assert( op->o_bd->be_nsuffix != NULL );
00262 
00263        op->o_req_dn = op->o_bd->be_suffix[ 0 ];
00264        op->o_req_ndn = op->o_bd->be_nsuffix[ 0 ];
00265 
00266        op->o_dn = op->o_bd->be_rootdn;
00267        op->o_ndn = op->o_bd->be_rootndn;
00268 
00269        op->ors_scope = LDAP_SCOPE_SUBTREE;
00270        op->ors_tlimit = SLAP_NO_LIMIT;
00271        op->ors_slimit = SLAP_NO_LIMIT;
00272        op->ors_attrs = slap_anlist_no_attrs;
00273 
00274        op->ors_filterstr.bv_len = STRLENOF( "(&(=*)(!(=*)))" )
00275               + vn->vn_attr->ad_cname.bv_len
00276               + vn->vn_vernum->ad_cname.bv_len;
00277        op->ors_filterstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
00278        snprintf( op->ors_filterstr.bv_val, op->ors_filterstr.bv_len + 1,
00279               "(&(%s=*)(!(%s=*)))",
00280               vn->vn_attr->ad_cname.bv_val,
00281               vn->vn_vernum->ad_cname.bv_val );
00282 
00283        op->ors_filter = str2filter_x( op, op->ors_filterstr.bv_val );
00284        if ( op->ors_filter == NULL ) {
00285               rs.sr_err = LDAP_OTHER;
00286               goto done_search;
00287        }
00288        
00289        op->o_callback = &sc;
00290        sc.sc_response = vernum_repair_cb;
00291        sc.sc_private = &rcb;
00292        rcb.bd = &db;
00293        db = *be;
00294        db.bd_info = (BackendInfo *)on;
00295 
00296        (void)op->o_bd->bd_info->bi_op_search( op, &rs );
00297 
00298        op->o_tag = LDAP_REQ_MODIFY;
00299        sc.sc_response = slap_null_cb;
00300        sc.sc_private = NULL;
00301        memset( &op->oq_modify, 0, sizeof( req_modify_s ) );
00302 
00303        for ( rmod = rcb.mods; rmod != NULL; ) {
00304               vernum_mod_t *rnext;
00305               Modifications mod;
00306               struct berval vals[2] = { BER_BVNULL };
00307               SlapReply rs2 = { REP_RESULT };
00308 
00309               mod.sml_flags = SLAP_MOD_INTERNAL;
00310               mod.sml_op = LDAP_MOD_REPLACE;
00311               mod.sml_desc = vn->vn_vernum;
00312               mod.sml_type = vn->vn_vernum->ad_cname;
00313               mod.sml_values = vals;
00314               mod.sml_values[0] = val_init;
00315               mod.sml_nvalues = NULL;
00316               mod.sml_numvals = 1;
00317               mod.sml_next = NULL;
00318 
00319               op->o_req_dn = rmod->ndn;
00320               op->o_req_ndn = rmod->ndn;
00321 
00322               op->orm_modlist = &mod;
00323 
00324               op->o_bd->be_modify( op, &rs2 );
00325 
00326               slap_mods_free( op->orm_modlist->sml_next, 1 );
00327               if ( rs2.sr_err == LDAP_SUCCESS ) {
00328                      Debug( LDAP_DEBUG_TRACE, "%s: vernum_repair: entry DN=\"%s\" repaired\n",
00329                             op->o_log_prefix, rmod->ndn.bv_val, 0 );
00330                      nrepaired++;
00331 
00332               } else {
00333                      Debug( LDAP_DEBUG_ANY, "%s: vernum_repair: entry DN=\"%s\" repair failed (%d)\n",
00334                             op->o_log_prefix, rmod->ndn.bv_val, rs2.sr_err );
00335               }
00336 
00337               rnext = rmod->next;
00338               op->o_tmpfree( rmod, op->o_tmpmemctx );
00339               rmod = rnext;
00340        }
00341 
00342 done_search:;
00343        op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
00344        filter_free_x( op, op->ors_filter, 1 );
00345 
00346        Log1( LDAP_DEBUG_STATS, LDAP_LEVEL_INFO,
00347               "vernum: repaired=%d\n", nrepaired );
00348 
00349        return 0;
00350 }
00351 
00352 static int
00353 vernum_db_open(
00354        BackendDB     *be,
00355        ConfigReply   *cr )
00356 {
00357        slap_overinst *on = (slap_overinst *)be->bd_info;
00358        vernum_t *vn = (vernum_t *)on->on_bi.bi_private;
00359 
00360        if ( SLAP_SINGLE_SHADOW( be ) ) {
00361               Log1( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
00362                      "vernum incompatible with shadow database \"%s\".\n",
00363                      be->be_suffix[ 0 ].bv_val );
00364               return 1;
00365        }
00366 
00367        /* default: unicodePwd & msDS-KeyVersionNumber */
00368        if ( vn->vn_attr == NULL ) {
00369               const char *text = NULL;
00370               int rc;
00371 
00372               rc = slap_str2ad( "unicodePwd", &vn->vn_attr, &text );
00373               if ( rc != LDAP_SUCCESS ) {
00374                      Debug( LDAP_DEBUG_ANY, "vernum: unable to find attribute 'unicodePwd' (%d: %s)\n",
00375                             rc, text, 0 );
00376                      return 1;
00377               }
00378 
00379               vn->vn_vernum = ad_msDS_KeyVersionNumber;
00380        }
00381 
00382        return vernum_repair( be );
00383 }
00384 
00385 static int
00386 vernum_db_destroy(
00387        BackendDB     *be,
00388        ConfigReply   *cr )
00389 {
00390        slap_overinst *on = (slap_overinst *)be->bd_info;
00391        vernum_t *vn = (vernum_t *)on->on_bi.bi_private;
00392 
00393        if ( vn ) {
00394               ch_free( vn );
00395               on->on_bi.bi_private = NULL;
00396        }
00397 
00398        return 0;
00399 }
00400 
00401 static struct {
00402        char   *desc;
00403        AttributeDescription **adp;
00404 } as[] = {
00405        { "( 1.2.840.113556.1.4.1782 "
00406               "NAME 'msDS-KeyVersionNumber' "
00407               "DESC 'in the original specification the syntax is 2.5.5.9' "
00408               "SYNTAX '1.3.6.1.4.1.1466.115.121.1.27' "
00409               "EQUALITY integerMatch "
00410               "SINGLE-VALUE "
00411               "USAGE dSAOperation "
00412               "NO-USER-MODIFICATION " 
00413               ")",
00414               &ad_msDS_KeyVersionNumber },
00415        { NULL }
00416 };
00417 
00418 int
00419 vernum_initialize(void)
00420 {
00421        int code, i;
00422 
00423        for ( i = 0; as[ i ].desc != NULL; i++ ) {
00424               code = register_at( as[ i ].desc, as[ i ].adp, 0 );
00425               if ( code ) {
00426                      Debug( LDAP_DEBUG_ANY,
00427                             "vernum_initialize: register_at #%d failed\n",
00428                             i, 0, 0 );
00429                      return code;
00430               }
00431 
00432               /* Allow Manager to set these as needed */
00433               if ( is_at_no_user_mod( (*as[ i ].adp)->ad_type ) ) {
00434                      (*as[ i ].adp)->ad_type->sat_flags |=
00435                             SLAP_AT_MANAGEABLE;
00436               }
00437        }
00438 
00439        vernum.on_bi.bi_type = "vernum";
00440 
00441        vernum.on_bi.bi_op_add = vernum_op_add;
00442        vernum.on_bi.bi_op_modify = vernum_op_modify;
00443 
00444        vernum.on_bi.bi_db_init = vernum_db_init;
00445        vernum.on_bi.bi_db_open = vernum_db_open;
00446        vernum.on_bi.bi_db_destroy = vernum_db_destroy;
00447 
00448        return overlay_register( &vernum );
00449 }
00450 
00451 #if SLAPD_OVER_VERNUM == SLAPD_MOD_DYNAMIC
00452 int
00453 init_module( int argc, char *argv[] )
00454 {
00455        return vernum_initialize();
00456 }
00457 #endif /* SLAPD_OVER_VERNUM == SLAPD_MOD_DYNAMIC */
00458 
00459 #endif /* SLAPD_OVER_VERNUM */