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",
105 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
106 fprintf( stderr, "%s: could not open database.\n",
108 exit( EXIT_FAILURE );
111 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
112 Entry *e = str2entry( buf );
115 * Initialize text buffer
117 bvtext.bv_len = textlen;
118 bvtext.bv_val = textbuf;
119 bvtext.bv_val[0] = '\0';
122 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
125 if( continuemode ) continue;
129 /* make sure the DN is not empty */
130 if( !e->e_nname.bv_len ) {
131 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
132 progname, e->e_dn, lineno );
135 if( continuemode ) continue;
140 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
143 fprintf( stderr, "%s: line %d: "
144 "database (%s) not configured to hold \"%s\"\n",
146 be ? be->be_suffix[0].bv_val : "<none>",
148 fprintf( stderr, "%s: line %d: "
149 "database (%s) not configured to hold \"%s\"\n",
151 be ? be->be_nsuffix[0].bv_val : "<none>",
155 if( continuemode ) continue;
159 if( global_schemacheck ) {
160 Attribute *sc = attr_find( e->e_attrs,
161 slap_schema.si_ad_structuralObjectClass );
162 Attribute *oc = attr_find( e->e_attrs,
163 slap_schema.si_ad_objectClass );
166 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
167 progname, e->e_dn, lineno,
168 "no objectClass attribute");
171 if( continuemode ) continue;
176 struct berval vals[2];
178 rc = structural_class( oc->a_vals, vals,
179 NULL, &text, textbuf, textlen );
181 if( rc != LDAP_SUCCESS ) {
182 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
183 progname, e->e_dn, lineno, rc, text );
186 if( continuemode ) continue;
191 vals[1].bv_val = NULL;
193 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
194 vals, NULL /* FIXME */ );
198 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
200 if( rc != LDAP_SUCCESS ) {
201 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
202 progname, e->e_dn, lineno, rc, text );
205 if( continuemode ) continue;
210 if ( SLAP_LASTMOD(be) ) {
212 time_t now = slap_get_time();
213 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
214 struct berval vals[ 2 ];
216 struct berval name, timestamp;
218 struct berval nvals[ 2 ];
220 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
223 vals[1].bv_val = NULL;
226 nvals[1].bv_val = NULL;
229 lutil_gentime( timebuf, sizeof(timebuf), ltm );
231 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
234 timestamp.bv_val = timebuf;
235 timestamp.bv_len = strlen(timebuf);
237 if ( be->be_rootndn.bv_len == 0 ) {
238 name.bv_val = SLAPD_ANONYMOUS;
239 name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
240 nname.bv_val = SLAPD_ANONYMOUS;
241 nname.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
243 name = be->be_rootdn;
244 nname = be->be_rootndn;
247 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
250 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
251 vals[0].bv_val = uuidbuf;
252 attr_merge_normalize_one( e,
253 slap_schema.si_ad_entryUUID, vals, NULL );
256 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
261 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
264 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
269 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
272 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
276 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
279 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
283 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
286 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
290 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
293 if ( !is_entry_syncProviderSubentry( e ) &&
294 !is_entry_syncConsumerSubentry( e ) &&
295 update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
296 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
297 if ( maxcsn.bv_len != 0 ) {
298 value_match( &match, slap_schema.si_ad_entryCSN,
299 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
300 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
301 &maxcsn, &attr->a_nvals[0], &text );
307 ch_free( maxcsn.bv_val );
308 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
315 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
320 if ( update_ctxcsn == SLAP_TOOL_CTXCSN_KEEP &&
321 ( replica_promotion || replica_demotion )) {
322 if ( is_entry_syncProviderSubentry( e )) {
323 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
324 fprintf( stderr, "%s: consumer and provider subentries "
325 "are both present\n", progname );
328 sei = LDAP_SLIST_FIRST( &consumer_subentry );
330 ch_free( sei->cn.bv_val );
331 ch_free( sei->ndn.bv_val );
332 ch_free( sei->rdn.bv_val );
333 ch_free( sei->cookie.bv_val );
334 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
336 sei = LDAP_SLIST_FIRST( &consumer_subentry );
340 if ( provider_subentry ) {
341 fprintf( stderr, "%s: multiple provider subentries are "
342 "present : add -w flag to refresh\n", progname );
347 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
348 if ( attr == NULL ) {
352 provider_subentry = 1;
353 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
354 } else if ( is_entry_syncConsumerSubentry( e )) {
355 if ( provider_subentry ) {
356 fprintf( stderr, "%s: consumer and provider subentries "
357 "are both present\n", progname );
363 attr = attr_find( e->e_attrs, slap_schema.si_ad_cn );
365 if ( attr == NULL ) {
370 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
371 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
372 value_match( &match, slap_schema.si_ad_cn,
373 slap_schema.si_ad_cn->ad_type->sat_equality,
374 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
375 &sei->cn, &attr->a_nvals[0], &text );
378 fprintf( stderr, "%s: multiple consumer subentries "
379 "have the same id : add -w flag to refresh\n",
383 sei = LDAP_SLIST_FIRST( &consumer_subentry );
385 ch_free( sei->cn.bv_val );
386 ch_free( sei->ndn.bv_val );
387 ch_free( sei->rdn.bv_val );
388 ch_free( sei->cookie.bv_val );
389 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
391 sei = LDAP_SLIST_FIRST( &consumer_subentry );
396 sei = ch_calloc( 1, sizeof( struct subentryinfo ));
397 ber_dupbv( &sei->cn, &attr->a_nvals[0] );
398 ber_dupbv( &sei->ndn, &e->e_nname );
399 dnExtractRdn( &sei->ndn, &sei->rdn, NULL );
400 attr = attr_find( e->e_attrs, slap_schema.si_ad_syncreplCookie );
401 if ( attr == NULL ) {
402 ch_free( sei->cn.bv_val );
403 ch_free( sei->ndn.bv_val );
404 ch_free( sei->rdn.bv_val );
405 ch_free( sei->cookie.bv_val );
410 ber_dupbv( &sei->cookie, &attr->a_nvals[0] );
411 LDAP_SLIST_INSERT_HEAD( &consumer_subentry, sei, sei_next );
415 if (( !is_entry_syncProviderSubentry( e ) &&
416 !is_entry_syncConsumerSubentry( e )) ||
417 ( !replica_promotion && !replica_demotion ))
419 /* dryrun moved earlier */
423 ID id = be->be_entry_put( be, e, &bvtext );
425 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
426 "(line=%d): %s\n", progname, e->e_dn,
427 lineno, bvtext.bv_val );
430 if( continuemode ) continue;
435 fprintf( stderr, "added: \"%s\" (%08lx)\n",
436 e->e_dn, (long) id );
440 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
449 bvtext.bv_len = textlen;
450 bvtext.bv_val = textbuf;
451 bvtext.bv_val[0] = '\0';
453 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
455 maxcsn.bv_val = NULL;
456 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
457 sc.octet_str = &sei->cookie;
458 slap_parse_sync_cookie( &sc );
459 if ( maxcsn.bv_len != 0 ) {
460 value_match( &match, slap_schema.si_ad_syncreplCookie,
461 slap_schema.si_ad_syncreplCookie->ad_type->sat_ordering,
462 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
463 &maxcsn, &sc.ctxcsn[0], &text );
469 ch_free( maxcsn.bv_val );
470 ber_dupbv( &maxcsn, &sc.ctxcsn[0] );
473 slap_sync_cookie_free( &sc, 0 );
477 slap_compose_sync_cookie( NULL, &mc, &maxcsn, -1, -1 );
479 if ( SLAP_LASTMOD(be) && replica_promotion ) {
480 if ( provider_subentry || update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
481 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
482 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
483 (struct berval *)&slap_ldapsync_cn_bv, NULL );
484 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
486 if ( ctxcsn_id == NOID ) {
487 ctxcsn_e = slap_create_context_csn_entry( be, &maxcsn );
489 /* dryrun moved earlier */
493 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
494 if( ctxcsn_id == NOID ) {
495 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
500 fprintf( stderr, "added: \"%s\" (%08lx)\n",
501 ctxcsn_e->e_dn, (long) ctxcsn_id );
505 fprintf( stderr, "(dry) added: \"%s\"\n", ctxcsn_e->e_dn );
508 entry_free( ctxcsn_e );
510 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
511 if ( ret == LDAP_SUCCESS ) {
512 attr = attr_find( ctxcsn_e->e_attrs,
513 slap_schema.si_ad_contextCSN );
514 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
515 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
516 attr->a_vals[0].bv_len = maxcsn.bv_len;
518 /* dryrun moved earlier */
522 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
523 if( ctxcsn_id == NOID ) {
524 fprintf( stderr, "%s: could not modify ctxcsn "
525 "subentry\n", progname);
529 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
530 ctxcsn_e->e_dn, (long) ctxcsn_id );
534 fprintf( stderr, "(dry) modified: \"%s\"\n",
539 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
545 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
546 ( update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
547 provider_subentry )) {
549 ber_dupbv( &slap_syncrepl_bv, (struct berval *) &slap_syncrepl_bvc );
550 ber_dupbv( &slap_syncrepl_cn_bv,
551 (struct berval *) &slap_syncrepl_cn_bvc );
553 if ( replica_id_list == NULL ) {
554 replica_id_list = ch_calloc( 2, sizeof( int ));
555 replica_id_list[0] = 0;
556 replica_id_list[1] = -1;
559 for ( i = 0; replica_id_list[i] > -1 ; i++ ) {
560 slap_syncrepl_bv.bv_len = snprintf( slap_syncrepl_bv.bv_val,
561 slap_syncrepl_bvc.bv_len+1,
562 "syncrepl%d", replica_id_list[i] );
563 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
564 slap_syncrepl_cn_bvc.bv_len+1,
565 "cn=syncrepl%d", replica_id_list[i] );
566 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
567 (struct berval *)&slap_syncrepl_cn_bv, NULL );
568 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
570 if ( ctxcsn_id == NOID ) {
571 ctxcsn_e = slap_create_syncrepl_entry( be, &mc,
572 &slap_syncrepl_cn_bv,
575 /* dryrun moved earlier */
579 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
580 if( ctxcsn_id == NOID ) {
581 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
586 fprintf( stderr, "added: \"%s\" (%08lx)\n",
587 ctxcsn_e->e_dn, (long) ctxcsn_id );
591 fprintf( stderr, "(dry) added: \"%s\"\n",
595 entry_free( ctxcsn_e );
597 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
598 if ( ret == LDAP_SUCCESS ) {
599 attr = attr_find( ctxcsn_e->e_attrs,
600 slap_schema.si_ad_syncreplCookie );
601 AC_MEMCPY( attr->a_vals[0].bv_val, mc.bv_val, mc.bv_len );
602 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
603 attr->a_vals[0].bv_len = maxcsn.bv_len;
605 /* dryrun moved earlier */
609 ctxcsn_id = be->be_entry_modify( be,
611 if( ctxcsn_id == NOID ) {
612 fprintf( stderr, "%s: could not modify ctxcsn "
613 "subentry\n", progname);
617 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
618 ctxcsn_e->e_dn, (long) ctxcsn_id );
622 fprintf( stderr, "(dry) modified: \"%s\"\n",
627 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
634 if ( slap_syncrepl_bv.bv_val ) {
635 ch_free( slap_syncrepl_bv.bv_val );
637 if ( slap_syncrepl_cn_bv.bv_val ) {
638 ch_free( slap_syncrepl_cn_bv.bv_val );
640 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
641 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
643 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
644 ctxcsn_id = be->be_dn2id_get( be, &sei->ndn );
646 if ( ctxcsn_id == NOID ) {
647 ctxcsn_e = slap_create_syncrepl_entry( be, &sei->cookie,
648 &sei->rdn, &sei->cn );
650 /* dryrun moved earlier */
654 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
655 if( ctxcsn_id == NOID ) {
656 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
661 fprintf( stderr, "added: \"%s\" (%08lx)\n",
662 ctxcsn_e->e_dn, (long) ctxcsn_id );
666 fprintf( stderr, "(dry) added: \"%s\"\n",
670 entry_free( ctxcsn_e );
672 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
673 if ( ret == LDAP_SUCCESS ) {
674 attr = attr_find( ctxcsn_e->e_attrs,
675 slap_schema.si_ad_syncreplCookie );
676 AC_MEMCPY( attr->a_vals[0].bv_val, sei->cookie.bv_val, sei->cookie.bv_len );
677 attr->a_vals[0].bv_val[sei->cookie.bv_len] = '\0';
678 attr->a_vals[0].bv_len = sei->cookie.bv_len;
680 /* dryrun moved earlier */
684 ctxcsn_id = be->be_entry_modify( be,
686 if( ctxcsn_id == NOID ) {
687 fprintf( stderr, "%s: could not modify ctxcsn "
688 "subentry\n", progname);
692 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
693 ctxcsn_e->e_dn, (long) ctxcsn_id );
697 fprintf( stderr, "(dry) modified: \"%s\"\n",
702 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
709 if ( slap_syncrepl_bv.bv_val ) {
710 ch_free( slap_syncrepl_bv.bv_val );
712 if ( slap_syncrepl_cn_bv.bv_val ) {
713 ch_free( slap_syncrepl_cn_bv.bv_val );
717 sei = LDAP_SLIST_FIRST( &consumer_subentry );
719 ch_free( sei->cn.bv_val );
720 ch_free( sei->ndn.bv_val );
721 ch_free( sei->rdn.bv_val );
722 ch_free( sei->cookie.bv_val );
723 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
725 sei = LDAP_SLIST_FIRST( &consumer_subentry );
731 if( be->be_entry_close( be ) ) {