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 ];
46 slapadd( int argc, char **argv )
50 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
51 size_t textlen = sizeof textbuf;
52 const char *progname = "slapadd";
55 unsigned long sid = SLAP_SYNC_SID_MAX + 1;
58 OperationBuffer opbuf;
62 int lineno, nextline, ldifrc;
64 int rc = EXIT_SUCCESS;
73 if ( isatty (2) ) enable_meter = 1;
74 slap_tool_init( progname, SLAPADD, argc, argv );
76 memset( &opbuf, 0, sizeof(opbuf) );
78 op->o_hdr = &opbuf.ob_hdr;
80 if( !be->be_entry_open ||
81 !be->be_entry_close ||
86 !be->be_entry_modify)) )
88 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
91 fprintf( stderr, "\t(dry) continuing...\n" );
98 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
100 /* do not check values in quick mode */
101 if ( slapMode & SLAP_TOOL_QUICK ) {
102 if ( slapMode & SLAP_TOOL_VALUE_CHECK ) {
103 fprintf( stderr, "%s: value-check incompatible with quick mode; disabled.\n", progname );
104 slapMode &= ~SLAP_TOOL_VALUE_CHECK;
111 /* enforce schema checking unless not disabled */
112 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
113 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
116 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
117 fprintf( stderr, "%s: could not open database.\n",
119 exit( EXIT_FAILURE );
122 (void)slap_tool_update_ctxcsn_init();
126 /* tools default to "none" */
127 && slap_debug == LDAP_DEBUG_NONE
129 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
130 && S_ISREG(stat_buf.st_mode) ) {
131 enable_meter = !lutil_meter_open(
133 &lutil_meter_text_display,
134 &lutil_meter_linear_estimator,
140 /* nextline is the line number of the end of the current entry */
141 for( lineno=1; ( ldifrc = ldif_read_record( ldiffp, &nextline, &buf, &lmax )) > 0;
147 if ( lineno < jumpline )
150 e = str2entry2( buf, checkvals );
153 lutil_meter_update( &meter,
158 * Initialize text buffer
160 bvtext.bv_len = textlen;
161 bvtext.bv_val = textbuf;
162 bvtext.bv_val[0] = '\0';
165 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
168 if( continuemode ) continue;
172 /* make sure the DN is not empty */
173 if( BER_BVISEMPTY( &e->e_nname ) &&
174 !BER_BVISEMPTY( be->be_nsuffix ))
176 fprintf( stderr, "%s: line %d: "
177 "cannot add entry with empty dn=\"%s\"",
178 progname, lineno, e->e_dn );
179 bd = select_backend( &e->e_nname, nosubordinates );
183 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
184 if ( bdtmp == bd ) break;
188 assert( bdtmp != NULL );
190 fprintf( stderr, "; did you mean to use database #%d (%s)?",
192 bd->be_suffix[0].bv_val );
195 fprintf( stderr, "\n" );
198 if( continuemode ) continue;
203 bd = select_backend( &e->e_nname, nosubordinates );
205 fprintf( stderr, "%s: line %d: "
206 "database #%d (%s) not configured to hold \"%s\"",
209 be->be_suffix[0].bv_val,
214 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
215 if ( bdtmp == bd ) break;
219 assert( bdtmp != NULL );
221 fprintf( stderr, "; did you mean to use database #%d (%s)?",
223 bd->be_suffix[0].bv_val );
226 fprintf( stderr, "; no database configured for that naming context" );
228 fprintf( stderr, "\n" );
231 if( continuemode ) continue;
235 rc = slap_tool_entry_check( progname, op, e, lineno, &text, textbuf, textlen );
236 if ( rc != LDAP_SUCCESS ) {
239 if( continuemode ) continue;
243 if ( SLAP_LASTMOD(be) ) {
244 time_t now = slap_get_time();
245 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
246 struct berval vals[ 2 ];
248 struct berval name, timestamp;
250 struct berval nvals[ 2 ];
252 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
258 GOT_ALL = (GOT_CSN|GOT_UUID)
262 vals[1].bv_val = NULL;
265 nvals[1].bv_val = NULL;
267 csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
270 timestamp.bv_val = timebuf;
271 timestamp.bv_len = sizeof(timebuf);
273 slap_timestamp( &now, ×tamp );
275 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
276 BER_BVSTR( &name, SLAPD_ANONYMOUS );
279 name = be->be_rootdn;
280 nname = be->be_rootndn;
283 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
287 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
288 vals[0].bv_val = uuidbuf;
289 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
292 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
297 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
300 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
304 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
307 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
312 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
315 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
320 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
323 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
327 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
330 if ( SLAP_SINGLE_SHADOW(be) && got != GOT_ALL ) {
331 char buf[SLAP_TEXT_BUFLEN];
333 snprintf( buf, sizeof(buf),
335 ( !(got & GOT_UUID) ? slap_schema.si_ad_entryUUID->ad_cname.bv_val : "" ),
336 ( !(got & GOT_CSN) ? "," : "" ),
337 ( !(got & GOT_CSN) ? slap_schema.si_ad_entryCSN->ad_cname.bv_val : "" ) );
339 Debug( LDAP_DEBUG_ANY, "%s: warning, missing attrs %s from entry dn=\"%s\"\n",
340 progname, buf, e->e_name.bv_val );
343 sid = slap_tool_update_ctxcsn_check( progname, e );
347 id = be->be_entry_put( be, e, &bvtext );
349 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
350 "(line=%d): %s\n", progname, e->e_dn,
351 lineno, bvtext.bv_val );
354 if( continuemode ) continue;
358 fprintf( stderr, "added: \"%s\" (%08lx)\n",
359 e->e_dn, (long) id );
362 fprintf( stderr, "added: \"%s\"\n",
372 bvtext.bv_len = textlen;
373 bvtext.bv_val = textbuf;
374 bvtext.bv_val[0] = '\0';
376 if ( enable_meter ) {
377 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
378 lutil_meter_close( &meter );
381 if ( rc == EXIT_SUCCESS ) {
382 rc = slap_tool_update_ctxcsn( progname, sid, &bvtext );
388 if ( enable_meter ) {
389 fprintf( stderr, "Closing DB..." );
391 if( be->be_entry_close( be ) ) {
398 if ( enable_meter ) {
399 fprintf( stderr, "\n" );
403 if ( slap_tool_destroy())