]> git.sur5r.net Git - openldap/blob - servers/slapd/slapadd.c
warn if entryUUID,entryCSN are missing and the database is shadow (ITS#6281)
[openldap] / servers / slapd / slapadd.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2009 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 Kurt Zeilenga for inclusion
19  * in OpenLDAP Software.  Additional signficant contributors include
20  *    Jong Hyuk Choi
21  *    Pierangelo Masarati
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/stdlib.h>
29
30 #include <ac/ctype.h>
31 #include <ac/string.h>
32 #include <ac/socket.h>
33 #include <ac/unistd.h>
34
35 #include <lber.h>
36 #include <ldif.h>
37 #include <lutil.h>
38 #include <lutil_meter.h>
39 #include <sys/stat.h>
40
41 #include "slapcommon.h"
42
43 static char csnbuf[ LDAP_PVT_CSNSTR_BUFSIZE ];
44 static char maxcsnbuf[ LDAP_PVT_CSNSTR_BUFSIZE * ( SLAP_SYNC_SID_MAX + 1 ) ];
45
46 int
47 slapadd( int argc, char **argv )
48 {
49         char *buf = NULL;
50         const char *text;
51         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
52         size_t textlen = sizeof textbuf;
53         const char *progname = "slapadd";
54
55         struct berval csn;
56         struct berval maxcsn[ SLAP_SYNC_SID_MAX + 1 ];
57         unsigned long sid;
58         struct berval bvtext;
59         Attribute *attr;
60         Entry *ctxcsn_e;
61         ID      ctxcsn_id, id;
62         OperationBuffer opbuf;
63         Operation *op;
64
65         int match;
66         int checkvals;
67         int lineno, nextline, ldifrc;
68         int lmax;
69         int rc = EXIT_SUCCESS;
70         int manage = 0; 
71
72         int enable_meter = 0;
73         lutil_meter_t meter;
74         struct stat stat_buf;
75
76         /* default "000" */
77         csnsid = 0;
78
79         if ( isatty (2) ) enable_meter = 1;
80         slap_tool_init( progname, SLAPADD, argc, argv );
81
82         memset( &opbuf, 0, sizeof(opbuf) );
83         op = &opbuf.ob_op;
84         op->o_hdr = &opbuf.ob_hdr;
85
86         if( !be->be_entry_open ||
87                 !be->be_entry_close ||
88                 !be->be_entry_put ||
89                 (update_ctxcsn &&
90                  (!be->be_dn2id_get ||
91                   !be->be_entry_get ||
92                   !be->be_entry_modify)) )
93         {
94                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
95                         progname );
96                 if ( dryrun ) {
97                         fprintf( stderr, "\t(dry) continuing...\n" );
98
99                 } else {
100                         exit( EXIT_FAILURE );
101                 }
102         }
103
104         checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
105
106         lmax = 0;
107         nextline = 0;
108
109         /* enforce schema checking unless not disabled */
110         if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
111                 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
112         }
113
114         if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
115                 fprintf( stderr, "%s: could not open database.\n",
116                         progname );
117                 exit( EXIT_FAILURE );
118         }
119
120         if ( update_ctxcsn ) {
121                 maxcsn[ 0 ].bv_val = maxcsnbuf;
122                 for ( sid = 1; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
123                         maxcsn[ sid ].bv_val = maxcsn[ sid - 1 ].bv_val + LDAP_PVT_CSNSTR_BUFSIZE;
124                         maxcsn[ sid ].bv_len = 0;
125                 }
126         }
127
128         if ( enable_meter 
129 #ifdef LDAP_DEBUG
130                 /* tools default to "none" */
131                 && slap_debug == LDAP_DEBUG_NONE
132 #endif
133                 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
134                 && S_ISREG(stat_buf.st_mode) ) {
135                 enable_meter = !lutil_meter_open(
136                         &meter,
137                         &lutil_meter_text_display,
138                         &lutil_meter_linear_estimator,
139                         stat_buf.st_size);
140         } else {
141                 enable_meter = 0;
142         }
143
144         /* nextline is the line number of the end of the current entry */
145         for( lineno=1; ( ldifrc = ldif_read_record( ldiffp, &nextline, &buf, &lmax )) > 0;
146                 lineno=nextline+1 )
147         {
148                 BackendDB *bd;
149                 Entry *e;
150
151                 if ( lineno < jumpline )
152                         continue;
153
154                 e = str2entry2( buf, checkvals );
155
156                 if ( enable_meter )
157                         lutil_meter_update( &meter,
158                                          ftell( ldiffp->fp ),
159                                          0);
160
161                 /*
162                  * Initialize text buffer
163                  */
164                 bvtext.bv_len = textlen;
165                 bvtext.bv_val = textbuf;
166                 bvtext.bv_val[0] = '\0';
167
168                 if( e == NULL ) {
169                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
170                                 progname, lineno );
171                         rc = EXIT_FAILURE;
172                         if( continuemode ) continue;
173                         break;
174                 }
175
176                 /* make sure the DN is not empty */
177                 if( BER_BVISEMPTY( &e->e_nname ) &&
178                         !BER_BVISEMPTY( be->be_nsuffix ))
179                 {
180                         fprintf( stderr, "%s: line %d: "
181                                 "cannot add entry with empty dn=\"%s\"",
182                                 progname, lineno, e->e_dn );
183                         bd = select_backend( &e->e_nname, nosubordinates );
184                         if ( bd ) {
185                                 BackendDB *bdtmp;
186                                 int dbidx = 0;
187                                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
188                                         if ( bdtmp == bd ) break;
189                                         dbidx++;
190                                 }
191
192                                 assert( bdtmp != NULL );
193                                 
194                                 fprintf( stderr, "; did you mean to use database #%d (%s)?",
195                                         dbidx,
196                                         bd->be_suffix[0].bv_val );
197
198                         }
199                         fprintf( stderr, "\n" );
200                         rc = EXIT_FAILURE;
201                         entry_free( e );
202                         if( continuemode ) continue;
203                         break;
204                 }
205
206                 /* check backend */
207                 bd = select_backend( &e->e_nname, nosubordinates );
208                 if ( bd != be ) {
209                         fprintf( stderr, "%s: line %d: "
210                                 "database #%d (%s) not configured to hold \"%s\"",
211                                 progname, lineno,
212                                 dbnum,
213                                 be->be_suffix[0].bv_val,
214                                 e->e_dn );
215                         if ( bd ) {
216                                 BackendDB *bdtmp;
217                                 int dbidx = 0;
218                                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
219                                         if ( bdtmp == bd ) break;
220                                         dbidx++;
221                                 }
222
223                                 assert( bdtmp != NULL );
224                                 
225                                 fprintf( stderr, "; did you mean to use database #%d (%s)?",
226                                         dbidx,
227                                         bd->be_suffix[0].bv_val );
228
229                         } else {
230                                 fprintf( stderr, "; no database configured for that naming context" );
231                         }
232                         fprintf( stderr, "\n" );
233                         rc = EXIT_FAILURE;
234                         entry_free( e );
235                         if( continuemode ) continue;
236                         break;
237                 }
238
239                 {
240                         Attribute *oc = attr_find( e->e_attrs,
241                                 slap_schema.si_ad_objectClass );
242
243                         if( oc == NULL ) {
244                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
245                                         progname, e->e_dn, lineno,
246                                         "no objectClass attribute");
247                                 rc = EXIT_FAILURE;
248                                 entry_free( e );
249                                 if( continuemode ) continue;
250                                 break;
251                         }
252
253                         /* check schema */
254                         op->o_bd = be;
255
256                         if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
257                                 rc = entry_schema_check( op, e, NULL, manage, 1, NULL,
258                                         &text, textbuf, textlen );
259
260                                 if( rc != LDAP_SUCCESS ) {
261                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
262                                                 progname, e->e_dn, lineno, rc, text );
263                                         rc = EXIT_FAILURE;
264                                         entry_free( e );
265                                         if( continuemode ) continue;
266                                         break;
267                                 }
268                                 textbuf[ 0 ] = '\0';
269                         }
270                 }
271
272                 if ( SLAP_LASTMOD(be) ) {
273                         time_t now = slap_get_time();
274                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
275                         struct berval vals[ 2 ];
276
277                         struct berval name, timestamp;
278
279                         struct berval nvals[ 2 ];
280                         struct berval nname;
281                         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
282
283                         enum {
284                                 GOT_NONE = 0x0,
285                                 GOT_CSN = 0x1,
286                                 GOT_UUID = 0x2,
287                                 GOT_ALL = (GOT_CSN|GOT_UUID)
288                         } got = GOT_ALL;
289
290                         vals[1].bv_len = 0;
291                         vals[1].bv_val = NULL;
292
293                         nvals[1].bv_len = 0;
294                         nvals[1].bv_val = NULL;
295
296                         csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
297                         csn.bv_val = csnbuf;
298
299                         timestamp.bv_val = timebuf;
300                         timestamp.bv_len = sizeof(timebuf);
301
302                         slap_timestamp( &now, &timestamp );
303
304                         if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
305                                 BER_BVSTR( &name, SLAPD_ANONYMOUS );
306                                 nname = name;
307                         } else {
308                                 name = be->be_rootdn;
309                                 nname = be->be_rootndn;
310                         }
311
312                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
313                                 == NULL )
314                         {
315                                 got &= ~GOT_UUID;
316                                 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
317                                 vals[0].bv_val = uuidbuf;
318                                 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
319                         }
320
321                         if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
322                                 == NULL )
323                         {
324                                 vals[0] = name;
325                                 nvals[0] = nname;
326                                 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
327                         }
328
329                         if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
330                                 == NULL )
331                         {
332                                 vals[0] = timestamp;
333                                 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
334                         }
335
336                         if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
337                                 == NULL )
338                         {
339                                 got &= ~GOT_CSN;
340                                 vals[0] = csn;
341                                 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
342                         }
343
344                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
345                                 == NULL )
346                         {
347                                 vals[0] = name;
348                                 nvals[0] = nname;
349                                 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
350                         }
351
352                         if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
353                                 == NULL )
354                         {
355                                 vals[0] = timestamp;
356                                 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
357                         }
358
359                         if ( SLAP_SINGLE_SHADOW(be) && got != GOT_ALL ) {
360                                 char buf[SLAP_TEXT_BUFLEN];
361
362                                 snprintf( buf, sizeof(buf),
363                                         "%s%s%s",
364                                         ( !(got & GOT_UUID) ? slap_schema.si_ad_entryUUID->ad_cname.bv_val : "" ),
365                                         ( !(got & GOT_CSN) ? "," : "" ),
366                                         ( !(got & GOT_CSN) ? slap_schema.si_ad_entryCSN->ad_cname.bv_val : "" ) );
367
368                                 Debug( LDAP_DEBUG_ANY, "%s: warning, missing attrs %s from entry dn=\"%s\"\n",
369                                         progname, buf, e->e_name.bv_val );
370                         }
371
372                         if ( update_ctxcsn ) {
373                                 int rc_sid;
374
375                                 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
376                                 assert( attr != NULL );
377
378                                 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] );
379                                 if ( rc_sid < 0 ) {
380                                         Debug( LDAP_DEBUG_ANY, "%s: could not "
381                                                 "extract SID from entryCSN=%s, entry dn=\"%s\"\n",
382                                                 progname, attr->a_nvals[ 0 ].bv_val, e->e_name.bv_val );
383
384                                 } else {
385                                         assert( rc_sid <= SLAP_SYNC_SID_MAX );
386
387                                         sid = (unsigned)rc_sid;
388                                         if ( maxcsn[ sid ].bv_len != 0 ) {
389                                                 match = 0;
390                                                 value_match( &match, slap_schema.si_ad_entryCSN,
391                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
392                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
393                                                         &maxcsn[ sid ], &attr->a_nvals[0], &text );
394                                         } else {
395                                                 match = -1;
396                                         }
397                                         if ( match < 0 ) {
398                                                 strcpy( maxcsn[ sid ].bv_val, attr->a_nvals[0].bv_val );
399                                                 maxcsn[ sid ].bv_len = attr->a_nvals[0].bv_len;
400                                         }
401                                 }
402                         }
403                 }
404
405                 if ( !dryrun ) {
406                         id = be->be_entry_put( be, e, &bvtext );
407                         if( id == NOID ) {
408                                 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
409                                                                  "(line=%d): %s\n", progname, e->e_dn,
410                                                                  lineno, bvtext.bv_val );
411                                 rc = EXIT_FAILURE;
412                                 entry_free( e );
413                                 if( continuemode ) continue;
414                                 break;
415                         }
416                         if ( verbose )
417                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
418                                         e->e_dn, (long) id );
419                 } else {
420                         if ( verbose )
421                                 fprintf( stderr, "added: \"%s\"\n",
422                                         e->e_dn );
423                 }
424
425                 entry_free( e );
426         }
427
428         if ( ldifrc < 0 )
429                 rc = EXIT_FAILURE;
430
431         bvtext.bv_len = textlen;
432         bvtext.bv_val = textbuf;
433         bvtext.bv_val[0] = '\0';
434
435         if ( enable_meter ) {
436                 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
437                 lutil_meter_close( &meter );
438         }
439
440         if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) {
441                 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
442                 if ( ctxcsn_id == NOID ) {
443                         fprintf( stderr, "%s: context entry is missing\n", progname );
444                         rc = EXIT_FAILURE;
445                 } else {
446                         ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
447                         if ( ctxcsn_e != NULL ) {
448                                 Entry *e = entry_dup( ctxcsn_e );
449                                 int change;
450                                 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
451                                 if ( attr ) {
452                                         int             i;
453
454                                         change = 0;
455
456                                         for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) {
457                                                 int rc_sid;
458
459                                                 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] );
460                                                 if ( rc_sid < 0 ) {
461                                                         Debug( LDAP_DEBUG_ANY,
462                                                                 "%s: unable to extract SID "
463                                                                 "from #%d contextCSN=%s\n",
464                                                                 progname, i,
465                                                                 attr->a_nvals[ i ].bv_val );
466                                                         continue;
467                                                 }
468
469                                                 assert( rc_sid <= SLAP_SYNC_SID_MAX );
470
471                                                 sid = (unsigned)rc_sid;
472
473                                                 if ( maxcsn[ sid ].bv_len == 0 ) {
474                                                         match = -1;
475
476                                                 } else {
477                                                         value_match( &match, slap_schema.si_ad_entryCSN,
478                                                                 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
479                                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
480                                                                 &maxcsn[ sid ], &attr->a_nvals[i], &text );
481                                                 }
482
483                                                 if ( match > 0 ) {
484                                                         change = 1;
485                                                 } else {
486                                                         AC_MEMCPY( maxcsn[ sid ].bv_val,
487                                                                 attr->a_nvals[ i ].bv_val,
488                                                                 attr->a_nvals[ i ].bv_len );
489                                                         maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0';
490                                                         maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len;
491                                                 }
492                                         }
493
494                                         if ( change ) {
495                                                 if ( attr->a_nvals != attr->a_vals ) {
496                                                         ber_bvarray_free( attr->a_nvals );
497                                                 }
498                                                 attr->a_nvals = NULL;
499                                                 ber_bvarray_free( attr->a_vals );
500                                                 attr->a_vals = NULL;
501                                                 attr->a_numvals = 0;
502                                         }
503                                 } else {
504                                         change = 1;
505                                 }
506
507                                 if ( change ) {
508                                         for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
509                                                 if ( maxcsn[ sid ].bv_len ) {
510                                                         attr_merge_one( e, slap_schema.si_ad_contextCSN,
511                                                                 &maxcsn[ sid], NULL );
512                                                 }
513                                         }
514
515                                         ctxcsn_id = be->be_entry_modify( be, e, &bvtext );
516                                         if( ctxcsn_id == NOID ) {
517                                                 fprintf( stderr, "%s: could not modify ctxcsn\n",
518                                                         progname);
519                                                 rc = EXIT_FAILURE;
520                                         } else if ( verbose ) {
521                                                 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
522                                                         e->e_dn, (long) ctxcsn_id );
523                                         }
524                                 }
525                                 entry_free( e );
526                         }
527                 } 
528         }
529
530         ch_free( buf );
531
532         if ( !dryrun ) {
533                 if ( enable_meter ) {
534                         fprintf( stderr, "Closing DB..." );
535                 }
536                 if( be->be_entry_close( be ) ) {
537                         rc = EXIT_FAILURE;
538                 }
539
540                 if( be->be_sync ) {
541                         be->be_sync( be );
542                 }
543                 if ( enable_meter ) {
544                         fprintf( stderr, "\n" );
545                 }
546         }
547
548         if ( slap_tool_destroy())
549                 rc = EXIT_FAILURE;
550
551         return rc;
552 }
553