2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2014 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 ];
44 slapmodify( int argc, char **argv )
48 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
49 size_t textlen = sizeof textbuf;
50 const char *progname = "slapmodify";
56 OperationBuffer opbuf;
59 int checkvals, ldifrc;
60 unsigned long lineno, nextline;
62 int rc = EXIT_SUCCESS;
71 if ( isatty (2) ) enable_meter = 1;
72 slap_tool_init( progname, SLAPMODIFY, argc, argv );
74 memset( &opbuf, 0, sizeof(opbuf) );
76 op->o_hdr = &opbuf.ob_hdr;
78 if ( !be->be_entry_open ||
79 !be->be_entry_close ||
83 !be->be_entry_modify )
85 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
88 fprintf( stderr, "\t(dry) continuing...\n" );
95 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
100 /* enforce schema checking unless not disabled */
101 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
102 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
105 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
106 fprintf( stderr, "%s: could not open database.\n",
108 exit( EXIT_FAILURE );
111 (void)slap_tool_update_ctxcsn_init();
115 /* tools default to "none" */
116 && slap_debug == LDAP_DEBUG_NONE
118 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
119 && S_ISREG(stat_buf.st_mode) ) {
120 enable_meter = !lutil_meter_open(
122 &lutil_meter_text_display,
123 &lutil_meter_linear_estimator,
129 /* nextline is the line number of the end of the current entry */
130 for( lineno=1; ( ldifrc = ldif_read_record( ldiffp, &nextline, &buf, &lmax )) > 0;
142 char *request = "(unknown)";
144 ber_str2bv( buf, 0, 0, &rbuf );
146 if ( lineno < jumpline )
150 lutil_meter_update( &meter,
155 * Initialize text buffer
157 bvtext.bv_len = textlen;
158 bvtext.bv_val = textbuf;
159 bvtext.bv_val[0] = '\0';
161 local_rc = ldap_parse_ldif_record( &rbuf, lineno, &lr,
162 "slapmodify", LDIF_NO_CONTROLS );
164 if ( local_rc != LDAP_SUCCESS ) {
165 fprintf( stderr, "%s: could not parse entry (line=%lu)\n",
168 if( continuemode ) continue;
172 switch ( lr.lr_op ) {
177 case LDAP_REQ_MODIFY:
181 case LDAP_REQ_DELETE:
182 if ( be->be_entry_delete )
187 /* backend does not support delete, fallthrough */
189 case LDAP_REQ_MODRDN:
190 fprintf( stderr, "%s: request 0x%lx not supported (line=%lu)\n",
191 progname, (unsigned long)lr.lr_op, lineno );
193 if( continuemode ) continue;
197 /* record skipped e.g. version: or comment or something we don't handle yet */
201 local_rc = dnNormalize( 0, NULL, NULL, &lr.lr_dn, &ndn, NULL );
202 if ( local_rc != LDAP_SUCCESS ) {
203 fprintf( stderr, "%s: DN=\"%s\" normalization failed (line=%lu)\n",
204 progname, lr.lr_dn.bv_val, lineno );
206 if( continuemode ) continue;
210 /* make sure the DN is not empty */
211 if( BER_BVISEMPTY( &ndn ) &&
212 !BER_BVISEMPTY( be->be_nsuffix ))
214 fprintf( stderr, "%s: line %lu: "
215 "%s entry with empty dn=\"\"",
216 progname, lineno, request );
217 bd = select_backend( &ndn, nosubordinates );
221 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
222 if ( bdtmp == bd ) break;
226 assert( bdtmp != NULL );
228 fprintf( stderr, "; did you mean to use database #%d (%s)?",
230 bd->be_suffix[0].bv_val );
233 fprintf( stderr, "\n" );
235 SLAP_FREE( ndn.bv_val );
236 ldap_ldif_record_done( &lr );
237 if( continuemode ) continue;
242 bd = select_backend( &ndn, nosubordinates );
244 fprintf( stderr, "%s: line %lu: "
245 "database #%d (%s) not configured to hold \"%s\"",
248 be->be_suffix[0].bv_val,
253 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
254 if ( bdtmp == bd ) break;
258 assert( bdtmp != NULL );
260 fprintf( stderr, "; did you mean to use database #%d (%s)?",
262 bd->be_suffix[0].bv_val );
265 fprintf( stderr, "; no database configured for that naming context" );
267 fprintf( stderr, "\n" );
269 SLAP_FREE( ndn.bv_val );
270 ldap_ldif_record_done( &lr );
271 if( continuemode ) continue;
276 id = be->be_dn2id_get( be, &ndn );
277 e = be->be_entry_get( be, id );
279 Entry *e_tmp = entry_dup( e );
280 /* FIXME: release? */
284 for ( n = 0; lr.lrop_mods[ n ] != NULL; n++ ) {
285 LDAPMod *mod = lr.lrop_mods[ n ];
286 Modification mods = { 0 };
288 int bin = (mod->mod_op & LDAP_MOD_BVALUES);
292 local_rc = slap_str2ad( mod->mod_type, &mods.sm_desc, &text );
293 if ( local_rc != LDAP_SUCCESS ) {
294 fprintf( stderr, "%s: slap_str2ad(\"%s\") failed for entry \"%s\" (%d: %s, lineno=%lu)\n",
295 progname, mod->mod_type, lr.lr_dn.bv_val, local_rc, text, lineno );
298 if( continuemode ) continue;
299 SLAP_FREE( ndn.bv_val );
300 ldap_ldif_record_done( &lr );
305 mods.sm_type = mods.sm_desc->ad_cname;
307 if ( mods.sm_desc->ad_type->sat_syntax->ssyn_pretty ) {
311 assert( mods.sm_desc->ad_type->sat_syntax->ssyn_validate != NULL );
314 if ( mods.sm_desc->ad_type->sat_equality &&
315 mods.sm_desc->ad_type->sat_equality->smr_normalize )
320 if ( bin && mod->mod_bvalues ) {
321 for ( i = 0; mod->mod_bvalues[ i ] != NULL; i++ )
324 } else if ( !bin && mod->mod_values ) {
325 for ( i = 0; mod->mod_values[ i ] != NULL; i++ )
329 mods.sm_values = SLAP_CALLOC( sizeof( struct berval ), i + 1 );
331 mods.sm_nvalues = SLAP_CALLOC( sizeof( struct berval ), i + 1 );
333 mods.sm_nvalues = NULL;
337 for ( i = 0; i < mods.sm_numvals; i++ ) {
341 bv = *mod->mod_bvalues[ i ];
343 ber_str2bv( mod->mod_values[ i ], 0, 0, &bv );
347 local_rc = ordered_value_pretty( mods.sm_desc,
348 &bv, &mods.sm_values[i], NULL );
351 local_rc = ordered_value_validate( mods.sm_desc,
355 if ( local_rc != LDAP_SUCCESS ) {
356 fprintf( stderr, "%s: DN=\"%s\": unable to %s attr=%s value #%d\n",
357 progname, e->e_dn, pretty ? "prettify" : "validate",
358 mods.sm_desc->ad_cname.bv_val, i );
362 ber_bvarray_free( mods.sm_values );
363 ber_bvarray_free( mods.sm_nvalues );
364 if( continuemode ) continue;
365 SLAP_FREE( ndn.bv_val );
366 ldap_ldif_record_done( &lr );
372 ber_dupbv( &mods.sm_values[i], &bv );
376 local_rc = ordered_value_normalize(
377 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
379 mods.sm_desc->ad_type->sat_equality,
380 &mods.sm_values[i], &mods.sm_nvalues[i],
382 if ( local_rc != LDAP_SUCCESS ) {
383 fprintf( stderr, "%s: DN=\"%s\": unable to normalize attr=%s value #%d\n",
384 progname, e->e_dn, mods.sm_desc->ad_cname.bv_val, i );
388 ber_bvarray_free( mods.sm_values );
389 ber_bvarray_free( mods.sm_nvalues );
390 if( continuemode ) continue;
391 SLAP_FREE( ndn.bv_val );
392 ldap_ldif_record_done( &lr );
399 mods.sm_op = (mod->mod_op & ~LDAP_MOD_BVALUES);
402 if ( mods.sm_desc == slap_schema.si_ad_objectClass ) {
406 switch ( mods.sm_op ) {
408 local_rc = modify_add_values( e, &mods,
409 0, &text, textbuf, textlen );
412 case LDAP_MOD_DELETE:
413 local_rc = modify_delete_values( e, &mods,
414 0, &text, textbuf, textlen );
417 case LDAP_MOD_REPLACE:
418 local_rc = modify_replace_values( e, &mods,
419 0, &text, textbuf, textlen );
422 case LDAP_MOD_INCREMENT:
423 local_rc = modify_increment_values( e, &mods,
424 0, &text, textbuf, textlen );
428 if ( local_rc != LDAP_SUCCESS ) {
429 fprintf( stderr, "%s: DN=\"%s\": unable to modify attr=%s\n",
430 progname, e->e_dn, mods.sm_desc->ad_cname.bv_val );
432 ber_bvarray_free( mods.sm_values );
433 ber_bvarray_free( mods.sm_nvalues );
434 if( continuemode ) continue;
435 SLAP_FREE( ndn.bv_val );
436 ldap_ldif_record_done( &lr );
442 rc = slap_tool_entry_check( progname, op, e, lineno, &text, textbuf, textlen );
443 if ( rc != LDAP_SUCCESS ) {
445 SLAP_FREE( ndn.bv_val );
446 ldap_ldif_record_done( &lr );
447 if( continuemode ) continue;
452 if ( SLAP_LASTMOD(be) ) {
453 time_t now = slap_get_time();
454 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
455 struct berval vals[ 2 ];
457 struct berval name, timestamp;
459 struct berval nvals[ 2 ];
461 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
466 vals[1].bv_val = NULL;
469 nvals[1].bv_val = NULL;
471 csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
474 timestamp.bv_val = timebuf;
475 timestamp.bv_len = sizeof(timebuf);
477 slap_timestamp( &now, ×tamp );
479 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
480 BER_BVSTR( &name, SLAPD_ANONYMOUS );
483 name = be->be_rootdn;
484 nname = be->be_rootndn;
487 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
489 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
490 vals[0].bv_val = uuidbuf;
491 if ( a->a_vals != a->a_nvals ) {
492 SLAP_FREE( a->a_nvals[0].bv_val );
493 SLAP_FREE( a->a_nvals );
495 SLAP_FREE( a->a_vals[0].bv_val );
496 SLAP_FREE( a->a_vals );
501 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
503 a = attr_find( e->e_attrs, slap_schema.si_ad_creatorsName );
507 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
510 ber_bvreplace( &a->a_vals[0], &name );
511 ber_bvreplace( &a->a_nvals[0], &nname );
514 a = attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp );
517 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
520 ber_bvreplace( &a->a_vals[0], ×tamp );
523 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
526 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
529 ber_bvreplace( &a->a_vals[0], &csn );
532 a = attr_find( e->e_attrs, slap_schema.si_ad_modifiersName );
536 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
539 ber_bvreplace( &a->a_vals[0], &name );
540 ber_bvreplace( &a->a_nvals[0], &nname );
543 a = attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp );
546 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
549 ber_bvreplace( &a->a_vals[0], ×tamp );
553 if ( mod_err ) break;
555 /* check schema, objectClass etc */
558 switch ( lr.lr_op ) {
560 id = be->be_entry_put( be, e, &bvtext );
564 case LDAP_REQ_MODIFY:
565 id = be->be_entry_modify( be, e, &bvtext );
569 case LDAP_REQ_DELETE:
570 rc = be->be_entry_delete( be, id, &bvtext );
575 if( rc != LDAP_SUCCESS ) {
576 fprintf( stderr, "%s: could not %s entry dn=\"%s\" "
577 "(line=%lu): %s\n", progname, request, e->e_dn,
578 lineno, bvtext.bv_val );
581 if( continuemode ) continue;
585 sid = slap_tool_update_ctxcsn_check( progname, e );
588 fprintf( stderr, "%s: \"%s\" (%08lx)\n",
589 request, e->e_dn, (long) id );
592 fprintf( stderr, "%s: \"%s\"\n",
603 bvtext.bv_len = textlen;
604 bvtext.bv_val = textbuf;
605 bvtext.bv_val[0] = '\0';
607 if ( enable_meter ) {
608 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
609 lutil_meter_close( &meter );
612 if ( rc == EXIT_SUCCESS ) {
613 rc = slap_tool_update_ctxcsn( progname, sid, &bvtext );
619 if ( enable_meter ) {
620 fprintf( stderr, "Closing DB..." );
622 if( be->be_entry_close( be ) ) {
629 if ( enable_meter ) {
630 fprintf( stderr, "\n" );
634 if ( slap_tool_destroy())