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_normalize_one( e,
230 slap_schema.si_ad_entryUUID, vals, NULL );
233 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
238 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
241 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
246 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
249 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
253 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
256 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
260 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
263 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
267 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
270 if ( !is_entry_syncProviderSubentry( e ) &&
271 !is_entry_syncConsumerSubentry( e ) &&
272 update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
273 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
274 if ( maxcsn.bv_len != 0 ) {
275 value_match( &match, slap_schema.si_ad_entryCSN,
276 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
277 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
278 &maxcsn, &attr->a_nvals[0], &text );
284 ch_free( maxcsn.bv_val );
285 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
290 if ( update_ctxcsn == SLAP_TOOL_CTXCSN_KEEP ) {
291 if ( is_entry_syncProviderSubentry( e )) {
292 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
293 fprintf( stderr, "%s: consumer and provider subentries "
294 "are both present\n", progname );
297 sei = LDAP_SLIST_FIRST( &consumer_subentry );
299 ch_free( sei->cn.bv_val );
300 ch_free( sei->ndn.bv_val );
301 ch_free( sei->rdn.bv_val );
302 ch_free( sei->cookie.bv_val );
303 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
305 sei = LDAP_SLIST_FIRST( &consumer_subentry );
309 if ( provider_subentry ) {
310 fprintf( stderr, "%s: multiple provider subentries are "
311 "present : add -w flag to refresh\n", progname );
316 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
317 if ( attr == NULL ) {
321 provider_subentry = 1;
322 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
323 } else if ( is_entry_syncConsumerSubentry( e )) {
324 if ( provider_subentry ) {
325 fprintf( stderr, "%s: consumer and provider subentries "
326 "are both present\n", progname );
332 attr = attr_find( e->e_attrs, slap_schema.si_ad_cn );
334 if ( attr == NULL ) {
339 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
340 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
341 value_match( &match, slap_schema.si_ad_cn,
342 slap_schema.si_ad_cn->ad_type->sat_equality,
343 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
344 &sei->cn, &attr->a_nvals[0], &text );
347 fprintf( stderr, "%s: multiple consumer subentries "
348 "have the same id : add -w flag to refresh\n",
352 sei = LDAP_SLIST_FIRST( &consumer_subentry );
354 ch_free( sei->cn.bv_val );
355 ch_free( sei->ndn.bv_val );
356 ch_free( sei->rdn.bv_val );
357 ch_free( sei->cookie.bv_val );
358 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
360 sei = LDAP_SLIST_FIRST( &consumer_subentry );
365 sei = ch_calloc( 1, sizeof( struct subentryinfo ));
366 ber_dupbv( &sei->cn, &attr->a_nvals[0] );
367 ber_dupbv( &sei->ndn, &e->e_nname );
368 dnExtractRdn( &sei->ndn, &sei->rdn, NULL );
369 attr = attr_find( e->e_attrs, slap_schema.si_ad_syncreplCookie );
370 if ( attr == NULL ) {
371 ch_free( sei->cn.bv_val );
372 ch_free( sei->ndn.bv_val );
373 ch_free( sei->rdn.bv_val );
374 ch_free( sei->cookie.bv_val );
379 ber_dupbv( &sei->cookie, &attr->a_nvals[0] );
380 LDAP_SLIST_INSERT_HEAD( &consumer_subentry, sei, sei_next );
384 if ( !is_entry_syncProviderSubentry( e ) &&
385 !is_entry_syncConsumerSubentry( e )) {
387 ID id = be->be_entry_put( be, e, &bvtext );
389 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
390 "(line=%d): %s\n", progname, e->e_dn,
391 lineno, bvtext.bv_val );
394 if( continuemode ) continue;
399 fprintf( stderr, "added: \"%s\" (%08lx)\n",
400 e->e_dn, (long) id );
404 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
412 bvtext.bv_len = textlen;
413 bvtext.bv_val = textbuf;
414 bvtext.bv_val[0] = '\0';
416 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
418 maxcsn.bv_val = NULL;
419 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
420 if ( maxcsn.bv_len != 0 ) {
421 value_match( &match, slap_schema.si_ad_syncreplCookie,
422 slap_schema.si_ad_syncreplCookie->ad_type->sat_ordering,
423 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
424 &maxcsn, &sei->cookie, &text );
430 ch_free( maxcsn.bv_val );
431 ber_dupbv( &maxcsn, &sei->cookie );
436 if ( SLAP_LASTMOD(be) && replica_promotion ) {
437 if ( provider_subentry || update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
438 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
439 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
440 (struct berval *)&slap_ldapsync_cn_bv, NULL );
441 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
443 if ( ctxcsn_id == NOID ) {
444 ctxcsn_e = slap_create_context_csn_entry( be, &maxcsn );
446 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
447 if( ctxcsn_id == NOID ) {
448 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
453 fprintf( stderr, "added: \"%s\" (%08lx)\n",
454 ctxcsn_e->e_dn, (long) ctxcsn_id );
458 fprintf( stderr, "(dry) added: \"%s\"\n", ctxcsn_e->e_dn );
461 entry_free( ctxcsn_e );
463 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
464 if ( ret == LDAP_SUCCESS ) {
465 attr = attr_find( ctxcsn_e->e_attrs,
466 slap_schema.si_ad_contextCSN );
467 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
468 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
469 attr->a_vals[0].bv_len = maxcsn.bv_len;
471 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
472 if( ctxcsn_id == NOID ) {
473 fprintf( stderr, "%s: could not modify ctxcsn "
474 "subentry\n", progname);
478 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
479 ctxcsn_e->e_dn, (long) ctxcsn_id );
483 fprintf( stderr, "(dry) modified: \"%s\"\n",
488 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
494 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
495 ( update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
496 provider_subentry )) {
498 ber_dupbv( &slap_syncrepl_bv, (struct berval *) &slap_syncrepl_bvc );
499 ber_dupbv( &slap_syncrepl_cn_bv,
500 (struct berval *) &slap_syncrepl_cn_bvc );
502 if ( replica_id_list == NULL ) {
503 replica_id_list = ch_calloc( 2, sizeof( int ));
504 replica_id_list[0] = 0;
505 replica_id_list[1] = -1;
508 for ( i = 0; replica_id_list[i] > -1 ; i++ ) {
509 slap_syncrepl_bv.bv_len = snprintf( slap_syncrepl_bv.bv_val,
510 slap_syncrepl_bvc.bv_len,
511 "syncrepl%d", replica_id_list[i] );
512 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
513 slap_syncrepl_cn_bvc.bv_len,
514 "cn=syncrepl%d", replica_id_list[i] );
515 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
516 (struct berval *)&slap_syncrepl_cn_bv, NULL );
517 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
519 if ( ctxcsn_id == NOID ) {
520 ctxcsn_e = slap_create_syncrepl_entry( be, &maxcsn,
521 &slap_syncrepl_cn_bv,
524 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
525 if( ctxcsn_id == NOID ) {
526 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
531 fprintf( stderr, "added: \"%s\" (%08lx)\n",
532 ctxcsn_e->e_dn, (long) ctxcsn_id );
536 fprintf( stderr, "(dry) added: \"%s\"\n",
540 entry_free( ctxcsn_e );
542 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
543 if ( ret == LDAP_SUCCESS ) {
544 attr = attr_find( ctxcsn_e->e_attrs,
545 slap_schema.si_ad_syncreplCookie );
546 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
547 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
548 attr->a_vals[0].bv_len = maxcsn.bv_len;
550 ctxcsn_id = be->be_entry_modify( be,
552 if( ctxcsn_id == NOID ) {
553 fprintf( stderr, "%s: could not modify ctxcsn "
554 "subentry\n", progname);
558 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
559 ctxcsn_e->e_dn, (long) ctxcsn_id );
563 fprintf( stderr, "(dry) modified: \"%s\"\n",
568 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
575 if ( slap_syncrepl_bv.bv_val ) {
576 ch_free( slap_syncrepl_bv.bv_val );
578 if ( slap_syncrepl_cn_bv.bv_val ) {
579 ch_free( slap_syncrepl_cn_bv.bv_val );
581 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
582 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
584 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
585 ctxcsn_id = be->be_dn2id_get( be, &sei->ndn );
587 if ( ctxcsn_id == NOID ) {
588 ctxcsn_e = slap_create_syncrepl_entry( be, &sei->cookie,
589 &sei->rdn, &sei->cn );
591 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
592 if( ctxcsn_id == NOID ) {
593 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
598 fprintf( stderr, "added: \"%s\" (%08lx)\n",
599 ctxcsn_e->e_dn, (long) ctxcsn_id );
603 fprintf( stderr, "(dry) added: \"%s\"\n",
607 entry_free( ctxcsn_e );
609 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
610 if ( ret == LDAP_SUCCESS ) {
611 attr = attr_find( ctxcsn_e->e_attrs,
612 slap_schema.si_ad_syncreplCookie );
613 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
614 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
615 attr->a_vals[0].bv_len = maxcsn.bv_len;
617 ctxcsn_id = be->be_entry_modify( be,
619 if( ctxcsn_id == NOID ) {
620 fprintf( stderr, "%s: could not modify ctxcsn "
621 "subentry\n", progname);
625 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
626 ctxcsn_e->e_dn, (long) ctxcsn_id );
630 fprintf( stderr, "(dry) modified: \"%s\"\n",
635 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
642 if ( slap_syncrepl_bv.bv_val ) {
643 ch_free( slap_syncrepl_bv.bv_val );
645 if ( slap_syncrepl_cn_bv.bv_val ) {
646 ch_free( slap_syncrepl_cn_bv.bv_val );
650 sei = LDAP_SLIST_FIRST( &consumer_subentry );
652 ch_free( sei->cn.bv_val );
653 ch_free( sei->ndn.bv_val );
654 ch_free( sei->rdn.bv_val );
655 ch_free( sei->cookie.bv_val );
656 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
658 sei = LDAP_SLIST_FIRST( &consumer_subentry );
663 if( be->be_entry_close( be )) rc = EXIT_FAILURE;