2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
6 * Portions Copyright 2003 IBM Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Kurt Zeilenga for inclusion
19 * in OpenLDAP Software. Additional signficant contributors include
28 #include <ac/stdlib.h>
31 #include <ac/string.h>
32 #include <ac/socket.h>
33 #include <ac/unistd.h>
39 #include "slapcommon.h"
41 static char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
42 static const struct berval slap_syncrepl_bvc = BER_BVC("syncreplxxx");
43 static const struct berval slap_syncrepl_cn_bvc = BER_BVC("cn=syncreplxxx");
44 static struct berval slap_syncrepl_bv = BER_BVNULL;
45 static struct berval slap_syncrepl_cn_bv = BER_BVNULL;
52 LDAP_SLIST_ENTRY( subentryinfo ) sei_next;
56 slapadd( int argc, char **argv )
61 int rc = EXIT_SUCCESS;
64 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
65 size_t textlen = sizeof textbuf;
66 const char *progname = "slapadd";
69 struct berval maxcsn = BER_BVNULL;
70 struct berval ldifcsn = BER_BVNULL;
72 int provider_subentry = 0;
73 struct subentryinfo *sei;
74 LDAP_SLIST_HEAD( consumer_subentry_slist, subentryinfo ) consumer_subentry;
78 struct berval ctxcsn_ndn = BER_BVNULL;
83 struct sync_cookie sc;
85 lutil_log_initialize(argc, argv );
87 slap_tool_init( progname, SLAPADD, argc, argv );
89 LDAP_SLIST_INIT( &consumer_subentry );
91 if( !be->be_entry_open ||
92 !be->be_entry_close ||
95 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
98 fprintf( stderr, "\t(dry) continuing...\n" );
101 exit( EXIT_FAILURE );
108 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
109 fprintf( stderr, "%s: could not open database.\n",
111 exit( EXIT_FAILURE );
114 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
115 Entry *e = str2entry( buf );
118 * Initialize text buffer
120 bvtext.bv_len = textlen;
121 bvtext.bv_val = textbuf;
122 bvtext.bv_val[0] = '\0';
125 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
128 if( continuemode ) continue;
132 /* make sure the DN is not empty */
133 if( !e->e_nname.bv_len ) {
134 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
135 progname, e->e_dn, lineno );
138 if( continuemode ) continue;
143 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
146 fprintf( stderr, "%s: line %d: "
147 "database (%s) not configured to hold \"%s\"\n",
149 be ? be->be_suffix[0].bv_val : "<none>",
151 fprintf( stderr, "%s: line %d: "
152 "database (%s) not configured to hold \"%s\"\n",
154 be ? be->be_nsuffix[0].bv_val : "<none>",
158 if( continuemode ) continue;
162 if( global_schemacheck ) {
163 Attribute *sc = attr_find( e->e_attrs,
164 slap_schema.si_ad_structuralObjectClass );
165 Attribute *oc = attr_find( e->e_attrs,
166 slap_schema.si_ad_objectClass );
169 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
170 progname, e->e_dn, lineno,
171 "no objectClass attribute");
174 if( continuemode ) continue;
179 struct berval vals[2];
181 rc = structural_class( oc->a_vals, vals,
182 NULL, &text, textbuf, textlen );
184 if( rc != LDAP_SUCCESS ) {
185 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
186 progname, e->e_dn, lineno, rc, text );
189 if( continuemode ) continue;
194 vals[1].bv_val = NULL;
196 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
197 vals, NULL /* FIXME */ );
201 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
203 if( rc != LDAP_SUCCESS ) {
204 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
205 progname, e->e_dn, lineno, rc, text );
208 if( continuemode ) continue;
213 if ( SLAP_LASTMOD(be) ) {
215 time_t now = slap_get_time();
216 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
217 struct berval vals[ 2 ];
219 struct berval name, timestamp;
221 struct berval nvals[ 2 ];
223 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
226 vals[1].bv_val = NULL;
229 nvals[1].bv_val = NULL;
232 lutil_gentime( timebuf, sizeof(timebuf), ltm );
234 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
237 timestamp.bv_val = timebuf;
238 timestamp.bv_len = strlen(timebuf);
240 if ( be->be_rootndn.bv_len == 0 ) {
241 name.bv_val = SLAPD_ANONYMOUS;
242 name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
243 nname.bv_val = SLAPD_ANONYMOUS;
244 nname.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
246 name = be->be_rootdn;
247 nname = be->be_rootndn;
250 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
253 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
254 vals[0].bv_val = uuidbuf;
255 attr_merge_normalize_one( e,
256 slap_schema.si_ad_entryUUID, vals, NULL );
259 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
264 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
267 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
272 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
275 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
279 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
282 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
286 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
289 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
293 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
296 if ( !is_entry_syncProviderSubentry( e ) &&
297 !is_entry_syncConsumerSubentry( e ) &&
298 update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
299 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
300 if ( maxcsn.bv_len != 0 ) {
301 value_match( &match, slap_schema.si_ad_entryCSN,
302 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
303 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
304 &maxcsn, &attr->a_nvals[0], &text );
310 ch_free( maxcsn.bv_val );
311 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
318 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
323 if ( update_ctxcsn == SLAP_TOOL_CTXCSN_KEEP &&
324 ( replica_promotion || replica_demotion )) {
325 if ( is_entry_syncProviderSubentry( e )) {
326 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
327 fprintf( stderr, "%s: consumer and provider subentries "
328 "are both present\n", progname );
331 sei = LDAP_SLIST_FIRST( &consumer_subentry );
333 ch_free( sei->cn.bv_val );
334 ch_free( sei->ndn.bv_val );
335 ch_free( sei->rdn.bv_val );
336 ch_free( sei->cookie.bv_val );
337 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
339 sei = LDAP_SLIST_FIRST( &consumer_subentry );
343 if ( provider_subentry ) {
344 fprintf( stderr, "%s: multiple provider subentries are "
345 "present : add -w flag to refresh\n", progname );
350 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
351 if ( attr == NULL ) {
355 provider_subentry = 1;
356 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
357 } else if ( is_entry_syncConsumerSubentry( e )) {
358 if ( provider_subentry ) {
359 fprintf( stderr, "%s: consumer and provider subentries "
360 "are both present\n", progname );
366 attr = attr_find( e->e_attrs, slap_schema.si_ad_cn );
368 if ( attr == NULL ) {
373 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
374 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
375 value_match( &match, slap_schema.si_ad_cn,
376 slap_schema.si_ad_cn->ad_type->sat_equality,
377 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
378 &sei->cn, &attr->a_nvals[0], &text );
381 fprintf( stderr, "%s: multiple consumer subentries "
382 "have the same id : add -w flag to refresh\n",
386 sei = LDAP_SLIST_FIRST( &consumer_subentry );
388 ch_free( sei->cn.bv_val );
389 ch_free( sei->ndn.bv_val );
390 ch_free( sei->rdn.bv_val );
391 ch_free( sei->cookie.bv_val );
392 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
394 sei = LDAP_SLIST_FIRST( &consumer_subentry );
399 sei = ch_calloc( 1, sizeof( struct subentryinfo ));
400 ber_dupbv( &sei->cn, &attr->a_nvals[0] );
401 ber_dupbv( &sei->ndn, &e->e_nname );
402 dnExtractRdn( &sei->ndn, &sei->rdn, NULL );
403 attr = attr_find( e->e_attrs, slap_schema.si_ad_syncreplCookie );
404 if ( attr == NULL ) {
405 ch_free( sei->cn.bv_val );
406 ch_free( sei->ndn.bv_val );
407 ch_free( sei->rdn.bv_val );
408 ch_free( sei->cookie.bv_val );
413 ber_dupbv( &sei->cookie, &attr->a_nvals[0] );
414 LDAP_SLIST_INSERT_HEAD( &consumer_subentry, sei, sei_next );
418 if (( !is_entry_syncProviderSubentry( e ) &&
419 !is_entry_syncConsumerSubentry( e )) ||
420 ( !replica_promotion && !replica_demotion ))
422 /* dryrun moved earlier */
426 ID id = be->be_entry_put( be, e, &bvtext );
428 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
429 "(line=%d): %s\n", progname, e->e_dn,
430 lineno, bvtext.bv_val );
433 if( continuemode ) continue;
438 fprintf( stderr, "added: \"%s\" (%08lx)\n",
439 e->e_dn, (long) id );
443 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
452 bvtext.bv_len = textlen;
453 bvtext.bv_val = textbuf;
454 bvtext.bv_val[0] = '\0';
456 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
458 maxcsn.bv_val = NULL;
459 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
460 sc.octet_str = &sei->cookie;
461 slap_parse_sync_cookie( &sc );
462 if ( maxcsn.bv_len != 0 ) {
463 value_match( &match, slap_schema.si_ad_syncreplCookie,
464 slap_schema.si_ad_syncreplCookie->ad_type->sat_ordering,
465 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
466 &maxcsn, &sc.ctxcsn[0], &text );
472 ch_free( maxcsn.bv_val );
473 ber_dupbv( &maxcsn, &sc.ctxcsn[0] );
476 slap_sync_cookie_free( &sc, 0 );
480 slap_compose_sync_cookie( NULL, &mc, &maxcsn, -1, -1 );
482 if ( SLAP_LASTMOD(be) && replica_promotion ) {
483 if ( provider_subentry || update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
484 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
485 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
486 (struct berval *)&slap_ldapsync_cn_bv, NULL );
487 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
489 if ( ctxcsn_id == NOID ) {
490 ctxcsn_e = slap_create_context_csn_entry( be, &maxcsn );
492 /* dryrun moved earlier */
496 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
497 if( ctxcsn_id == NOID ) {
498 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
503 fprintf( stderr, "added: \"%s\" (%08lx)\n",
504 ctxcsn_e->e_dn, (long) ctxcsn_id );
508 fprintf( stderr, "(dry) added: \"%s\"\n", ctxcsn_e->e_dn );
511 entry_free( ctxcsn_e );
513 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
514 if ( ret == LDAP_SUCCESS ) {
515 attr = attr_find( ctxcsn_e->e_attrs,
516 slap_schema.si_ad_contextCSN );
517 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
518 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
519 attr->a_vals[0].bv_len = maxcsn.bv_len;
521 /* dryrun moved earlier */
525 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
526 if( ctxcsn_id == NOID ) {
527 fprintf( stderr, "%s: could not modify ctxcsn "
528 "subentry\n", progname);
532 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
533 ctxcsn_e->e_dn, (long) ctxcsn_id );
537 fprintf( stderr, "(dry) modified: \"%s\"\n",
542 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
548 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
549 ( update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
550 provider_subentry )) {
552 ber_dupbv( &slap_syncrepl_bv, (struct berval *) &slap_syncrepl_bvc );
553 ber_dupbv( &slap_syncrepl_cn_bv,
554 (struct berval *) &slap_syncrepl_cn_bvc );
556 if ( replica_id_list == NULL ) {
557 replica_id_list = ch_calloc( 2, sizeof( int ));
558 replica_id_list[0] = 0;
559 replica_id_list[1] = -1;
562 for ( i = 0; replica_id_list[i] > -1 ; i++ ) {
563 slap_syncrepl_bv.bv_len = snprintf( slap_syncrepl_bv.bv_val,
564 slap_syncrepl_bvc.bv_len,
565 "syncrepl%d", replica_id_list[i] );
566 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
567 slap_syncrepl_cn_bvc.bv_len,
568 "cn=syncrepl%d", replica_id_list[i] );
569 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
570 (struct berval *)&slap_syncrepl_cn_bv, NULL );
571 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
573 if ( ctxcsn_id == NOID ) {
574 ctxcsn_e = slap_create_syncrepl_entry( be, &mc,
575 &slap_syncrepl_cn_bv,
578 /* dryrun moved earlier */
582 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
583 if( ctxcsn_id == NOID ) {
584 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
589 fprintf( stderr, "added: \"%s\" (%08lx)\n",
590 ctxcsn_e->e_dn, (long) ctxcsn_id );
594 fprintf( stderr, "(dry) added: \"%s\"\n",
598 entry_free( ctxcsn_e );
600 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
601 if ( ret == LDAP_SUCCESS ) {
602 attr = attr_find( ctxcsn_e->e_attrs,
603 slap_schema.si_ad_syncreplCookie );
604 AC_MEMCPY( attr->a_vals[0].bv_val, mc.bv_val, mc.bv_len );
605 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
606 attr->a_vals[0].bv_len = maxcsn.bv_len;
608 /* dryrun moved earlier */
612 ctxcsn_id = be->be_entry_modify( be,
614 if( ctxcsn_id == NOID ) {
615 fprintf( stderr, "%s: could not modify ctxcsn "
616 "subentry\n", progname);
620 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
621 ctxcsn_e->e_dn, (long) ctxcsn_id );
625 fprintf( stderr, "(dry) modified: \"%s\"\n",
630 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
637 if ( slap_syncrepl_bv.bv_val ) {
638 ch_free( slap_syncrepl_bv.bv_val );
640 if ( slap_syncrepl_cn_bv.bv_val ) {
641 ch_free( slap_syncrepl_cn_bv.bv_val );
643 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
644 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
646 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
647 ctxcsn_id = be->be_dn2id_get( be, &sei->ndn );
649 if ( ctxcsn_id == NOID ) {
650 ctxcsn_e = slap_create_syncrepl_entry( be, &sei->cookie,
651 &sei->rdn, &sei->cn );
653 /* dryrun moved earlier */
657 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
658 if( ctxcsn_id == NOID ) {
659 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
664 fprintf( stderr, "added: \"%s\" (%08lx)\n",
665 ctxcsn_e->e_dn, (long) ctxcsn_id );
669 fprintf( stderr, "(dry) added: \"%s\"\n",
673 entry_free( ctxcsn_e );
675 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
676 if ( ret == LDAP_SUCCESS ) {
677 attr = attr_find( ctxcsn_e->e_attrs,
678 slap_schema.si_ad_syncreplCookie );
679 AC_MEMCPY( attr->a_vals[0].bv_val, sei->cookie.bv_val, sei->cookie.bv_len );
680 attr->a_vals[0].bv_val[sei->cookie.bv_len] = '\0';
681 attr->a_vals[0].bv_len = sei->cookie.bv_len;
683 /* dryrun moved earlier */
687 ctxcsn_id = be->be_entry_modify( be,
689 if( ctxcsn_id == NOID ) {
690 fprintf( stderr, "%s: could not modify ctxcsn "
691 "subentry\n", progname);
695 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
696 ctxcsn_e->e_dn, (long) ctxcsn_id );
700 fprintf( stderr, "(dry) modified: \"%s\"\n",
705 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
712 if ( slap_syncrepl_bv.bv_val ) {
713 ch_free( slap_syncrepl_bv.bv_val );
715 if ( slap_syncrepl_cn_bv.bv_val ) {
716 ch_free( slap_syncrepl_cn_bv.bv_val );
720 sei = LDAP_SLIST_FIRST( &consumer_subentry );
722 ch_free( sei->cn.bv_val );
723 ch_free( sei->ndn.bv_val );
724 ch_free( sei->rdn.bv_val );
725 ch_free( sei->cookie.bv_val );
726 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
728 sei = LDAP_SLIST_FIRST( &consumer_subentry );
734 if( be->be_entry_close( be ) ) {