]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/slapadd.c
declare oc_bvfind_undef()
[openldap] / servers / slapd / slapadd.c
index eaa3b9cb54385ba09a1d102313d949f55bf97d80..2d648278161dd4b3fb3950d685b9ee61366a8347 100644 (file)
@@ -66,8 +66,8 @@ slapadd( int argc, char **argv )
        const char *progname = "slapadd";
 
        struct berval csn;
-       struct berval maxcsn = { 0, NULL };
-       struct berval ldifcsn = { 0, NULL };
+       struct berval maxcsn = BER_BVNULL;
+       struct berval ldifcsn = BER_BVNULL;
        int match;
        int     provider_subentry = 0;
        struct subentryinfo *sei;
@@ -75,13 +75,12 @@ slapadd( int argc, char **argv )
        Attribute *attr;
        Entry *ctxcsn_e;
        ID      ctxcsn_id;
-       struct berval   ctxcsn_ndn = { 0, NULL };
+       struct berval   ctxcsn_ndn = BER_BVNULL;
        int ret;
        struct berval bvtext;
        int i;
-#ifdef NEW_LOGGING
-       lutil_log_initialize(argc, argv );
-#endif
+       struct berval mc;
+       struct sync_cookie sc;
        slap_tool_init( progname, SLAPADD, argc, argv );
 
        LDAP_SLIST_INIT( &consumer_subentry );
@@ -92,13 +91,18 @@ slapadd( int argc, char **argv )
        {
                fprintf( stderr, "%s: database doesn't support necessary operations.\n",
                        progname );
-               exit( EXIT_FAILURE );
+               if ( dryrun ) {
+                       fprintf( stderr, "\t(dry) continuing...\n" );
+
+               } else {
+                       exit( EXIT_FAILURE );
+               }
        }
 
        lmax = 0;
        lineno = 0;
 
-       if( be->be_entry_open( be, 1 ) != 0 ) {
+       if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
                fprintf( stderr, "%s: could not open database.\n",
                        progname );
                exit( EXIT_FAILURE );
@@ -230,11 +234,9 @@ slapadd( int argc, char **argv )
                        timestamp.bv_val = timebuf;
                        timestamp.bv_len = strlen(timebuf);
 
-                       if ( be->be_rootndn.bv_len == 0 ) {
-                               name.bv_val = SLAPD_ANONYMOUS;
-                               name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
-                               nname.bv_val = SLAPD_ANONYMOUS;
-                               nname.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
+                       if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
+                               BER_BVSTR( &name, SLAPD_ANONYMOUS );
+                               nname = name;
                        } else {
                                name = be->be_rootdn;
                                nname = be->be_rootndn;
@@ -306,7 +308,15 @@ slapadd( int argc, char **argv )
                        }
                }
 
-               if ( update_ctxcsn == SLAP_TOOL_CTXCSN_KEEP ) {
+               if ( dryrun ) {
+                       if ( verbose ) {
+                               fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
+                       }
+                       goto done;
+               }
+
+               if ( update_ctxcsn == SLAP_TOOL_CTXCSN_KEEP &&
+                       ( replica_promotion || replica_demotion )) {
                        if ( is_entry_syncProviderSubentry( e )) { 
                                if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
                                        fprintf( stderr, "%s: consumer and provider subentries "
@@ -400,8 +410,13 @@ slapadd( int argc, char **argv )
                        }
                }
 
-               if ( !is_entry_syncProviderSubentry( e ) &&
-                        !is_entry_syncConsumerSubentry( e )) {
+               if (( !is_entry_syncProviderSubentry( e ) &&
+                                !is_entry_syncConsumerSubentry( e )) ||
+                                ( !replica_promotion && !replica_demotion ))
+               {
+                       /* dryrun moved earlier */
+                       assert( !dryrun );
+
                        if (!dryrun) {
                                ID id = be->be_entry_put( be, e, &bvtext );
                                if( id == NOID ) {
@@ -425,6 +440,7 @@ slapadd( int argc, char **argv )
                        }
                }
 
+done:;
                entry_free( e );
        }
 
@@ -436,22 +452,28 @@ slapadd( int argc, char **argv )
                maxcsn.bv_len = 0;
                maxcsn.bv_val = NULL;
                LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
+                       sc.octet_str = &sei->cookie;
+                       slap_parse_sync_cookie( &sc );
                        if ( maxcsn.bv_len != 0 ) {
                                value_match( &match, slap_schema.si_ad_syncreplCookie,
                                        slap_schema.si_ad_syncreplCookie->ad_type->sat_ordering,
                                        SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
-                                       &maxcsn, &sei->cookie, &text );
+                                       &maxcsn, &sc.ctxcsn[0], &text );
                        } else {
                                match = -1;
                        }
                        if ( match < 0 ) {
                                if ( maxcsn.bv_val )
                                        ch_free( maxcsn.bv_val );
-                               ber_dupbv( &maxcsn, &sei->cookie );
+                               ber_dupbv( &maxcsn, &sc.ctxcsn[0] );
                        }
+                       sc.octet_str = NULL;
+                       slap_sync_cookie_free( &sc, 0 );
                }
        }
 
+       slap_compose_sync_cookie( NULL, &mc, &maxcsn, -1, -1 );
+
        if ( SLAP_LASTMOD(be) && replica_promotion ) {
                if ( provider_subentry || update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
                         !LDAP_SLIST_EMPTY( &consumer_subentry )) {
@@ -461,6 +483,10 @@ slapadd( int argc, char **argv )
                
                        if ( ctxcsn_id == NOID ) {
                                ctxcsn_e = slap_create_context_csn_entry( be, &maxcsn );
+                               
+                               /* dryrun moved earlier */
+                               assert( !dryrun );
+
                                if ( !dryrun ) {
                                        ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
                                        if( ctxcsn_id == NOID ) {
@@ -486,6 +512,10 @@ slapadd( int argc, char **argv )
                                        AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
                                        attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
                                        attr->a_vals[0].bv_len = maxcsn.bv_len;
+                               
+                                       /* dryrun moved earlier */
+                                       assert( !dryrun );
+
                                        if ( !dryrun ) {
                                                ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
                                                if( ctxcsn_id == NOID ) {
@@ -526,19 +556,23 @@ slapadd( int argc, char **argv )
 
                for ( i = 0; replica_id_list[i] > -1 ; i++ ) {
                        slap_syncrepl_bv.bv_len = snprintf( slap_syncrepl_bv.bv_val,
-                                                                       slap_syncrepl_bvc.bv_len,
+                                                                       slap_syncrepl_bvc.bv_len+1,
                                                                        "syncrepl%d", replica_id_list[i] );
                        slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
-                                                                               slap_syncrepl_cn_bvc.bv_len,
+                                                                               slap_syncrepl_cn_bvc.bv_len+1,
                                                                                "cn=syncrepl%d", replica_id_list[i] );
                        build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
                                                  (struct berval *)&slap_syncrepl_cn_bv, NULL );
                        ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
 
                        if ( ctxcsn_id == NOID ) {
-                               ctxcsn_e = slap_create_syncrepl_entry( be, &maxcsn,
-                                                                                               &slap_syncrepl_cn_bv,
-                                                                                               &slap_syncrepl_bv );
+                               ctxcsn_e = slap_create_syncrepl_entry( be, &mc,
+                                               &slap_syncrepl_cn_bv,
+                                               &slap_syncrepl_bv );
+
+                               /* dryrun moved earlier */
+                               assert( !dryrun );
+
                                if ( !dryrun ) {
                                        ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
                                        if( ctxcsn_id == NOID ) {
@@ -562,9 +596,13 @@ slapadd( int argc, char **argv )
                                if ( ret == LDAP_SUCCESS ) {
                                        attr = attr_find( ctxcsn_e->e_attrs,
                                                                          slap_schema.si_ad_syncreplCookie );
-                                       AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
+                                       AC_MEMCPY( attr->a_vals[0].bv_val, mc.bv_val, mc.bv_len );
                                        attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
                                        attr->a_vals[0].bv_len = maxcsn.bv_len;
+                               
+                                       /* dryrun moved earlier */
+                                       assert( !dryrun );
+
                                        if ( !dryrun ) {
                                                ctxcsn_id = be->be_entry_modify( be,
                                                                                        ctxcsn_e, &bvtext );
@@ -605,7 +643,11 @@ slapadd( int argc, char **argv )
 
                        if ( ctxcsn_id == NOID ) {
                                ctxcsn_e = slap_create_syncrepl_entry( be, &sei->cookie,
-                                                                                               &sei->rdn, &sei->cn );
+                                               &sei->rdn, &sei->cn );
+
+                               /* dryrun moved earlier */
+                               assert( !dryrun );
+
                                if ( !dryrun ) {
                                        ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
                                        if( ctxcsn_id == NOID ) {
@@ -629,9 +671,13 @@ slapadd( int argc, char **argv )
                                if ( ret == LDAP_SUCCESS ) {
                                        attr = attr_find( ctxcsn_e->e_attrs,
                                                                          slap_schema.si_ad_syncreplCookie );
-                                       AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
-                                       attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
-                                       attr->a_vals[0].bv_len = maxcsn.bv_len;
+                                       AC_MEMCPY( attr->a_vals[0].bv_val, sei->cookie.bv_val, sei->cookie.bv_len );
+                                       attr->a_vals[0].bv_val[sei->cookie.bv_len] = '\0';
+                                       attr->a_vals[0].bv_len = sei->cookie.bv_len;
+                                       
+                                       /* dryrun moved earlier */
+                                       assert( !dryrun );
+
                                        if ( !dryrun ) {
                                                ctxcsn_id = be->be_entry_modify( be,
                                                                                        ctxcsn_e, &bvtext );
@@ -679,12 +725,18 @@ slapadd( int argc, char **argv )
 
        ch_free( buf );
 
-       if( be->be_entry_close( be )) rc = EXIT_FAILURE;
+       if ( !dryrun ) {
+               if( be->be_entry_close( be ) ) {
+                       rc = EXIT_FAILURE;
+               }
 
-       if( be->be_sync ) {
-               be->be_sync( be );
+               if( be->be_sync ) {
+                       be->be_sync( be );
+               }
        }
 
        slap_tool_destroy();
+
        return rc;
 }
+