2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2011 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 static char csnbuf[ LDAP_PVT_CSNSTR_BUFSIZE ];
60 static unsigned long sid = SLAP_SYNC_SID_MAX + 1;
62 static int enable_meter;
63 static lutil_meter_t meter;
64 static const char *progname = "slapadd";
65 static OperationBuffer opbuf;
69 static ldap_pvt_thread_mutex_t add_mutex;
70 static ldap_pvt_thread_cond_t add_cond;
84 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
85 size_t textlen = sizeof textbuf;
87 Operation *op = &opbuf.ob_op;
88 op->o_hdr = &opbuf.ob_hdr;
91 erec->lineno = erec->nextline+1;
92 /* nextline is the line number of the end of the current entry */
93 ldifrc = ldif_read_record( ldiffp, &erec->nextline, &buf, &lmax );
95 return ldifrc < 0 ? -1 : 0;
100 if ( erec->lineno < jumpline )
103 e = str2entry2( buf, checkvals );
106 lutil_meter_update( &meter,
111 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
112 progname, erec->lineno );
116 /* make sure the DN is not empty */
117 if( BER_BVISEMPTY( &e->e_nname ) &&
118 !BER_BVISEMPTY( be->be_nsuffix ))
120 fprintf( stderr, "%s: line %d: "
121 "cannot add entry with empty dn=\"%s\"",
122 progname, erec->lineno, e->e_dn );
123 bd = select_backend( &e->e_nname, nosubordinates );
127 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
128 if ( bdtmp == bd ) break;
132 assert( bdtmp != NULL );
134 fprintf( stderr, "; did you mean to use database #%d (%s)?",
136 bd->be_suffix[0].bv_val );
139 fprintf( stderr, "\n" );
145 bd = select_backend( &e->e_nname, nosubordinates );
147 fprintf( stderr, "%s: line %d: "
148 "database #%d (%s) not configured to hold \"%s\"",
149 progname, erec->lineno,
151 be->be_suffix[0].bv_val,
156 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
157 if ( bdtmp == bd ) break;
161 assert( bdtmp != NULL );
163 fprintf( stderr, "; did you mean to use database #%d (%s)?",
165 bd->be_suffix[0].bv_val );
168 fprintf( stderr, "; no database configured for that naming context" );
170 fprintf( stderr, "\n" );
175 if ( slap_tool_entry_check( progname, op, e, erec->lineno, &text, textbuf, textlen ) !=
181 if ( SLAP_LASTMOD(be) ) {
182 time_t now = slap_get_time();
183 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
184 struct berval vals[ 2 ];
186 struct berval name, timestamp;
188 struct berval nvals[ 2 ];
190 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
196 GOT_ALL = (GOT_CSN|GOT_UUID)
200 vals[1].bv_val = NULL;
203 nvals[1].bv_val = NULL;
205 csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
208 timestamp.bv_val = timebuf;
209 timestamp.bv_len = sizeof(timebuf);
211 slap_timestamp( &now, ×tamp );
213 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
214 BER_BVSTR( &name, SLAPD_ANONYMOUS );
217 name = be->be_rootdn;
218 nname = be->be_rootndn;
221 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
225 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
226 vals[0].bv_val = uuidbuf;
227 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
230 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
235 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
238 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
242 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
245 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
250 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
253 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
258 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
261 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
265 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
268 if ( SLAP_SINGLE_SHADOW(be) && got != GOT_ALL ) {
269 char buf[SLAP_TEXT_BUFLEN];
271 snprintf( buf, sizeof(buf),
273 ( !(got & GOT_UUID) ? slap_schema.si_ad_entryUUID->ad_cname.bv_val : "" ),
274 ( !(got & GOT_CSN) ? "," : "" ),
275 ( !(got & GOT_CSN) ? slap_schema.si_ad_entryCSN->ad_cname.bv_val : "" ) );
277 Debug( LDAP_DEBUG_ANY, "%s: warning, missing attrs %s from entry dn=\"%s\"\n",
278 progname, buf, e->e_name.bv_val );
281 sid = slap_tool_update_ctxcsn_check( progname, e );
289 getrec_thr(void *ctx)
291 ldap_pvt_thread_mutex_lock( &add_mutex );
293 trec.rc = getrec0((Erec *)&trec);
296 ldap_pvt_thread_cond_wait( &add_cond, &add_mutex );
297 /* eof or read failure */
298 if ( trec.rc == 0 || trec.rc == -1 )
301 ldap_pvt_thread_mutex_unlock( &add_mutex );
309 if ( slap_tool_thread_max < 2 )
310 return getrec0(erec);
313 ldap_pvt_thread_yield();
315 erec->lineno = trec.lineno;
316 erec->nextline = trec.nextline;
319 ldap_pvt_thread_mutex_lock( &add_mutex );
320 ldap_pvt_thread_mutex_unlock( &add_mutex );
321 ldap_pvt_thread_cond_signal( &add_cond );
326 slapadd( int argc, char **argv )
328 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
329 size_t textlen = sizeof textbuf;
331 struct berval bvtext;
332 ldap_pvt_thread_t thr;
336 int rc = EXIT_SUCCESS;
338 struct stat stat_buf;
343 if ( isatty (2) ) enable_meter = 1;
344 slap_tool_init( progname, SLAPADD, argc, argv );
346 if( !be->be_entry_open ||
347 !be->be_entry_close ||
350 (!be->be_dn2id_get ||
352 !be->be_entry_modify)) )
354 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
357 fprintf( stderr, "\t(dry) continuing...\n" );
360 exit( EXIT_FAILURE );
364 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
366 /* do not check values in quick mode */
367 if ( slapMode & SLAP_TOOL_QUICK ) {
368 if ( slapMode & SLAP_TOOL_VALUE_CHECK ) {
369 fprintf( stderr, "%s: value-check incompatible with quick mode; disabled.\n", progname );
370 slapMode &= ~SLAP_TOOL_VALUE_CHECK;
374 /* enforce schema checking unless not disabled */
375 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
376 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
379 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
380 fprintf( stderr, "%s: could not open database.\n",
382 exit( EXIT_FAILURE );
385 (void)slap_tool_update_ctxcsn_init();
389 /* tools default to "none" */
390 && slap_debug == LDAP_DEBUG_NONE
392 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
393 && S_ISREG(stat_buf.st_mode) ) {
394 enable_meter = !lutil_meter_open(
396 &lutil_meter_text_display,
397 &lutil_meter_linear_estimator,
403 if ( slap_tool_thread_max > 1 ) {
404 ldap_pvt_thread_mutex_init( &add_mutex );
405 ldap_pvt_thread_cond_init( &add_cond );
406 ldap_pvt_thread_create( &thr, 0, getrec_thr, NULL );
413 ldifrc = getrec( &erec );
415 if ( ldifrc == -2 && continuemode )
422 * Initialize text buffer
424 bvtext.bv_len = textlen;
425 bvtext.bv_val = textbuf;
426 bvtext.bv_val[0] = '\0';
428 id = be->be_entry_put( be, erec.e, &bvtext );
430 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
431 "(line=%d): %s\n", progname, erec.e->e_dn,
432 erec.lineno, bvtext.bv_val );
434 entry_free( erec.e );
435 if( continuemode ) continue;
439 fprintf( stderr, "added: \"%s\" (%08lx)\n",
440 erec.e->e_dn, (long) id );
443 fprintf( stderr, "added: \"%s\"\n",
447 entry_free( erec.e );
450 if ( slap_tool_thread_max > 1 ) {
453 ldap_pvt_thread_cond_signal( &add_cond );
454 ldap_pvt_thread_join( thr, NULL );
460 bvtext.bv_len = textlen;
461 bvtext.bv_val = textbuf;
462 bvtext.bv_val[0] = '\0';
464 if ( enable_meter ) {
465 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
466 lutil_meter_close( &meter );
469 if ( rc == EXIT_SUCCESS ) {
470 rc = slap_tool_update_ctxcsn( progname, sid, &bvtext );
476 if ( enable_meter ) {
477 fprintf( stderr, "Closing DB..." );
479 if( be->be_entry_close( be ) ) {
486 if ( enable_meter ) {
487 fprintf( stderr, "\n" );
491 if ( slap_tool_destroy())