2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2017 The OpenLDAP Foundation.
5 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
6 * Portions Copyright 2003 IBM Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Kurt Zeilenga for inclusion
19 * in OpenLDAP Software. Additional signficant contributors include
28 #include <ac/stdlib.h>
31 #include <ac/string.h>
32 #include <ac/socket.h>
33 #include <ac/unistd.h>
38 #include <lutil_meter.h>
41 #include "slapcommon.h"
45 # define ftello(fp) _ftelli64(fp)
47 /* Ideally we would use _ftelli64 but that was only available
48 * starting in MSVCR80.DLL. The approach used here is inaccurate
49 * because returning the underlying file handle's file pointer
50 * doesn't take the stdio buffer offset into account. But, it
51 * works with all versions of MSVCRT.
53 # define ftello(fp) _telli64(fileno(fp))
57 extern int slap_DN_strict; /* dn.c */
59 static char csnbuf[ LDAP_PVT_CSNSTR_BUFSIZE ];
64 unsigned long nextline;
70 unsigned long nextline;
76 static unsigned long sid = SLAP_SYNC_SID_MAX + 1;
78 static int enable_meter;
79 static lutil_meter_t meter;
80 static const char *progname = "slapadd";
81 static OperationBuffer opbuf;
85 static ldap_pvt_thread_mutex_t add_mutex;
86 static ldap_pvt_thread_cond_t add_cond;
100 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
101 size_t textlen = sizeof textbuf;
103 Operation *op = &opbuf.ob_op;
104 op->o_hdr = &opbuf.ob_hdr;
107 erec->lineno = erec->nextline+1;
108 /* nextline is the line number of the end of the current entry */
109 ldifrc = ldif_read_record( ldiffp, &erec->nextline, &buf, &lmax );
111 return ldifrc < 0 ? -1 : 0;
117 if ( erec->lineno < jumpline )
121 prev_DN_strict = slap_DN_strict;
124 e = str2entry2( buf, checkvals );
126 slap_DN_strict = prev_DN_strict;
130 lutil_meter_update( &meter,
131 ftello( ldiffp->fp ),
135 fprintf( stderr, "%s: could not parse entry (line=%lu)\n",
136 progname, erec->lineno );
140 /* make sure the DN is not empty */
141 if( BER_BVISEMPTY( &e->e_nname ) &&
142 !BER_BVISEMPTY( be->be_nsuffix ))
144 fprintf( stderr, "%s: line %lu: "
145 "cannot add entry with empty dn=\"%s\"",
146 progname, erec->lineno, e->e_dn );
147 bd = select_backend( &e->e_nname, nosubordinates );
151 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
152 if ( bdtmp == bd ) break;
156 assert( bdtmp != NULL );
158 fprintf( stderr, "; did you mean to use database #%d (%s)?",
160 bd->be_suffix[0].bv_val );
163 fprintf( stderr, "\n" );
169 bd = select_backend( &e->e_nname, nosubordinates );
171 fprintf( stderr, "%s: line %lu: "
172 "database #%d (%s) not configured to hold \"%s\"",
173 progname, erec->lineno,
175 be->be_suffix[0].bv_val,
180 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
181 if ( bdtmp == bd ) break;
185 assert( bdtmp != NULL );
187 fprintf( stderr, "; did you mean to use database #%d (%s)?",
189 bd->be_suffix[0].bv_val );
192 fprintf( stderr, "; no database configured for that naming context" );
194 fprintf( stderr, "\n" );
199 if ( slap_tool_entry_check( progname, op, e, erec->lineno, &text, textbuf, textlen ) !=
205 if ( SLAP_LASTMOD(be) ) {
206 time_t now = slap_get_time();
207 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
208 struct berval vals[ 2 ];
210 struct berval name, timestamp;
212 struct berval nvals[ 2 ];
214 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
220 GOT_ALL = (GOT_CSN|GOT_UUID)
224 vals[1].bv_val = NULL;
227 nvals[1].bv_val = NULL;
229 csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
232 timestamp.bv_val = timebuf;
233 timestamp.bv_len = sizeof(timebuf);
235 slap_timestamp( &now, ×tamp );
237 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
238 BER_BVSTR( &name, SLAPD_ANONYMOUS );
241 name = be->be_rootdn;
242 nname = be->be_rootndn;
245 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
249 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
250 vals[0].bv_val = uuidbuf;
251 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
254 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
259 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
262 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
266 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
269 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
274 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
277 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
282 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
285 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
289 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
292 if ( SLAP_SINGLE_SHADOW(be) && got != GOT_ALL ) {
293 char buf[SLAP_TEXT_BUFLEN];
295 snprintf( buf, sizeof(buf),
297 ( !(got & GOT_UUID) ? slap_schema.si_ad_entryUUID->ad_cname.bv_val : "" ),
298 ( !(got & GOT_CSN) ? "," : "" ),
299 ( !(got & GOT_CSN) ? slap_schema.si_ad_entryCSN->ad_cname.bv_val : "" ) );
301 Debug( LDAP_DEBUG_ANY, "%s: warning, missing attrs %s from entry dn=\"%s\"\n",
302 progname, buf, e->e_name.bv_val );
305 sid = slap_tool_update_ctxcsn_check( progname, e );
313 getrec_thr(void *ctx)
315 ldap_pvt_thread_mutex_lock( &add_mutex );
317 trec.rc = getrec0((Erec *)&trec);
320 ldap_pvt_thread_cond_wait( &add_cond, &add_mutex );
321 /* eof or read failure */
322 if ( trec.rc == 0 || trec.rc == -1 )
325 ldap_pvt_thread_mutex_unlock( &add_mutex );
329 static int ldif_threaded;
335 if ( !ldif_threaded )
336 return getrec0(erec);
339 ldap_pvt_thread_yield();
341 erec->lineno = trec.lineno;
342 erec->nextline = trec.nextline;
345 ldap_pvt_thread_mutex_lock( &add_mutex );
346 ldap_pvt_thread_mutex_unlock( &add_mutex );
347 ldap_pvt_thread_cond_signal( &add_cond );
352 slapadd( int argc, char **argv )
354 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
355 size_t textlen = sizeof textbuf;
357 struct berval bvtext;
358 ldap_pvt_thread_t thr;
363 int rc = EXIT_SUCCESS;
365 struct stat stat_buf;
370 if ( isatty (2) ) enable_meter = 1;
371 slap_tool_init( progname, SLAPADD, argc, argv );
373 if( !be->be_entry_open ||
374 !be->be_entry_close ||
377 (!be->be_dn2id_get ||
379 !be->be_entry_modify)) )
381 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
384 fprintf( stderr, "\t(dry) continuing...\n" );
387 exit( EXIT_FAILURE );
391 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
393 /* do not check values in quick mode */
394 if ( slapMode & SLAP_TOOL_QUICK ) {
395 if ( slapMode & SLAP_TOOL_VALUE_CHECK ) {
396 fprintf( stderr, "%s: value-check incompatible with quick mode; disabled.\n", progname );
397 slapMode &= ~SLAP_TOOL_VALUE_CHECK;
401 /* enforce schema checking unless not disabled */
402 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
403 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
406 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
407 fprintf( stderr, "%s: could not open database.\n",
409 exit( EXIT_FAILURE );
412 (void)slap_tool_update_ctxcsn_init();
416 /* tools default to "none" */
417 && slap_debug == LDAP_DEBUG_NONE
419 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
420 && S_ISREG(stat_buf.st_mode) ) {
421 enable_meter = !lutil_meter_open(
423 &lutil_meter_text_display,
424 &lutil_meter_linear_estimator,
430 if ( slap_tool_thread_max > 1 ) {
431 ldap_pvt_thread_mutex_init( &add_mutex );
432 ldap_pvt_thread_cond_init( &add_cond );
433 ldap_pvt_thread_create( &thr, 0, getrec_thr, NULL );
441 ldifrc = getrec( &erec );
443 if ( ldifrc == -2 && continuemode )
450 * Initialize text buffer
452 bvtext.bv_len = textlen;
453 bvtext.bv_val = textbuf;
454 bvtext.bv_val[0] = '\0';
456 id = be->be_entry_put( be, erec.e, &bvtext );
458 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
459 "(line=%lu): %s\n", progname, erec.e->e_dn,
460 erec.lineno, bvtext.bv_val );
463 if ( prev ) entry_free( prev );
470 fprintf( stderr, "added: \"%s\" (%08lx)\n",
471 erec.e->e_dn, (long) id );
474 fprintf( stderr, "added: \"%s\"\n",
478 if ( prev ) entry_free( prev );
482 if ( ldif_threaded ) {
483 ldap_pvt_thread_mutex_lock( &add_mutex );
486 ldap_pvt_thread_cond_signal( &add_cond );
487 ldap_pvt_thread_mutex_unlock( &add_mutex );
488 ldap_pvt_thread_join( thr, NULL );
490 if ( erec.e ) entry_free( erec.e );
495 bvtext.bv_len = textlen;
496 bvtext.bv_val = textbuf;
497 bvtext.bv_val[0] = '\0';
499 if ( enable_meter ) {
500 lutil_meter_update( &meter, ftello( ldiffp->fp ), 1);
501 lutil_meter_close( &meter );
504 if ( rc == EXIT_SUCCESS ) {
505 rc = slap_tool_update_ctxcsn( progname, sid, &bvtext );
511 if ( enable_meter ) {
512 fprintf( stderr, "Closing DB..." );
514 if( be->be_entry_close( be ) ) {
521 if ( enable_meter ) {
522 fprintf( stderr, "\n" );
526 if ( slap_tool_destroy())