]> git.sur5r.net Git - openldap/blob - servers/slapd/slapmodify.c
Merge remote-tracking branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_5
[openldap] / servers / slapd / slapmodify.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2014 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
43 int
44 slapmodify( int argc, char **argv )
45 {
46         char *buf = NULL;
47         const char *text;
48         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
49         size_t textlen = sizeof textbuf;
50         const char *progname = "slapmodify";
51
52         struct berval csn;
53         unsigned long sid;
54         struct berval bvtext;
55         ID id;
56         OperationBuffer opbuf;
57         Operation *op;
58
59         int checkvals, ldifrc;
60         unsigned long lineno, nextline;
61         int lmax;
62         int rc = EXIT_SUCCESS;
63
64         int enable_meter = 0;
65         lutil_meter_t meter;
66         struct stat stat_buf;
67
68         /* default "000" */
69         csnsid = 0;
70
71         if ( isatty (2) ) enable_meter = 1;
72         slap_tool_init( progname, SLAPMODIFY, argc, argv );
73
74         memset( &opbuf, 0, sizeof(opbuf) );
75         op = &opbuf.ob_op;
76         op->o_hdr = &opbuf.ob_hdr;
77         op->o_bd = be;
78
79         if ( !be->be_entry_open ||
80                 !be->be_entry_close ||
81                 !be->be_entry_put ||
82                 !be->be_dn2id_get ||
83                 !be->be_entry_get ||
84                 !be->be_entry_modify )
85         {
86                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
87                         progname );
88                 if ( dryrun ) {
89                         fprintf( stderr, "\t(dry) continuing...\n" );
90
91                 } else {
92                         exit( EXIT_FAILURE );
93                 }
94         }
95
96         checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
97
98         lmax = 0;
99         nextline = 0;
100
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);
104         }
105
106         if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
107                 fprintf( stderr, "%s: could not open database.\n",
108                         progname );
109                 exit( EXIT_FAILURE );
110         }
111
112         (void)slap_tool_update_ctxcsn_init();
113
114         if ( enable_meter 
115 #ifdef LDAP_DEBUG
116                 /* tools default to "none" */
117                 && slap_debug == LDAP_DEBUG_NONE
118 #endif
119                 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
120                 && S_ISREG(stat_buf.st_mode) ) {
121                 enable_meter = !lutil_meter_open(
122                         &meter,
123                         &lutil_meter_text_display,
124                         &lutil_meter_linear_estimator,
125                         stat_buf.st_size);
126         } else {
127                 enable_meter = 0;
128         }
129
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;
132                 lineno=nextline+1 )
133         {
134                 BackendDB *bd;
135                 Entry *e_orig = NULL, *e = NULL;
136                 struct berval rbuf;
137                 LDIFRecord lr;
138                 struct berval ndn = BER_BVNULL;
139                 int n;
140                 int is_oc = 0;
141                 int local_rc;
142                 int mod_err = 0;
143                 char *request = "(unknown)";
144
145                 ber_str2bv( buf, 0, 0, &rbuf );
146
147                 if ( lineno < jumpline )
148                         continue;
149
150                 if ( enable_meter )
151                         lutil_meter_update( &meter,
152                                          ftell( ldiffp->fp ),
153                                          0);
154
155                 /*
156                  * Initialize text buffer
157                  */
158                 bvtext.bv_len = textlen;
159                 bvtext.bv_val = textbuf;
160                 bvtext.bv_val[0] = '\0';
161
162                 local_rc = ldap_parse_ldif_record( &rbuf, lineno, &lr,
163                         "slapmodify", LDIF_NO_CONTROLS );
164
165                 if ( local_rc != LDAP_SUCCESS ) {
166                         fprintf( stderr, "%s: could not parse entry (line=%lu)\n",
167                                 progname, lineno );
168                         rc = EXIT_FAILURE;
169                         if( continuemode ) continue;
170                         break;
171                 }
172
173                 switch ( lr.lr_op ) {
174                 case LDAP_REQ_ADD:
175                         request = "add";
176                         break;
177
178                 case LDAP_REQ_MODIFY:
179                         request = "modify";
180                         break;
181
182                 case LDAP_REQ_DELETE:
183                         if ( be->be_entry_delete )
184                         {
185                                 request = "delete";
186                                 break;
187                         }
188                         /* backend does not support delete, fallthrough */
189
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 );
193                         rc = EXIT_FAILURE;
194                         goto cleanup;
195
196                 default:
197                         /* record skipped e.g. version: or comment or something we don't handle yet */
198                         goto cleanup;
199                 }
200
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 );
205                         rc = EXIT_FAILURE;
206                         goto cleanup;
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 %lu: "
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                         goto cleanup;
235                 }
236
237                 /* check backend */
238                 bd = select_backend( &ndn, nosubordinates );
239                 if ( bd != be ) {
240                         fprintf( stderr, "%s: line %lu: "
241                                 "database #%d (%s) not configured to hold \"%s\"",
242                                 progname, lineno,
243                                 dbnum,
244                                 be->be_suffix[0].bv_val,
245                                 lr.lr_dn.bv_val );
246                         if ( bd ) {
247                                 BackendDB *bdtmp;
248                                 int dbidx = 0;
249                                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
250                                         if ( bdtmp == bd ) break;
251                                         dbidx++;
252                                 }
253
254                                 assert( bdtmp != NULL );
255                                 
256                                 fprintf( stderr, "; did you mean to use database #%d (%s)?",
257                                         dbidx,
258                                         bd->be_suffix[0].bv_val );
259
260                         } else {
261                                 fprintf( stderr, "; no database configured for that naming context" );
262                         }
263                         fprintf( stderr, "\n" );
264                         rc = EXIT_FAILURE;
265                         goto cleanup;
266                 }
267
268                 /* get id and/or entry */
269                 switch ( lr.lr_op ) {
270                         case LDAP_REQ_ADD:
271                                 e = entry_alloc();
272                                 ber_dupbv( &e->e_name, &lr.lr_dn );
273                                 ber_dupbv( &e->e_nname, &ndn );
274                                 break;
275
276                         //case LDAP_REQ_MODRDN:
277                         case LDAP_REQ_DELETE:
278                         case LDAP_REQ_MODIFY:
279                                 id = be->be_dn2id_get( be, &ndn );
280                                 rc = (id == NOID);
281                                 if ( rc == LDAP_SUCCESS && lr.lr_op != LDAP_REQ_DELETE ) {
282                                         e_orig = be->be_entry_get( be, id );
283                                         e = entry_dup( e_orig );
284                                 }
285                                 break;
286                 }
287
288                 if ( rc != LDAP_SUCCESS ) {
289                         fprintf( stderr, "%s: no such entry \"%s\" in database (lineno=%d)\n",
290                                 progname, ndn.bv_val, lineno );
291                         rc = EXIT_FAILURE;
292                         goto cleanup;
293                 }
294
295                 if ( lr.lrop_mods ) {
296                         for ( n = 0; lr.lrop_mods && lr.lrop_mods[ n ] != NULL; n++ ) {
297                                 LDAPMod *mod = lr.lrop_mods[ n ];
298                                 Modification mods = { 0 };
299                                 unsigned i = 0;
300                                 int bin = (mod->mod_op & LDAP_MOD_BVALUES);
301                                 int pretty = 0;
302                                 int normalize = 0;
303
304                                 local_rc = slap_str2ad( mod->mod_type, &mods.sm_desc, &text );
305                                 if ( local_rc != LDAP_SUCCESS ) {
306                                         fprintf( stderr, "%s: slap_str2ad(\"%s\") failed for entry \"%s\" (%d: %s, lineno=%lu)\n",
307                                                 progname, mod->mod_type, lr.lr_dn.bv_val, local_rc, text, lineno );
308                                         rc = EXIT_FAILURE;
309                                         goto cleanup;
310                                 }
311
312                                 mods.sm_type = mods.sm_desc->ad_cname;
313
314                                 if ( mods.sm_desc->ad_type->sat_syntax->ssyn_pretty ) {
315                                         pretty = 1;
316
317                                 } else {
318                                         assert( mods.sm_desc->ad_type->sat_syntax->ssyn_validate != NULL );
319                                 }
320
321                                 if ( mods.sm_desc->ad_type->sat_equality &&
322                                         mods.sm_desc->ad_type->sat_equality->smr_normalize )
323                                 {
324                                         normalize = 1;
325                                 }
326
327                                 if ( bin && mod->mod_bvalues ) {
328                                         for ( i = 0; mod->mod_bvalues[ i ] != NULL; i++ )
329                                                 ;
330
331                                 } else if ( !bin && mod->mod_values ) {
332                                         for ( i = 0; mod->mod_values[ i ] != NULL; i++ )
333                                                 ;
334                                 }
335
336                                 if ( i != 0 )
337                                 {
338                                         mods.sm_values = SLAP_CALLOC( sizeof( struct berval ), i + 1 );
339                                         if ( normalize ) {
340                                                 mods.sm_nvalues = SLAP_CALLOC( sizeof( struct berval ), i + 1 );
341                                         } else {
342                                                 mods.sm_nvalues = NULL;
343                                         }
344                                 }
345                                 mods.sm_numvals = i;
346
347                                 for ( i = 0; i < mods.sm_numvals; i++ ) {
348                                         struct berval bv;
349
350                                         if ( bin ) {
351                                                 bv = *mod->mod_bvalues[ i ];
352                                         } else {
353                                                 ber_str2bv( mod->mod_values[ i ], 0, 0, &bv );
354                                         }
355
356                                         if ( pretty ) {
357                                                 local_rc = ordered_value_pretty( mods.sm_desc,
358                                                 &bv, &mods.sm_values[i], NULL );
359
360                                         } else {
361                                                 local_rc = ordered_value_validate( mods.sm_desc,
362                                                         &bv, 0 );
363                                         }
364
365                                         if ( local_rc != LDAP_SUCCESS ) {
366                                                 fprintf( stderr, "%s: DN=\"%s\": unable to %s attr=%s value #%d\n",
367                                                         progname, e->e_dn, pretty ? "prettify" : "validate",
368                                                         mods.sm_desc->ad_cname.bv_val, i );
369                                                 /* handle error */
370                                                 rc = EXIT_FAILURE;
371                                                 ber_bvarray_free( mods.sm_values );
372                                                 ber_bvarray_free( mods.sm_nvalues );
373                                                 goto cleanup;
374                                         }
375
376                                         if ( !pretty ) {
377                                                 ber_dupbv( &mods.sm_values[i], &bv );
378                                         }
379
380                                         if ( normalize ) {
381                                                 local_rc = ordered_value_normalize(
382                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
383                                                         mods.sm_desc,
384                                                         mods.sm_desc->ad_type->sat_equality,
385                                                         &mods.sm_values[i], &mods.sm_nvalues[i],
386                                                         NULL );
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 );
390                                                         /* handle error */
391                                                         rc = EXIT_FAILURE;
392                                                         ber_bvarray_free( mods.sm_values );
393                                                         ber_bvarray_free( mods.sm_nvalues );
394                                                         goto cleanup;
395                                                 }
396                                         }
397                                 }
398
399                                 mods.sm_op = (mod->mod_op & ~LDAP_MOD_BVALUES);
400                                 mods.sm_flags = 0;
401
402                                 if ( mods.sm_desc == slap_schema.si_ad_objectClass ) {
403                                         is_oc = 1;
404                                 }
405
406                                 switch ( mods.sm_op ) {
407                                 case LDAP_MOD_ADD:
408                                         local_rc = modify_add_values( e, &mods,
409                                                 0, &text, textbuf, textlen );
410                                         break;
411
412                                 case LDAP_MOD_DELETE:
413                                         local_rc = modify_delete_values( e, &mods,
414                                                 0, &text, textbuf, textlen );
415                                         break;
416
417                                 case LDAP_MOD_REPLACE:
418                                         local_rc = modify_replace_values( e, &mods,
419                                                 0, &text, textbuf, textlen );
420                                         break;
421
422                                 case LDAP_MOD_INCREMENT:
423                                         local_rc = modify_increment_values( e, &mods,
424                                                 0, &text, textbuf, textlen );
425                                         break;
426                                 }
427
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 );
431                                         rc = EXIT_FAILURE;
432                                         ber_bvarray_free( mods.sm_values );
433                                         ber_bvarray_free( mods.sm_nvalues );
434                                         goto cleanup;
435                                 }
436                         }
437
438                         rc = slap_tool_entry_check( progname, op, e, lineno, &text, textbuf, textlen );
439                         if ( rc != LDAP_SUCCESS ) {
440                                 rc = EXIT_FAILURE;
441                                 goto cleanup;
442                         }
443                 }
444
445                 if ( SLAP_LASTMOD(be) && e != NULL ) {
446                         time_t now = slap_get_time();
447                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
448                         struct berval vals[ 2 ];
449
450                         struct berval name, timestamp;
451
452                         struct berval nvals[ 2 ];
453                         struct berval nname;
454                         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
455
456                         Attribute *a;
457
458                         vals[1].bv_len = 0;
459                         vals[1].bv_val = NULL;
460
461                         nvals[1].bv_len = 0;
462                         nvals[1].bv_val = NULL;
463
464                         csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
465                         csn.bv_val = csnbuf;
466
467                         timestamp.bv_val = timebuf;
468                         timestamp.bv_len = sizeof(timebuf);
469
470                         slap_timestamp( &now, &timestamp );
471
472                         if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
473                                 BER_BVSTR( &name, SLAPD_ANONYMOUS );
474                                 nname = name;
475                         } else {
476                                 name = be->be_rootdn;
477                                 nname = be->be_rootndn;
478                         }
479
480                         a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
481                         if ( a != NULL ) {
482                                 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
483                                 vals[0].bv_val = uuidbuf;
484                                 if ( a->a_vals != a->a_nvals ) {
485                                         SLAP_FREE( a->a_nvals[0].bv_val );
486                                         SLAP_FREE( a->a_nvals );
487                                 }
488                                 SLAP_FREE( a->a_vals[0].bv_val );
489                                 SLAP_FREE( a->a_vals );
490                                 a->a_vals = NULL;
491                                 a->a_nvals = NULL;
492                                 a->a_numvals = 0;
493                         }
494                         attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
495
496                         a = attr_find( e->e_attrs, slap_schema.si_ad_creatorsName );
497                         if ( a == NULL ) {
498                                 vals[0] = name;
499                                 nvals[0] = nname;
500                                 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
501
502                         } else {
503                                 ber_bvreplace( &a->a_vals[0], &name );
504                                 ber_bvreplace( &a->a_nvals[0], &nname );
505                         }
506
507                         a = attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp );
508                         if ( a == NULL ) {
509                                 vals[0] = timestamp;
510                                 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
511
512                         } else {
513                                 ber_bvreplace( &a->a_vals[0], &timestamp );
514                         }
515
516                         a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
517                         if ( a == NULL ) {
518                                 vals[0] = csn;
519                                 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
520
521                         } else {
522                                 ber_bvreplace( &a->a_vals[0], &csn );
523                         }
524
525                         a = attr_find( e->e_attrs, slap_schema.si_ad_modifiersName );
526                         if ( a == NULL ) {
527                                 vals[0] = name;
528                                 nvals[0] = nname;
529                                 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
530
531                         } else {
532                                 ber_bvreplace( &a->a_vals[0], &name );
533                                 ber_bvreplace( &a->a_nvals[0], &nname );
534                         }
535
536                         a = attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp );
537                         if ( a == NULL ) {
538                                 vals[0] = timestamp;
539                                 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
540
541                         } else {
542                                 ber_bvreplace( &a->a_vals[0], &timestamp );
543                         }
544                 }
545
546                 /* check schema, objectClass etc */
547
548                 if ( !dryrun ) {
549                         switch ( lr.lr_op ) {
550                         case LDAP_REQ_ADD:
551                                 id = be->be_entry_put( be, e, &bvtext );
552                                 rc = (id == NOID);
553                                 break;
554
555                         case LDAP_REQ_MODIFY:
556                                 id = be->be_entry_modify( be, e, &bvtext );
557                                 rc = (id == NOID);
558                                 break;
559
560                         case LDAP_REQ_DELETE:
561                                 rc = be->be_entry_delete( be, id, &bvtext );
562                                 e_orig = NULL;
563                                 break;
564
565                         }
566
567                         if( rc != LDAP_SUCCESS ) {
568                                 fprintf( stderr, "%s: could not %s entry dn=\"%s\" "
569                                         "(line=%lu): %s\n", progname, request, e->e_dn,
570                                         lineno, bvtext.bv_val );
571                                 rc = EXIT_FAILURE;
572                                 goto cleanup;
573                         }
574
575                         sid = slap_tool_update_ctxcsn_check( progname, e );
576
577                         if ( verbose )
578                                 fprintf( stderr, "%s: \"%s\" (%08lx)\n",
579                                         request, e->e_dn, (long) id );
580                 } else {
581                         if ( verbose )
582                                 fprintf( stderr, "%s: \"%s\"\n",
583                                         request, e->e_dn );
584                 }
585
586 cleanup:;
587                 ldap_ldif_record_done( &lr );
588                 SLAP_FREE( ndn.bv_val );
589                 if ( e ) entry_free( e );
590                 if ( e_orig ) be_entry_release_w( op, e_orig );
591                 if ( rc != LDAP_SUCCESS && !continuemode ) break;
592         }
593
594         if ( ldifrc < 0 )
595                 rc = EXIT_FAILURE;
596
597         bvtext.bv_len = textlen;
598         bvtext.bv_val = textbuf;
599         bvtext.bv_val[0] = '\0';
600
601         if ( enable_meter ) {
602                 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
603                 lutil_meter_close( &meter );
604         }
605
606         if ( rc == EXIT_SUCCESS ) {
607                 rc = slap_tool_update_ctxcsn( progname, sid, &bvtext );
608         }
609
610         ch_free( buf );
611
612         if ( !dryrun ) {
613                 if ( enable_meter ) {
614                         fprintf( stderr, "Closing DB..." );
615                 }
616                 if( be->be_entry_close( be ) ) {
617                         rc = EXIT_FAILURE;
618                 }
619
620                 if( be->be_sync ) {
621                         be->be_sync( be );
622                 }
623                 if ( enable_meter ) {
624                         fprintf( stderr, "\n" );
625                 }
626         }
627
628         if ( slap_tool_destroy())
629                 rc = EXIT_FAILURE;
630
631         return rc;
632 }
633