]> git.sur5r.net Git - openldap/blob - servers/slapd/slapmodify.c
add slapmodify (ITS#6165; may need cleanup)
[openldap] / servers / slapd / slapmodify.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2010 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 Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25
26 #include "ac/stdlib.h"
27
28 #include "ac/ctype.h"
29 #include "ac/string.h"
30 #include "ac/socket.h"
31 #include "ac/unistd.h"
32
33 #include "lber.h"
34 #include "ldif.h"
35 #include "lutil.h"
36 #include "lutil_meter.h"
37 #include <sys/stat.h>
38
39 #include "slapcommon.h"
40
41 static char csnbuf[ LDAP_PVT_CSNSTR_BUFSIZE ];
42 static char maxcsnbuf[ LDAP_PVT_CSNSTR_BUFSIZE * ( SLAP_SYNC_SID_MAX + 1 ) ];
43
44 int
45 slapmodify( int argc, char **argv )
46 {
47         char *buf = NULL;
48         const char *text;
49         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
50         size_t textlen = sizeof textbuf;
51         const char *progname = "slapmodify";
52
53         struct berval csn;
54         struct berval maxcsn[ SLAP_SYNC_SID_MAX + 1 ];
55         unsigned long sid;
56         struct berval bvtext;
57         Entry *ctxcsn_e;
58         ID      ctxcsn_id, id;
59         OperationBuffer opbuf;
60         Operation *op;
61
62         int match;
63         int checkvals;
64         int lineno, nextline, ldifrc;
65         int lmax;
66         int rc = EXIT_SUCCESS;
67         int manage = 0; 
68
69         int enable_meter = 0;
70         lutil_meter_t meter;
71         struct stat stat_buf;
72
73         /* default "000" */
74         csnsid = 0;
75
76         if ( isatty (2) ) enable_meter = 1;
77         slap_tool_init( progname, SLAPMODIFY, argc, argv );
78
79         memset( &opbuf, 0, sizeof(opbuf) );
80         op = &opbuf.ob_op;
81         op->o_hdr = &opbuf.ob_hdr;
82
83         if ( !be->be_entry_open ||
84                 !be->be_entry_close ||
85                 !be->be_entry_put ||
86                 !be->be_dn2id_get ||
87                 !be->be_entry_get ||
88                 !be->be_entry_modify )
89         {
90                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
91                         progname );
92                 if ( dryrun ) {
93                         fprintf( stderr, "\t(dry) continuing...\n" );
94
95                 } else {
96                         exit( EXIT_FAILURE );
97                 }
98         }
99
100         checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
101
102         lmax = 0;
103         nextline = 0;
104
105         /* enforce schema checking unless not disabled */
106         if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
107                 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
108         }
109
110         if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
111                 fprintf( stderr, "%s: could not open database.\n",
112                         progname );
113                 exit( EXIT_FAILURE );
114         }
115
116         if ( enable_meter 
117 #ifdef LDAP_DEBUG
118                 /* tools default to "none" */
119                 && slap_debug == LDAP_DEBUG_NONE
120 #endif
121                 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
122                 && S_ISREG(stat_buf.st_mode) ) {
123                 enable_meter = !lutil_meter_open(
124                         &meter,
125                         &lutil_meter_text_display,
126                         &lutil_meter_linear_estimator,
127                         stat_buf.st_size);
128         } else {
129                 enable_meter = 0;
130         }
131
132         /* nextline is the line number of the end of the current entry */
133         for( lineno=1; ( ldifrc = ldif_read_record( ldiffp, &nextline, &buf, &lmax )) > 0;
134                 lineno=nextline+1 )
135         {
136                 BackendDB *bd;
137                 Entry *e;
138                 struct berval rbuf;
139                 LDIFRecord lr;
140                 struct berval ndn;
141                 int n;
142                 int is_oc = 0;
143                 int local_rc;
144                 int mod_err = 0;
145                 char *request = "(unknown)";
146
147                 ber_str2bv( buf, 0, 0, &rbuf );
148
149                 if ( lineno < jumpline )
150                         continue;
151
152                 if ( enable_meter )
153                         lutil_meter_update( &meter,
154                                          ftell( ldiffp->fp ),
155                                          0);
156
157                 /*
158                  * Initialize text buffer
159                  */
160                 bvtext.bv_len = textlen;
161                 bvtext.bv_val = textbuf;
162                 bvtext.bv_val[0] = '\0';
163
164                 local_rc = ldap_parse_ldif_record( &rbuf, lineno, &lr,
165                         "slapmodify", LDIF_NO_CONTROLS );
166
167                 if ( local_rc != LDAP_SUCCESS ) {
168                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
169                                 progname, lineno );
170                         rc = EXIT_FAILURE;
171                         if( continuemode ) continue;
172                         break;
173                 }
174
175                 switch ( lr.lr_op ) {
176                 case LDAP_REQ_ADD:
177                         request = "add";
178                         break;
179
180                 case LDAP_REQ_MODIFY:
181                         request = "modify";
182                         break;
183
184                 case LDAP_REQ_MODRDN:
185                 case LDAP_REQ_DELETE:
186                         fprintf( stderr, "%s: request 0x%lx not supported (line=%d)\n",
187                                 progname, (unsigned long)lr.lr_op, lineno );
188                         rc = EXIT_FAILURE;
189                         if( continuemode ) continue;
190                         goto done;
191
192                 default:
193                         fprintf( stderr, "%s: unknown request 0x%lx (line=%d)\n",
194                                 progname, (unsigned long)lr.lr_op, lineno );
195                         rc = EXIT_FAILURE;
196                         if( continuemode ) continue;
197                         goto done;
198                 }
199
200                 local_rc = dnNormalize( 0, NULL, NULL, &lr.lr_dn, &ndn, NULL );
201                 if ( local_rc != LDAP_SUCCESS ) {
202                         fprintf( stderr, "%s: DN=\"%s\" normalization failed (line=%d)\n",
203                                 progname, lr.lr_dn.bv_val, lineno );
204                         rc = EXIT_FAILURE;
205                         if( continuemode ) continue;
206                         break;
207                 }
208
209                 /* make sure the DN is not empty */
210                 if( BER_BVISEMPTY( &ndn ) &&
211                         !BER_BVISEMPTY( be->be_nsuffix ))
212                 {
213                         fprintf( stderr, "%s: line %d: "
214                                 "%s entry with empty dn=\"\"",
215                                 progname, lineno, request );
216                         bd = select_backend( &ndn, nosubordinates );
217                         if ( bd ) {
218                                 BackendDB *bdtmp;
219                                 int dbidx = 0;
220                                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
221                                         if ( bdtmp == bd ) break;
222                                         dbidx++;
223                                 }
224
225                                 assert( bdtmp != NULL );
226                                 
227                                 fprintf( stderr, "; did you mean to use database #%d (%s)?",
228                                         dbidx,
229                                         bd->be_suffix[0].bv_val );
230
231                         }
232                         fprintf( stderr, "\n" );
233                         rc = EXIT_FAILURE;
234                         SLAP_FREE( ndn.bv_val );
235                         ldap_ldif_record_done( &lr );
236                         if( continuemode ) continue;
237                         break;
238                 }
239
240                 /* check backend */
241                 bd = select_backend( &ndn, nosubordinates );
242                 if ( bd != be ) {
243                         fprintf( stderr, "%s: line %d: "
244                                 "database #%d (%s) not configured to hold \"%s\"",
245                                 progname, lineno,
246                                 dbnum,
247                                 be->be_suffix[0].bv_val,
248                                 lr.lr_dn.bv_val );
249                         if ( bd ) {
250                                 BackendDB *bdtmp;
251                                 int dbidx = 0;
252                                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
253                                         if ( bdtmp == bd ) break;
254                                         dbidx++;
255                                 }
256
257                                 assert( bdtmp != NULL );
258                                 
259                                 fprintf( stderr, "; did you mean to use database #%d (%s)?",
260                                         dbidx,
261                                         bd->be_suffix[0].bv_val );
262
263                         } else {
264                                 fprintf( stderr, "; no database configured for that naming context" );
265                         }
266                         fprintf( stderr, "\n" );
267                         rc = EXIT_FAILURE;
268                         SLAP_FREE( ndn.bv_val );
269                         ldap_ldif_record_done( &lr );
270                         if( continuemode ) continue;
271                         break;
272                 }
273
274                 /* get entry */
275                 id = be->be_dn2id_get( be, &ndn );
276                 e = be->be_entry_get( be, id );
277                 if ( e != NULL ) {
278                         Entry *e_tmp = entry_dup( e );
279                         /* FIXME: release? */
280                         e = e_tmp;
281                 }
282
283                 for ( n = 0; lr.lrop_mods[ n ] != NULL; n++ ) {
284                         LDAPMod *mod = lr.lrop_mods[ n ];
285                         Modification mods = { 0 };
286                         unsigned i = 0;
287                         int bin = (mod->mod_op & LDAP_MOD_BVALUES);
288                         int pretty = 0;
289                         int normalize = 0;
290
291                         local_rc = slap_str2ad( mod->mod_type, &mods.sm_desc, &text );
292                         if ( local_rc != LDAP_SUCCESS ) {
293                                 fprintf( stderr, "%s: slap_str2ad(\"%s\") failed for entry \"%s\" (%d: %s, lineno=%d)\n",
294                                         progname, mod->mod_type, lr.lr_dn.bv_val, local_rc, text, lineno );
295                                 rc = EXIT_FAILURE;
296                                 mod_err = 1;
297                                 if( continuemode ) continue;
298                                 SLAP_FREE( ndn.bv_val );
299                                 ldap_ldif_record_done( &lr );
300                                 entry_free( e );
301                                 goto done;
302                         }
303
304                         mods.sm_type = mods.sm_desc->ad_cname;
305
306                         if ( mods.sm_desc->ad_type->sat_syntax->ssyn_pretty ) {
307                                 pretty = 1;
308
309                         } else {
310                                 assert( mods.sm_desc->ad_type->sat_syntax->ssyn_validate != NULL );
311                         }
312
313                         if ( mods.sm_desc->ad_type->sat_equality &&
314                                 mods.sm_desc->ad_type->sat_equality->smr_normalize )
315                         {
316                                 normalize = 1;
317                         }
318
319                         if ( bin && mod->mod_bvalues ) {
320                                 for ( i = 0; mod->mod_bvalues[ i ] != NULL; i++ )
321                                         ;
322
323                         } else if ( !bin && mod->mod_values ) {
324                                 for ( i = 0; mod->mod_values[ i ] != NULL; i++ )
325                                         ;
326                         }
327
328                         mods.sm_values = SLAP_CALLOC( sizeof( struct berval ), i + 1 );
329                         if ( normalize ) {
330                                 mods.sm_nvalues = SLAP_CALLOC( sizeof( struct berval ), i + 1 );
331                         } else {
332                                 mods.sm_nvalues = NULL;
333                         }
334                         mods.sm_numvals = i;
335
336                         for ( i = 0; i < mods.sm_numvals; i++ ) {
337                                 struct berval bv;
338
339                                 if ( bin ) {
340                                         bv = *mod->mod_bvalues[ i ];
341                                 } else {
342                                         ber_str2bv( mod->mod_values[ i ], 0, 0, &bv );
343                                 }
344
345                                 if ( pretty ) {
346                                         local_rc = ordered_value_pretty( mods.sm_desc,
347                                         &bv, &mods.sm_values[i], NULL );
348
349                                 } else {
350                                         local_rc = ordered_value_validate( mods.sm_desc,
351                                                 &bv, 0 );
352                                 }
353
354                                 if ( local_rc != LDAP_SUCCESS ) {
355                                         fprintf( stderr, "%s: DN=\"%s\": unable to %s attr=%s value #%d\n",
356                                                 progname, e->e_dn, pretty ? "prettify" : "validate",
357                                                 mods.sm_desc->ad_cname.bv_val, i );
358                                         /* handle error */
359                                         mod_err = 1;
360                                         rc = EXIT_FAILURE;
361                                         ber_bvarray_free( mods.sm_values );
362                                         ber_bvarray_free( mods.sm_nvalues );
363                                         if( continuemode ) continue;
364                                         SLAP_FREE( ndn.bv_val );
365                                         ldap_ldif_record_done( &lr );
366                                         entry_free( e );
367                                         goto done;
368                                 }
369
370                                 if ( !pretty ) {
371                                         ber_dupbv( &mods.sm_values[i], &bv );
372                                 }
373
374                                 if ( normalize ) {
375                                         local_rc = ordered_value_normalize(
376                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
377                                                 mods.sm_desc,
378                                                 mods.sm_desc->ad_type->sat_equality,
379                                                 &mods.sm_values[i], &mods.sm_nvalues[i],
380                                                 NULL );
381                                         if ( local_rc != LDAP_SUCCESS ) {
382                                                 fprintf( stderr, "%s: DN=\"%s\": unable to normalize attr=%s value #%d\n",
383                                                         progname, e->e_dn, mods.sm_desc->ad_cname.bv_val, i );
384                                                 /* handle error */
385                                                 mod_err = 1;
386                                                 rc = EXIT_FAILURE;
387                                                 ber_bvarray_free( mods.sm_values );
388                                                 ber_bvarray_free( mods.sm_nvalues );
389                                                 if( continuemode ) continue;
390                                                 SLAP_FREE( ndn.bv_val );
391                                                 ldap_ldif_record_done( &lr );
392                                                 entry_free( e );
393                                                 goto done;
394                                         }
395                                 }
396                         }
397
398                         mods.sm_op = (mod->mod_op & ~LDAP_MOD_BVALUES);
399                         mods.sm_flags = 0;
400
401                         if ( mods.sm_desc == slap_schema.si_ad_objectClass ) {
402                                 is_oc = 1;
403                         }
404
405                         switch ( mods.sm_op ) {
406                         case LDAP_MOD_ADD:
407                                 local_rc = modify_add_values( e, &mods,
408                                         0, &text, textbuf, textlen );
409                                 break;
410
411                         case LDAP_MOD_DELETE:
412                                 local_rc = modify_delete_values( e, &mods,
413                                         0, &text, textbuf, textlen );
414                                 break;
415
416                         case LDAP_MOD_REPLACE:
417                                 local_rc = modify_replace_values( e, &mods,
418                                         0, &text, textbuf, textlen );
419                                 break;
420
421                         case LDAP_MOD_INCREMENT:
422                                 local_rc = modify_increment_values( e, &mods,
423                                         0, &text, textbuf, textlen );
424                                 break;
425                         }
426
427                         if ( local_rc != LDAP_SUCCESS ) {
428                                 fprintf( stderr, "%s: DN=\"%s\": unable to modify attr=%s\n",
429                                         progname, e->e_dn, mods.sm_desc->ad_cname.bv_val );
430                                 rc = EXIT_FAILURE;
431                                 ber_bvarray_free( mods.sm_values );
432                                 ber_bvarray_free( mods.sm_nvalues );
433                                 if( continuemode ) continue;
434                                 SLAP_FREE( ndn.bv_val );
435                                 ldap_ldif_record_done( &lr );
436                                 entry_free( e );
437                                 goto done;
438                         }
439                 }
440
441                 if ( SLAP_LASTMOD(be) ) {
442                         time_t now = slap_get_time();
443                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
444                         struct berval vals[ 2 ];
445
446                         struct berval name, timestamp;
447
448                         struct berval nvals[ 2 ];
449                         struct berval nname;
450                         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
451
452                         Attribute *a;
453
454                         vals[1].bv_len = 0;
455                         vals[1].bv_val = NULL;
456
457                         nvals[1].bv_len = 0;
458                         nvals[1].bv_val = NULL;
459
460                         csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
461                         csn.bv_val = csnbuf;
462
463                         timestamp.bv_val = timebuf;
464                         timestamp.bv_len = sizeof(timebuf);
465
466                         slap_timestamp( &now, &timestamp );
467
468                         if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
469                                 BER_BVSTR( &name, SLAPD_ANONYMOUS );
470                                 nname = name;
471                         } else {
472                                 name = be->be_rootdn;
473                                 nname = be->be_rootndn;
474                         }
475
476                         a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
477                         if ( a != NULL ) {
478                                 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
479                                 vals[0].bv_val = uuidbuf;
480                                 if ( a->a_vals != a->a_nvals ) {
481                                         SLAP_FREE( a->a_nvals[0].bv_val );
482                                         SLAP_FREE( a->a_nvals );
483                                 }
484                                 SLAP_FREE( a->a_vals[0].bv_val );
485                                 SLAP_FREE( a->a_vals );
486                                 a->a_vals = NULL;
487                                 a->a_nvals = NULL;
488                                 a->a_numvals = 0;
489                         }
490                         attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
491
492                         a = attr_find( e->e_attrs, slap_schema.si_ad_creatorsName );
493                         if ( a == NULL ) {
494                                 vals[0] = name;
495                                 nvals[0] = nname;
496                                 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
497
498                         } else {
499                                 ber_bvreplace( &a->a_vals[0], &name );
500                                 ber_bvreplace( &a->a_nvals[0], &nname );
501                         }
502
503                         a = attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp );
504                         if ( a == NULL ) {
505                                 vals[0] = timestamp;
506                                 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
507
508                         } else {
509                                 ber_bvreplace( &a->a_vals[0], &timestamp );
510                         }
511
512                         a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
513                         if ( a == NULL ) {
514                                 vals[0] = csn;
515                                 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
516
517                         } else {
518                                 ber_bvreplace( &a->a_vals[0], &csn );
519                         }
520
521                         a = attr_find( e->e_attrs, slap_schema.si_ad_modifiersName );
522                         if ( a == NULL ) {
523                                 vals[0] = name;
524                                 nvals[0] = nname;
525                                 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
526
527                         } else {
528                                 ber_bvreplace( &a->a_vals[0], &name );
529                                 ber_bvreplace( &a->a_nvals[0], &nname );
530                         }
531
532                         a = attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp );
533                         if ( a == NULL ) {
534                                 vals[0] = timestamp;
535                                 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
536
537                         } else {
538                                 ber_bvreplace( &a->a_vals[0], &timestamp );
539                         }
540                 }
541
542                 if ( mod_err ) break;
543
544                 /* check schema, objectClass etc */
545
546                 if ( !dryrun ) {
547                         switch ( lr.lr_op ) {
548                         case LDAP_REQ_ADD:
549                                 id = be->be_entry_put( be, e, &bvtext );
550                                 break;
551
552                         case LDAP_REQ_MODIFY:
553                                 id = be->be_entry_modify( be, e, &bvtext );
554                                 break;
555
556                         }
557
558                         if( id == NOID ) {
559                                 fprintf( stderr, "%s: could not %s entry dn=\"%s\" "
560                                         "(line=%d): %s\n", progname, request, e->e_dn,
561                                         lineno, bvtext.bv_val );
562                                 rc = EXIT_FAILURE;
563                                 entry_free( e );
564                                 if( continuemode ) continue;
565                                 break;
566                         }
567
568                         if ( verbose )
569                                 fprintf( stderr, "%s: \"%s\" (%08lx)\n",
570                                         request, e->e_dn, (long) id );
571                 } else {
572                         if ( verbose )
573                                 fprintf( stderr, "%s: \"%s\"\n",
574                                         request, e->e_dn );
575                 }
576
577                 entry_free( e );
578         }
579
580 done:;
581         if ( ldifrc < 0 )
582                 rc = EXIT_FAILURE;
583
584         bvtext.bv_len = textlen;
585         bvtext.bv_val = textbuf;
586         bvtext.bv_val[0] = '\0';
587
588         if ( enable_meter ) {
589                 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
590                 lutil_meter_close( &meter );
591         }
592
593         if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) {
594                 struct berval ctxdn;
595                 if ( SLAP_SYNC_SUBENTRY( be )) {
596                         build_new_dn( &ctxdn, &be->be_nsuffix[0],
597                                 (struct berval *)&slap_ldapsync_cn_bv, NULL );
598                 } else {
599                         ctxdn = be->be_nsuffix[0];
600                 }
601                 ctxcsn_id = be->be_dn2id_get( be, &ctxdn );
602                 if ( ctxcsn_id == NOID ) {
603                         if ( SLAP_SYNC_SUBENTRY( be )) {
604                                 ctxcsn_e = slap_create_context_csn_entry( be, NULL );
605                                 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
606                                         if ( maxcsn[ sid ].bv_len ) {
607                                                 attr_merge_one( ctxcsn_e, slap_schema.si_ad_contextCSN,
608                                                         &maxcsn[ sid ], NULL );
609                                         }
610                                 }
611                                 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
612                                 if ( ctxcsn_id == NOID ) {
613                                         fprintf( stderr, "%s: couldn't create context entry\n", progname );
614                                         rc = EXIT_FAILURE;
615                                 }
616                         } else {
617                                 fprintf( stderr, "%s: context entry is missing\n", progname );
618                                 rc = EXIT_FAILURE;
619                         }
620                 } else {
621                         ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
622                         if ( ctxcsn_e != NULL ) {
623                                 Entry *e = entry_dup( ctxcsn_e );
624                                 int change;
625                                 Attribute *attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
626                                 if ( attr ) {
627                                         int             i;
628
629                                         change = 0;
630
631                                         for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) {
632                                                 int rc_sid;
633
634                                                 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] );
635                                                 if ( rc_sid < 0 ) {
636                                                         Debug( LDAP_DEBUG_ANY,
637                                                                 "%s: unable to extract SID "
638                                                                 "from #%d contextCSN=%s\n",
639                                                                 progname, i,
640                                                                 attr->a_nvals[ i ].bv_val );
641                                                         continue;
642                                                 }
643
644                                                 assert( rc_sid <= SLAP_SYNC_SID_MAX );
645
646                                                 sid = (unsigned)rc_sid;
647
648                                                 if ( maxcsn[ sid ].bv_len == 0 ) {
649                                                         match = -1;
650
651                                                 } else {
652                                                         value_match( &match, slap_schema.si_ad_entryCSN,
653                                                                 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
654                                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
655                                                                 &maxcsn[ sid ], &attr->a_nvals[i], &text );
656                                                 }
657
658                                                 if ( match > 0 ) {
659                                                         change = 1;
660                                                 } else {
661                                                         AC_MEMCPY( maxcsn[ sid ].bv_val,
662                                                                 attr->a_nvals[ i ].bv_val,
663                                                                 attr->a_nvals[ i ].bv_len );
664                                                         maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0';
665                                                         maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len;
666                                                 }
667                                         }
668
669                                         if ( change ) {
670                                                 if ( attr->a_nvals != attr->a_vals ) {
671                                                         ber_bvarray_free( attr->a_nvals );
672                                                 }
673                                                 attr->a_nvals = NULL;
674                                                 ber_bvarray_free( attr->a_vals );
675                                                 attr->a_vals = NULL;
676                                                 attr->a_numvals = 0;
677                                         }
678                                 } else {
679                                         change = 1;
680                                 }
681
682                                 if ( change ) {
683                                         for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
684                                                 if ( maxcsn[ sid ].bv_len ) {
685                                                         attr_merge_one( e, slap_schema.si_ad_contextCSN,
686                                                                 &maxcsn[ sid], NULL );
687                                                 }
688                                         }
689
690                                         ctxcsn_id = be->be_entry_modify( be, e, &bvtext );
691                                         if( ctxcsn_id == NOID ) {
692                                                 fprintf( stderr, "%s: could not modify ctxcsn\n",
693                                                         progname);
694                                                 rc = EXIT_FAILURE;
695                                         } else if ( verbose ) {
696                                                 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
697                                                         e->e_dn, (long) ctxcsn_id );
698                                         }
699                                 }
700                                 entry_free( e );
701                         }
702                 } 
703         }
704
705         ch_free( buf );
706
707         if ( !dryrun ) {
708                 if ( enable_meter ) {
709                         fprintf( stderr, "Closing DB..." );
710                 }
711                 if( be->be_entry_close( be ) ) {
712                         rc = EXIT_FAILURE;
713                 }
714
715                 if( be->be_sync ) {
716                         be->be_sync( be );
717                 }
718                 if ( enable_meter ) {
719                         fprintf( stderr, "\n" );
720                 }
721         }
722
723         if ( slap_tool_destroy())
724                 rc = EXIT_FAILURE;
725
726         return rc;
727 }
728