Back to index

openldap  2.4.31
pguid.c
Go to the documentation of this file.
00001 /* pguid.c - Parent GUID 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_PGUID
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 (parentUUID) that contains the value
00038  * of the entryUUID of the parent entry (used by Samba4)
00039  */
00040 
00041 static AttributeDescription *ad_parentUUID;
00042 
00043 static slap_overinst               pguid;
00044 
00045 static int
00046 pguid_op_add( Operation *op, SlapReply *rs )
00047 {
00048        slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
00049 
00050        struct berval pdn, pndn;
00051        Entry *e = NULL;
00052        Attribute *a;
00053        int rc;
00054 
00055        /* don't care about suffix entry */
00056        if ( dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] ) ) {
00057               return SLAP_CB_CONTINUE;
00058        }
00059 
00060        dnParent( &op->o_req_dn, &pdn );
00061        dnParent( &op->o_req_ndn, &pndn );
00062 
00063        rc = overlay_entry_get_ov( op, &pndn, NULL, slap_schema.si_ad_entryUUID, 0, &e, on );
00064        if ( rc != LDAP_SUCCESS || e == NULL ) {
00065               Debug( LDAP_DEBUG_ANY, "%s: pguid_op_add: unable to get parent entry DN=\"%s\" (%d)\n",
00066                      op->o_log_prefix, pdn.bv_val, rc );
00067               return SLAP_CB_CONTINUE;
00068        }
00069 
00070        a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
00071        if ( a == NULL ) {
00072               Debug( LDAP_DEBUG_ANY, "%s: pguid_op_add: unable to find entryUUID of parent entry DN=\"%s\" (%d)\n",
00073                      op->o_log_prefix, pdn.bv_val, rc );
00074               
00075        } else {
00076               assert( a->a_numvals == 1 );
00077 
00078               if ( op->ora_e != NULL ) {
00079                      attr_merge_one( op->ora_e, ad_parentUUID, &a->a_vals[0], a->a_nvals == a->a_vals ? NULL : &a->a_nvals[0] );
00080               
00081               } else {
00082                      Modifications *ml;
00083                      Modifications *mod;
00084 
00085                      assert( op->ora_modlist != NULL );
00086 
00087                      for ( ml = op->ora_modlist; ml != NULL; ml = ml->sml_next ) {
00088                             if ( ml->sml_mod.sm_desc == slap_schema.si_ad_entryUUID ) {
00089                                    break;
00090                             }
00091                      }
00092 
00093                      if ( ml == NULL ) {
00094                             ml = op->ora_modlist;
00095                      }
00096 
00097                      mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
00098                      mod->sml_flags = SLAP_MOD_INTERNAL;
00099                      mod->sml_op = LDAP_MOD_ADD;
00100                      mod->sml_desc = ad_parentUUID;
00101                      mod->sml_type = ad_parentUUID->ad_cname;
00102                      mod->sml_values = ch_malloc( sizeof( struct berval ) * 2 );
00103                      mod->sml_nvalues = NULL;
00104                      mod->sml_numvals = 1;
00105 
00106                      ber_dupbv( &mod->sml_values[0], &a->a_vals[0] );
00107                      BER_BVZERO( &mod->sml_values[1] );
00108 
00109                      mod->sml_next = ml->sml_next;
00110                      ml->sml_next = mod;
00111               }
00112        }
00113 
00114        if ( e != NULL ) {
00115               (void)overlay_entry_release_ov( op, e, 0, on );
00116        }
00117 
00118        return SLAP_CB_CONTINUE;
00119 }
00120 
00121 static int
00122 pguid_op_rename( Operation *op, SlapReply *rs )
00123 {
00124        slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
00125 
00126        Entry *e = NULL;
00127        Attribute *a;
00128        int rc;
00129 
00130        if ( op->orr_nnewSup == NULL ) {
00131               return SLAP_CB_CONTINUE;
00132        }
00133 
00134        rc = overlay_entry_get_ov( op, op->orr_nnewSup, NULL, slap_schema.si_ad_entryUUID, 0, &e, on );
00135        if ( rc != LDAP_SUCCESS || e == NULL ) {
00136               Debug( LDAP_DEBUG_ANY, "%s: pguid_op_rename: unable to get newSuperior entry DN=\"%s\" (%d)\n",
00137                      op->o_log_prefix, op->orr_newSup->bv_val, rc );
00138               return SLAP_CB_CONTINUE;
00139        }
00140 
00141        a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
00142        if ( a == NULL ) {
00143               Debug( LDAP_DEBUG_ANY, "%s: pguid_op_rename: unable to find entryUUID of newSuperior entry DN=\"%s\" (%d)\n",
00144                      op->o_log_prefix, op->orr_newSup->bv_val, rc );
00145               
00146        } else {
00147               Modifications *mod;
00148 
00149               assert( a->a_numvals == 1 );
00150 
00151               mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
00152               mod->sml_flags = SLAP_MOD_INTERNAL;
00153               mod->sml_op = LDAP_MOD_REPLACE;
00154               mod->sml_desc = ad_parentUUID;
00155               mod->sml_type = ad_parentUUID->ad_cname;
00156               mod->sml_values = ch_malloc( sizeof( struct berval ) * 2 );
00157               mod->sml_nvalues = NULL;
00158               mod->sml_numvals = 1;
00159 
00160               ber_dupbv( &mod->sml_values[0], &a->a_vals[0] );
00161               BER_BVZERO( &mod->sml_values[1] );
00162 
00163               mod->sml_next = op->orr_modlist;
00164               op->orr_modlist = mod;
00165        }
00166 
00167        if ( e != NULL ) {
00168               (void)overlay_entry_release_ov( op, e, 0, on );
00169        }
00170 
00171        return SLAP_CB_CONTINUE;
00172 }
00173 
00174 static int
00175 pguid_db_init(
00176        BackendDB     *be,
00177        ConfigReply   *cr)
00178 {
00179        if ( SLAP_ISGLOBALOVERLAY( be ) ) {
00180               Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
00181                      "pguid_db_init: pguid cannot be used as global overlay.\n" );
00182               return 1;
00183        }
00184 
00185        if ( be->be_nsuffix == NULL ) {
00186               Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
00187                      "pguid_db_init: database must have suffix\n" );
00188               return 1;
00189        }
00190 
00191        if ( BER_BVISNULL( &be->be_rootndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
00192               Log1( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
00193                      "pguid_db_init: missing rootdn for database DN=\"%s\", YMMV\n",
00194                      be->be_suffix[ 0 ].bv_val );
00195        }
00196 
00197        return 0;
00198 }
00199 
00200 typedef struct pguid_mod_t {
00201        struct berval ndn;
00202        struct berval pguid;
00203        struct pguid_mod_t *next;
00204 } pguid_mod_t;
00205 
00206 typedef struct {
00207        slap_overinst *on;
00208        pguid_mod_t *mods;
00209 } pguid_repair_cb_t;
00210 
00211 static int
00212 pguid_repair_cb( Operation *op, SlapReply *rs )
00213 {
00214        int rc;
00215        pguid_repair_cb_t *pcb = op->o_callback->sc_private;
00216        Entry *e = NULL;
00217        Attribute *a;
00218        struct berval pdn, pndn;
00219 
00220        switch ( rs->sr_type ) {
00221        case REP_SEARCH:
00222               break;
00223 
00224        case REP_SEARCHREF:
00225        case REP_RESULT:
00226               return rs->sr_err;
00227 
00228        default:
00229               assert( 0 );
00230        }
00231 
00232        assert( rs->sr_entry != NULL );
00233 
00234        dnParent( &rs->sr_entry->e_name, &pdn );
00235        dnParent( &rs->sr_entry->e_nname, &pndn );
00236 
00237        rc = overlay_entry_get_ov( op, &pndn, NULL, slap_schema.si_ad_entryUUID, 0, &e, pcb->on );
00238        if ( rc != LDAP_SUCCESS || e == NULL ) {
00239               Debug( LDAP_DEBUG_ANY, "%s: pguid_repair_cb: unable to get parent entry DN=\"%s\" (%d)\n",
00240                      op->o_log_prefix, pdn.bv_val, rc );
00241               return 0;
00242        }
00243 
00244        a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
00245        if ( a == NULL ) {
00246               Debug( LDAP_DEBUG_ANY, "%s: pguid_repair_cb: unable to find entryUUID of parent entry DN=\"%s\" (%d)\n",
00247                      op->o_log_prefix, pdn.bv_val, rc );
00248               
00249        } else {
00250               ber_len_t len;
00251               pguid_mod_t *mod;
00252 
00253               assert( a->a_numvals == 1 );
00254 
00255               len = sizeof( pguid_mod_t ) + rs->sr_entry->e_nname.bv_len + 1 + a->a_vals[0].bv_len + 1;
00256               mod = op->o_tmpalloc( len, op->o_tmpmemctx );
00257               mod->ndn.bv_len = rs->sr_entry->e_nname.bv_len;
00258               mod->ndn.bv_val = (char *)&mod[1];
00259               mod->pguid.bv_len = a->a_vals[0].bv_len;
00260               mod->pguid.bv_val = (char *)&mod->ndn.bv_val[mod->ndn.bv_len + 1];
00261               lutil_strncopy( mod->ndn.bv_val, rs->sr_entry->e_nname.bv_val, rs->sr_entry->e_nname.bv_len );
00262               lutil_strncopy( mod->pguid.bv_val, a->a_vals[0].bv_val, a->a_vals[0].bv_len );
00263 
00264               mod->next = pcb->mods;
00265               pcb->mods = mod;
00266 
00267               Debug( LDAP_DEBUG_TRACE, "%s: pguid_repair_cb: scheduling entry DN=\"%s\" for repair\n",
00268                      op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
00269        }
00270 
00271        if ( e != NULL ) {
00272               (void)overlay_entry_release_ov( op, e, 0, pcb->on );
00273        }
00274 
00275        return 0;
00276 }
00277 
00278 static int
00279 pguid_repair( BackendDB *be )
00280 {
00281        slap_overinst *on = (slap_overinst *)be->bd_info;
00282        void *ctx = ldap_pvt_thread_pool_context();
00283        Connection conn = { 0 };
00284        OperationBuffer opbuf;
00285        Operation *op;
00286        slap_callback sc = { 0 };
00287        pguid_repair_cb_t pcb = { 0 };
00288        SlapReply rs = { REP_RESULT };
00289        pguid_mod_t *pmod;
00290        int nrepaired = 0;
00291 
00292        connection_fake_init2( &conn, &opbuf, ctx, 0 );
00293        op = &opbuf.ob_op;
00294 
00295        op->o_tag = LDAP_REQ_SEARCH;
00296        memset( &op->oq_search, 0, sizeof( op->oq_search ) );
00297 
00298        op->o_bd = select_backend( &be->be_nsuffix[ 0 ], 0 );
00299 
00300        op->o_req_dn = op->o_bd->be_suffix[ 0 ];
00301        op->o_req_ndn = op->o_bd->be_nsuffix[ 0 ];
00302 
00303        op->o_dn = op->o_bd->be_rootdn;
00304        op->o_ndn = op->o_bd->be_rootndn;
00305 
00306        op->ors_scope = LDAP_SCOPE_SUBORDINATE;
00307        op->ors_tlimit = SLAP_NO_LIMIT;
00308        op->ors_slimit = SLAP_NO_LIMIT;
00309        op->ors_attrs = slap_anlist_no_attrs;
00310 
00311        op->ors_filterstr.bv_len = STRLENOF( "(!(=*))" ) + ad_parentUUID->ad_cname.bv_len;
00312        op->ors_filterstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
00313        snprintf( op->ors_filterstr.bv_val, op->ors_filterstr.bv_len + 1,
00314               "(!(%s=*))", ad_parentUUID->ad_cname.bv_val );
00315 
00316        op->ors_filter = str2filter_x( op, op->ors_filterstr.bv_val );
00317        if ( op->ors_filter == NULL ) {
00318               rs.sr_err = LDAP_OTHER;
00319               goto done_search;
00320        }
00321        
00322        op->o_callback = &sc;
00323        sc.sc_response = pguid_repair_cb;
00324        sc.sc_private = &pcb;
00325        pcb.on = on;
00326 
00327        (void)op->o_bd->bd_info->bi_op_search( op, &rs );
00328 
00329        op->o_tag = LDAP_REQ_MODIFY;
00330        sc.sc_response = slap_null_cb;
00331        sc.sc_private = NULL;
00332        memset( &op->oq_modify, 0, sizeof( req_modify_s ) );
00333 
00334        for ( pmod = pcb.mods; pmod != NULL; ) {
00335               pguid_mod_t *pnext;
00336 
00337               Modifications *mod;
00338               SlapReply rs2 = { REP_RESULT };
00339 
00340               mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
00341               mod->sml_flags = SLAP_MOD_INTERNAL;
00342               mod->sml_op = LDAP_MOD_REPLACE;
00343               mod->sml_desc = ad_parentUUID;
00344               mod->sml_type = ad_parentUUID->ad_cname;
00345               mod->sml_values = ch_malloc( sizeof( struct berval ) * 2 );
00346               mod->sml_nvalues = NULL;
00347               mod->sml_numvals = 1;
00348               mod->sml_next = NULL;
00349 
00350               ber_dupbv( &mod->sml_values[0], &pmod->pguid );
00351               BER_BVZERO( &mod->sml_values[1] );
00352 
00353               op->o_req_dn = pmod->ndn;
00354               op->o_req_ndn = pmod->ndn;
00355 
00356               op->orm_modlist = mod;
00357               op->o_bd->be_modify( op, &rs2 );
00358               slap_mods_free( op->orm_modlist, 1 );
00359               if ( rs2.sr_err == LDAP_SUCCESS ) {
00360                      Debug( LDAP_DEBUG_TRACE, "%s: pguid_repair: entry DN=\"%s\" repaired\n",
00361                             op->o_log_prefix, pmod->ndn.bv_val, 0 );
00362                      nrepaired++;
00363 
00364               } else {
00365                      Debug( LDAP_DEBUG_ANY, "%s: pguid_repair: entry DN=\"%s\" repair failed (%d)\n",
00366                             op->o_log_prefix, pmod->ndn.bv_val, rs2.sr_err );
00367               }
00368 
00369               pnext = pmod->next;
00370               op->o_tmpfree( pmod, op->o_tmpmemctx );
00371               pmod = pnext;
00372        }
00373 
00374 done_search:;
00375        op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
00376        filter_free_x( op, op->ors_filter, 1 );
00377 
00378        Log1( LDAP_DEBUG_STATS, LDAP_LEVEL_INFO,
00379               "pguid: repaired=%d\n", nrepaired );
00380 
00381        return rs.sr_err;
00382 }
00383 
00384 /* search all entries without parentUUID; "repair" them */
00385 static int
00386 pguid_db_open(
00387        BackendDB     *be,
00388        ConfigReply   *cr )
00389 {
00390        if ( SLAP_SINGLE_SHADOW( be ) ) {
00391               Log1( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
00392                      "pguid incompatible with shadow database \"%s\".\n",
00393                      be->be_suffix[ 0 ].bv_val );
00394               return 1;
00395        }
00396 
00397        pguid_repair( be );
00398 
00399        return 0;
00400 }
00401 
00402 static struct {
00403        char   *desc;
00404        AttributeDescription **adp;
00405 } as[] = {
00406        { "( 1.3.6.1.4.1.4203.666.1.59 "
00407               "NAME 'parentUUID' "
00408               "DESC 'the value of the entryUUID of the parent' "
00409               "EQUALITY UUIDMatch "
00410               "ORDERING UUIDOrderingMatch "
00411               "SYNTAX 1.3.6.1.1.16.1 "
00412               "USAGE dSAOperation "
00413               "SINGLE-VALUE "
00414               "NO-USER-MODIFICATION " 
00415               ")",
00416               &ad_parentUUID },
00417        { NULL }
00418 };
00419 
00420 int
00421 pguid_initialize(void)
00422 {
00423        int code, i;
00424 
00425        for ( i = 0; as[ i ].desc != NULL; i++ ) {
00426               code = register_at( as[ i ].desc, as[ i ].adp, 0 );
00427               if ( code ) {
00428                      Debug( LDAP_DEBUG_ANY,
00429                             "pguid_initialize: register_at #%d failed\n",
00430                             i, 0, 0 );
00431                      return code;
00432               }
00433 
00434               /* Allow Manager to set these as needed */
00435               if ( is_at_no_user_mod( (*as[ i ].adp)->ad_type ) ) {
00436                      (*as[ i ].adp)->ad_type->sat_flags |=
00437                             SLAP_AT_MANAGEABLE;
00438               }
00439        }
00440 
00441        pguid.on_bi.bi_type = "pguid";
00442 
00443        pguid.on_bi.bi_op_add = pguid_op_add;
00444        pguid.on_bi.bi_op_modrdn = pguid_op_rename;
00445 
00446        pguid.on_bi.bi_db_init = pguid_db_init;
00447        pguid.on_bi.bi_db_open = pguid_db_open;
00448 
00449        return overlay_register( &pguid );
00450 }
00451 
00452 #if SLAPD_OVER_PGUID == SLAPD_MOD_DYNAMIC
00453 int
00454 init_module( int argc, char *argv[] )
00455 {
00456        return pguid_initialize();
00457 }
00458 #endif /* SLAPD_OVER_PGUID == SLAPD_MOD_DYNAMIC */
00459 
00460 #endif /* SLAPD_OVER_PGUID */