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 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 if ( is_entry_syncProviderSubentry( e )) {
313 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
314 fprintf( stderr, "%s: consumer and provider subentries "
315 "are both present\n", progname );
318 sei = LDAP_SLIST_FIRST( &consumer_subentry );
320 ch_free( sei->cn.bv_val );
321 ch_free( sei->ndn.bv_val );
322 ch_free( sei->rdn.bv_val );
323 ch_free( sei->cookie.bv_val );
324 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
326 sei = LDAP_SLIST_FIRST( &consumer_subentry );
330 if ( provider_subentry ) {
331 fprintf( stderr, "%s: multiple provider subentries are "
332 "present : add -w flag to refresh\n", progname );
337 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
338 if ( attr == NULL ) {
342 provider_subentry = 1;
343 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
344 } else if ( is_entry_syncConsumerSubentry( e )) {
345 if ( provider_subentry ) {
346 fprintf( stderr, "%s: consumer and provider subentries "
347 "are both present\n", progname );
353 attr = attr_find( e->e_attrs, slap_schema.si_ad_cn );
355 if ( attr == NULL ) {
360 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
361 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
362 value_match( &match, slap_schema.si_ad_cn,
363 slap_schema.si_ad_cn->ad_type->sat_equality,
364 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
365 &sei->cn, &attr->a_nvals[0], &text );
368 fprintf( stderr, "%s: multiple consumer subentries "
369 "have the same id : add -w flag to refresh\n",
373 sei = LDAP_SLIST_FIRST( &consumer_subentry );
375 ch_free( sei->cn.bv_val );
376 ch_free( sei->ndn.bv_val );
377 ch_free( sei->rdn.bv_val );
378 ch_free( sei->cookie.bv_val );
379 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
381 sei = LDAP_SLIST_FIRST( &consumer_subentry );
386 sei = ch_calloc( 1, sizeof( struct subentryinfo ));
387 ber_dupbv( &sei->cn, &attr->a_nvals[0] );
388 ber_dupbv( &sei->ndn, &e->e_nname );
389 dnExtractRdn( &sei->ndn, &sei->rdn, NULL );
390 attr = attr_find( e->e_attrs, slap_schema.si_ad_syncreplCookie );
391 if ( attr == NULL ) {
392 ch_free( sei->cn.bv_val );
393 ch_free( sei->ndn.bv_val );
394 ch_free( sei->rdn.bv_val );
395 ch_free( sei->cookie.bv_val );
400 ber_dupbv( &sei->cookie, &attr->a_nvals[0] );
401 LDAP_SLIST_INSERT_HEAD( &consumer_subentry, sei, sei_next );
405 if ( !is_entry_syncProviderSubentry( e ) &&
406 !is_entry_syncConsumerSubentry( e )) {
408 ID id = be->be_entry_put( be, e, &bvtext );
410 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
411 "(line=%d): %s\n", progname, e->e_dn,
412 lineno, bvtext.bv_val );
415 if( continuemode ) continue;
420 fprintf( stderr, "added: \"%s\" (%08lx)\n",
421 e->e_dn, (long) id );
425 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
433 bvtext.bv_len = textlen;
434 bvtext.bv_val = textbuf;
435 bvtext.bv_val[0] = '\0';
437 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
439 maxcsn.bv_val = NULL;
440 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
441 sc.octet_str = &sei->cookie;
442 slap_parse_sync_cookie( &sc );
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, &sc.ctxcsn[0], &text );
453 ch_free( maxcsn.bv_val );
454 ber_dupbv( &maxcsn, &sc.ctxcsn[0] );
457 slap_sync_cookie_free( &sc, 0 );
461 slap_compose_sync_cookie( NULL, &mc, &maxcsn, -1, -1 );
463 if ( SLAP_LASTMOD(be) && replica_promotion ) {
464 if ( provider_subentry || update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
465 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
466 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
467 (struct berval *)&slap_ldapsync_cn_bv, NULL );
468 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
470 if ( ctxcsn_id == NOID ) {
471 ctxcsn_e = slap_create_context_csn_entry( be, &maxcsn );
473 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
474 if( ctxcsn_id == NOID ) {
475 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
480 fprintf( stderr, "added: \"%s\" (%08lx)\n",
481 ctxcsn_e->e_dn, (long) ctxcsn_id );
485 fprintf( stderr, "(dry) added: \"%s\"\n", ctxcsn_e->e_dn );
488 entry_free( ctxcsn_e );
490 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
491 if ( ret == LDAP_SUCCESS ) {
492 attr = attr_find( ctxcsn_e->e_attrs,
493 slap_schema.si_ad_contextCSN );
494 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
495 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
496 attr->a_vals[0].bv_len = maxcsn.bv_len;
498 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
499 if( ctxcsn_id == NOID ) {
500 fprintf( stderr, "%s: could not modify ctxcsn "
501 "subentry\n", progname);
505 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
506 ctxcsn_e->e_dn, (long) ctxcsn_id );
510 fprintf( stderr, "(dry) modified: \"%s\"\n",
515 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
521 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
522 ( update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
523 provider_subentry )) {
525 ber_dupbv( &slap_syncrepl_bv, (struct berval *) &slap_syncrepl_bvc );
526 ber_dupbv( &slap_syncrepl_cn_bv,
527 (struct berval *) &slap_syncrepl_cn_bvc );
529 if ( replica_id_list == NULL ) {
530 replica_id_list = ch_calloc( 2, sizeof( int ));
531 replica_id_list[0] = 0;
532 replica_id_list[1] = -1;
535 for ( i = 0; replica_id_list[i] > -1 ; i++ ) {
536 slap_syncrepl_bv.bv_len = snprintf( slap_syncrepl_bv.bv_val,
537 slap_syncrepl_bvc.bv_len,
538 "syncrepl%d", replica_id_list[i] );
539 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
540 slap_syncrepl_cn_bvc.bv_len,
541 "cn=syncrepl%d", replica_id_list[i] );
542 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
543 (struct berval *)&slap_syncrepl_cn_bv, NULL );
544 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
546 if ( ctxcsn_id == NOID ) {
547 ctxcsn_e = slap_create_syncrepl_entry( be, &mc,
548 &slap_syncrepl_cn_bv,
551 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
552 if( ctxcsn_id == NOID ) {
553 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
558 fprintf( stderr, "added: \"%s\" (%08lx)\n",
559 ctxcsn_e->e_dn, (long) ctxcsn_id );
563 fprintf( stderr, "(dry) added: \"%s\"\n",
567 entry_free( ctxcsn_e );
569 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
570 if ( ret == LDAP_SUCCESS ) {
571 attr = attr_find( ctxcsn_e->e_attrs,
572 slap_schema.si_ad_syncreplCookie );
573 AC_MEMCPY( attr->a_vals[0].bv_val, mc.bv_val, mc.bv_len );
574 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
575 attr->a_vals[0].bv_len = maxcsn.bv_len;
577 ctxcsn_id = be->be_entry_modify( be,
579 if( ctxcsn_id == NOID ) {
580 fprintf( stderr, "%s: could not modify ctxcsn "
581 "subentry\n", progname);
585 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
586 ctxcsn_e->e_dn, (long) ctxcsn_id );
590 fprintf( stderr, "(dry) modified: \"%s\"\n",
595 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
602 if ( slap_syncrepl_bv.bv_val ) {
603 ch_free( slap_syncrepl_bv.bv_val );
605 if ( slap_syncrepl_cn_bv.bv_val ) {
606 ch_free( slap_syncrepl_cn_bv.bv_val );
608 } else if ( SLAP_LASTMOD(be) && replica_demotion &&
609 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
611 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
612 ctxcsn_id = be->be_dn2id_get( be, &sei->ndn );
614 if ( ctxcsn_id == NOID ) {
615 ctxcsn_e = slap_create_syncrepl_entry( be, &sei->cookie,
616 &sei->rdn, &sei->cn );
618 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
619 if( ctxcsn_id == NOID ) {
620 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
625 fprintf( stderr, "added: \"%s\" (%08lx)\n",
626 ctxcsn_e->e_dn, (long) ctxcsn_id );
630 fprintf( stderr, "(dry) added: \"%s\"\n",
634 entry_free( ctxcsn_e );
636 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
637 if ( ret == LDAP_SUCCESS ) {
638 attr = attr_find( ctxcsn_e->e_attrs,
639 slap_schema.si_ad_syncreplCookie );
640 AC_MEMCPY( attr->a_vals[0].bv_val, sei->cookie.bv_val, sei->cookie.bv_len );
641 attr->a_vals[0].bv_val[sei->cookie.bv_len] = '\0';
642 attr->a_vals[0].bv_len = sei->cookie.bv_len;
644 ctxcsn_id = be->be_entry_modify( be,
646 if( ctxcsn_id == NOID ) {
647 fprintf( stderr, "%s: could not modify ctxcsn "
648 "subentry\n", progname);
652 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
653 ctxcsn_e->e_dn, (long) ctxcsn_id );
657 fprintf( stderr, "(dry) modified: \"%s\"\n",
662 fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
669 if ( slap_syncrepl_bv.bv_val ) {
670 ch_free( slap_syncrepl_bv.bv_val );
672 if ( slap_syncrepl_cn_bv.bv_val ) {
673 ch_free( slap_syncrepl_cn_bv.bv_val );
677 sei = LDAP_SLIST_FIRST( &consumer_subentry );
679 ch_free( sei->cn.bv_val );
680 ch_free( sei->ndn.bv_val );
681 ch_free( sei->rdn.bv_val );
682 ch_free( sei->cookie.bv_val );
683 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
685 sei = LDAP_SLIST_FIRST( &consumer_subentry );
690 if( be->be_entry_close( be )) rc = EXIT_FAILURE;