3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdlib.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15 #include <ac/unistd.h>
21 #include "slapcommon.h"
23 static char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
24 static const struct berval slap_syncrepl_bvc = BER_BVC("syncreplxxx");
25 static const struct berval slap_syncrepl_cn_bvc = BER_BVC("cn=syncreplxxx");
26 static struct berval slap_syncrepl_bv = BER_BVNULL;
27 static struct berval slap_syncrepl_cn_bv = BER_BVNULL;
34 LDAP_SLIST_ENTRY( subentryinfo ) sei_next;
38 main( int argc, char **argv )
43 int rc = EXIT_SUCCESS;
46 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
47 size_t textlen = sizeof textbuf;
50 struct berval maxcsn = { 0, NULL };
51 struct berval ldifcsn = { 0, NULL };
53 int provider_subentry = 0;
54 struct subentryinfo *sei;
55 LDAP_SLIST_HEAD( consumer_subentry_slist, subentryinfo ) consumer_subentry;
59 struct berval ctxcsn_ndn = { 0, NULL };
64 lutil_log_initialize(argc, argv );
66 slap_tool_init( "slapadd", SLAPADD, argc, argv );
68 LDAP_SLIST_INIT( &consumer_subentry );
70 if( !be->be_entry_open ||
71 !be->be_entry_close ||
74 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
82 if( be->be_entry_open( be, 1 ) != 0 ) {
83 fprintf( stderr, "%s: could not open database.\n",
88 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
89 Entry *e = str2entry( buf );
92 * Initialize text buffer
94 bvtext.bv_len = textlen;
95 bvtext.bv_val = textbuf;
96 bvtext.bv_val[0] = '\0';
99 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
102 if( continuemode ) continue;
106 /* make sure the DN is not empty */
107 if( !e->e_nname.bv_len ) {
108 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
109 progname, e->e_dn, lineno );
112 if( continuemode ) continue;
117 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
120 fprintf( stderr, "%s: line %d: "
121 "database (%s) not configured to hold \"%s\"\n",
123 be ? be->be_suffix[0].bv_val : "<none>",
125 fprintf( stderr, "%s: line %d: "
126 "database (%s) not configured to hold \"%s\"\n",
128 be ? be->be_nsuffix[0].bv_val : "<none>",
132 if( continuemode ) continue;
136 if( global_schemacheck ) {
137 Attribute *sc = attr_find( e->e_attrs,
138 slap_schema.si_ad_structuralObjectClass );
139 Attribute *oc = attr_find( e->e_attrs,
140 slap_schema.si_ad_objectClass );
143 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
144 progname, e->e_dn, lineno,
145 "no objectClass attribute");
148 if( continuemode ) continue;
153 struct berval vals[2];
155 rc = structural_class( oc->a_vals, vals,
156 NULL, &text, textbuf, textlen );
158 if( rc != LDAP_SUCCESS ) {
159 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
160 progname, e->e_dn, lineno, rc, text );
163 if( continuemode ) continue;
168 vals[1].bv_val = NULL;
170 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
171 vals, NULL /* FIXME */ );
175 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
177 if( rc != LDAP_SUCCESS ) {
178 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
179 progname, e->e_dn, lineno, rc, text );
182 if( continuemode ) continue;
187 if ( SLAP_LASTMOD(be) ) {
189 time_t now = slap_get_time();
190 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
191 struct berval vals[ 2 ];
193 struct berval name, timestamp;
195 struct berval nvals[ 2 ];
197 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
200 vals[1].bv_val = NULL;
203 nvals[1].bv_val = NULL;
206 lutil_gentime( timebuf, sizeof(timebuf), ltm );
208 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
211 timestamp.bv_val = timebuf;
212 timestamp.bv_len = strlen(timebuf);
214 if ( be->be_rootndn.bv_len == 0 ) {
215 name.bv_val = SLAPD_ANONYMOUS;
216 name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
217 nname.bv_val = SLAPD_ANONYMOUS;
218 nname.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
220 name = be->be_rootdn;
221 nname = be->be_rootndn;
224 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
227 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
228 vals[0].bv_val = uuidbuf;
229 attr_merge( e, slap_schema.si_ad_entryUUID, vals, NULL );
232 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
237 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
240 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
245 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
248 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
252 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
255 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
259 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
262 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
266 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
269 if ( !is_entry_syncProviderSubentry( e ) &&
270 !is_entry_syncConsumerSubentry( e ) &&
271 update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
272 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
273 if ( maxcsn.bv_len != 0 ) {
274 value_match( &match, slap_schema.si_ad_entryCSN,
275 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
276 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
277 &maxcsn, &attr->a_nvals[0], &text );
283 ch_free( maxcsn.bv_val );
284 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
289 if ( update_ctxcsn == SLAP_TOOL_CTXCSN_KEEP ) {
290 if ( is_entry_syncProviderSubentry( e )) {
291 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
292 fprintf( stderr, "%s: consumer and provider subentries "
293 "are both present\n", progname );
296 sei = LDAP_SLIST_FIRST( &consumer_subentry );
298 ch_free( sei->cn.bv_val );
299 ch_free( sei->ndn.bv_val );
300 ch_free( sei->rdn.bv_val );
301 ch_free( sei->cookie.bv_val );
302 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
304 sei = LDAP_SLIST_FIRST( &consumer_subentry );
308 if ( provider_subentry ) {
309 fprintf( stderr, "%s: multiple provider subentries are "
310 "present : add -w flag to refresh\n", progname );
315 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
316 if ( attr == NULL ) {
320 provider_subentry = 1;
321 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
322 } else if ( is_entry_syncConsumerSubentry( e )) {
323 if ( provider_subentry ) {
324 fprintf( stderr, "%s: consumer and provider subentries "
325 "are both present\n", progname );
331 attr = attr_find( e->e_attrs, slap_schema.si_ad_cn );
333 if ( attr == NULL ) {
338 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
339 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
340 value_match( &match, slap_schema.si_ad_cn,
341 slap_schema.si_ad_cn->ad_type->sat_equality,
342 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
343 &sei->cn, &attr->a_nvals[0], &text );
346 fprintf( stderr, "%s: multiple consumer subentries "
347 "have the same id : add -w flag to refresh\n",
351 sei = LDAP_SLIST_FIRST( &consumer_subentry );
353 ch_free( sei->cn.bv_val );
354 ch_free( sei->ndn.bv_val );
355 ch_free( sei->rdn.bv_val );
356 ch_free( sei->cookie.bv_val );
357 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
359 sei = LDAP_SLIST_FIRST( &consumer_subentry );
364 sei = ch_calloc( 1, sizeof( struct subentryinfo ));
365 ber_dupbv( &sei->cn, &attr->a_nvals[0] );
366 ber_dupbv( &sei->ndn, &e->e_nname );
367 dnExtractRdn( &sei->ndn, &sei->rdn, NULL );
368 attr = attr_find( e->e_attrs, slap_schema.si_ad_syncreplCookie );
369 if ( attr == NULL ) {
370 ch_free( sei->cn.bv_val );
371 ch_free( sei->ndn.bv_val );
372 ch_free( sei->rdn.bv_val );
373 ch_free( sei->cookie.bv_val );
378 ber_dupbv( &sei->cookie, &attr->a_nvals[0] );
379 LDAP_SLIST_INSERT_HEAD( &consumer_subentry, sei, sei_next );
383 if ( !is_entry_syncProviderSubentry( e ) &&
384 !is_entry_syncConsumerSubentry( e )) {
386 ID id = be->be_entry_put( be, e, &bvtext );
388 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
389 "(line=%d): %s\n", progname, e->e_dn,
390 lineno, bvtext.bv_val );
393 if( continuemode ) continue;
398 fprintf( stderr, "added: \"%s\" (%08lx)\n",
399 e->e_dn, (long) id );
403 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
411 bvtext.bv_len = textlen;
412 bvtext.bv_val = textbuf;
413 bvtext.bv_val[0] = '\0';
415 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
417 maxcsn.bv_val = NULL;
418 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
419 if ( maxcsn.bv_len != 0 ) {
420 value_match( &match, slap_schema.si_ad_syncreplCookie,
421 slap_schema.si_ad_syncreplCookie->ad_type->sat_ordering,
422 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
423 &maxcsn, &sei->cookie, &text );
429 ch_free( maxcsn.bv_val );
430 ber_dupbv( &maxcsn, &sei->cookie );
435 if ( SLAP_LASTMOD(be) && replica_promotion ) {
436 if ( provider_subentry || update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
437 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
438 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
439 (struct berval *)&slap_ldapsync_cn_bv, NULL );
440 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
442 if ( ctxcsn_id == NOID ) {
443 ctxcsn_e = slap_create_context_csn_entry( be, &maxcsn );
445 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
446 if( ctxcsn_id == NOID ) {
447 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
452 fprintf( stderr, "added: \"%s\" (%08lx)\n",
453 ctxcsn_e->e_dn, (long) ctxcsn_id );
457 fprintf( stderr, "(dry) added: \"%s\"\n", ctxcsn_e->e_dn );
460 entry_free( ctxcsn_e );
462 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
463 if ( ret == LDAP_SUCCESS ) {
464 attr = attr_find( ctxcsn_e->e_attrs,
465 slap_schema.si_ad_contextCSN );
466 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
467 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
468 attr->a_vals[0].bv_len = maxcsn.bv_len;
470 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
471 if( ctxcsn_id == NOID ) {
472 fprintf( stderr, "%s: could not modify ctxcsn "
473 "subentry\n", progname);
477 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
478 ctxcsn_e->e_dn, (long) ctxcsn_id );
482 fprintf( stderr, "(dry) modified: \"%s\"\n",
487 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
493 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
494 ( update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
495 provider_subentry )) {
497 ber_dupbv( &slap_syncrepl_bv, (struct berval *) &slap_syncrepl_bvc );
498 ber_dupbv( &slap_syncrepl_cn_bv,
499 (struct berval *) &slap_syncrepl_cn_bvc );
501 if ( replica_id_list == NULL ) {
502 replica_id_list = ch_calloc( 2, sizeof( int ));
503 replica_id_list[0] = 0;
504 replica_id_list[1] = -1;
507 for ( i = 0; replica_id_list[i] > -1 ; i++ ) {
508 slap_syncrepl_bv.bv_len = snprintf( slap_syncrepl_bv.bv_val,
509 slap_syncrepl_bvc.bv_len,
510 "syncrepl%d", replica_id_list[i] );
511 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
512 slap_syncrepl_cn_bvc.bv_len,
513 "cn=syncrepl%d", replica_id_list[i] );
514 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
515 (struct berval *)&slap_syncrepl_cn_bv, NULL );
516 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
518 if ( ctxcsn_id == NOID ) {
519 ctxcsn_e = slap_create_syncrepl_entry( be, &maxcsn,
520 &slap_syncrepl_cn_bv,
523 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
524 if( ctxcsn_id == NOID ) {
525 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
530 fprintf( stderr, "added: \"%s\" (%08lx)\n",
531 ctxcsn_e->e_dn, (long) ctxcsn_id );
535 fprintf( stderr, "(dry) added: \"%s\"\n",
539 entry_free( ctxcsn_e );
541 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
542 if ( ret == LDAP_SUCCESS ) {
543 attr = attr_find( ctxcsn_e->e_attrs,
544 slap_schema.si_ad_syncreplCookie );
545 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
546 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
547 attr->a_vals[0].bv_len = maxcsn.bv_len;
549 ctxcsn_id = be->be_entry_modify( be,
551 if( ctxcsn_id == NOID ) {
552 fprintf( stderr, "%s: could not modify ctxcsn "
553 "subentry\n", progname);
557 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
558 ctxcsn_e->e_dn, (long) ctxcsn_id );
562 fprintf( stderr, "(dry) modified: \"%s\"\n",
567 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
574 if ( slap_syncrepl_bv.bv_val ) {
575 ch_free( slap_syncrepl_bv.bv_val );
577 if ( slap_syncrepl_cn_bv.bv_val ) {
578 ch_free( slap_syncrepl_cn_bv.bv_val );
580 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
581 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
583 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
584 ctxcsn_id = be->be_dn2id_get( be, &sei->ndn );
586 if ( ctxcsn_id == NOID ) {
587 ctxcsn_e = slap_create_syncrepl_entry( be, &sei->cookie,
588 &sei->rdn, &sei->cn );
590 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
591 if( ctxcsn_id == NOID ) {
592 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
597 fprintf( stderr, "added: \"%s\" (%08lx)\n",
598 ctxcsn_e->e_dn, (long) ctxcsn_id );
602 fprintf( stderr, "(dry) added: \"%s\"\n",
606 entry_free( ctxcsn_e );
608 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
609 if ( ret == LDAP_SUCCESS ) {
610 attr = attr_find( ctxcsn_e->e_attrs,
611 slap_schema.si_ad_syncreplCookie );
612 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
613 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
614 attr->a_vals[0].bv_len = maxcsn.bv_len;
616 ctxcsn_id = be->be_entry_modify( be,
618 if( ctxcsn_id == NOID ) {
619 fprintf( stderr, "%s: could not modify ctxcsn "
620 "subentry\n", progname);
624 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
625 ctxcsn_e->e_dn, (long) ctxcsn_id );
629 fprintf( stderr, "(dry) modified: \"%s\"\n",
634 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
641 if ( slap_syncrepl_bv.bv_val ) {
642 ch_free( slap_syncrepl_bv.bv_val );
644 if ( slap_syncrepl_cn_bv.bv_val ) {
645 ch_free( slap_syncrepl_cn_bv.bv_val );
649 sei = LDAP_SLIST_FIRST( &consumer_subentry );
651 ch_free( sei->cn.bv_val );
652 ch_free( sei->ndn.bv_val );
653 ch_free( sei->rdn.bv_val );
654 ch_free( sei->cookie.bv_val );
655 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
657 sei = LDAP_SLIST_FIRST( &consumer_subentry );
662 if( be->be_entry_close( be )) rc = EXIT_FAILURE;