2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2010 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 Pierangelo Masarati for inclusion
19 * in OpenLDAP Software.
26 #include "ac/stdlib.h"
29 #include "ac/string.h"
30 #include "ac/socket.h"
31 #include "ac/unistd.h"
36 #include "lutil_meter.h"
39 #include "slapcommon.h"
41 static char csnbuf[ LDAP_PVT_CSNSTR_BUFSIZE ];
42 static char maxcsnbuf[ LDAP_PVT_CSNSTR_BUFSIZE * ( SLAP_SYNC_SID_MAX + 1 ) ];
45 slapmodify( int argc, char **argv )
49 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
50 size_t textlen = sizeof textbuf;
51 const char *progname = "slapmodify";
54 struct berval maxcsn[ SLAP_SYNC_SID_MAX + 1 ];
59 OperationBuffer opbuf;
64 int lineno, nextline, ldifrc;
66 int rc = EXIT_SUCCESS;
76 if ( isatty (2) ) enable_meter = 1;
77 slap_tool_init( progname, SLAPMODIFY, argc, argv );
79 memset( &opbuf, 0, sizeof(opbuf) );
81 op->o_hdr = &opbuf.ob_hdr;
83 if ( !be->be_entry_open ||
84 !be->be_entry_close ||
88 !be->be_entry_modify )
90 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
93 fprintf( stderr, "\t(dry) continuing...\n" );
100 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
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);
110 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
111 fprintf( stderr, "%s: could not open database.\n",
113 exit( EXIT_FAILURE );
118 /* tools default to "none" */
119 && slap_debug == LDAP_DEBUG_NONE
121 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
122 && S_ISREG(stat_buf.st_mode) ) {
123 enable_meter = !lutil_meter_open(
125 &lutil_meter_text_display,
126 &lutil_meter_linear_estimator,
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;
145 char *request = "(unknown)";
147 ber_str2bv( buf, 0, 0, &rbuf );
149 if ( lineno < jumpline )
153 lutil_meter_update( &meter,
158 * Initialize text buffer
160 bvtext.bv_len = textlen;
161 bvtext.bv_val = textbuf;
162 bvtext.bv_val[0] = '\0';
164 local_rc = ldap_parse_ldif_record( &rbuf, lineno, &lr,
165 "slapmodify", LDIF_NO_CONTROLS );
167 if ( local_rc != LDAP_SUCCESS ) {
168 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
171 if( continuemode ) continue;
175 switch ( lr.lr_op ) {
180 case LDAP_REQ_MODIFY:
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 );
189 if( continuemode ) continue;
193 fprintf( stderr, "%s: unknown request 0x%lx (line=%d)\n",
194 progname, (unsigned long)lr.lr_op, lineno );
196 if( continuemode ) continue;
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 );
205 if( continuemode ) continue;
209 /* make sure the DN is not empty */
210 if( BER_BVISEMPTY( &ndn ) &&
211 !BER_BVISEMPTY( be->be_nsuffix ))
213 fprintf( stderr, "%s: line %d: "
214 "%s entry with empty dn=\"\"",
215 progname, lineno, request );
216 bd = select_backend( &ndn, nosubordinates );
220 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
221 if ( bdtmp == bd ) break;
225 assert( bdtmp != NULL );
227 fprintf( stderr, "; did you mean to use database #%d (%s)?",
229 bd->be_suffix[0].bv_val );
232 fprintf( stderr, "\n" );
234 SLAP_FREE( ndn.bv_val );
235 ldap_ldif_record_done( &lr );
236 if( continuemode ) continue;
241 bd = select_backend( &ndn, nosubordinates );
243 fprintf( stderr, "%s: line %d: "
244 "database #%d (%s) not configured to hold \"%s\"",
247 be->be_suffix[0].bv_val,
252 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
253 if ( bdtmp == bd ) break;
257 assert( bdtmp != NULL );
259 fprintf( stderr, "; did you mean to use database #%d (%s)?",
261 bd->be_suffix[0].bv_val );
264 fprintf( stderr, "; no database configured for that naming context" );
266 fprintf( stderr, "\n" );
268 SLAP_FREE( ndn.bv_val );
269 ldap_ldif_record_done( &lr );
270 if( continuemode ) continue;
275 id = be->be_dn2id_get( be, &ndn );
276 e = be->be_entry_get( be, id );
278 Entry *e_tmp = entry_dup( e );
279 /* FIXME: release? */
283 for ( n = 0; lr.lrop_mods[ n ] != NULL; n++ ) {
284 LDAPMod *mod = lr.lrop_mods[ n ];
285 Modification mods = { 0 };
287 int bin = (mod->mod_op & LDAP_MOD_BVALUES);
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 );
297 if( continuemode ) continue;
298 SLAP_FREE( ndn.bv_val );
299 ldap_ldif_record_done( &lr );
304 mods.sm_type = mods.sm_desc->ad_cname;
306 if ( mods.sm_desc->ad_type->sat_syntax->ssyn_pretty ) {
310 assert( mods.sm_desc->ad_type->sat_syntax->ssyn_validate != NULL );
313 if ( mods.sm_desc->ad_type->sat_equality &&
314 mods.sm_desc->ad_type->sat_equality->smr_normalize )
319 if ( bin && mod->mod_bvalues ) {
320 for ( i = 0; mod->mod_bvalues[ i ] != NULL; i++ )
323 } else if ( !bin && mod->mod_values ) {
324 for ( i = 0; mod->mod_values[ i ] != NULL; i++ )
328 mods.sm_values = SLAP_CALLOC( sizeof( struct berval ), i + 1 );
330 mods.sm_nvalues = SLAP_CALLOC( sizeof( struct berval ), i + 1 );
332 mods.sm_nvalues = NULL;
336 for ( i = 0; i < mods.sm_numvals; i++ ) {
340 bv = *mod->mod_bvalues[ i ];
342 ber_str2bv( mod->mod_values[ i ], 0, 0, &bv );
346 local_rc = ordered_value_pretty( mods.sm_desc,
347 &bv, &mods.sm_values[i], NULL );
350 local_rc = ordered_value_validate( mods.sm_desc,
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 );
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 );
371 ber_dupbv( &mods.sm_values[i], &bv );
375 local_rc = ordered_value_normalize(
376 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
378 mods.sm_desc->ad_type->sat_equality,
379 &mods.sm_values[i], &mods.sm_nvalues[i],
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 );
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 );
398 mods.sm_op = (mod->mod_op & ~LDAP_MOD_BVALUES);
401 if ( mods.sm_desc == slap_schema.si_ad_objectClass ) {
405 switch ( mods.sm_op ) {
407 local_rc = modify_add_values( e, &mods,
408 0, &text, textbuf, textlen );
411 case LDAP_MOD_DELETE:
412 local_rc = modify_delete_values( e, &mods,
413 0, &text, textbuf, textlen );
416 case LDAP_MOD_REPLACE:
417 local_rc = modify_replace_values( e, &mods,
418 0, &text, textbuf, textlen );
421 case LDAP_MOD_INCREMENT:
422 local_rc = modify_increment_values( e, &mods,
423 0, &text, textbuf, textlen );
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 );
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 );
441 if ( SLAP_LASTMOD(be) ) {
442 time_t now = slap_get_time();
443 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
444 struct berval vals[ 2 ];
446 struct berval name, timestamp;
448 struct berval nvals[ 2 ];
450 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
455 vals[1].bv_val = NULL;
458 nvals[1].bv_val = NULL;
460 csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
463 timestamp.bv_val = timebuf;
464 timestamp.bv_len = sizeof(timebuf);
466 slap_timestamp( &now, ×tamp );
468 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
469 BER_BVSTR( &name, SLAPD_ANONYMOUS );
472 name = be->be_rootdn;
473 nname = be->be_rootndn;
476 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
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 );
484 SLAP_FREE( a->a_vals[0].bv_val );
485 SLAP_FREE( a->a_vals );
490 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
492 a = attr_find( e->e_attrs, slap_schema.si_ad_creatorsName );
496 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
499 ber_bvreplace( &a->a_vals[0], &name );
500 ber_bvreplace( &a->a_nvals[0], &nname );
503 a = attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp );
506 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
509 ber_bvreplace( &a->a_vals[0], ×tamp );
512 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
515 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
518 ber_bvreplace( &a->a_vals[0], &csn );
521 a = attr_find( e->e_attrs, slap_schema.si_ad_modifiersName );
525 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
528 ber_bvreplace( &a->a_vals[0], &name );
529 ber_bvreplace( &a->a_nvals[0], &nname );
532 a = attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp );
535 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
538 ber_bvreplace( &a->a_vals[0], ×tamp );
542 if ( mod_err ) break;
544 /* check schema, objectClass etc */
547 switch ( lr.lr_op ) {
549 id = be->be_entry_put( be, e, &bvtext );
552 case LDAP_REQ_MODIFY:
553 id = be->be_entry_modify( be, e, &bvtext );
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 );
564 if( continuemode ) continue;
569 fprintf( stderr, "%s: \"%s\" (%08lx)\n",
570 request, e->e_dn, (long) id );
573 fprintf( stderr, "%s: \"%s\"\n",
584 bvtext.bv_len = textlen;
585 bvtext.bv_val = textbuf;
586 bvtext.bv_val[0] = '\0';
588 if ( enable_meter ) {
589 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
590 lutil_meter_close( &meter );
593 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) {
595 if ( SLAP_SYNC_SUBENTRY( be )) {
596 build_new_dn( &ctxdn, &be->be_nsuffix[0],
597 (struct berval *)&slap_ldapsync_cn_bv, NULL );
599 ctxdn = be->be_nsuffix[0];
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 );
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 );
617 fprintf( stderr, "%s: context entry is missing\n", progname );
621 ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
622 if ( ctxcsn_e != NULL ) {
623 Entry *e = entry_dup( ctxcsn_e );
625 Attribute *attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
631 for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) {
634 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] );
636 Debug( LDAP_DEBUG_ANY,
637 "%s: unable to extract SID "
638 "from #%d contextCSN=%s\n",
640 attr->a_nvals[ i ].bv_val );
644 assert( rc_sid <= SLAP_SYNC_SID_MAX );
646 sid = (unsigned)rc_sid;
648 if ( maxcsn[ sid ].bv_len == 0 ) {
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 );
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;
670 if ( attr->a_nvals != attr->a_vals ) {
671 ber_bvarray_free( attr->a_nvals );
673 attr->a_nvals = NULL;
674 ber_bvarray_free( attr->a_vals );
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 );
690 ctxcsn_id = be->be_entry_modify( be, e, &bvtext );
691 if( ctxcsn_id == NOID ) {
692 fprintf( stderr, "%s: could not modify ctxcsn\n",
695 } else if ( verbose ) {
696 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
697 e->e_dn, (long) ctxcsn_id );
708 if ( enable_meter ) {
709 fprintf( stderr, "Closing DB..." );
711 if( be->be_entry_close( be ) ) {
718 if ( enable_meter ) {
719 fprintf( stderr, "\n" );
723 if ( slap_tool_destroy())