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 = { 0, NULL };
70 struct berval ldifcsn = { 0, NULL };
72 int provider_subentry = 0;
73 struct subentryinfo *sei;
74 LDAP_SLIST_HEAD( consumer_subentry_slist, subentryinfo ) consumer_subentry;
78 struct berval ctxcsn_ndn = { 0, NULL };
83 lutil_log_initialize(argc, argv );
85 slap_tool_init( progname, SLAPADD, argc, argv );
87 LDAP_SLIST_INIT( &consumer_subentry );
89 if( !be->be_entry_open ||
90 !be->be_entry_close ||
93 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
101 if( be->be_entry_open( be, 1 ) != 0 ) {
102 fprintf( stderr, "%s: could not open database.\n",
104 exit( EXIT_FAILURE );
107 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
108 Entry *e = str2entry( buf );
111 * Initialize text buffer
113 bvtext.bv_len = textlen;
114 bvtext.bv_val = textbuf;
115 bvtext.bv_val[0] = '\0';
118 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
121 if( continuemode ) continue;
125 /* make sure the DN is not empty */
126 if( !e->e_nname.bv_len ) {
127 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
128 progname, e->e_dn, lineno );
131 if( continuemode ) continue;
136 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
139 fprintf( stderr, "%s: line %d: "
140 "database (%s) not configured to hold \"%s\"\n",
142 be ? be->be_suffix[0].bv_val : "<none>",
144 fprintf( stderr, "%s: line %d: "
145 "database (%s) not configured to hold \"%s\"\n",
147 be ? be->be_nsuffix[0].bv_val : "<none>",
151 if( continuemode ) continue;
155 if( global_schemacheck ) {
156 Attribute *sc = attr_find( e->e_attrs,
157 slap_schema.si_ad_structuralObjectClass );
158 Attribute *oc = attr_find( e->e_attrs,
159 slap_schema.si_ad_objectClass );
162 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
163 progname, e->e_dn, lineno,
164 "no objectClass attribute");
167 if( continuemode ) continue;
172 struct berval vals[2];
174 rc = structural_class( oc->a_vals, vals,
175 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 vals[1].bv_val = NULL;
189 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
190 vals, NULL /* FIXME */ );
194 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
196 if( rc != LDAP_SUCCESS ) {
197 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
198 progname, e->e_dn, lineno, rc, text );
201 if( continuemode ) continue;
206 if ( SLAP_LASTMOD(be) ) {
208 time_t now = slap_get_time();
209 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
210 struct berval vals[ 2 ];
212 struct berval name, timestamp;
214 struct berval nvals[ 2 ];
216 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
219 vals[1].bv_val = NULL;
222 nvals[1].bv_val = NULL;
225 lutil_gentime( timebuf, sizeof(timebuf), ltm );
227 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
230 timestamp.bv_val = timebuf;
231 timestamp.bv_len = strlen(timebuf);
233 if ( be->be_rootndn.bv_len == 0 ) {
234 name.bv_val = SLAPD_ANONYMOUS;
235 name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
236 nname.bv_val = SLAPD_ANONYMOUS;
237 nname.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
239 name = be->be_rootdn;
240 nname = be->be_rootndn;
243 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
246 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
247 vals[0].bv_val = uuidbuf;
248 attr_merge_normalize_one( e,
249 slap_schema.si_ad_entryUUID, vals, NULL );
252 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
257 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
260 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
265 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
268 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
272 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
275 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
279 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
282 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
286 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
289 if ( !is_entry_syncProviderSubentry( e ) &&
290 !is_entry_syncConsumerSubentry( e ) &&
291 update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
292 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
293 if ( maxcsn.bv_len != 0 ) {
294 value_match( &match, slap_schema.si_ad_entryCSN,
295 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
296 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
297 &maxcsn, &attr->a_nvals[0], &text );
303 ch_free( maxcsn.bv_val );
304 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
309 if ( update_ctxcsn == SLAP_TOOL_CTXCSN_KEEP ) {
310 if ( is_entry_syncProviderSubentry( e )) {
311 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
312 fprintf( stderr, "%s: consumer and provider subentries "
313 "are both present\n", progname );
316 sei = LDAP_SLIST_FIRST( &consumer_subentry );
318 ch_free( sei->cn.bv_val );
319 ch_free( sei->ndn.bv_val );
320 ch_free( sei->rdn.bv_val );
321 ch_free( sei->cookie.bv_val );
322 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
324 sei = LDAP_SLIST_FIRST( &consumer_subentry );
328 if ( provider_subentry ) {
329 fprintf( stderr, "%s: multiple provider subentries are "
330 "present : add -w flag to refresh\n", progname );
335 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
336 if ( attr == NULL ) {
340 provider_subentry = 1;
341 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
342 } else if ( is_entry_syncConsumerSubentry( e )) {
343 if ( provider_subentry ) {
344 fprintf( stderr, "%s: consumer and provider subentries "
345 "are both present\n", progname );
351 attr = attr_find( e->e_attrs, slap_schema.si_ad_cn );
353 if ( attr == NULL ) {
358 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
359 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
360 value_match( &match, slap_schema.si_ad_cn,
361 slap_schema.si_ad_cn->ad_type->sat_equality,
362 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
363 &sei->cn, &attr->a_nvals[0], &text );
366 fprintf( stderr, "%s: multiple consumer subentries "
367 "have the same id : add -w flag to refresh\n",
371 sei = LDAP_SLIST_FIRST( &consumer_subentry );
373 ch_free( sei->cn.bv_val );
374 ch_free( sei->ndn.bv_val );
375 ch_free( sei->rdn.bv_val );
376 ch_free( sei->cookie.bv_val );
377 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
379 sei = LDAP_SLIST_FIRST( &consumer_subentry );
384 sei = ch_calloc( 1, sizeof( struct subentryinfo ));
385 ber_dupbv( &sei->cn, &attr->a_nvals[0] );
386 ber_dupbv( &sei->ndn, &e->e_nname );
387 dnExtractRdn( &sei->ndn, &sei->rdn, NULL );
388 attr = attr_find( e->e_attrs, slap_schema.si_ad_syncreplCookie );
389 if ( attr == NULL ) {
390 ch_free( sei->cn.bv_val );
391 ch_free( sei->ndn.bv_val );
392 ch_free( sei->rdn.bv_val );
393 ch_free( sei->cookie.bv_val );
398 ber_dupbv( &sei->cookie, &attr->a_nvals[0] );
399 LDAP_SLIST_INSERT_HEAD( &consumer_subentry, sei, sei_next );
403 if ( !is_entry_syncProviderSubentry( e ) &&
404 !is_entry_syncConsumerSubentry( e )) {
406 ID id = be->be_entry_put( be, e, &bvtext );
408 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
409 "(line=%d): %s\n", progname, e->e_dn,
410 lineno, bvtext.bv_val );
413 if( continuemode ) continue;
418 fprintf( stderr, "added: \"%s\" (%08lx)\n",
419 e->e_dn, (long) id );
423 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
431 bvtext.bv_len = textlen;
432 bvtext.bv_val = textbuf;
433 bvtext.bv_val[0] = '\0';
435 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
437 maxcsn.bv_val = NULL;
438 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
439 if ( maxcsn.bv_len != 0 ) {
440 value_match( &match, slap_schema.si_ad_syncreplCookie,
441 slap_schema.si_ad_syncreplCookie->ad_type->sat_ordering,
442 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
443 &maxcsn, &sei->cookie, &text );
449 ch_free( maxcsn.bv_val );
450 ber_dupbv( &maxcsn, &sei->cookie );
455 if ( SLAP_LASTMOD(be) && replica_promotion ) {
456 if ( provider_subentry || update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
457 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
458 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
459 (struct berval *)&slap_ldapsync_cn_bv, NULL );
460 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
462 if ( ctxcsn_id == NOID ) {
463 ctxcsn_e = slap_create_context_csn_entry( be, &maxcsn );
465 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
466 if( ctxcsn_id == NOID ) {
467 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
472 fprintf( stderr, "added: \"%s\" (%08lx)\n",
473 ctxcsn_e->e_dn, (long) ctxcsn_id );
477 fprintf( stderr, "(dry) added: \"%s\"\n", ctxcsn_e->e_dn );
480 entry_free( ctxcsn_e );
482 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
483 if ( ret == LDAP_SUCCESS ) {
484 attr = attr_find( ctxcsn_e->e_attrs,
485 slap_schema.si_ad_contextCSN );
486 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
487 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
488 attr->a_vals[0].bv_len = maxcsn.bv_len;
490 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
491 if( ctxcsn_id == NOID ) {
492 fprintf( stderr, "%s: could not modify ctxcsn "
493 "subentry\n", progname);
497 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
498 ctxcsn_e->e_dn, (long) ctxcsn_id );
502 fprintf( stderr, "(dry) modified: \"%s\"\n",
507 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
513 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
514 ( update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
515 provider_subentry )) {
517 ber_dupbv( &slap_syncrepl_bv, (struct berval *) &slap_syncrepl_bvc );
518 ber_dupbv( &slap_syncrepl_cn_bv,
519 (struct berval *) &slap_syncrepl_cn_bvc );
521 if ( replica_id_list == NULL ) {
522 replica_id_list = ch_calloc( 2, sizeof( int ));
523 replica_id_list[0] = 0;
524 replica_id_list[1] = -1;
527 for ( i = 0; replica_id_list[i] > -1 ; i++ ) {
528 slap_syncrepl_bv.bv_len = snprintf( slap_syncrepl_bv.bv_val,
529 slap_syncrepl_bvc.bv_len,
530 "syncrepl%d", replica_id_list[i] );
531 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
532 slap_syncrepl_cn_bvc.bv_len,
533 "cn=syncrepl%d", replica_id_list[i] );
534 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
535 (struct berval *)&slap_syncrepl_cn_bv, NULL );
536 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
538 if ( ctxcsn_id == NOID ) {
539 ctxcsn_e = slap_create_syncrepl_entry( be, &maxcsn,
540 &slap_syncrepl_cn_bv,
543 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
544 if( ctxcsn_id == NOID ) {
545 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
550 fprintf( stderr, "added: \"%s\" (%08lx)\n",
551 ctxcsn_e->e_dn, (long) ctxcsn_id );
555 fprintf( stderr, "(dry) added: \"%s\"\n",
559 entry_free( ctxcsn_e );
561 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
562 if ( ret == LDAP_SUCCESS ) {
563 attr = attr_find( ctxcsn_e->e_attrs,
564 slap_schema.si_ad_syncreplCookie );
565 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
566 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
567 attr->a_vals[0].bv_len = maxcsn.bv_len;
569 ctxcsn_id = be->be_entry_modify( be,
571 if( ctxcsn_id == NOID ) {
572 fprintf( stderr, "%s: could not modify ctxcsn "
573 "subentry\n", progname);
577 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
578 ctxcsn_e->e_dn, (long) ctxcsn_id );
582 fprintf( stderr, "(dry) modified: \"%s\"\n",
587 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
594 if ( slap_syncrepl_bv.bv_val ) {
595 ch_free( slap_syncrepl_bv.bv_val );
597 if ( slap_syncrepl_cn_bv.bv_val ) {
598 ch_free( slap_syncrepl_cn_bv.bv_val );
600 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
601 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
603 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
604 ctxcsn_id = be->be_dn2id_get( be, &sei->ndn );
606 if ( ctxcsn_id == NOID ) {
607 ctxcsn_e = slap_create_syncrepl_entry( be, &sei->cookie,
608 &sei->rdn, &sei->cn );
610 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
611 if( ctxcsn_id == NOID ) {
612 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
617 fprintf( stderr, "added: \"%s\" (%08lx)\n",
618 ctxcsn_e->e_dn, (long) ctxcsn_id );
622 fprintf( stderr, "(dry) added: \"%s\"\n",
626 entry_free( ctxcsn_e );
628 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
629 if ( ret == LDAP_SUCCESS ) {
630 attr = attr_find( ctxcsn_e->e_attrs,
631 slap_schema.si_ad_syncreplCookie );
632 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
633 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
634 attr->a_vals[0].bv_len = maxcsn.bv_len;
636 ctxcsn_id = be->be_entry_modify( be,
638 if( ctxcsn_id == NOID ) {
639 fprintf( stderr, "%s: could not modify ctxcsn "
640 "subentry\n", progname);
644 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
645 ctxcsn_e->e_dn, (long) ctxcsn_id );
649 fprintf( stderr, "(dry) modified: \"%s\"\n",
654 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
661 if ( slap_syncrepl_bv.bv_val ) {
662 ch_free( slap_syncrepl_bv.bv_val );
664 if ( slap_syncrepl_cn_bv.bv_val ) {
665 ch_free( slap_syncrepl_cn_bv.bv_val );
669 sei = LDAP_SLIST_FIRST( &consumer_subentry );
671 ch_free( sei->cn.bv_val );
672 ch_free( sei->ndn.bv_val );
673 ch_free( sei->rdn.bv_val );
674 ch_free( sei->cookie.bv_val );
675 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
677 sei = LDAP_SLIST_FIRST( &consumer_subentry );
682 if( be->be_entry_close( be )) rc = EXIT_FAILURE;