2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2015 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 extern int slap_DN_strict; /* dn.c */
43 static char csnbuf[ LDAP_PVT_CSNSTR_BUFSIZE ];
46 slapmodify( int argc, char **argv )
50 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
51 size_t textlen = sizeof textbuf;
52 const char *progname = "slapmodify";
58 OperationBuffer opbuf;
61 int checkvals, ldifrc;
62 unsigned long lineno, nextline;
64 int rc = EXIT_SUCCESS;
73 if ( isatty (2) ) enable_meter = 1;
74 slap_tool_init( progname, SLAPMODIFY, argc, argv );
76 memset( &opbuf, 0, sizeof(opbuf) );
78 op->o_hdr = &opbuf.ob_hdr;
81 if ( !be->be_entry_open ||
82 !be->be_entry_close ||
86 !be->be_entry_modify )
88 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
91 fprintf( stderr, "\t(dry) continuing...\n" );
98 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
103 /* enforce schema checking unless not disabled and allow unknown
104 * attributes otherwise */
105 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
106 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
111 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
112 fprintf( stderr, "%s: could not open database.\n",
114 exit( EXIT_FAILURE );
117 (void)slap_tool_update_ctxcsn_init();
121 /* tools default to "none" */
122 && slap_debug == LDAP_DEBUG_NONE
124 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
125 && S_ISREG(stat_buf.st_mode) ) {
126 enable_meter = !lutil_meter_open(
128 &lutil_meter_text_display,
129 &lutil_meter_linear_estimator,
135 /* nextline is the line number of the end of the current entry */
136 for( lineno=1; ( ldifrc = ldif_read_record( ldiffp, &nextline, &buf, &lmax )) > 0;
140 Entry *e_orig = NULL, *e = NULL;
143 struct berval ndn = BER_BVNULL;
148 char *request = "(unknown)";
150 ber_str2bv( buf, 0, 0, &rbuf );
152 if ( lineno < jumpline )
156 lutil_meter_update( &meter,
161 * Initialize text buffer
163 bvtext.bv_len = textlen;
164 bvtext.bv_val = textbuf;
165 bvtext.bv_val[0] = '\0';
167 local_rc = ldap_parse_ldif_record( &rbuf, lineno, &lr,
168 "slapmodify", LDIF_NO_CONTROLS );
170 if ( local_rc != LDAP_SUCCESS ) {
171 fprintf( stderr, "%s: could not parse entry (line=%lu)\n",
174 if( continuemode ) continue;
178 switch ( lr.lr_op ) {
183 case LDAP_REQ_MODIFY:
187 case LDAP_REQ_DELETE:
188 if ( be->be_entry_delete )
193 /* backend does not support delete, fallthrough */
195 case LDAP_REQ_MODRDN:
196 fprintf( stderr, "%s: request 0x%lx not supported (line=%lu)\n",
197 progname, (unsigned long)lr.lr_op, lineno );
202 /* record skipped e.g. version: or comment or something we don't handle yet */
206 local_rc = dnNormalize( 0, NULL, NULL, &lr.lr_dn, &ndn, NULL );
207 if ( local_rc != LDAP_SUCCESS ) {
208 fprintf( stderr, "%s: DN=\"%s\" normalization failed (line=%lu)\n",
209 progname, lr.lr_dn.bv_val, lineno );
214 /* make sure the DN is not empty */
215 if( BER_BVISEMPTY( &ndn ) &&
216 !BER_BVISEMPTY( be->be_nsuffix ))
218 fprintf( stderr, "%s: line %lu: "
219 "%s entry with empty dn=\"\"",
220 progname, lineno, request );
221 bd = select_backend( &ndn, nosubordinates );
225 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
226 if ( bdtmp == bd ) break;
230 assert( bdtmp != NULL );
232 fprintf( stderr, "; did you mean to use database #%d (%s)?",
234 bd->be_suffix[0].bv_val );
237 fprintf( stderr, "\n" );
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" );
273 /* get id and/or entry */
274 switch ( lr.lr_op ) {
277 ber_dupbv( &e->e_name, &lr.lr_dn );
278 ber_dupbv( &e->e_nname, &ndn );
281 //case LDAP_REQ_MODRDN:
282 case LDAP_REQ_DELETE:
283 case LDAP_REQ_MODIFY:
284 id = be->be_dn2id_get( be, &ndn );
286 if ( rc == LDAP_SUCCESS && lr.lr_op != LDAP_REQ_DELETE ) {
287 e_orig = be->be_entry_get( be, id );
288 e = entry_dup( e_orig );
293 if ( rc != LDAP_SUCCESS ) {
294 fprintf( stderr, "%s: no such entry \"%s\" in database (lineno=%lu)\n",
295 progname, ndn.bv_val, lineno );
300 if ( lr.lrop_mods ) {
301 for ( n = 0; lr.lrop_mods && lr.lrop_mods[ n ] != NULL; n++ ) {
302 LDAPMod *mod = lr.lrop_mods[ n ];
303 Modification mods = { 0 };
305 int bin = (mod->mod_op & LDAP_MOD_BVALUES);
309 local_rc = slap_str2ad( mod->mod_type, &mods.sm_desc, &text );
310 if ( local_rc != LDAP_SUCCESS ) {
311 fprintf( stderr, "%s: slap_str2ad(\"%s\") failed for entry \"%s\" (%d: %s, lineno=%lu)\n",
312 progname, mod->mod_type, lr.lr_dn.bv_val, local_rc, text, lineno );
317 mods.sm_type = mods.sm_desc->ad_cname;
319 if ( mods.sm_desc->ad_type->sat_syntax->ssyn_pretty ) {
323 assert( mods.sm_desc->ad_type->sat_syntax->ssyn_validate != NULL );
326 if ( mods.sm_desc->ad_type->sat_equality &&
327 mods.sm_desc->ad_type->sat_equality->smr_normalize )
332 if ( bin && mod->mod_bvalues ) {
333 for ( i = 0; mod->mod_bvalues[ i ] != NULL; i++ )
336 } else if ( !bin && mod->mod_values ) {
337 for ( i = 0; mod->mod_values[ i ] != NULL; i++ )
343 mods.sm_values = SLAP_CALLOC( sizeof( struct berval ), i + 1 );
345 mods.sm_nvalues = SLAP_CALLOC( sizeof( struct berval ), i + 1 );
347 mods.sm_nvalues = NULL;
352 for ( i = 0; i < mods.sm_numvals; i++ ) {
356 bv = *mod->mod_bvalues[ i ];
358 ber_str2bv( mod->mod_values[ i ], 0, 0, &bv );
362 local_rc = ordered_value_pretty( mods.sm_desc,
363 &bv, &mods.sm_values[i], NULL );
366 local_rc = ordered_value_validate( mods.sm_desc,
370 if ( local_rc != LDAP_SUCCESS ) {
371 fprintf( stderr, "%s: DN=\"%s\": unable to %s attr=%s value #%d\n",
372 progname, e->e_dn, pretty ? "prettify" : "validate",
373 mods.sm_desc->ad_cname.bv_val, i );
376 ber_bvarray_free( mods.sm_values );
377 ber_bvarray_free( mods.sm_nvalues );
382 ber_dupbv( &mods.sm_values[i], &bv );
386 local_rc = ordered_value_normalize(
387 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
389 mods.sm_desc->ad_type->sat_equality,
390 &mods.sm_values[i], &mods.sm_nvalues[i],
392 if ( local_rc != LDAP_SUCCESS ) {
393 fprintf( stderr, "%s: DN=\"%s\": unable to normalize attr=%s value #%d\n",
394 progname, e->e_dn, mods.sm_desc->ad_cname.bv_val, i );
397 ber_bvarray_free( mods.sm_values );
398 ber_bvarray_free( mods.sm_nvalues );
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 ber_bvarray_free( mods.sm_values );
434 ber_bvarray_free( mods.sm_nvalues );
436 if ( local_rc != LDAP_SUCCESS ) {
437 fprintf( stderr, "%s: DN=\"%s\": unable to modify attr=%s\n",
438 progname, e->e_dn, mods.sm_desc->ad_cname.bv_val );
444 rc = slap_tool_entry_check( progname, op, e, lineno, &text, textbuf, textlen );
445 if ( rc != LDAP_SUCCESS ) {
451 if ( SLAP_LASTMOD(be) && e != NULL ) {
452 time_t now = slap_get_time();
453 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
454 struct berval vals[ 2 ];
456 struct berval name, timestamp;
458 struct berval nvals[ 2 ];
460 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
465 vals[1].bv_val = NULL;
468 nvals[1].bv_val = NULL;
470 csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
473 timestamp.bv_val = timebuf;
474 timestamp.bv_len = sizeof(timebuf);
476 slap_timestamp( &now, ×tamp );
478 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
479 BER_BVSTR( &name, SLAPD_ANONYMOUS );
482 name = be->be_rootdn;
483 nname = be->be_rootndn;
486 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
488 if ( a->a_vals != a->a_nvals ) {
489 SLAP_FREE( a->a_nvals[0].bv_val );
490 SLAP_FREE( a->a_nvals );
492 SLAP_FREE( a->a_vals[0].bv_val );
493 SLAP_FREE( a->a_vals );
498 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
499 vals[0].bv_val = uuidbuf;
500 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
502 a = attr_find( e->e_attrs, slap_schema.si_ad_creatorsName );
506 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
509 ber_bvreplace( &a->a_vals[0], &name );
510 ber_bvreplace( &a->a_nvals[0], &nname );
513 a = attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp );
516 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
519 ber_bvreplace( &a->a_vals[0], ×tamp );
522 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
525 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
528 ber_bvreplace( &a->a_vals[0], &csn );
531 a = attr_find( e->e_attrs, slap_schema.si_ad_modifiersName );
535 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
538 ber_bvreplace( &a->a_vals[0], &name );
539 ber_bvreplace( &a->a_nvals[0], &nname );
542 a = attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp );
545 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
548 ber_bvreplace( &a->a_vals[0], ×tamp );
552 /* check schema, objectClass etc */
555 switch ( lr.lr_op ) {
557 id = be->be_entry_put( be, e, &bvtext );
561 case LDAP_REQ_MODIFY:
562 id = be->be_entry_modify( be, e, &bvtext );
566 case LDAP_REQ_DELETE:
567 rc = be->be_entry_delete( be, &ndn, &bvtext );
572 if( rc != LDAP_SUCCESS ) {
573 fprintf( stderr, "%s: could not %s entry dn=\"%s\" "
574 "(line=%lu): %s\n", progname, request, ndn.bv_val,
575 lineno, bvtext.bv_val );
580 sid = slap_tool_update_ctxcsn_check( progname, e );
583 fprintf( stderr, "%s: \"%s\" (%08lx)\n",
584 request, ndn.bv_val, (long) id );
587 fprintf( stderr, "%s: \"%s\"\n",
588 request, ndn.bv_val );
592 ldap_ldif_record_done( &lr );
593 SLAP_FREE( ndn.bv_val );
594 if ( e ) entry_free( e );
595 if ( e_orig ) be_entry_release_w( op, e_orig );
596 if ( rc != LDAP_SUCCESS && !continuemode ) break;
602 bvtext.bv_len = textlen;
603 bvtext.bv_val = textbuf;
604 bvtext.bv_val[0] = '\0';
606 if ( enable_meter ) {
607 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
608 lutil_meter_close( &meter );
611 if ( rc == EXIT_SUCCESS ) {
612 rc = slap_tool_update_ctxcsn( progname, sid, &bvtext );
618 if ( enable_meter ) {
619 fprintf( stderr, "Closing DB..." );
621 if( be->be_entry_close( be ) ) {
628 if ( enable_meter ) {
629 fprintf( stderr, "\n" );
633 if ( slap_tool_destroy())