2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 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 main( int argc, char **argv )
61 int rc = EXIT_SUCCESS;
64 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
65 size_t textlen = sizeof textbuf;
68 struct berval maxcsn = { 0, NULL };
69 struct berval ldifcsn = { 0, NULL };
71 int provider_subentry = 0;
72 struct subentryinfo *sei;
73 LDAP_SLIST_HEAD( consumer_subentry_slist, subentryinfo ) consumer_subentry;
77 struct berval ctxcsn_ndn = { 0, NULL };
82 lutil_log_initialize(argc, argv );
84 slap_tool_init( "slapadd", 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",
100 if( be->be_entry_open( be, 1 ) != 0 ) {
101 fprintf( stderr, "%s: could not open database.\n",
103 exit( EXIT_FAILURE );
106 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
107 Entry *e = str2entry( buf );
110 * Initialize text buffer
112 bvtext.bv_len = textlen;
113 bvtext.bv_val = textbuf;
114 bvtext.bv_val[0] = '\0';
117 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
120 if( continuemode ) continue;
124 /* make sure the DN is not empty */
125 if( !e->e_nname.bv_len ) {
126 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
127 progname, e->e_dn, lineno );
130 if( continuemode ) continue;
135 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
138 fprintf( stderr, "%s: line %d: "
139 "database (%s) not configured to hold \"%s\"\n",
141 be ? be->be_suffix[0].bv_val : "<none>",
143 fprintf( stderr, "%s: line %d: "
144 "database (%s) not configured to hold \"%s\"\n",
146 be ? be->be_nsuffix[0].bv_val : "<none>",
150 if( continuemode ) continue;
154 if( global_schemacheck ) {
155 Attribute *sc = attr_find( e->e_attrs,
156 slap_schema.si_ad_structuralObjectClass );
157 Attribute *oc = attr_find( e->e_attrs,
158 slap_schema.si_ad_objectClass );
161 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
162 progname, e->e_dn, lineno,
163 "no objectClass attribute");
166 if( continuemode ) continue;
171 struct berval vals[2];
173 rc = structural_class( oc->a_vals, vals,
174 NULL, &text, textbuf, textlen );
176 if( rc != LDAP_SUCCESS ) {
177 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
178 progname, e->e_dn, lineno, rc, text );
181 if( continuemode ) continue;
186 vals[1].bv_val = NULL;
188 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
189 vals, NULL /* FIXME */ );
193 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
195 if( rc != LDAP_SUCCESS ) {
196 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
197 progname, e->e_dn, lineno, rc, text );
200 if( continuemode ) continue;
205 if ( SLAP_LASTMOD(be) ) {
207 time_t now = slap_get_time();
208 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
209 struct berval vals[ 2 ];
211 struct berval name, timestamp;
213 struct berval nvals[ 2 ];
215 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
218 vals[1].bv_val = NULL;
221 nvals[1].bv_val = NULL;
224 lutil_gentime( timebuf, sizeof(timebuf), ltm );
226 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
229 timestamp.bv_val = timebuf;
230 timestamp.bv_len = strlen(timebuf);
232 if ( be->be_rootndn.bv_len == 0 ) {
233 name.bv_val = SLAPD_ANONYMOUS;
234 name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
235 nname.bv_val = SLAPD_ANONYMOUS;
236 nname.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
238 name = be->be_rootdn;
239 nname = be->be_rootndn;
242 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
245 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
246 vals[0].bv_val = uuidbuf;
247 attr_merge_normalize_one( e,
248 slap_schema.si_ad_entryUUID, vals, NULL );
251 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
256 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
259 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
264 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
267 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
271 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
274 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
278 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
281 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
285 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
288 if ( !is_entry_syncProviderSubentry( e ) &&
289 !is_entry_syncConsumerSubentry( e ) &&
290 update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
291 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
292 if ( maxcsn.bv_len != 0 ) {
293 value_match( &match, slap_schema.si_ad_entryCSN,
294 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
295 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
296 &maxcsn, &attr->a_nvals[0], &text );
302 ch_free( maxcsn.bv_val );
303 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
308 if ( update_ctxcsn == SLAP_TOOL_CTXCSN_KEEP ) {
309 if ( is_entry_syncProviderSubentry( e )) {
310 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
311 fprintf( stderr, "%s: consumer and provider subentries "
312 "are both present\n", progname );
315 sei = LDAP_SLIST_FIRST( &consumer_subentry );
317 ch_free( sei->cn.bv_val );
318 ch_free( sei->ndn.bv_val );
319 ch_free( sei->rdn.bv_val );
320 ch_free( sei->cookie.bv_val );
321 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
323 sei = LDAP_SLIST_FIRST( &consumer_subentry );
327 if ( provider_subentry ) {
328 fprintf( stderr, "%s: multiple provider subentries are "
329 "present : add -w flag to refresh\n", progname );
334 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
335 if ( attr == NULL ) {
339 provider_subentry = 1;
340 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
341 } else if ( is_entry_syncConsumerSubentry( e )) {
342 if ( provider_subentry ) {
343 fprintf( stderr, "%s: consumer and provider subentries "
344 "are both present\n", progname );
350 attr = attr_find( e->e_attrs, slap_schema.si_ad_cn );
352 if ( attr == NULL ) {
357 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
358 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
359 value_match( &match, slap_schema.si_ad_cn,
360 slap_schema.si_ad_cn->ad_type->sat_equality,
361 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
362 &sei->cn, &attr->a_nvals[0], &text );
365 fprintf( stderr, "%s: multiple consumer subentries "
366 "have the same id : add -w flag to refresh\n",
370 sei = LDAP_SLIST_FIRST( &consumer_subentry );
372 ch_free( sei->cn.bv_val );
373 ch_free( sei->ndn.bv_val );
374 ch_free( sei->rdn.bv_val );
375 ch_free( sei->cookie.bv_val );
376 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
378 sei = LDAP_SLIST_FIRST( &consumer_subentry );
383 sei = ch_calloc( 1, sizeof( struct subentryinfo ));
384 ber_dupbv( &sei->cn, &attr->a_nvals[0] );
385 ber_dupbv( &sei->ndn, &e->e_nname );
386 dnExtractRdn( &sei->ndn, &sei->rdn, NULL );
387 attr = attr_find( e->e_attrs, slap_schema.si_ad_syncreplCookie );
388 if ( attr == NULL ) {
389 ch_free( sei->cn.bv_val );
390 ch_free( sei->ndn.bv_val );
391 ch_free( sei->rdn.bv_val );
392 ch_free( sei->cookie.bv_val );
397 ber_dupbv( &sei->cookie, &attr->a_nvals[0] );
398 LDAP_SLIST_INSERT_HEAD( &consumer_subentry, sei, sei_next );
402 if ( !is_entry_syncProviderSubentry( e ) &&
403 !is_entry_syncConsumerSubentry( e )) {
405 ID id = be->be_entry_put( be, e, &bvtext );
407 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
408 "(line=%d): %s\n", progname, e->e_dn,
409 lineno, bvtext.bv_val );
412 if( continuemode ) continue;
417 fprintf( stderr, "added: \"%s\" (%08lx)\n",
418 e->e_dn, (long) id );
422 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
430 bvtext.bv_len = textlen;
431 bvtext.bv_val = textbuf;
432 bvtext.bv_val[0] = '\0';
434 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
436 maxcsn.bv_val = NULL;
437 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
438 if ( maxcsn.bv_len != 0 ) {
439 value_match( &match, slap_schema.si_ad_syncreplCookie,
440 slap_schema.si_ad_syncreplCookie->ad_type->sat_ordering,
441 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
442 &maxcsn, &sei->cookie, &text );
448 ch_free( maxcsn.bv_val );
449 ber_dupbv( &maxcsn, &sei->cookie );
454 if ( SLAP_LASTMOD(be) && replica_promotion ) {
455 if ( provider_subentry || update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
456 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
457 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
458 (struct berval *)&slap_ldapsync_cn_bv, NULL );
459 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
461 if ( ctxcsn_id == NOID ) {
462 ctxcsn_e = slap_create_context_csn_entry( be, &maxcsn );
464 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
465 if( ctxcsn_id == NOID ) {
466 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
471 fprintf( stderr, "added: \"%s\" (%08lx)\n",
472 ctxcsn_e->e_dn, (long) ctxcsn_id );
476 fprintf( stderr, "(dry) added: \"%s\"\n", ctxcsn_e->e_dn );
479 entry_free( ctxcsn_e );
481 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
482 if ( ret == LDAP_SUCCESS ) {
483 attr = attr_find( ctxcsn_e->e_attrs,
484 slap_schema.si_ad_contextCSN );
485 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
486 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
487 attr->a_vals[0].bv_len = maxcsn.bv_len;
489 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
490 if( ctxcsn_id == NOID ) {
491 fprintf( stderr, "%s: could not modify ctxcsn "
492 "subentry\n", progname);
496 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
497 ctxcsn_e->e_dn, (long) ctxcsn_id );
501 fprintf( stderr, "(dry) modified: \"%s\"\n",
506 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
512 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
513 ( update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
514 provider_subentry )) {
516 ber_dupbv( &slap_syncrepl_bv, (struct berval *) &slap_syncrepl_bvc );
517 ber_dupbv( &slap_syncrepl_cn_bv,
518 (struct berval *) &slap_syncrepl_cn_bvc );
520 if ( replica_id_list == NULL ) {
521 replica_id_list = ch_calloc( 2, sizeof( int ));
522 replica_id_list[0] = 0;
523 replica_id_list[1] = -1;
526 for ( i = 0; replica_id_list[i] > -1 ; i++ ) {
527 slap_syncrepl_bv.bv_len = snprintf( slap_syncrepl_bv.bv_val,
528 slap_syncrepl_bvc.bv_len,
529 "syncrepl%d", replica_id_list[i] );
530 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
531 slap_syncrepl_cn_bvc.bv_len,
532 "cn=syncrepl%d", replica_id_list[i] );
533 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
534 (struct berval *)&slap_syncrepl_cn_bv, NULL );
535 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
537 if ( ctxcsn_id == NOID ) {
538 ctxcsn_e = slap_create_syncrepl_entry( be, &maxcsn,
539 &slap_syncrepl_cn_bv,
542 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
543 if( ctxcsn_id == NOID ) {
544 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
549 fprintf( stderr, "added: \"%s\" (%08lx)\n",
550 ctxcsn_e->e_dn, (long) ctxcsn_id );
554 fprintf( stderr, "(dry) added: \"%s\"\n",
558 entry_free( ctxcsn_e );
560 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
561 if ( ret == LDAP_SUCCESS ) {
562 attr = attr_find( ctxcsn_e->e_attrs,
563 slap_schema.si_ad_syncreplCookie );
564 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
565 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
566 attr->a_vals[0].bv_len = maxcsn.bv_len;
568 ctxcsn_id = be->be_entry_modify( be,
570 if( ctxcsn_id == NOID ) {
571 fprintf( stderr, "%s: could not modify ctxcsn "
572 "subentry\n", progname);
576 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
577 ctxcsn_e->e_dn, (long) ctxcsn_id );
581 fprintf( stderr, "(dry) modified: \"%s\"\n",
586 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
593 if ( slap_syncrepl_bv.bv_val ) {
594 ch_free( slap_syncrepl_bv.bv_val );
596 if ( slap_syncrepl_cn_bv.bv_val ) {
597 ch_free( slap_syncrepl_cn_bv.bv_val );
599 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
600 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
602 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
603 ctxcsn_id = be->be_dn2id_get( be, &sei->ndn );
605 if ( ctxcsn_id == NOID ) {
606 ctxcsn_e = slap_create_syncrepl_entry( be, &sei->cookie,
607 &sei->rdn, &sei->cn );
609 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
610 if( ctxcsn_id == NOID ) {
611 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
616 fprintf( stderr, "added: \"%s\" (%08lx)\n",
617 ctxcsn_e->e_dn, (long) ctxcsn_id );
621 fprintf( stderr, "(dry) added: \"%s\"\n",
625 entry_free( ctxcsn_e );
627 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
628 if ( ret == LDAP_SUCCESS ) {
629 attr = attr_find( ctxcsn_e->e_attrs,
630 slap_schema.si_ad_syncreplCookie );
631 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
632 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
633 attr->a_vals[0].bv_len = maxcsn.bv_len;
635 ctxcsn_id = be->be_entry_modify( be,
637 if( ctxcsn_id == NOID ) {
638 fprintf( stderr, "%s: could not modify ctxcsn "
639 "subentry\n", progname);
643 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
644 ctxcsn_e->e_dn, (long) ctxcsn_id );
648 fprintf( stderr, "(dry) modified: \"%s\"\n",
653 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
660 if ( slap_syncrepl_bv.bv_val ) {
661 ch_free( slap_syncrepl_bv.bv_val );
663 if ( slap_syncrepl_cn_bv.bv_val ) {
664 ch_free( slap_syncrepl_cn_bv.bv_val );
668 sei = LDAP_SLIST_FIRST( &consumer_subentry );
670 ch_free( sei->cn.bv_val );
671 ch_free( sei->ndn.bv_val );
672 ch_free( sei->rdn.bv_val );
673 ch_free( sei->cookie.bv_val );
674 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
676 sei = LDAP_SLIST_FIRST( &consumer_subentry );
681 if( be->be_entry_close( be )) rc = EXIT_FAILURE;