]> git.sur5r.net Git - openldap/blob - servers/slapd/slapmodify.c
Merge remote-tracking branch 'origin/mdb.master'
[openldap] / servers / slapd / slapmodify.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2013 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
78         if ( !be->be_entry_open ||
79                 !be->be_entry_close ||
80                 !be->be_entry_put ||
81                 !be->be_dn2id_get ||
82                 !be->be_entry_get ||
83                 !be->be_entry_modify )
84         {
85                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
86                         progname );
87                 if ( dryrun ) {
88                         fprintf( stderr, "\t(dry) continuing...\n" );
89
90                 } else {
91                         exit( EXIT_FAILURE );
92                 }
93         }
94
95         checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
96
97         lmax = 0;
98         nextline = 0;
99
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);
103         }
104
105         if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
106                 fprintf( stderr, "%s: could not open database.\n",
107                         progname );
108                 exit( EXIT_FAILURE );
109         }
110
111         (void)slap_tool_update_ctxcsn_init();
112
113         if ( enable_meter 
114 #ifdef LDAP_DEBUG
115                 /* tools default to "none" */
116                 && slap_debug == LDAP_DEBUG_NONE
117 #endif
118                 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
119                 && S_ISREG(stat_buf.st_mode) ) {
120                 enable_meter = !lutil_meter_open(
121                         &meter,
122                         &lutil_meter_text_display,
123                         &lutil_meter_linear_estimator,
124                         stat_buf.st_size);
125         } else {
126                 enable_meter = 0;
127         }
128
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;
131                 lineno=nextline+1 )
132         {
133                 BackendDB *bd;
134                 Entry *e;
135                 struct berval rbuf;
136                 LDIFRecord lr;
137                 struct berval ndn;
138                 int n;
139                 int is_oc = 0;
140                 int local_rc;
141                 int mod_err = 0;
142                 char *request = "(unknown)";
143
144                 ber_str2bv( buf, 0, 0, &rbuf );
145
146                 if ( lineno < jumpline )
147                         continue;
148
149                 if ( enable_meter )
150                         lutil_meter_update( &meter,
151                                          ftell( ldiffp->fp ),
152                                          0);
153
154                 /*
155                  * Initialize text buffer
156                  */
157                 bvtext.bv_len = textlen;
158                 bvtext.bv_val = textbuf;
159                 bvtext.bv_val[0] = '\0';
160
161                 local_rc = ldap_parse_ldif_record( &rbuf, lineno, &lr,
162                         "slapmodify", LDIF_NO_CONTROLS );
163
164                 if ( local_rc != LDAP_SUCCESS ) {
165                         fprintf( stderr, "%s: could not parse entry (line=%lu)\n",
166                                 progname, lineno );
167                         rc = EXIT_FAILURE;
168                         if( continuemode ) continue;
169                         break;
170                 }
171
172                 switch ( lr.lr_op ) {
173                 case LDAP_REQ_ADD:
174                         request = "add";
175                         break;
176
177                 case LDAP_REQ_MODIFY:
178                         request = "modify";
179                         break;
180
181                 case LDAP_REQ_MODRDN:
182                 case LDAP_REQ_DELETE:
183                         fprintf( stderr, "%s: request 0x%lx not supported (line=%lu)\n",
184                                 progname, (unsigned long)lr.lr_op, lineno );
185                         rc = EXIT_FAILURE;
186                         if( continuemode ) continue;
187                         goto done;
188
189                 default:
190                         fprintf( stderr, "%s: unknown request 0x%lx (line=%lu)\n",
191                                 progname, (unsigned long)lr.lr_op, lineno );
192                         rc = EXIT_FAILURE;
193                         if( continuemode ) continue;
194                         goto done;
195                 }
196
197                 local_rc = dnNormalize( 0, NULL, NULL, &lr.lr_dn, &ndn, NULL );
198                 if ( local_rc != LDAP_SUCCESS ) {
199                         fprintf( stderr, "%s: DN=\"%s\" normalization failed (line=%lu)\n",
200                                 progname, lr.lr_dn.bv_val, lineno );
201                         rc = EXIT_FAILURE;
202                         if( continuemode ) continue;
203                         break;
204                 }
205
206                 /* make sure the DN is not empty */
207                 if( BER_BVISEMPTY( &ndn ) &&
208                         !BER_BVISEMPTY( be->be_nsuffix ))
209                 {
210                         fprintf( stderr, "%s: line %lu: "
211                                 "%s entry with empty dn=\"\"",
212                                 progname, lineno, request );
213                         bd = select_backend( &ndn, nosubordinates );
214                         if ( bd ) {
215                                 BackendDB *bdtmp;
216                                 int dbidx = 0;
217                                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
218                                         if ( bdtmp == bd ) break;
219                                         dbidx++;
220                                 }
221
222                                 assert( bdtmp != NULL );
223                                 
224                                 fprintf( stderr, "; did you mean to use database #%d (%s)?",
225                                         dbidx,
226                                         bd->be_suffix[0].bv_val );
227
228                         }
229                         fprintf( stderr, "\n" );
230                         rc = EXIT_FAILURE;
231                         SLAP_FREE( ndn.bv_val );
232                         ldap_ldif_record_done( &lr );
233                         if( continuemode ) continue;
234                         break;
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                         SLAP_FREE( ndn.bv_val );
266                         ldap_ldif_record_done( &lr );
267                         if( continuemode ) continue;
268                         break;
269                 }
270
271                 /* get entry */
272                 id = be->be_dn2id_get( be, &ndn );
273                 e = be->be_entry_get( be, id );
274                 if ( e != NULL ) {
275                         Entry *e_tmp = entry_dup( e );
276                         /* FIXME: release? */
277                         e = e_tmp;
278                 }
279
280                 for ( n = 0; lr.lrop_mods[ n ] != NULL; n++ ) {
281                         LDAPMod *mod = lr.lrop_mods[ n ];
282                         Modification mods = { 0 };
283                         unsigned i = 0;
284                         int bin = (mod->mod_op & LDAP_MOD_BVALUES);
285                         int pretty = 0;
286                         int normalize = 0;
287
288                         local_rc = slap_str2ad( mod->mod_type, &mods.sm_desc, &text );
289                         if ( local_rc != LDAP_SUCCESS ) {
290                                 fprintf( stderr, "%s: slap_str2ad(\"%s\") failed for entry \"%s\" (%d: %s, lineno=%lu)\n",
291                                         progname, mod->mod_type, lr.lr_dn.bv_val, local_rc, text, lineno );
292                                 rc = EXIT_FAILURE;
293                                 mod_err = 1;
294                                 if( continuemode ) continue;
295                                 SLAP_FREE( ndn.bv_val );
296                                 ldap_ldif_record_done( &lr );
297                                 entry_free( e );
298                                 goto done;
299                         }
300
301                         mods.sm_type = mods.sm_desc->ad_cname;
302
303                         if ( mods.sm_desc->ad_type->sat_syntax->ssyn_pretty ) {
304                                 pretty = 1;
305
306                         } else {
307                                 assert( mods.sm_desc->ad_type->sat_syntax->ssyn_validate != NULL );
308                         }
309
310                         if ( mods.sm_desc->ad_type->sat_equality &&
311                                 mods.sm_desc->ad_type->sat_equality->smr_normalize )
312                         {
313                                 normalize = 1;
314                         }
315
316                         if ( bin && mod->mod_bvalues ) {
317                                 for ( i = 0; mod->mod_bvalues[ i ] != NULL; i++ )
318                                         ;
319
320                         } else if ( !bin && mod->mod_values ) {
321                                 for ( i = 0; mod->mod_values[ i ] != NULL; i++ )
322                                         ;
323                         }
324
325                         mods.sm_values = SLAP_CALLOC( sizeof( struct berval ), i + 1 );
326                         if ( normalize ) {
327                                 mods.sm_nvalues = SLAP_CALLOC( sizeof( struct berval ), i + 1 );
328                         } else {
329                                 mods.sm_nvalues = NULL;
330                         }
331                         mods.sm_numvals = i;
332
333                         for ( i = 0; i < mods.sm_numvals; i++ ) {
334                                 struct berval bv;
335
336                                 if ( bin ) {
337                                         bv = *mod->mod_bvalues[ i ];
338                                 } else {
339                                         ber_str2bv( mod->mod_values[ i ], 0, 0, &bv );
340                                 }
341
342                                 if ( pretty ) {
343                                         local_rc = ordered_value_pretty( mods.sm_desc,
344                                         &bv, &mods.sm_values[i], NULL );
345
346                                 } else {
347                                         local_rc = ordered_value_validate( mods.sm_desc,
348                                                 &bv, 0 );
349                                 }
350
351                                 if ( local_rc != LDAP_SUCCESS ) {
352                                         fprintf( stderr, "%s: DN=\"%s\": unable to %s attr=%s value #%d\n",
353                                                 progname, e->e_dn, pretty ? "prettify" : "validate",
354                                                 mods.sm_desc->ad_cname.bv_val, i );
355                                         /* handle error */
356                                         mod_err = 1;
357                                         rc = EXIT_FAILURE;
358                                         ber_bvarray_free( mods.sm_values );
359                                         ber_bvarray_free( mods.sm_nvalues );
360                                         if( continuemode ) continue;
361                                         SLAP_FREE( ndn.bv_val );
362                                         ldap_ldif_record_done( &lr );
363                                         entry_free( e );
364                                         goto done;
365                                 }
366
367                                 if ( !pretty ) {
368                                         ber_dupbv( &mods.sm_values[i], &bv );
369                                 }
370
371                                 if ( normalize ) {
372                                         local_rc = ordered_value_normalize(
373                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
374                                                 mods.sm_desc,
375                                                 mods.sm_desc->ad_type->sat_equality,
376                                                 &mods.sm_values[i], &mods.sm_nvalues[i],
377                                                 NULL );
378                                         if ( local_rc != LDAP_SUCCESS ) {
379                                                 fprintf( stderr, "%s: DN=\"%s\": unable to normalize attr=%s value #%d\n",
380                                                         progname, e->e_dn, mods.sm_desc->ad_cname.bv_val, i );
381                                                 /* handle error */
382                                                 mod_err = 1;
383                                                 rc = EXIT_FAILURE;
384                                                 ber_bvarray_free( mods.sm_values );
385                                                 ber_bvarray_free( mods.sm_nvalues );
386                                                 if( continuemode ) continue;
387                                                 SLAP_FREE( ndn.bv_val );
388                                                 ldap_ldif_record_done( &lr );
389                                                 entry_free( e );
390                                                 goto done;
391                                         }
392                                 }
393                         }
394
395                         mods.sm_op = (mod->mod_op & ~LDAP_MOD_BVALUES);
396                         mods.sm_flags = 0;
397
398                         if ( mods.sm_desc == slap_schema.si_ad_objectClass ) {
399                                 is_oc = 1;
400                         }
401
402                         switch ( mods.sm_op ) {
403                         case LDAP_MOD_ADD:
404                                 local_rc = modify_add_values( e, &mods,
405                                         0, &text, textbuf, textlen );
406                                 break;
407
408                         case LDAP_MOD_DELETE:
409                                 local_rc = modify_delete_values( e, &mods,
410                                         0, &text, textbuf, textlen );
411                                 break;
412
413                         case LDAP_MOD_REPLACE:
414                                 local_rc = modify_replace_values( e, &mods,
415                                         0, &text, textbuf, textlen );
416                                 break;
417
418                         case LDAP_MOD_INCREMENT:
419                                 local_rc = modify_increment_values( e, &mods,
420                                         0, &text, textbuf, textlen );
421                                 break;
422                         }
423
424                         if ( local_rc != LDAP_SUCCESS ) {
425                                 fprintf( stderr, "%s: DN=\"%s\": unable to modify attr=%s\n",
426                                         progname, e->e_dn, mods.sm_desc->ad_cname.bv_val );
427                                 rc = EXIT_FAILURE;
428                                 ber_bvarray_free( mods.sm_values );
429                                 ber_bvarray_free( mods.sm_nvalues );
430                                 if( continuemode ) continue;
431                                 SLAP_FREE( ndn.bv_val );
432                                 ldap_ldif_record_done( &lr );
433                                 entry_free( e );
434                                 goto done;
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                         SLAP_FREE( ndn.bv_val );
442                         ldap_ldif_record_done( &lr );
443                         if( continuemode ) continue;
444                         entry_free( e );
445                         break;
446                 }
447
448                 if ( SLAP_LASTMOD(be) ) {
449                         time_t now = slap_get_time();
450                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
451                         struct berval vals[ 2 ];
452
453                         struct berval name, timestamp;
454
455                         struct berval nvals[ 2 ];
456                         struct berval nname;
457                         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
458
459                         Attribute *a;
460
461                         vals[1].bv_len = 0;
462                         vals[1].bv_val = NULL;
463
464                         nvals[1].bv_len = 0;
465                         nvals[1].bv_val = NULL;
466
467                         csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
468                         csn.bv_val = csnbuf;
469
470                         timestamp.bv_val = timebuf;
471                         timestamp.bv_len = sizeof(timebuf);
472
473                         slap_timestamp( &now, &timestamp );
474
475                         if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
476                                 BER_BVSTR( &name, SLAPD_ANONYMOUS );
477                                 nname = name;
478                         } else {
479                                 name = be->be_rootdn;
480                                 nname = be->be_rootndn;
481                         }
482
483                         a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
484                         if ( a != NULL ) {
485                                 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
486                                 vals[0].bv_val = uuidbuf;
487                                 if ( a->a_vals != a->a_nvals ) {
488                                         SLAP_FREE( a->a_nvals[0].bv_val );
489                                         SLAP_FREE( a->a_nvals );
490                                 }
491                                 SLAP_FREE( a->a_vals[0].bv_val );
492                                 SLAP_FREE( a->a_vals );
493                                 a->a_vals = NULL;
494                                 a->a_nvals = NULL;
495                                 a->a_numvals = 0;
496                         }
497                         attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
498
499                         a = attr_find( e->e_attrs, slap_schema.si_ad_creatorsName );
500                         if ( a == NULL ) {
501                                 vals[0] = name;
502                                 nvals[0] = nname;
503                                 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
504
505                         } else {
506                                 ber_bvreplace( &a->a_vals[0], &name );
507                                 ber_bvreplace( &a->a_nvals[0], &nname );
508                         }
509
510                         a = attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp );
511                         if ( a == NULL ) {
512                                 vals[0] = timestamp;
513                                 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
514
515                         } else {
516                                 ber_bvreplace( &a->a_vals[0], &timestamp );
517                         }
518
519                         a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
520                         if ( a == NULL ) {
521                                 vals[0] = csn;
522                                 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
523
524                         } else {
525                                 ber_bvreplace( &a->a_vals[0], &csn );
526                         }
527
528                         a = attr_find( e->e_attrs, slap_schema.si_ad_modifiersName );
529                         if ( a == NULL ) {
530                                 vals[0] = name;
531                                 nvals[0] = nname;
532                                 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
533
534                         } else {
535                                 ber_bvreplace( &a->a_vals[0], &name );
536                                 ber_bvreplace( &a->a_nvals[0], &nname );
537                         }
538
539                         a = attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp );
540                         if ( a == NULL ) {
541                                 vals[0] = timestamp;
542                                 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
543
544                         } else {
545                                 ber_bvreplace( &a->a_vals[0], &timestamp );
546                         }
547                 }
548
549                 if ( mod_err ) break;
550
551                 /* check schema, objectClass etc */
552
553                 if ( !dryrun ) {
554                         switch ( lr.lr_op ) {
555                         case LDAP_REQ_ADD:
556                                 id = be->be_entry_put( be, e, &bvtext );
557                                 break;
558
559                         case LDAP_REQ_MODIFY:
560                                 id = be->be_entry_modify( be, e, &bvtext );
561                                 break;
562
563                         }
564
565                         if( id == NOID ) {
566                                 fprintf( stderr, "%s: could not %s entry dn=\"%s\" "
567                                         "(line=%lu): %s\n", progname, request, e->e_dn,
568                                         lineno, bvtext.bv_val );
569                                 rc = EXIT_FAILURE;
570                                 entry_free( e );
571                                 if( continuemode ) continue;
572                                 break;
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                 entry_free( e );
587         }
588
589 done:;
590         if ( ldifrc < 0 )
591                 rc = EXIT_FAILURE;
592
593         bvtext.bv_len = textlen;
594         bvtext.bv_val = textbuf;
595         bvtext.bv_val[0] = '\0';
596
597         if ( enable_meter ) {
598                 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
599                 lutil_meter_close( &meter );
600         }
601
602         if ( rc == EXIT_SUCCESS ) {
603                 rc = slap_tool_update_ctxcsn( progname, sid, &bvtext );
604         }
605
606         ch_free( buf );
607
608         if ( !dryrun ) {
609                 if ( enable_meter ) {
610                         fprintf( stderr, "Closing DB..." );
611                 }
612                 if( be->be_entry_close( be ) ) {
613                         rc = EXIT_FAILURE;
614                 }
615
616                 if( be->be_sync ) {
617                         be->be_sync( be );
618                 }
619                 if ( enable_meter ) {
620                         fprintf( stderr, "\n" );
621                 }
622         }
623
624         if ( slap_tool_destroy())
625                 rc = EXIT_FAILURE;
626
627         return rc;
628 }
629