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;
84 slap_tool_init( progname, SLAPADD, argc, argv );
86 LDAP_SLIST_INIT( &consumer_subentry );
88 if( !be->be_entry_open ||
89 !be->be_entry_close ||
92 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
95 fprintf( stderr, "\t(dry) continuing...\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 ( BER_BVISEMPTY( &be->be_rootndn ) ) {
238 BER_BVSTR( &name, SLAPD_ANONYMOUS );
241 name = be->be_rootdn;
242 nname = be->be_rootndn;
245 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
248 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
249 vals[0].bv_val = uuidbuf;
250 attr_merge_normalize_one( e,
251 slap_schema.si_ad_entryUUID, vals, NULL );
254 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
259 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
262 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
267 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
270 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
274 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
277 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
281 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
284 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
288 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
291 if ( !is_entry_syncProviderSubentry( e ) &&
292 !is_entry_syncConsumerSubentry( e ) &&
293 update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
294 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
295 if ( maxcsn.bv_len != 0 ) {
296 value_match( &match, slap_schema.si_ad_entryCSN,
297 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
298 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
299 &maxcsn, &attr->a_nvals[0], &text );
305 ch_free( maxcsn.bv_val );
306 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
313 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
318 if ( update_ctxcsn == SLAP_TOOL_CTXCSN_KEEP &&
319 ( replica_promotion || replica_demotion )) {
320 if ( is_entry_syncProviderSubentry( e )) {
321 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
322 fprintf( stderr, "%s: consumer and provider subentries "
323 "are both present\n", progname );
326 sei = LDAP_SLIST_FIRST( &consumer_subentry );
328 ch_free( sei->cn.bv_val );
329 ch_free( sei->ndn.bv_val );
330 ch_free( sei->rdn.bv_val );
331 ch_free( sei->cookie.bv_val );
332 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
334 sei = LDAP_SLIST_FIRST( &consumer_subentry );
338 if ( provider_subentry ) {
339 fprintf( stderr, "%s: multiple provider subentries are "
340 "present : add -w flag to refresh\n", progname );
345 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
346 if ( attr == NULL ) {
350 provider_subentry = 1;
351 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
352 } else if ( is_entry_syncConsumerSubentry( e )) {
353 if ( provider_subentry ) {
354 fprintf( stderr, "%s: consumer and provider subentries "
355 "are both present\n", progname );
361 attr = attr_find( e->e_attrs, slap_schema.si_ad_cn );
363 if ( attr == NULL ) {
368 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
369 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
370 value_match( &match, slap_schema.si_ad_cn,
371 slap_schema.si_ad_cn->ad_type->sat_equality,
372 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
373 &sei->cn, &attr->a_nvals[0], &text );
376 fprintf( stderr, "%s: multiple consumer subentries "
377 "have the same id : add -w flag to refresh\n",
381 sei = LDAP_SLIST_FIRST( &consumer_subentry );
383 ch_free( sei->cn.bv_val );
384 ch_free( sei->ndn.bv_val );
385 ch_free( sei->rdn.bv_val );
386 ch_free( sei->cookie.bv_val );
387 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
389 sei = LDAP_SLIST_FIRST( &consumer_subentry );
394 sei = ch_calloc( 1, sizeof( struct subentryinfo ));
395 ber_dupbv( &sei->cn, &attr->a_nvals[0] );
396 ber_dupbv( &sei->ndn, &e->e_nname );
397 dnExtractRdn( &sei->ndn, &sei->rdn, NULL );
398 attr = attr_find( e->e_attrs, slap_schema.si_ad_syncreplCookie );
399 if ( attr == NULL ) {
400 ch_free( sei->cn.bv_val );
401 ch_free( sei->ndn.bv_val );
402 ch_free( sei->rdn.bv_val );
403 ch_free( sei->cookie.bv_val );
408 ber_dupbv( &sei->cookie, &attr->a_nvals[0] );
409 LDAP_SLIST_INSERT_HEAD( &consumer_subentry, sei, sei_next );
413 if (( !is_entry_syncProviderSubentry( e ) &&
414 !is_entry_syncConsumerSubentry( e )) ||
415 ( !replica_promotion && !replica_demotion ))
417 /* dryrun moved earlier */
421 ID id = be->be_entry_put( be, e, &bvtext );
423 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
424 "(line=%d): %s\n", progname, e->e_dn,
425 lineno, bvtext.bv_val );
428 if( continuemode ) continue;
433 fprintf( stderr, "added: \"%s\" (%08lx)\n",
434 e->e_dn, (long) id );
438 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
447 bvtext.bv_len = textlen;
448 bvtext.bv_val = textbuf;
449 bvtext.bv_val[0] = '\0';
451 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
453 maxcsn.bv_val = NULL;
454 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
455 sc.octet_str = &sei->cookie;
456 slap_parse_sync_cookie( &sc );
457 if ( maxcsn.bv_len != 0 ) {
458 value_match( &match, slap_schema.si_ad_syncreplCookie,
459 slap_schema.si_ad_syncreplCookie->ad_type->sat_ordering,
460 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
461 &maxcsn, &sc.ctxcsn[0], &text );
467 ch_free( maxcsn.bv_val );
468 ber_dupbv( &maxcsn, &sc.ctxcsn[0] );
471 slap_sync_cookie_free( &sc, 0 );
475 slap_compose_sync_cookie( NULL, &mc, &maxcsn, -1, -1 );
477 if ( SLAP_LASTMOD(be) && replica_promotion ) {
478 if ( provider_subentry || update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
479 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
480 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
481 (struct berval *)&slap_ldapsync_cn_bv, NULL );
482 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
484 if ( ctxcsn_id == NOID ) {
485 ctxcsn_e = slap_create_context_csn_entry( be, &maxcsn );
487 /* dryrun moved earlier */
491 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
492 if( ctxcsn_id == NOID ) {
493 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
498 fprintf( stderr, "added: \"%s\" (%08lx)\n",
499 ctxcsn_e->e_dn, (long) ctxcsn_id );
503 fprintf( stderr, "(dry) added: \"%s\"\n", ctxcsn_e->e_dn );
506 entry_free( ctxcsn_e );
508 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
509 if ( ret == LDAP_SUCCESS ) {
510 attr = attr_find( ctxcsn_e->e_attrs,
511 slap_schema.si_ad_contextCSN );
512 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
513 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
514 attr->a_vals[0].bv_len = maxcsn.bv_len;
516 /* dryrun moved earlier */
520 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
521 if( ctxcsn_id == NOID ) {
522 fprintf( stderr, "%s: could not modify ctxcsn "
523 "subentry\n", progname);
527 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
528 ctxcsn_e->e_dn, (long) ctxcsn_id );
532 fprintf( stderr, "(dry) modified: \"%s\"\n",
537 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
543 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
544 ( update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
545 provider_subentry )) {
547 ber_dupbv( &slap_syncrepl_bv, (struct berval *) &slap_syncrepl_bvc );
548 ber_dupbv( &slap_syncrepl_cn_bv,
549 (struct berval *) &slap_syncrepl_cn_bvc );
551 if ( replica_id_list == NULL ) {
552 replica_id_list = ch_calloc( 2, sizeof( int ));
553 replica_id_list[0] = 0;
554 replica_id_list[1] = -1;
557 for ( i = 0; replica_id_list[i] > -1 ; i++ ) {
558 slap_syncrepl_bv.bv_len = snprintf( slap_syncrepl_bv.bv_val,
559 slap_syncrepl_bvc.bv_len+1,
560 "syncrepl%d", replica_id_list[i] );
561 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
562 slap_syncrepl_cn_bvc.bv_len+1,
563 "cn=syncrepl%d", replica_id_list[i] );
564 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
565 (struct berval *)&slap_syncrepl_cn_bv, NULL );
566 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
568 if ( ctxcsn_id == NOID ) {
569 ctxcsn_e = slap_create_syncrepl_entry( be, &mc,
570 &slap_syncrepl_cn_bv,
573 /* dryrun moved earlier */
577 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
578 if( ctxcsn_id == NOID ) {
579 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
584 fprintf( stderr, "added: \"%s\" (%08lx)\n",
585 ctxcsn_e->e_dn, (long) ctxcsn_id );
589 fprintf( stderr, "(dry) added: \"%s\"\n",
593 entry_free( ctxcsn_e );
595 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
596 if ( ret == LDAP_SUCCESS ) {
597 attr = attr_find( ctxcsn_e->e_attrs,
598 slap_schema.si_ad_syncreplCookie );
599 AC_MEMCPY( attr->a_vals[0].bv_val, mc.bv_val, mc.bv_len );
600 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
601 attr->a_vals[0].bv_len = maxcsn.bv_len;
603 /* dryrun moved earlier */
607 ctxcsn_id = be->be_entry_modify( be,
609 if( ctxcsn_id == NOID ) {
610 fprintf( stderr, "%s: could not modify ctxcsn "
611 "subentry\n", progname);
615 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
616 ctxcsn_e->e_dn, (long) ctxcsn_id );
620 fprintf( stderr, "(dry) modified: \"%s\"\n",
625 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
632 if ( slap_syncrepl_bv.bv_val ) {
633 ch_free( slap_syncrepl_bv.bv_val );
635 if ( slap_syncrepl_cn_bv.bv_val ) {
636 ch_free( slap_syncrepl_cn_bv.bv_val );
638 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
639 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
641 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
642 ctxcsn_id = be->be_dn2id_get( be, &sei->ndn );
644 if ( ctxcsn_id == NOID ) {
645 ctxcsn_e = slap_create_syncrepl_entry( be, &sei->cookie,
646 &sei->rdn, &sei->cn );
648 /* dryrun moved earlier */
652 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
653 if( ctxcsn_id == NOID ) {
654 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
659 fprintf( stderr, "added: \"%s\" (%08lx)\n",
660 ctxcsn_e->e_dn, (long) ctxcsn_id );
664 fprintf( stderr, "(dry) added: \"%s\"\n",
668 entry_free( ctxcsn_e );
670 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
671 if ( ret == LDAP_SUCCESS ) {
672 attr = attr_find( ctxcsn_e->e_attrs,
673 slap_schema.si_ad_syncreplCookie );
674 AC_MEMCPY( attr->a_vals[0].bv_val, sei->cookie.bv_val, sei->cookie.bv_len );
675 attr->a_vals[0].bv_val[sei->cookie.bv_len] = '\0';
676 attr->a_vals[0].bv_len = sei->cookie.bv_len;
678 /* dryrun moved earlier */
682 ctxcsn_id = be->be_entry_modify( be,
684 if( ctxcsn_id == NOID ) {
685 fprintf( stderr, "%s: could not modify ctxcsn "
686 "subentry\n", progname);
690 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
691 ctxcsn_e->e_dn, (long) ctxcsn_id );
695 fprintf( stderr, "(dry) modified: \"%s\"\n",
700 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
707 if ( slap_syncrepl_bv.bv_val ) {
708 ch_free( slap_syncrepl_bv.bv_val );
710 if ( slap_syncrepl_cn_bv.bv_val ) {
711 ch_free( slap_syncrepl_cn_bv.bv_val );
715 sei = LDAP_SLIST_FIRST( &consumer_subentry );
717 ch_free( sei->cn.bv_val );
718 ch_free( sei->ndn.bv_val );
719 ch_free( sei->rdn.bv_val );
720 ch_free( sei->cookie.bv_val );
721 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
723 sei = LDAP_SLIST_FIRST( &consumer_subentry );
729 if( be->be_entry_close( be ) ) {