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",
103 if( be->be_entry_open( be, 1 ) != 0 ) {
104 fprintf( stderr, "%s: could not open database.\n",
106 exit( EXIT_FAILURE );
109 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
110 Entry *e = str2entry( buf );
113 * Initialize text buffer
115 bvtext.bv_len = textlen;
116 bvtext.bv_val = textbuf;
117 bvtext.bv_val[0] = '\0';
120 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
123 if( continuemode ) continue;
127 /* make sure the DN is not empty */
128 if( !e->e_nname.bv_len ) {
129 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
130 progname, e->e_dn, lineno );
133 if( continuemode ) continue;
138 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
141 fprintf( stderr, "%s: line %d: "
142 "database (%s) not configured to hold \"%s\"\n",
144 be ? be->be_suffix[0].bv_val : "<none>",
146 fprintf( stderr, "%s: line %d: "
147 "database (%s) not configured to hold \"%s\"\n",
149 be ? be->be_nsuffix[0].bv_val : "<none>",
153 if( continuemode ) continue;
157 if( global_schemacheck ) {
158 Attribute *sc = attr_find( e->e_attrs,
159 slap_schema.si_ad_structuralObjectClass );
160 Attribute *oc = attr_find( e->e_attrs,
161 slap_schema.si_ad_objectClass );
164 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
165 progname, e->e_dn, lineno,
166 "no objectClass attribute");
169 if( continuemode ) continue;
174 struct berval vals[2];
176 rc = structural_class( oc->a_vals, vals,
177 NULL, &text, textbuf, textlen );
179 if( rc != LDAP_SUCCESS ) {
180 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
181 progname, e->e_dn, lineno, rc, text );
184 if( continuemode ) continue;
189 vals[1].bv_val = NULL;
191 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
192 vals, NULL /* FIXME */ );
196 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
198 if( rc != LDAP_SUCCESS ) {
199 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
200 progname, e->e_dn, lineno, rc, text );
203 if( continuemode ) continue;
208 if ( SLAP_LASTMOD(be) ) {
210 time_t now = slap_get_time();
211 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
212 struct berval vals[ 2 ];
214 struct berval name, timestamp;
216 struct berval nvals[ 2 ];
218 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
221 vals[1].bv_val = NULL;
224 nvals[1].bv_val = NULL;
227 lutil_gentime( timebuf, sizeof(timebuf), ltm );
229 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
232 timestamp.bv_val = timebuf;
233 timestamp.bv_len = strlen(timebuf);
235 if ( be->be_rootndn.bv_len == 0 ) {
236 name.bv_val = SLAPD_ANONYMOUS;
237 name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
238 nname.bv_val = SLAPD_ANONYMOUS;
239 nname.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
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] );
311 if ( update_ctxcsn == SLAP_TOOL_CTXCSN_KEEP &&
312 ( replica_promotion || replica_demotion )) {
313 if ( is_entry_syncProviderSubentry( e )) {
314 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
315 fprintf( stderr, "%s: consumer and provider subentries "
316 "are both present\n", progname );
319 sei = LDAP_SLIST_FIRST( &consumer_subentry );
321 ch_free( sei->cn.bv_val );
322 ch_free( sei->ndn.bv_val );
323 ch_free( sei->rdn.bv_val );
324 ch_free( sei->cookie.bv_val );
325 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
327 sei = LDAP_SLIST_FIRST( &consumer_subentry );
331 if ( provider_subentry ) {
332 fprintf( stderr, "%s: multiple provider subentries are "
333 "present : add -w flag to refresh\n", progname );
338 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
339 if ( attr == NULL ) {
343 provider_subentry = 1;
344 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
345 } else if ( is_entry_syncConsumerSubentry( e )) {
346 if ( provider_subentry ) {
347 fprintf( stderr, "%s: consumer and provider subentries "
348 "are both present\n", progname );
354 attr = attr_find( e->e_attrs, slap_schema.si_ad_cn );
356 if ( attr == NULL ) {
361 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
362 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
363 value_match( &match, slap_schema.si_ad_cn,
364 slap_schema.si_ad_cn->ad_type->sat_equality,
365 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
366 &sei->cn, &attr->a_nvals[0], &text );
369 fprintf( stderr, "%s: multiple consumer subentries "
370 "have the same id : add -w flag to refresh\n",
374 sei = LDAP_SLIST_FIRST( &consumer_subentry );
376 ch_free( sei->cn.bv_val );
377 ch_free( sei->ndn.bv_val );
378 ch_free( sei->rdn.bv_val );
379 ch_free( sei->cookie.bv_val );
380 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
382 sei = LDAP_SLIST_FIRST( &consumer_subentry );
387 sei = ch_calloc( 1, sizeof( struct subentryinfo ));
388 ber_dupbv( &sei->cn, &attr->a_nvals[0] );
389 ber_dupbv( &sei->ndn, &e->e_nname );
390 dnExtractRdn( &sei->ndn, &sei->rdn, NULL );
391 attr = attr_find( e->e_attrs, slap_schema.si_ad_syncreplCookie );
392 if ( attr == NULL ) {
393 ch_free( sei->cn.bv_val );
394 ch_free( sei->ndn.bv_val );
395 ch_free( sei->rdn.bv_val );
396 ch_free( sei->cookie.bv_val );
401 ber_dupbv( &sei->cookie, &attr->a_nvals[0] );
402 LDAP_SLIST_INSERT_HEAD( &consumer_subentry, sei, sei_next );
406 if (( !is_entry_syncProviderSubentry( e ) &&
407 !is_entry_syncConsumerSubentry( e )) ||
408 ( !replica_promotion && !replica_demotion )) {
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 sc.octet_str = &sei->cookie;
444 slap_parse_sync_cookie( &sc );
445 if ( maxcsn.bv_len != 0 ) {
446 value_match( &match, slap_schema.si_ad_syncreplCookie,
447 slap_schema.si_ad_syncreplCookie->ad_type->sat_ordering,
448 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
449 &maxcsn, &sc.ctxcsn[0], &text );
455 ch_free( maxcsn.bv_val );
456 ber_dupbv( &maxcsn, &sc.ctxcsn[0] );
459 slap_sync_cookie_free( &sc, 0 );
463 slap_compose_sync_cookie( NULL, &mc, &maxcsn, -1, -1 );
465 if ( SLAP_LASTMOD(be) && replica_promotion ) {
466 if ( provider_subentry || update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
467 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
468 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
469 (struct berval *)&slap_ldapsync_cn_bv, NULL );
470 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
472 if ( ctxcsn_id == NOID ) {
473 ctxcsn_e = slap_create_context_csn_entry( be, &maxcsn );
475 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
476 if( ctxcsn_id == NOID ) {
477 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
482 fprintf( stderr, "added: \"%s\" (%08lx)\n",
483 ctxcsn_e->e_dn, (long) ctxcsn_id );
487 fprintf( stderr, "(dry) added: \"%s\"\n", ctxcsn_e->e_dn );
490 entry_free( ctxcsn_e );
492 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
493 if ( ret == LDAP_SUCCESS ) {
494 attr = attr_find( ctxcsn_e->e_attrs,
495 slap_schema.si_ad_contextCSN );
496 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
497 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
498 attr->a_vals[0].bv_len = maxcsn.bv_len;
500 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
501 if( ctxcsn_id == NOID ) {
502 fprintf( stderr, "%s: could not modify ctxcsn "
503 "subentry\n", progname);
507 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
508 ctxcsn_e->e_dn, (long) ctxcsn_id );
512 fprintf( stderr, "(dry) modified: \"%s\"\n",
517 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
523 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
524 ( update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
525 provider_subentry )) {
527 ber_dupbv( &slap_syncrepl_bv, (struct berval *) &slap_syncrepl_bvc );
528 ber_dupbv( &slap_syncrepl_cn_bv,
529 (struct berval *) &slap_syncrepl_cn_bvc );
531 if ( replica_id_list == NULL ) {
532 replica_id_list = ch_calloc( 2, sizeof( int ));
533 replica_id_list[0] = 0;
534 replica_id_list[1] = -1;
537 for ( i = 0; replica_id_list[i] > -1 ; i++ ) {
538 slap_syncrepl_bv.bv_len = snprintf( slap_syncrepl_bv.bv_val,
539 slap_syncrepl_bvc.bv_len,
540 "syncrepl%d", replica_id_list[i] );
541 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
542 slap_syncrepl_cn_bvc.bv_len,
543 "cn=syncrepl%d", replica_id_list[i] );
544 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
545 (struct berval *)&slap_syncrepl_cn_bv, NULL );
546 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
548 if ( ctxcsn_id == NOID ) {
549 ctxcsn_e = slap_create_syncrepl_entry( be, &mc,
550 &slap_syncrepl_cn_bv,
553 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
554 if( ctxcsn_id == NOID ) {
555 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
560 fprintf( stderr, "added: \"%s\" (%08lx)\n",
561 ctxcsn_e->e_dn, (long) ctxcsn_id );
565 fprintf( stderr, "(dry) added: \"%s\"\n",
569 entry_free( ctxcsn_e );
571 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
572 if ( ret == LDAP_SUCCESS ) {
573 attr = attr_find( ctxcsn_e->e_attrs,
574 slap_schema.si_ad_syncreplCookie );
575 AC_MEMCPY( attr->a_vals[0].bv_val, mc.bv_val, mc.bv_len );
576 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
577 attr->a_vals[0].bv_len = maxcsn.bv_len;
579 ctxcsn_id = be->be_entry_modify( be,
581 if( ctxcsn_id == NOID ) {
582 fprintf( stderr, "%s: could not modify ctxcsn "
583 "subentry\n", progname);
587 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
588 ctxcsn_e->e_dn, (long) ctxcsn_id );
592 fprintf( stderr, "(dry) modified: \"%s\"\n",
597 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
604 if ( slap_syncrepl_bv.bv_val ) {
605 ch_free( slap_syncrepl_bv.bv_val );
607 if ( slap_syncrepl_cn_bv.bv_val ) {
608 ch_free( slap_syncrepl_cn_bv.bv_val );
610 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
611 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
613 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
614 ctxcsn_id = be->be_dn2id_get( be, &sei->ndn );
616 if ( ctxcsn_id == NOID ) {
617 ctxcsn_e = slap_create_syncrepl_entry( be, &sei->cookie,
618 &sei->rdn, &sei->cn );
620 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
621 if( ctxcsn_id == NOID ) {
622 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
627 fprintf( stderr, "added: \"%s\" (%08lx)\n",
628 ctxcsn_e->e_dn, (long) ctxcsn_id );
632 fprintf( stderr, "(dry) added: \"%s\"\n",
636 entry_free( ctxcsn_e );
638 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
639 if ( ret == LDAP_SUCCESS ) {
640 attr = attr_find( ctxcsn_e->e_attrs,
641 slap_schema.si_ad_syncreplCookie );
642 AC_MEMCPY( attr->a_vals[0].bv_val, sei->cookie.bv_val, sei->cookie.bv_len );
643 attr->a_vals[0].bv_val[sei->cookie.bv_len] = '\0';
644 attr->a_vals[0].bv_len = sei->cookie.bv_len;
646 ctxcsn_id = be->be_entry_modify( be,
648 if( ctxcsn_id == NOID ) {
649 fprintf( stderr, "%s: could not modify ctxcsn "
650 "subentry\n", progname);
654 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
655 ctxcsn_e->e_dn, (long) ctxcsn_id );
659 fprintf( stderr, "(dry) modified: \"%s\"\n",
664 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
671 if ( slap_syncrepl_bv.bv_val ) {
672 ch_free( slap_syncrepl_bv.bv_val );
674 if ( slap_syncrepl_cn_bv.bv_val ) {
675 ch_free( slap_syncrepl_cn_bv.bv_val );
679 sei = LDAP_SLIST_FIRST( &consumer_subentry );
681 ch_free( sei->cn.bv_val );
682 ch_free( sei->ndn.bv_val );
683 ch_free( sei->rdn.bv_val );
684 ch_free( sei->cookie.bv_val );
685 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
687 sei = LDAP_SLIST_FIRST( &consumer_subentry );
692 if( be->be_entry_close( be )) rc = EXIT_FAILURE;