2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2014 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"
43 extern int slap_DN_strict; /* dn.c */
45 static char csnbuf[ LDAP_PVT_CSNSTR_BUFSIZE ];
50 unsigned long nextline;
56 unsigned long nextline;
62 static unsigned long sid = SLAP_SYNC_SID_MAX + 1;
64 static int enable_meter;
65 static lutil_meter_t meter;
66 static const char *progname = "slapadd";
67 static OperationBuffer opbuf;
71 static ldap_pvt_thread_mutex_t add_mutex;
72 static ldap_pvt_thread_cond_t add_cond;
86 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
87 size_t textlen = sizeof textbuf;
89 Operation *op = &opbuf.ob_op;
90 op->o_hdr = &opbuf.ob_hdr;
93 erec->lineno = erec->nextline+1;
94 /* nextline is the line number of the end of the current entry */
95 ldifrc = ldif_read_record( ldiffp, &erec->nextline, &buf, &lmax );
97 return ldifrc < 0 ? -1 : 0;
103 if ( erec->lineno < jumpline )
107 prev_DN_strict = slap_DN_strict;
110 e = str2entry2( buf, checkvals );
112 slap_DN_strict = prev_DN_strict;
116 lutil_meter_update( &meter,
117 ftello( ldiffp->fp ),
121 fprintf( stderr, "%s: could not parse entry (line=%lu)\n",
122 progname, erec->lineno );
126 /* make sure the DN is not empty */
127 if( BER_BVISEMPTY( &e->e_nname ) &&
128 !BER_BVISEMPTY( be->be_nsuffix ))
130 fprintf( stderr, "%s: line %lu: "
131 "cannot add entry with empty dn=\"%s\"",
132 progname, erec->lineno, e->e_dn );
133 bd = select_backend( &e->e_nname, nosubordinates );
137 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
138 if ( bdtmp == bd ) break;
142 assert( bdtmp != NULL );
144 fprintf( stderr, "; did you mean to use database #%d (%s)?",
146 bd->be_suffix[0].bv_val );
149 fprintf( stderr, "\n" );
155 bd = select_backend( &e->e_nname, nosubordinates );
157 fprintf( stderr, "%s: line %lu: "
158 "database #%d (%s) not configured to hold \"%s\"",
159 progname, erec->lineno,
161 be->be_suffix[0].bv_val,
166 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
167 if ( bdtmp == bd ) break;
171 assert( bdtmp != NULL );
173 fprintf( stderr, "; did you mean to use database #%d (%s)?",
175 bd->be_suffix[0].bv_val );
178 fprintf( stderr, "; no database configured for that naming context" );
180 fprintf( stderr, "\n" );
185 if ( slap_tool_entry_check( progname, op, e, erec->lineno, &text, textbuf, textlen ) !=
191 if ( SLAP_LASTMOD(be) ) {
192 time_t now = slap_get_time();
193 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
194 struct berval vals[ 2 ];
196 struct berval name, timestamp;
198 struct berval nvals[ 2 ];
200 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
206 GOT_ALL = (GOT_CSN|GOT_UUID)
210 vals[1].bv_val = NULL;
213 nvals[1].bv_val = NULL;
215 csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
218 timestamp.bv_val = timebuf;
219 timestamp.bv_len = sizeof(timebuf);
221 slap_timestamp( &now, ×tamp );
223 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
224 BER_BVSTR( &name, SLAPD_ANONYMOUS );
227 name = be->be_rootdn;
228 nname = be->be_rootndn;
231 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
235 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
236 vals[0].bv_val = uuidbuf;
237 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
240 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
245 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
248 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
252 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
255 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
260 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
263 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
268 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
271 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
275 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
278 if ( SLAP_SINGLE_SHADOW(be) && got != GOT_ALL ) {
279 char buf[SLAP_TEXT_BUFLEN];
281 snprintf( buf, sizeof(buf),
283 ( !(got & GOT_UUID) ? slap_schema.si_ad_entryUUID->ad_cname.bv_val : "" ),
284 ( !(got & GOT_CSN) ? "," : "" ),
285 ( !(got & GOT_CSN) ? slap_schema.si_ad_entryCSN->ad_cname.bv_val : "" ) );
287 Debug( LDAP_DEBUG_ANY, "%s: warning, missing attrs %s from entry dn=\"%s\"\n",
288 progname, buf, e->e_name.bv_val );
291 sid = slap_tool_update_ctxcsn_check( progname, e );
299 getrec_thr(void *ctx)
301 ldap_pvt_thread_mutex_lock( &add_mutex );
303 trec.rc = getrec0((Erec *)&trec);
306 ldap_pvt_thread_cond_wait( &add_cond, &add_mutex );
307 /* eof or read failure */
308 if ( trec.rc == 0 || trec.rc == -1 )
311 ldap_pvt_thread_mutex_unlock( &add_mutex );
315 static int ldif_threaded;
321 if ( !ldif_threaded )
322 return getrec0(erec);
325 ldap_pvt_thread_yield();
327 erec->lineno = trec.lineno;
328 erec->nextline = trec.nextline;
331 ldap_pvt_thread_mutex_lock( &add_mutex );
332 ldap_pvt_thread_mutex_unlock( &add_mutex );
333 ldap_pvt_thread_cond_signal( &add_cond );
338 slapadd( int argc, char **argv )
340 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
341 size_t textlen = sizeof textbuf;
343 struct berval bvtext;
344 ldap_pvt_thread_t thr;
349 int rc = EXIT_SUCCESS;
351 struct stat stat_buf;
356 if ( isatty (2) ) enable_meter = 1;
357 slap_tool_init( progname, SLAPADD, argc, argv );
359 if( !be->be_entry_open ||
360 !be->be_entry_close ||
363 (!be->be_dn2id_get ||
365 !be->be_entry_modify)) )
367 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
370 fprintf( stderr, "\t(dry) continuing...\n" );
373 exit( EXIT_FAILURE );
377 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
379 /* do not check values in quick mode */
380 if ( slapMode & SLAP_TOOL_QUICK ) {
381 if ( slapMode & SLAP_TOOL_VALUE_CHECK ) {
382 fprintf( stderr, "%s: value-check incompatible with quick mode; disabled.\n", progname );
383 slapMode &= ~SLAP_TOOL_VALUE_CHECK;
387 /* enforce schema checking unless not disabled */
388 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
389 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
392 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
393 fprintf( stderr, "%s: could not open database.\n",
395 exit( EXIT_FAILURE );
398 (void)slap_tool_update_ctxcsn_init();
402 /* tools default to "none" */
403 && slap_debug == LDAP_DEBUG_NONE
405 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
406 && S_ISREG(stat_buf.st_mode) ) {
407 enable_meter = !lutil_meter_open(
409 &lutil_meter_text_display,
410 &lutil_meter_linear_estimator,
416 if ( slap_tool_thread_max > 1 ) {
417 ldap_pvt_thread_mutex_init( &add_mutex );
418 ldap_pvt_thread_cond_init( &add_cond );
419 ldap_pvt_thread_create( &thr, 0, getrec_thr, NULL );
427 ldifrc = getrec( &erec );
429 if ( ldifrc == -2 && continuemode )
436 * Initialize text buffer
438 bvtext.bv_len = textlen;
439 bvtext.bv_val = textbuf;
440 bvtext.bv_val[0] = '\0';
442 id = be->be_entry_put( be, erec.e, &bvtext );
444 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
445 "(line=%lu): %s\n", progname, erec.e->e_dn,
446 erec.lineno, bvtext.bv_val );
449 if ( prev ) entry_free( prev );
456 fprintf( stderr, "added: \"%s\" (%08lx)\n",
457 erec.e->e_dn, (long) id );
460 fprintf( stderr, "added: \"%s\"\n",
464 if ( prev ) entry_free( prev );
468 if ( ldif_threaded ) {
469 ldap_pvt_thread_mutex_lock( &add_mutex );
472 ldap_pvt_thread_cond_signal( &add_cond );
473 ldap_pvt_thread_mutex_unlock( &add_mutex );
474 ldap_pvt_thread_join( thr, NULL );
476 if ( erec.e ) entry_free( erec.e );
481 bvtext.bv_len = textlen;
482 bvtext.bv_val = textbuf;
483 bvtext.bv_val[0] = '\0';
485 if ( enable_meter ) {
486 lutil_meter_update( &meter, ftello( ldiffp->fp ), 1);
487 lutil_meter_close( &meter );
490 if ( rc == EXIT_SUCCESS ) {
491 rc = slap_tool_update_ctxcsn( progname, sid, &bvtext );
497 if ( enable_meter ) {
498 fprintf( stderr, "Closing DB..." );
500 if( be->be_entry_close( be ) ) {
507 if ( enable_meter ) {
508 fprintf( stderr, "\n" );
512 if ( slap_tool_destroy())