3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Modified by Jong Hyuk Choi
9 * Modifications provided under the terms of the following condition:
11 * Copyright (c) 2003 by International Business Machines, Inc.
13 * International Business Machines, Inc. (hereinafter called IBM) grants
14 * permission under its copyrights to use, copy, modify, and distribute this
15 * Software with or without fee, provided that the above copyright notice and
16 * all paragraphs of this notice appear in all copies, and that the name of IBM
17 * not be used in connection with the marketing of any product incorporating
18 * the Software or modifications thereof, without specific, written prior
21 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
22 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
24 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
25 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
26 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 #include <ac/stdlib.h>
36 #include <ac/string.h>
37 #include <ac/socket.h>
38 #include <ac/unistd.h>
44 #include "slapcommon.h"
46 static char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
47 static const struct berval slap_syncrepl_bvc = BER_BVC("syncreplxxx");
48 static const struct berval slap_syncrepl_cn_bvc = BER_BVC("cn=syncreplxxx");
49 static struct berval slap_syncrepl_bv = BER_BVNULL;
50 static struct berval slap_syncrepl_cn_bv = BER_BVNULL;
57 LDAP_SLIST_ENTRY( subentryinfo ) sei_next;
61 main( int argc, char **argv )
66 int rc = EXIT_SUCCESS;
69 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
70 size_t textlen = sizeof textbuf;
73 struct berval maxcsn = { 0, NULL };
74 struct berval ldifcsn = { 0, NULL };
76 int provider_subentry = 0;
77 struct subentryinfo *sei;
78 LDAP_SLIST_HEAD( consumer_subentry_slist, subentryinfo ) consumer_subentry;
82 struct berval ctxcsn_ndn = { 0, NULL };
87 lutil_log_initialize(argc, argv );
89 slap_tool_init( "slapadd", SLAPADD, argc, argv );
91 LDAP_SLIST_INIT( &consumer_subentry );
93 if( !be->be_entry_open ||
94 !be->be_entry_close ||
97 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
105 if( 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] );
313 if ( update_ctxcsn == SLAP_TOOL_CTXCSN_KEEP ) {
314 if ( is_entry_syncProviderSubentry( e )) {
315 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
316 fprintf( stderr, "%s: consumer and provider subentries "
317 "are both present\n", progname );
320 sei = LDAP_SLIST_FIRST( &consumer_subentry );
322 ch_free( sei->cn.bv_val );
323 ch_free( sei->ndn.bv_val );
324 ch_free( sei->rdn.bv_val );
325 ch_free( sei->cookie.bv_val );
326 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
328 sei = LDAP_SLIST_FIRST( &consumer_subentry );
332 if ( provider_subentry ) {
333 fprintf( stderr, "%s: multiple provider subentries are "
334 "present : add -w flag to refresh\n", progname );
339 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
340 if ( attr == NULL ) {
344 provider_subentry = 1;
345 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
346 } else if ( is_entry_syncConsumerSubentry( e )) {
347 if ( provider_subentry ) {
348 fprintf( stderr, "%s: consumer and provider subentries "
349 "are both present\n", progname );
355 attr = attr_find( e->e_attrs, slap_schema.si_ad_cn );
357 if ( attr == NULL ) {
362 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
363 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
364 value_match( &match, slap_schema.si_ad_cn,
365 slap_schema.si_ad_cn->ad_type->sat_equality,
366 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
367 &sei->cn, &attr->a_nvals[0], &text );
370 fprintf( stderr, "%s: multiple consumer subentries "
371 "have the same id : add -w flag to refresh\n",
375 sei = LDAP_SLIST_FIRST( &consumer_subentry );
377 ch_free( sei->cn.bv_val );
378 ch_free( sei->ndn.bv_val );
379 ch_free( sei->rdn.bv_val );
380 ch_free( sei->cookie.bv_val );
381 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
383 sei = LDAP_SLIST_FIRST( &consumer_subentry );
388 sei = ch_calloc( 1, sizeof( struct subentryinfo ));
389 ber_dupbv( &sei->cn, &attr->a_nvals[0] );
390 ber_dupbv( &sei->ndn, &e->e_nname );
391 dnExtractRdn( &sei->ndn, &sei->rdn, NULL );
392 attr = attr_find( e->e_attrs, slap_schema.si_ad_syncreplCookie );
393 if ( attr == NULL ) {
394 ch_free( sei->cn.bv_val );
395 ch_free( sei->ndn.bv_val );
396 ch_free( sei->rdn.bv_val );
397 ch_free( sei->cookie.bv_val );
402 ber_dupbv( &sei->cookie, &attr->a_nvals[0] );
403 LDAP_SLIST_INSERT_HEAD( &consumer_subentry, sei, sei_next );
407 if ( !is_entry_syncProviderSubentry( e ) &&
408 !is_entry_syncConsumerSubentry( e )) {
410 ID id = be->be_entry_put( be, e, &bvtext );
412 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
413 "(line=%d): %s\n", progname, e->e_dn,
414 lineno, bvtext.bv_val );
417 if( continuemode ) continue;
422 fprintf( stderr, "added: \"%s\" (%08lx)\n",
423 e->e_dn, (long) id );
427 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
435 bvtext.bv_len = textlen;
436 bvtext.bv_val = textbuf;
437 bvtext.bv_val[0] = '\0';
439 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
441 maxcsn.bv_val = NULL;
442 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
443 if ( maxcsn.bv_len != 0 ) {
444 value_match( &match, slap_schema.si_ad_syncreplCookie,
445 slap_schema.si_ad_syncreplCookie->ad_type->sat_ordering,
446 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
447 &maxcsn, &sei->cookie, &text );
453 ch_free( maxcsn.bv_val );
454 ber_dupbv( &maxcsn, &sei->cookie );
459 if ( SLAP_LASTMOD(be) && replica_promotion ) {
460 if ( provider_subentry || update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
461 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
462 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
463 (struct berval *)&slap_ldapsync_cn_bv, NULL );
464 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
466 if ( ctxcsn_id == NOID ) {
467 ctxcsn_e = slap_create_context_csn_entry( be, &maxcsn );
469 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
470 if( ctxcsn_id == NOID ) {
471 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
476 fprintf( stderr, "added: \"%s\" (%08lx)\n",
477 ctxcsn_e->e_dn, (long) ctxcsn_id );
481 fprintf( stderr, "(dry) added: \"%s\"\n", ctxcsn_e->e_dn );
484 entry_free( ctxcsn_e );
486 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
487 if ( ret == LDAP_SUCCESS ) {
488 attr = attr_find( ctxcsn_e->e_attrs,
489 slap_schema.si_ad_contextCSN );
490 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
491 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
492 attr->a_vals[0].bv_len = maxcsn.bv_len;
494 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
495 if( ctxcsn_id == NOID ) {
496 fprintf( stderr, "%s: could not modify ctxcsn "
497 "subentry\n", progname);
501 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
502 ctxcsn_e->e_dn, (long) ctxcsn_id );
506 fprintf( stderr, "(dry) modified: \"%s\"\n",
511 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
517 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
518 ( update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
519 provider_subentry )) {
521 ber_dupbv( &slap_syncrepl_bv, (struct berval *) &slap_syncrepl_bvc );
522 ber_dupbv( &slap_syncrepl_cn_bv,
523 (struct berval *) &slap_syncrepl_cn_bvc );
525 if ( replica_id_list == NULL ) {
526 replica_id_list = ch_calloc( 2, sizeof( int ));
527 replica_id_list[0] = 0;
528 replica_id_list[1] = -1;
531 for ( i = 0; replica_id_list[i] > -1 ; i++ ) {
532 slap_syncrepl_bv.bv_len = snprintf( slap_syncrepl_bv.bv_val,
533 slap_syncrepl_bvc.bv_len,
534 "syncrepl%d", replica_id_list[i] );
535 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
536 slap_syncrepl_cn_bvc.bv_len,
537 "cn=syncrepl%d", replica_id_list[i] );
538 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
539 (struct berval *)&slap_syncrepl_cn_bv, NULL );
540 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
542 if ( ctxcsn_id == NOID ) {
543 ctxcsn_e = slap_create_syncrepl_entry( be, &maxcsn,
544 &slap_syncrepl_cn_bv,
547 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
548 if( ctxcsn_id == NOID ) {
549 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
554 fprintf( stderr, "added: \"%s\" (%08lx)\n",
555 ctxcsn_e->e_dn, (long) ctxcsn_id );
559 fprintf( stderr, "(dry) added: \"%s\"\n",
563 entry_free( ctxcsn_e );
565 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
566 if ( ret == LDAP_SUCCESS ) {
567 attr = attr_find( ctxcsn_e->e_attrs,
568 slap_schema.si_ad_syncreplCookie );
569 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
570 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
571 attr->a_vals[0].bv_len = maxcsn.bv_len;
573 ctxcsn_id = be->be_entry_modify( be,
575 if( ctxcsn_id == NOID ) {
576 fprintf( stderr, "%s: could not modify ctxcsn "
577 "subentry\n", progname);
581 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
582 ctxcsn_e->e_dn, (long) ctxcsn_id );
586 fprintf( stderr, "(dry) modified: \"%s\"\n",
591 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
598 if ( slap_syncrepl_bv.bv_val ) {
599 ch_free( slap_syncrepl_bv.bv_val );
601 if ( slap_syncrepl_cn_bv.bv_val ) {
602 ch_free( slap_syncrepl_cn_bv.bv_val );
604 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
605 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
607 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
608 ctxcsn_id = be->be_dn2id_get( be, &sei->ndn );
610 if ( ctxcsn_id == NOID ) {
611 ctxcsn_e = slap_create_syncrepl_entry( be, &sei->cookie,
612 &sei->rdn, &sei->cn );
614 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
615 if( ctxcsn_id == NOID ) {
616 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
621 fprintf( stderr, "added: \"%s\" (%08lx)\n",
622 ctxcsn_e->e_dn, (long) ctxcsn_id );
626 fprintf( stderr, "(dry) added: \"%s\"\n",
630 entry_free( ctxcsn_e );
632 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
633 if ( ret == LDAP_SUCCESS ) {
634 attr = attr_find( ctxcsn_e->e_attrs,
635 slap_schema.si_ad_syncreplCookie );
636 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
637 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
638 attr->a_vals[0].bv_len = maxcsn.bv_len;
640 ctxcsn_id = be->be_entry_modify( be,
642 if( ctxcsn_id == NOID ) {
643 fprintf( stderr, "%s: could not modify ctxcsn "
644 "subentry\n", progname);
648 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
649 ctxcsn_e->e_dn, (long) ctxcsn_id );
653 fprintf( stderr, "(dry) modified: \"%s\"\n",
658 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
665 if ( slap_syncrepl_bv.bv_val ) {
666 ch_free( slap_syncrepl_bv.bv_val );
668 if ( slap_syncrepl_cn_bv.bv_val ) {
669 ch_free( slap_syncrepl_cn_bv.bv_val );
673 sei = LDAP_SLIST_FIRST( &consumer_subentry );
675 ch_free( sei->cn.bv_val );
676 ch_free( sei->ndn.bv_val );
677 ch_free( sei->rdn.bv_val );
678 ch_free( sei->cookie.bv_val );
679 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
681 sei = LDAP_SLIST_FIRST( &consumer_subentry );
686 if( be->be_entry_close( be )) rc = EXIT_FAILURE;