]> git.sur5r.net Git - openldap/blob - servers/slapd/slapadd.c
slapadd -i <rid list> fix
[openldap] / servers / slapd / slapadd.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 The OpenLDAP Foundation.
5  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
6  * Portions Copyright 2003 IBM Corporation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Kurt Zeilenga for inclusion
19  * in OpenLDAP Software.  Additional signficant contributors include
20  *    Jong Hyuk Choi
21  *    Pierangelo Masarati
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/stdlib.h>
29
30 #include <ac/ctype.h>
31 #include <ac/string.h>
32 #include <ac/socket.h>
33 #include <ac/unistd.h>
34
35 #include <lber.h>
36 #include <ldif.h>
37 #include <lutil.h>
38
39 #include "slapcommon.h"
40
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;
46
47 struct subentryinfo {
48         struct berval cn;
49         struct berval ndn;
50         struct berval rdn;
51         struct berval cookie;
52         LDAP_SLIST_ENTRY( subentryinfo ) sei_next;
53 };
54
55 int
56 slapadd( int argc, char **argv )
57 {
58         char            *buf = NULL;
59         int         lineno;
60         int         lmax;
61         int                     rc = EXIT_SUCCESS;
62
63         const char *text;
64         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
65         size_t textlen = sizeof textbuf;
66         const char *progname = "slapadd";
67
68         struct berval csn;
69         struct berval maxcsn = BER_BVNULL;
70         struct berval ldifcsn = BER_BVNULL;
71         int match;
72         int     provider_subentry = 0;
73         struct subentryinfo *sei;
74         LDAP_SLIST_HEAD( consumer_subentry_slist, subentryinfo ) consumer_subentry;
75         Attribute *attr;
76         Entry *ctxcsn_e;
77         ID      ctxcsn_id;
78         struct berval   ctxcsn_ndn = BER_BVNULL;
79         int ret;
80         struct berval bvtext;
81         int i;
82         struct berval mc;
83         struct sync_cookie sc;
84 #ifdef NEW_LOGGING
85         lutil_log_initialize(argc, argv );
86 #endif
87         slap_tool_init( progname, SLAPADD, argc, argv );
88
89         LDAP_SLIST_INIT( &consumer_subentry );
90
91         if( !be->be_entry_open ||
92                 !be->be_entry_close ||
93                 !be->be_entry_put )
94         {
95                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
96                         progname );
97                 if ( dryrun ) {
98                         fprintf( stderr, "\t(dry) continuing...\n" );
99
100                 } else {
101                         exit( EXIT_FAILURE );
102                 }
103         }
104
105         lmax = 0;
106         lineno = 0;
107
108         if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
109                 fprintf( stderr, "%s: could not open database.\n",
110                         progname );
111                 exit( EXIT_FAILURE );
112         }
113
114         while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
115                 Entry *e = str2entry( buf );
116
117                 /*
118                  * Initialize text buffer
119                  */
120                 bvtext.bv_len = textlen;
121                 bvtext.bv_val = textbuf;
122                 bvtext.bv_val[0] = '\0';
123
124                 if( e == NULL ) {
125                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
126                                 progname, lineno );
127                         rc = EXIT_FAILURE;
128                         if( continuemode ) continue;
129                         break;
130                 }
131
132                 /* make sure the DN is not empty */
133                 if( !e->e_nname.bv_len ) {
134                         fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
135                                 progname, e->e_dn, lineno );
136                         rc = EXIT_FAILURE;
137                         entry_free( e );
138                         if( continuemode ) continue;
139                         break;
140                 }
141
142                 /* check backend */
143                 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
144                         != be )
145                 {
146                         fprintf( stderr, "%s: line %d: "
147                                 "database (%s) not configured to hold \"%s\"\n",
148                                 progname, lineno,
149                                 be ? be->be_suffix[0].bv_val : "<none>",
150                                 e->e_dn );
151                         fprintf( stderr, "%s: line %d: "
152                                 "database (%s) not configured to hold \"%s\"\n",
153                                 progname, lineno,
154                                 be ? be->be_nsuffix[0].bv_val : "<none>",
155                                 e->e_ndn );
156                         rc = EXIT_FAILURE;
157                         entry_free( e );
158                         if( continuemode ) continue;
159                         break;
160                 }
161
162                 if( global_schemacheck ) {
163                         Attribute *sc = attr_find( e->e_attrs,
164                                 slap_schema.si_ad_structuralObjectClass );
165                         Attribute *oc = attr_find( e->e_attrs,
166                                 slap_schema.si_ad_objectClass );
167
168                         if( oc == NULL ) {
169                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
170                                         progname, e->e_dn, lineno,
171                                         "no objectClass attribute");
172                                 rc = EXIT_FAILURE;
173                                 entry_free( e );
174                                 if( continuemode ) continue;
175                                 break;
176                         }
177
178                         if( sc == NULL ) {
179                                 struct berval vals[2];
180
181                                 rc = structural_class( oc->a_vals, vals,
182                                         NULL, &text, textbuf, textlen );
183
184                                 if( rc != LDAP_SUCCESS ) {
185                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
186                                                 progname, e->e_dn, lineno, rc, text );
187                                         rc = EXIT_FAILURE;
188                                         entry_free( e );
189                                         if( continuemode ) continue;
190                                         break;
191                                 }
192
193                                 vals[1].bv_len = 0;
194                                 vals[1].bv_val = NULL;
195
196                                 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
197                                         vals, NULL /* FIXME */ );
198                         }
199
200                         /* check schema */
201                         rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
202
203                         if( rc != LDAP_SUCCESS ) {
204                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
205                                         progname, e->e_dn, lineno, rc, text );
206                                 rc = EXIT_FAILURE;
207                                 entry_free( e );
208                                 if( continuemode ) continue;
209                                 break;
210                         }
211                 }
212
213                 if ( SLAP_LASTMOD(be) ) {
214                         struct tm *ltm;
215                         time_t now = slap_get_time();
216                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
217                         struct berval vals[ 2 ];
218
219                         struct berval name, timestamp;
220
221                         struct berval nvals[ 2 ];
222                         struct berval nname;
223                         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
224
225                         vals[1].bv_len = 0;
226                         vals[1].bv_val = NULL;
227
228                         nvals[1].bv_len = 0;
229                         nvals[1].bv_val = NULL;
230
231                         ltm = gmtime(&now);
232                         lutil_gentime( timebuf, sizeof(timebuf), ltm );
233
234                         csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
235                         csn.bv_val = csnbuf;
236
237                         timestamp.bv_val = timebuf;
238                         timestamp.bv_len = strlen(timebuf);
239
240                         if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
241                                 BER_BVSTR( &name, SLAPD_ANONYMOUS );
242                                 nname = name;
243                         } else {
244                                 name = be->be_rootdn;
245                                 nname = be->be_rootndn;
246                         }
247
248                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
249                                 == NULL )
250                         {
251                                 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
252                                 vals[0].bv_val = uuidbuf;
253                                 attr_merge_normalize_one( e,
254                                                         slap_schema.si_ad_entryUUID, vals, NULL );
255                         }
256
257                         if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
258                                 == NULL )
259                         {
260                                 vals[0] = name;
261                                 nvals[0] = nname;
262                                 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
263                         }
264
265                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
266                                 == NULL )
267                         {
268                                 vals[0] = name;
269                                 nvals[0] = nname;
270                                 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
271                         }
272
273                         if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
274                                 == NULL )
275                         {
276                                 vals[0] = timestamp;
277                                 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
278                         }
279
280                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
281                                 == NULL )
282                         {
283                                 vals[0] = timestamp;
284                                 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
285                         }
286
287                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
288                                 == NULL )
289                         {
290                                 vals[0] = csn;
291                                 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
292                         }
293
294                         if ( !is_entry_syncProviderSubentry( e ) &&
295                                  !is_entry_syncConsumerSubentry( e ) &&
296                                  update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
297                                 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
298                                 if ( maxcsn.bv_len != 0 ) {
299                                         value_match( &match, slap_schema.si_ad_entryCSN,
300                                                 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
301                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
302                                                 &maxcsn, &attr->a_nvals[0], &text );
303                                 } else {
304                                         match = -1;
305                                 }
306                                 if ( match < 0 ) {
307                                         if ( maxcsn.bv_val )
308                                                 ch_free( maxcsn.bv_val );
309                                         ber_dupbv( &maxcsn, &attr->a_nvals[0] );
310                                 }
311                         }
312                 }
313
314                 if ( dryrun ) {
315                         if ( verbose ) {
316                                 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
317                         }
318                         goto done;
319                 }
320
321                 if ( update_ctxcsn == SLAP_TOOL_CTXCSN_KEEP &&
322                         ( replica_promotion || replica_demotion )) {
323                         if ( is_entry_syncProviderSubentry( e )) { 
324                                 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
325                                         fprintf( stderr, "%s: consumer and provider subentries "
326                                                                          "are both present\n", progname );
327                                         rc = EXIT_FAILURE;
328                                         entry_free( e );
329                                         sei = LDAP_SLIST_FIRST( &consumer_subentry );
330                                         while ( sei ) {
331                                                 ch_free( sei->cn.bv_val );
332                                                 ch_free( sei->ndn.bv_val );
333                                                 ch_free( sei->rdn.bv_val );
334                                                 ch_free( sei->cookie.bv_val );
335                                                 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
336                                                 ch_free( sei );
337                                                 sei = LDAP_SLIST_FIRST( &consumer_subentry );
338                                         }
339                                         break;
340                                 }
341                                 if ( provider_subentry ) {
342                                         fprintf( stderr, "%s: multiple provider subentries are "
343                                                         "present : add -w flag to refresh\n", progname );
344                                         rc = EXIT_FAILURE;
345                                         entry_free( e );
346                                         break;
347                                 }
348                                 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
349                                 if ( attr == NULL ) {
350                                         entry_free( e );
351                                         continue;
352                                 }
353                                 provider_subentry = 1;
354                                 ber_dupbv( &maxcsn, &attr->a_nvals[0] );
355                         } else if ( is_entry_syncConsumerSubentry( e )) {
356                                 if ( provider_subentry ) {
357                                         fprintf( stderr, "%s: consumer and provider subentries "
358                                                                          "are both present\n", progname );
359                                         rc = EXIT_FAILURE;
360                                         entry_free( e );
361                                         break;
362                                 }
363
364                                 attr = attr_find( e->e_attrs, slap_schema.si_ad_cn );
365
366                                 if ( attr == NULL ) {
367                                         entry_free( e );
368                                         continue;
369                                 }
370
371                                 if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
372                                         LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
373                                                 value_match( &match, slap_schema.si_ad_cn,
374                                                         slap_schema.si_ad_cn->ad_type->sat_equality,
375                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
376                                                         &sei->cn, &attr->a_nvals[0], &text );
377                                         }
378                                         if ( !match ) {
379                                                 fprintf( stderr, "%s: multiple consumer subentries "
380                                                                 "have the same id : add -w flag to refresh\n",
381                                                                 progname );
382                                                 rc = EXIT_FAILURE;
383                                                 entry_free( e );
384                                                 sei = LDAP_SLIST_FIRST( &consumer_subentry );
385                                                 while ( sei ) {
386                                                         ch_free( sei->cn.bv_val );
387                                                         ch_free( sei->ndn.bv_val );
388                                                         ch_free( sei->rdn.bv_val );
389                                                         ch_free( sei->cookie.bv_val );
390                                                         LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
391                                                         ch_free( sei );
392                                                         sei = LDAP_SLIST_FIRST( &consumer_subentry );
393                                                 }
394                                                 break;
395                                         }
396                                 }
397                                 sei = ch_calloc( 1, sizeof( struct subentryinfo ));
398                                 ber_dupbv( &sei->cn, &attr->a_nvals[0] );
399                                 ber_dupbv( &sei->ndn, &e->e_nname );
400                                 dnExtractRdn( &sei->ndn, &sei->rdn, NULL );
401                                 attr = attr_find( e->e_attrs, slap_schema.si_ad_syncreplCookie );
402                                 if ( attr == NULL ) {
403                                         ch_free( sei->cn.bv_val );
404                                         ch_free( sei->ndn.bv_val );
405                                         ch_free( sei->rdn.bv_val );
406                                         ch_free( sei->cookie.bv_val );
407                                         ch_free( sei );
408                                         entry_free( e );
409                                         continue;
410                                 }
411                                 ber_dupbv( &sei->cookie, &attr->a_nvals[0] );
412                                 LDAP_SLIST_INSERT_HEAD( &consumer_subentry, sei, sei_next );
413                         }
414                 }
415
416                 if (( !is_entry_syncProviderSubentry( e ) &&
417                                  !is_entry_syncConsumerSubentry( e )) ||
418                                  ( !replica_promotion && !replica_demotion ))
419                 {
420                         /* dryrun moved earlier */
421                         assert( !dryrun );
422
423                         if (!dryrun) {
424                                 ID id = be->be_entry_put( be, e, &bvtext );
425                                 if( id == NOID ) {
426                                         fprintf( stderr, "%s: could not add entry dn=\"%s\" "
427                                                                          "(line=%d): %s\n", progname, e->e_dn,
428                                                                          lineno, bvtext.bv_val );
429                                         rc = EXIT_FAILURE;
430                                         entry_free( e );
431                                         if( continuemode ) continue;
432                                         break;
433                                 }
434         
435                                 if ( verbose ) {
436                                         fprintf( stderr, "added: \"%s\" (%08lx)\n",
437                                                 e->e_dn, (long) id );
438                                 }
439                         } else {
440                                 if ( verbose ) {
441                                         fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
442                                 }
443                         }
444                 }
445
446 done:;
447                 entry_free( e );
448         }
449
450         bvtext.bv_len = textlen;
451         bvtext.bv_val = textbuf;
452         bvtext.bv_val[0] = '\0';
453
454         if ( !LDAP_SLIST_EMPTY( &consumer_subentry )) {
455                 maxcsn.bv_len = 0;
456                 maxcsn.bv_val = NULL;
457                 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
458                         sc.octet_str = &sei->cookie;
459                         slap_parse_sync_cookie( &sc );
460                         if ( maxcsn.bv_len != 0 ) {
461                                 value_match( &match, slap_schema.si_ad_syncreplCookie,
462                                         slap_schema.si_ad_syncreplCookie->ad_type->sat_ordering,
463                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
464                                         &maxcsn, &sc.ctxcsn[0], &text );
465                         } else {
466                                 match = -1;
467                         }
468                         if ( match < 0 ) {
469                                 if ( maxcsn.bv_val )
470                                         ch_free( maxcsn.bv_val );
471                                 ber_dupbv( &maxcsn, &sc.ctxcsn[0] );
472                         }
473                         sc.octet_str = NULL;
474                         slap_sync_cookie_free( &sc, 0 );
475                 }
476         }
477
478         slap_compose_sync_cookie( NULL, &mc, &maxcsn, -1, -1 );
479
480         if ( SLAP_LASTMOD(be) && replica_promotion ) {
481                 if ( provider_subentry || update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
482                          !LDAP_SLIST_EMPTY( &consumer_subentry )) {
483                         build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
484                                                   (struct berval *)&slap_ldapsync_cn_bv, NULL );
485                         ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
486                 
487                         if ( ctxcsn_id == NOID ) {
488                                 ctxcsn_e = slap_create_context_csn_entry( be, &maxcsn );
489                                 
490                                 /* dryrun moved earlier */
491                                 assert( !dryrun );
492
493                                 if ( !dryrun ) {
494                                         ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
495                                         if( ctxcsn_id == NOID ) {
496                                                 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
497                                                                                  progname);
498                                                 rc = EXIT_FAILURE;
499                                         }
500                                         if ( verbose ) {
501                                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
502                                                                                  ctxcsn_e->e_dn, (long) ctxcsn_id );
503                                         }
504                                 } else {
505                                         if ( verbose ) {
506                                                 fprintf( stderr, "(dry) added: \"%s\"\n", ctxcsn_e->e_dn );
507                                         }
508                                 }
509                                 entry_free( ctxcsn_e );
510                         } else {
511                                 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
512                                 if ( ret == LDAP_SUCCESS ) {
513                                         attr = attr_find( ctxcsn_e->e_attrs,
514                                                                                 slap_schema.si_ad_contextCSN );
515                                         AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
516                                         attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
517                                         attr->a_vals[0].bv_len = maxcsn.bv_len;
518                                 
519                                         /* dryrun moved earlier */
520                                         assert( !dryrun );
521
522                                         if ( !dryrun ) {
523                                                 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
524                                                 if( ctxcsn_id == NOID ) {
525                                                         fprintf( stderr, "%s: could not modify ctxcsn "
526                                                                                          "subentry\n", progname);
527                                                         rc = EXIT_FAILURE;
528                                                 }
529                                                 if ( verbose ) {
530                                                         fprintf( stderr, "modified: \"%s\" (%08lx)\n",
531                                                                                          ctxcsn_e->e_dn, (long) ctxcsn_id );
532                                                 }
533                                         } else {
534                                                 if ( verbose ) {
535                                                         fprintf( stderr, "(dry) modified: \"%s\"\n",
536                                                                                          ctxcsn_e->e_dn );
537                                                 }
538                                         }
539                                 } else {
540                                         fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
541                                                                          progname);
542                                         rc = EXIT_FAILURE;
543                                 }
544                         }
545                 } 
546         } else if ( SLAP_LASTMOD(be) && replica_demotion &&
547                                 ( update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH ||
548                                 provider_subentry )) {
549
550                 ber_dupbv( &slap_syncrepl_bv, (struct berval *) &slap_syncrepl_bvc );
551                 ber_dupbv( &slap_syncrepl_cn_bv,
552                                         (struct berval *) &slap_syncrepl_cn_bvc );
553
554                 if ( replica_id_list == NULL ) {
555                         replica_id_list = ch_calloc( 2, sizeof( int ));
556                         replica_id_list[0] = 0;
557                         replica_id_list[1] = -1;
558                 }
559
560                 for ( i = 0; replica_id_list[i] > -1 ; i++ ) {
561                         slap_syncrepl_bv.bv_len = snprintf( slap_syncrepl_bv.bv_val,
562                                                                         slap_syncrepl_bvc.bv_len+1,
563                                                                         "syncrepl%d", replica_id_list[i] );
564                         slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
565                                                                                 slap_syncrepl_cn_bvc.bv_len+1,
566                                                                                 "cn=syncrepl%d", replica_id_list[i] );
567                         build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0],
568                                                   (struct berval *)&slap_syncrepl_cn_bv, NULL );
569                         ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
570
571                         if ( ctxcsn_id == NOID ) {
572                                 ctxcsn_e = slap_create_syncrepl_entry( be, &mc,
573                                                 &slap_syncrepl_cn_bv,
574                                                 &slap_syncrepl_bv );
575
576                                 /* dryrun moved earlier */
577                                 assert( !dryrun );
578
579                                 if ( !dryrun ) {
580                                         ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
581                                         if( ctxcsn_id == NOID ) {
582                                                 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
583                                                                                  progname);
584                                                 rc = EXIT_FAILURE;
585                                         }
586                                         if ( verbose ) {
587                                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
588                                                                                  ctxcsn_e->e_dn, (long) ctxcsn_id );
589                                         }
590                                 } else {
591                                         if ( verbose ) {
592                                                 fprintf( stderr, "(dry) added: \"%s\"\n",
593                                                                                         ctxcsn_e->e_dn );
594                                         }
595                                 }
596                                 entry_free( ctxcsn_e );
597                         } else {
598                                 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
599                                 if ( ret == LDAP_SUCCESS ) {
600                                         attr = attr_find( ctxcsn_e->e_attrs,
601                                                                           slap_schema.si_ad_syncreplCookie );
602                                         AC_MEMCPY( attr->a_vals[0].bv_val, mc.bv_val, mc.bv_len );
603                                         attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
604                                         attr->a_vals[0].bv_len = maxcsn.bv_len;
605                                 
606                                         /* dryrun moved earlier */
607                                         assert( !dryrun );
608
609                                         if ( !dryrun ) {
610                                                 ctxcsn_id = be->be_entry_modify( be,
611                                                                                         ctxcsn_e, &bvtext );
612                                                 if( ctxcsn_id == NOID ) {
613                                                         fprintf( stderr, "%s: could not modify ctxcsn "
614                                                                                          "subentry\n", progname);
615                                                         rc = EXIT_FAILURE;
616                                                 }
617                                                 if ( verbose ) {
618                                                         fprintf( stderr, "modified: \"%s\" (%08lx)\n",
619                                                                                          ctxcsn_e->e_dn, (long) ctxcsn_id );
620                                                 }
621                                         } else {
622                                                 if ( verbose ) {
623                                                         fprintf( stderr, "(dry) modified: \"%s\"\n",
624                                                                                          ctxcsn_e->e_dn );
625                                                 }
626                                         }
627                                 } else {
628                                         fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
629                                                                          progname);
630                                         rc = EXIT_FAILURE;
631                                 }
632                         }
633                 }
634                 
635                 if ( slap_syncrepl_bv.bv_val ) {
636                         ch_free( slap_syncrepl_bv.bv_val );
637                 }
638                 if ( slap_syncrepl_cn_bv.bv_val ) {
639                         ch_free( slap_syncrepl_cn_bv.bv_val );
640                 }
641         } else if ( SLAP_LASTMOD(be) && replica_demotion &&
642                                 !LDAP_SLIST_EMPTY( &consumer_subentry )) {
643
644                 LDAP_SLIST_FOREACH( sei, &consumer_subentry, sei_next ) {
645                         ctxcsn_id = be->be_dn2id_get( be, &sei->ndn );
646
647                         if ( ctxcsn_id == NOID ) {
648                                 ctxcsn_e = slap_create_syncrepl_entry( be, &sei->cookie,
649                                                 &sei->rdn, &sei->cn );
650
651                                 /* dryrun moved earlier */
652                                 assert( !dryrun );
653
654                                 if ( !dryrun ) {
655                                         ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
656                                         if( ctxcsn_id == NOID ) {
657                                                 fprintf( stderr, "%s: could not add ctxcsn subentry\n",
658                                                                                  progname);
659                                                 rc = EXIT_FAILURE;
660                                         }
661                                         if ( verbose ) {
662                                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
663                                                                                  ctxcsn_e->e_dn, (long) ctxcsn_id );
664                                         }
665                                 } else {
666                                         if ( verbose ) {
667                                                 fprintf( stderr, "(dry) added: \"%s\"\n",
668                                                                                         ctxcsn_e->e_dn );
669                                         }
670                                 }
671                                 entry_free( ctxcsn_e );
672                         } else {
673                                 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
674                                 if ( ret == LDAP_SUCCESS ) {
675                                         attr = attr_find( ctxcsn_e->e_attrs,
676                                                                           slap_schema.si_ad_syncreplCookie );
677                                         AC_MEMCPY( attr->a_vals[0].bv_val, sei->cookie.bv_val, sei->cookie.bv_len );
678                                         attr->a_vals[0].bv_val[sei->cookie.bv_len] = '\0';
679                                         attr->a_vals[0].bv_len = sei->cookie.bv_len;
680                                         
681                                         /* dryrun moved earlier */
682                                         assert( !dryrun );
683
684                                         if ( !dryrun ) {
685                                                 ctxcsn_id = be->be_entry_modify( be,
686                                                                                         ctxcsn_e, &bvtext );
687                                                 if( ctxcsn_id == NOID ) {
688                                                         fprintf( stderr, "%s: could not modify ctxcsn "
689                                                                                          "subentry\n", progname);
690                                                         rc = EXIT_FAILURE;
691                                                 }
692                                                 if ( verbose ) {
693                                                         fprintf( stderr, "modified: \"%s\" (%08lx)\n",
694                                                                                          ctxcsn_e->e_dn, (long) ctxcsn_id );
695                                                 }
696                                         } else {
697                                                 if ( verbose ) {
698                                                         fprintf( stderr, "(dry) modified: \"%s\"\n",
699                                                                                          ctxcsn_e->e_dn );
700                                                 }
701                                         }
702                                 } else {
703                                         fprintf( stderr, "%s: could not modify ctxcsn subentry\n",
704                                                                          progname);
705                                         rc = EXIT_FAILURE;
706                                 }
707                         }
708                 }
709                 
710                 if ( slap_syncrepl_bv.bv_val ) {
711                         ch_free( slap_syncrepl_bv.bv_val );
712                 }
713                 if ( slap_syncrepl_cn_bv.bv_val ) {
714                         ch_free( slap_syncrepl_cn_bv.bv_val );
715                 }
716         }
717
718         sei = LDAP_SLIST_FIRST( &consumer_subentry );
719         while ( sei ) {
720                 ch_free( sei->cn.bv_val );
721                 ch_free( sei->ndn.bv_val );
722                 ch_free( sei->rdn.bv_val );
723                 ch_free( sei->cookie.bv_val );
724                 LDAP_SLIST_REMOVE_HEAD( &consumer_subentry, sei_next );
725                 ch_free( sei );
726                 sei = LDAP_SLIST_FIRST( &consumer_subentry );
727         }
728
729         ch_free( buf );
730
731         if ( !dryrun ) {
732                 if( be->be_entry_close( be ) ) {
733                         rc = EXIT_FAILURE;
734                 }
735
736                 if( be->be_sync ) {
737                         be->be_sync( be );
738                 }
739         }
740
741         slap_tool_destroy();
742
743         return rc;
744 }
745