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;
79 if ( !be->be_entry_open ||
80 !be->be_entry_close ||
84 !be->be_entry_modify )
86 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
89 fprintf( stderr, "\t(dry) continuing...\n" );
96 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
101 /* enforce schema checking unless not disabled */
102 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
103 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
106 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
107 fprintf( stderr, "%s: could not open database.\n",
109 exit( EXIT_FAILURE );
112 (void)slap_tool_update_ctxcsn_init();
116 /* tools default to "none" */
117 && slap_debug == LDAP_DEBUG_NONE
119 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
120 && S_ISREG(stat_buf.st_mode) ) {
121 enable_meter = !lutil_meter_open(
123 &lutil_meter_text_display,
124 &lutil_meter_linear_estimator,
130 /* nextline is the line number of the end of the current entry */
131 for( lineno=1; ( ldifrc = ldif_read_record( ldiffp, &nextline, &buf, &lmax )) > 0;
143 char *request = "(unknown)";
145 ber_str2bv( buf, 0, 0, &rbuf );
147 if ( lineno < jumpline )
151 lutil_meter_update( &meter,
156 * Initialize text buffer
158 bvtext.bv_len = textlen;
159 bvtext.bv_val = textbuf;
160 bvtext.bv_val[0] = '\0';
162 local_rc = ldap_parse_ldif_record( &rbuf, lineno, &lr,
163 "slapmodify", LDIF_NO_CONTROLS );
165 if ( local_rc != LDAP_SUCCESS ) {
166 fprintf( stderr, "%s: could not parse entry (line=%lu)\n",
169 if( continuemode ) continue;
173 switch ( lr.lr_op ) {
178 case LDAP_REQ_MODIFY:
182 case LDAP_REQ_DELETE:
183 if ( be->be_entry_delete )
188 /* backend does not support delete, fallthrough */
190 case LDAP_REQ_MODRDN:
191 fprintf( stderr, "%s: request 0x%lx not supported (line=%lu)\n",
192 progname, (unsigned long)lr.lr_op, lineno );
194 if( continuemode ) continue;
198 /* record skipped e.g. version: or comment or something we don't handle yet */
202 local_rc = dnNormalize( 0, NULL, NULL, &lr.lr_dn, &ndn, NULL );
203 if ( local_rc != LDAP_SUCCESS ) {
204 fprintf( stderr, "%s: DN=\"%s\" normalization failed (line=%lu)\n",
205 progname, lr.lr_dn.bv_val, lineno );
207 if( continuemode ) continue;
211 /* make sure the DN is not empty */
212 if( BER_BVISEMPTY( &ndn ) &&
213 !BER_BVISEMPTY( be->be_nsuffix ))
215 fprintf( stderr, "%s: line %lu: "
216 "%s entry with empty dn=\"\"",
217 progname, lineno, request );
218 bd = select_backend( &ndn, nosubordinates );
222 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
223 if ( bdtmp == bd ) break;
227 assert( bdtmp != NULL );
229 fprintf( stderr, "; did you mean to use database #%d (%s)?",
231 bd->be_suffix[0].bv_val );
234 fprintf( stderr, "\n" );
236 SLAP_FREE( ndn.bv_val );
237 ldap_ldif_record_done( &lr );
238 if( continuemode ) continue;
243 bd = select_backend( &ndn, nosubordinates );
245 fprintf( stderr, "%s: line %lu: "
246 "database #%d (%s) not configured to hold \"%s\"",
249 be->be_suffix[0].bv_val,
254 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
255 if ( bdtmp == bd ) break;
259 assert( bdtmp != NULL );
261 fprintf( stderr, "; did you mean to use database #%d (%s)?",
263 bd->be_suffix[0].bv_val );
266 fprintf( stderr, "; no database configured for that naming context" );
268 fprintf( stderr, "\n" );
270 SLAP_FREE( ndn.bv_val );
271 ldap_ldif_record_done( &lr );
272 if( continuemode ) continue;
277 id = be->be_dn2id_get( be, &ndn );
278 e = be->be_entry_get( be, id );
280 Entry *e_tmp = entry_dup( e );
281 /* FIXME: release? */
285 if ( lr.lrop_mods ) {
286 for ( n = 0; lr.lrop_mods && lr.lrop_mods[ n ] != NULL; n++ ) {
287 LDAPMod *mod = lr.lrop_mods[ n ];
288 Modification mods = { 0 };
290 int bin = (mod->mod_op & LDAP_MOD_BVALUES);
294 local_rc = slap_str2ad( mod->mod_type, &mods.sm_desc, &text );
295 if ( local_rc != LDAP_SUCCESS ) {
296 fprintf( stderr, "%s: slap_str2ad(\"%s\") failed for entry \"%s\" (%d: %s, lineno=%lu)\n",
297 progname, mod->mod_type, lr.lr_dn.bv_val, local_rc, text, lineno );
300 if( continuemode ) continue;
301 SLAP_FREE( ndn.bv_val );
302 ldap_ldif_record_done( &lr );
307 mods.sm_type = mods.sm_desc->ad_cname;
309 if ( mods.sm_desc->ad_type->sat_syntax->ssyn_pretty ) {
313 assert( mods.sm_desc->ad_type->sat_syntax->ssyn_validate != NULL );
316 if ( mods.sm_desc->ad_type->sat_equality &&
317 mods.sm_desc->ad_type->sat_equality->smr_normalize )
322 if ( bin && mod->mod_bvalues ) {
323 for ( i = 0; mod->mod_bvalues[ i ] != NULL; i++ )
326 } else if ( !bin && mod->mod_values ) {
327 for ( i = 0; mod->mod_values[ i ] != NULL; i++ )
333 mods.sm_values = SLAP_CALLOC( sizeof( struct berval ), i + 1 );
335 mods.sm_nvalues = SLAP_CALLOC( sizeof( struct berval ), i + 1 );
337 mods.sm_nvalues = NULL;
342 for ( i = 0; i < mods.sm_numvals; i++ ) {
346 bv = *mod->mod_bvalues[ i ];
348 ber_str2bv( mod->mod_values[ i ], 0, 0, &bv );
352 local_rc = ordered_value_pretty( mods.sm_desc,
353 &bv, &mods.sm_values[i], NULL );
356 local_rc = ordered_value_validate( mods.sm_desc,
360 if ( local_rc != LDAP_SUCCESS ) {
361 fprintf( stderr, "%s: DN=\"%s\": unable to %s attr=%s value #%d\n",
362 progname, e->e_dn, pretty ? "prettify" : "validate",
363 mods.sm_desc->ad_cname.bv_val, i );
367 ber_bvarray_free( mods.sm_values );
368 ber_bvarray_free( mods.sm_nvalues );
369 if( continuemode ) continue;
370 SLAP_FREE( ndn.bv_val );
371 ldap_ldif_record_done( &lr );
377 ber_dupbv( &mods.sm_values[i], &bv );
381 local_rc = ordered_value_normalize(
382 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
384 mods.sm_desc->ad_type->sat_equality,
385 &mods.sm_values[i], &mods.sm_nvalues[i],
387 if ( local_rc != LDAP_SUCCESS ) {
388 fprintf( stderr, "%s: DN=\"%s\": unable to normalize attr=%s value #%d\n",
389 progname, e->e_dn, mods.sm_desc->ad_cname.bv_val, i );
393 ber_bvarray_free( mods.sm_values );
394 ber_bvarray_free( mods.sm_nvalues );
395 if( continuemode ) continue;
396 SLAP_FREE( ndn.bv_val );
397 ldap_ldif_record_done( &lr );
404 mods.sm_op = (mod->mod_op & ~LDAP_MOD_BVALUES);
407 if ( mods.sm_desc == slap_schema.si_ad_objectClass ) {
411 switch ( mods.sm_op ) {
413 local_rc = modify_add_values( e, &mods,
414 0, &text, textbuf, textlen );
417 case LDAP_MOD_DELETE:
418 local_rc = modify_delete_values( e, &mods,
419 0, &text, textbuf, textlen );
422 case LDAP_MOD_REPLACE:
423 local_rc = modify_replace_values( e, &mods,
424 0, &text, textbuf, textlen );
427 case LDAP_MOD_INCREMENT:
428 local_rc = modify_increment_values( e, &mods,
429 0, &text, textbuf, textlen );
433 if ( local_rc != LDAP_SUCCESS ) {
434 fprintf( stderr, "%s: DN=\"%s\": unable to modify attr=%s\n",
435 progname, e->e_dn, mods.sm_desc->ad_cname.bv_val );
437 ber_bvarray_free( mods.sm_values );
438 ber_bvarray_free( mods.sm_nvalues );
439 if( continuemode ) continue;
440 SLAP_FREE( ndn.bv_val );
441 ldap_ldif_record_done( &lr );
447 rc = slap_tool_entry_check( progname, op, e, lineno, &text, textbuf, textlen );
448 if ( rc != LDAP_SUCCESS ) {
450 SLAP_FREE( ndn.bv_val );
451 ldap_ldif_record_done( &lr );
452 if( continuemode ) continue;
458 if ( SLAP_LASTMOD(be) ) {
459 time_t now = slap_get_time();
460 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
461 struct berval vals[ 2 ];
463 struct berval name, timestamp;
465 struct berval nvals[ 2 ];
467 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
472 vals[1].bv_val = NULL;
475 nvals[1].bv_val = NULL;
477 csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
480 timestamp.bv_val = timebuf;
481 timestamp.bv_len = sizeof(timebuf);
483 slap_timestamp( &now, ×tamp );
485 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
486 BER_BVSTR( &name, SLAPD_ANONYMOUS );
489 name = be->be_rootdn;
490 nname = be->be_rootndn;
493 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
495 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
496 vals[0].bv_val = uuidbuf;
497 if ( a->a_vals != a->a_nvals ) {
498 SLAP_FREE( a->a_nvals[0].bv_val );
499 SLAP_FREE( a->a_nvals );
501 SLAP_FREE( a->a_vals[0].bv_val );
502 SLAP_FREE( a->a_vals );
507 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
509 a = attr_find( e->e_attrs, slap_schema.si_ad_creatorsName );
513 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
516 ber_bvreplace( &a->a_vals[0], &name );
517 ber_bvreplace( &a->a_nvals[0], &nname );
520 a = attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp );
523 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
526 ber_bvreplace( &a->a_vals[0], ×tamp );
529 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
532 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
535 ber_bvreplace( &a->a_vals[0], &csn );
538 a = attr_find( e->e_attrs, slap_schema.si_ad_modifiersName );
542 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
545 ber_bvreplace( &a->a_vals[0], &name );
546 ber_bvreplace( &a->a_nvals[0], &nname );
549 a = attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp );
552 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
555 ber_bvreplace( &a->a_vals[0], ×tamp );
559 if ( mod_err ) break;
561 /* check schema, objectClass etc */
564 switch ( lr.lr_op ) {
566 id = be->be_entry_put( be, e, &bvtext );
570 case LDAP_REQ_MODIFY:
571 id = be->be_entry_modify( be, e, &bvtext );
575 case LDAP_REQ_DELETE:
576 rc = be->be_entry_delete( be, id, &bvtext );
581 if( rc != LDAP_SUCCESS ) {
582 fprintf( stderr, "%s: could not %s entry dn=\"%s\" "
583 "(line=%lu): %s\n", progname, request, e->e_dn,
584 lineno, bvtext.bv_val );
587 if( continuemode ) continue;
591 sid = slap_tool_update_ctxcsn_check( progname, e );
594 fprintf( stderr, "%s: \"%s\" (%08lx)\n",
595 request, e->e_dn, (long) id );
598 fprintf( stderr, "%s: \"%s\"\n",
609 bvtext.bv_len = textlen;
610 bvtext.bv_val = textbuf;
611 bvtext.bv_val[0] = '\0';
613 if ( enable_meter ) {
614 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
615 lutil_meter_close( &meter );
618 if ( rc == EXIT_SUCCESS ) {
619 rc = slap_tool_update_ctxcsn( progname, sid, &bvtext );
625 if ( enable_meter ) {
626 fprintf( stderr, "Closing DB..." );
628 if( be->be_entry_close( be ) ) {
635 if ( enable_meter ) {
636 fprintf( stderr, "\n" );
640 if ( slap_tool_destroy())