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