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 ( BER_BVISEMPTY( &be->be_rootndn ) ) {
241 BER_BVSTR( &name, SLAPD_ANONYMOUS );
244 name = be->be_rootdn;
245 nname = be->be_rootndn;
248 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
251 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
252 vals[0].bv_val = uuidbuf;
253 attr_merge_normalize_one( e,
254 slap_schema.si_ad_entryUUID, vals, NULL );
257 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
262 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
265 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
270 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
273 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
277 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
280 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
284 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
287 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
291 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
294 if ( !is_entry_syncProviderSubentry( e ) &&
295 !is_entry_syncConsumerSubentry( e ) &&
296 update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
297 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
298 if ( maxcsn.bv_len != 0 ) {
299 value_match( &match, slap_schema.si_ad_entryCSN,
300 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
301 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
302 &maxcsn, &attr->a_nvals[0], &text );
308 ch_free( maxcsn.bv_val );
309 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
316 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
321 if ( update_ctxcsn == SLAP_TOOL_CTXCSN_KEEP &&
322 ( replica_promotion || replica_demotion )) {
323 if ( is_entry_syncProviderSubentry( e )) {
324 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
325 fprintf( stderr, "%s: consumer and provider subentries "
326 "are both present\n", progname );
329 sei = LDAP_SLIST_FIRST( &consumer_subentry );
331 ch_free( sei->cn.bv_val );
332 ch_free( sei->ndn.bv_val );
333 ch_free( sei->rdn.bv_val );
334 ch_free( sei->cookie.bv_val );
335 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
337 sei = LDAP_SLIST_FIRST( &consumer_subentry );
341 if ( provider_subentry ) {
342 fprintf( stderr, "%s: multiple provider subentries are "
343 "present : add -w flag to refresh\n", progname );
348 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
349 if ( attr == NULL ) {
353 provider_subentry = 1;
354 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
355 } else if ( is_entry_syncConsumerSubentry( e )) {
356 if ( provider_subentry ) {
357 fprintf( stderr, "%s: consumer and provider subentries "
358 "are both present\n", progname );
364 attr = attr_find( e->e_attrs, slap_schema.si_ad_cn );
366 if ( attr == NULL ) {
371 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
372 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
373 value_match( &match, slap_schema.si_ad_cn,
374 slap_schema.si_ad_cn->ad_type->sat_equality,
375 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
376 &sei->cn, &attr->a_nvals[0], &text );
379 fprintf( stderr, "%s: multiple consumer subentries "
380 "have the same id : add -w flag to refresh\n",
384 sei = LDAP_SLIST_FIRST( &consumer_subentry );
386 ch_free( sei->cn.bv_val );
387 ch_free( sei->ndn.bv_val );
388 ch_free( sei->rdn.bv_val );
389 ch_free( sei->cookie.bv_val );
390 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
392 sei = LDAP_SLIST_FIRST( &consumer_subentry );
397 sei = ch_calloc( 1, sizeof( struct subentryinfo ));
398 ber_dupbv( &sei->cn, &attr->a_nvals[0] );
399 ber_dupbv( &sei->ndn, &e->e_nname );
400 dnExtractRdn( &sei->ndn, &sei->rdn, NULL );
401 attr = attr_find( e->e_attrs, slap_schema.si_ad_syncreplCookie );
402 if ( attr == NULL ) {
403 ch_free( sei->cn.bv_val );
404 ch_free( sei->ndn.bv_val );
405 ch_free( sei->rdn.bv_val );
406 ch_free( sei->cookie.bv_val );
411 ber_dupbv( &sei->cookie, &attr->a_nvals[0] );
412 LDAP_SLIST_INSERT_HEAD( &consumer_subentry, sei, sei_next );
416 if (( !is_entry_syncProviderSubentry( e ) &&
417 !is_entry_syncConsumerSubentry( e )) ||
418 ( !replica_promotion && !replica_demotion ))
420 /* dryrun moved earlier */
424 ID id = be->be_entry_put( be, e, &bvtext );
426 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
427 "(line=%d): %s\n", progname, e->e_dn,
428 lineno, bvtext.bv_val );
431 if( continuemode ) continue;
436 fprintf( stderr, "added: \"%s\" (%08lx)\n",
437 e->e_dn, (long) id );
441 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
450 bvtext.bv_len = textlen;
451 bvtext.bv_val = textbuf;
452 bvtext.bv_val[0] = '\0';
454 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
456 maxcsn.bv_val = NULL;
457 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
458 sc.octet_str = &sei->cookie;
459 slap_parse_sync_cookie( &sc );
460 if ( maxcsn.bv_len != 0 ) {
461 value_match( &match, slap_schema.si_ad_syncreplCookie,
462 slap_schema.si_ad_syncreplCookie->ad_type->sat_ordering,
463 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
464 &maxcsn, &sc.ctxcsn[0], &text );
470 ch_free( maxcsn.bv_val );
471 ber_dupbv( &maxcsn, &sc.ctxcsn[0] );
474 slap_sync_cookie_free( &sc, 0 );
478 slap_compose_sync_cookie( NULL, &mc, &maxcsn, -1, -1 );
480 if ( SLAP_LASTMOD(be) && replica_promotion ) {
481 if ( provider_subentry || update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
482 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
483 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
484 (struct berval *)&slap_ldapsync_cn_bv, NULL );
485 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
487 if ( ctxcsn_id == NOID ) {
488 ctxcsn_e = slap_create_context_csn_entry( be, &maxcsn );
490 /* dryrun moved earlier */
494 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
495 if( ctxcsn_id == NOID ) {
496 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
501 fprintf( stderr, "added: \"%s\" (%08lx)\n",
502 ctxcsn_e->e_dn, (long) ctxcsn_id );
506 fprintf( stderr, "(dry) added: \"%s\"\n", ctxcsn_e->e_dn );
509 entry_free( ctxcsn_e );
511 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
512 if ( ret == LDAP_SUCCESS ) {
513 attr = attr_find( ctxcsn_e->e_attrs,
514 slap_schema.si_ad_contextCSN );
515 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
516 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
517 attr->a_vals[0].bv_len = maxcsn.bv_len;
519 /* dryrun moved earlier */
523 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
524 if( ctxcsn_id == NOID ) {
525 fprintf( stderr, "%s: could not modify ctxcsn "
526 "subentry\n", progname);
530 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
531 ctxcsn_e->e_dn, (long) ctxcsn_id );
535 fprintf( stderr, "(dry) modified: \"%s\"\n",
540 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
546 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
547 ( update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
548 provider_subentry )) {
550 ber_dupbv( &slap_syncrepl_bv, (struct berval *) &slap_syncrepl_bvc );
551 ber_dupbv( &slap_syncrepl_cn_bv,
552 (struct berval *) &slap_syncrepl_cn_bvc );
554 if ( replica_id_list == NULL ) {
555 replica_id_list = ch_calloc( 2, sizeof( int ));
556 replica_id_list[0] = 0;
557 replica_id_list[1] = -1;
560 for ( i = 0; replica_id_list[i] > -1 ; i++ ) {
561 slap_syncrepl_bv.bv_len = snprintf( slap_syncrepl_bv.bv_val,
562 slap_syncrepl_bvc.bv_len+1,
563 "syncrepl%d", replica_id_list[i] );
564 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
565 slap_syncrepl_cn_bvc.bv_len+1,
566 "cn=syncrepl%d", replica_id_list[i] );
567 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
568 (struct berval *)&slap_syncrepl_cn_bv, NULL );
569 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
571 if ( ctxcsn_id == NOID ) {
572 ctxcsn_e = slap_create_syncrepl_entry( be, &mc,
573 &slap_syncrepl_cn_bv,
576 /* dryrun moved earlier */
580 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
581 if( ctxcsn_id == NOID ) {
582 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
587 fprintf( stderr, "added: \"%s\" (%08lx)\n",
588 ctxcsn_e->e_dn, (long) ctxcsn_id );
592 fprintf( stderr, "(dry) added: \"%s\"\n",
596 entry_free( ctxcsn_e );
598 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
599 if ( ret == LDAP_SUCCESS ) {
600 attr = attr_find( ctxcsn_e->e_attrs,
601 slap_schema.si_ad_syncreplCookie );
602 AC_MEMCPY( attr->a_vals[0].bv_val, mc.bv_val, mc.bv_len );
603 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
604 attr->a_vals[0].bv_len = maxcsn.bv_len;
606 /* dryrun moved earlier */
610 ctxcsn_id = be->be_entry_modify( be,
612 if( ctxcsn_id == NOID ) {
613 fprintf( stderr, "%s: could not modify ctxcsn "
614 "subentry\n", progname);
618 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
619 ctxcsn_e->e_dn, (long) ctxcsn_id );
623 fprintf( stderr, "(dry) modified: \"%s\"\n",
628 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
635 if ( slap_syncrepl_bv.bv_val ) {
636 ch_free( slap_syncrepl_bv.bv_val );
638 if ( slap_syncrepl_cn_bv.bv_val ) {
639 ch_free( slap_syncrepl_cn_bv.bv_val );
641 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
642 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
644 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
645 ctxcsn_id = be->be_dn2id_get( be, &sei->ndn );
647 if ( ctxcsn_id == NOID ) {
648 ctxcsn_e = slap_create_syncrepl_entry( be, &sei->cookie,
649 &sei->rdn, &sei->cn );
651 /* dryrun moved earlier */
655 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
656 if( ctxcsn_id == NOID ) {
657 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
662 fprintf( stderr, "added: \"%s\" (%08lx)\n",
663 ctxcsn_e->e_dn, (long) ctxcsn_id );
667 fprintf( stderr, "(dry) added: \"%s\"\n",
671 entry_free( ctxcsn_e );
673 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
674 if ( ret == LDAP_SUCCESS ) {
675 attr = attr_find( ctxcsn_e->e_attrs,
676 slap_schema.si_ad_syncreplCookie );
677 AC_MEMCPY( attr->a_vals[0].bv_val, sei->cookie.bv_val, sei->cookie.bv_len );
678 attr->a_vals[0].bv_val[sei->cookie.bv_len] = '\0';
679 attr->a_vals[0].bv_len = sei->cookie.bv_len;
681 /* dryrun moved earlier */
685 ctxcsn_id = be->be_entry_modify( be,
687 if( ctxcsn_id == NOID ) {
688 fprintf( stderr, "%s: could not modify ctxcsn "
689 "subentry\n", progname);
693 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
694 ctxcsn_e->e_dn, (long) ctxcsn_id );
698 fprintf( stderr, "(dry) modified: \"%s\"\n",
703 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
710 if ( slap_syncrepl_bv.bv_val ) {
711 ch_free( slap_syncrepl_bv.bv_val );
713 if ( slap_syncrepl_cn_bv.bv_val ) {
714 ch_free( slap_syncrepl_cn_bv.bv_val );
718 sei = LDAP_SLIST_FIRST( &consumer_subentry );
720 ch_free( sei->cn.bv_val );
721 ch_free( sei->ndn.bv_val );
722 ch_free( sei->rdn.bv_val );
723 ch_free( sei->cookie.bv_val );
724 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
726 sei = LDAP_SLIST_FIRST( &consumer_subentry );
732 if( be->be_entry_close( be ) ) {