2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2009 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_LUTIL_CSNSTR_BUFSIZE ];
44 static char maxcsnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE * ( SLAP_SYNC_SID_MAX + 1 ) ];
47 slapadd( int argc, char **argv )
51 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
52 size_t textlen = sizeof textbuf;
53 const char *progname = "slapadd";
56 struct berval maxcsn[ SLAP_SYNC_SID_MAX + 1 ];
62 OperationBuffer opbuf;
69 int rc = EXIT_SUCCESS;
79 if ( isatty (2) ) enable_meter = 1;
80 slap_tool_init( progname, SLAPADD, argc, argv );
82 memset( &opbuf, 0, sizeof(opbuf) );
84 op->o_hdr = &opbuf.ob_hdr;
86 if( !be->be_entry_open ||
87 !be->be_entry_close ||
92 !be->be_entry_modify)) )
94 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
97 fprintf( stderr, "\t(dry) continuing...\n" );
100 exit( EXIT_FAILURE );
104 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
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);
114 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
115 fprintf( stderr, "%s: could not open database.\n",
117 exit( EXIT_FAILURE );
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_LUTIL_CSNSTR_BUFSIZE;
124 maxcsn[ sid ].bv_len = 0;
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; ldif_read_record( ldiffp, &nextline, &buf, &lmax );
142 lineno=nextline+1 ) {
145 if ( lineno < jumpline )
148 e = str2entry2( buf, checkvals );
151 lutil_meter_update( &meter,
156 * Initialize text buffer
158 bvtext.bv_len = textlen;
159 bvtext.bv_val = textbuf;
160 bvtext.bv_val[0] = '\0';
163 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
166 if( continuemode ) continue;
170 /* make sure the DN is not empty */
171 if( BER_BVISEMPTY( &e->e_nname ) &&
172 !BER_BVISEMPTY( be->be_nsuffix )) {
173 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
174 progname, e->e_dn, lineno );
177 if( continuemode ) continue;
182 if( select_backend( &e->e_nname, nosubordinates )
185 fprintf( stderr, "%s: line %d: "
186 "database (%s) not configured to hold \"%s\"\n",
188 be ? be->be_suffix[0].bv_val : "<none>",
190 fprintf( stderr, "%s: line %d: "
191 "database (%s) not configured to hold \"%s\"\n",
193 be ? be->be_nsuffix[0].bv_val : "<none>",
197 if( continuemode ) continue;
202 Attribute *oc = attr_find( e->e_attrs,
203 slap_schema.si_ad_objectClass );
206 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
207 progname, e->e_dn, lineno,
208 "no objectClass attribute");
211 if( continuemode ) continue;
218 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
219 rc = entry_schema_check( op, e, NULL, manage, 1, NULL,
220 &text, textbuf, textlen );
222 if( rc != LDAP_SUCCESS ) {
223 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
224 progname, e->e_dn, lineno, rc, text );
227 if( continuemode ) continue;
234 if ( SLAP_LASTMOD(be) ) {
235 time_t now = slap_get_time();
236 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
237 struct berval vals[ 2 ];
239 struct berval name, timestamp;
241 struct berval nvals[ 2 ];
243 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
246 vals[1].bv_val = NULL;
249 nvals[1].bv_val = NULL;
251 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
254 timestamp.bv_val = timebuf;
255 timestamp.bv_len = sizeof(timebuf);
257 slap_timestamp( &now, ×tamp );
259 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
260 BER_BVSTR( &name, SLAPD_ANONYMOUS );
263 name = be->be_rootdn;
264 nname = be->be_rootndn;
267 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
270 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
271 vals[0].bv_val = uuidbuf;
272 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
275 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
280 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
283 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
287 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
290 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
294 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
297 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
302 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
305 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
309 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
312 if ( update_ctxcsn ) {
315 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
316 assert( attr != NULL );
318 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] );
320 Debug( LDAP_DEBUG_ANY, "%s: could not "
321 "extract SID from entryCSN=%s\n",
322 progname, attr->a_nvals[ 0 ].bv_val, 0 );
325 assert( rc_sid <= SLAP_SYNC_SID_MAX );
327 sid = (unsigned)rc_sid;
328 if ( maxcsn[ sid ].bv_len != 0 ) {
330 value_match( &match, slap_schema.si_ad_entryCSN,
331 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
332 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
333 &maxcsn[ sid ], &attr->a_nvals[0], &text );
338 strcpy( maxcsn[ sid ].bv_val, attr->a_nvals[0].bv_val );
339 maxcsn[ sid ].bv_len = attr->a_nvals[0].bv_len;
346 id = be->be_entry_put( be, e, &bvtext );
348 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
349 "(line=%d): %s\n", progname, e->e_dn,
350 lineno, bvtext.bv_val );
353 if( continuemode ) continue;
357 fprintf( stderr, "added: \"%s\" (%08lx)\n",
358 e->e_dn, (long) id );
361 fprintf( stderr, "added: \"%s\"\n",
368 bvtext.bv_len = textlen;
369 bvtext.bv_val = textbuf;
370 bvtext.bv_val[0] = '\0';
372 if ( enable_meter ) {
373 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
374 lutil_meter_close( &meter );
377 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) {
378 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
379 if ( ctxcsn_id == NOID ) {
380 fprintf( stderr, "%s: context entry is missing\n", progname );
383 ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
384 if ( ctxcsn_e != NULL ) {
385 Entry *e = entry_dup( ctxcsn_e );
387 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
393 for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) {
396 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] );
398 Debug( LDAP_DEBUG_ANY,
399 "%s: unable to extract SID "
400 "from #%d contextCSN=%s\n",
402 attr->a_nvals[ i ].bv_val );
406 assert( rc_sid <= SLAP_SYNC_SID_MAX );
408 sid = (unsigned)rc_sid;
410 if ( maxcsn[ sid ].bv_len == 0 ) {
414 value_match( &match, slap_schema.si_ad_entryCSN,
415 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
416 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
417 &maxcsn[ sid ], &attr->a_nvals[i], &text );
423 AC_MEMCPY( maxcsn[ sid ].bv_val,
424 attr->a_nvals[ i ].bv_val,
425 attr->a_nvals[ i ].bv_len );
426 maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0';
427 maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len;
432 if ( attr->a_nvals != attr->a_vals ) {
433 ber_bvarray_free( attr->a_nvals );
435 attr->a_nvals = NULL;
436 ber_bvarray_free( attr->a_vals );
445 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
446 if ( maxcsn[ sid ].bv_len ) {
447 attr_merge_one( e, slap_schema.si_ad_contextCSN,
448 &maxcsn[ sid], NULL );
452 ctxcsn_id = be->be_entry_modify( be, e, &bvtext );
453 if( ctxcsn_id == NOID ) {
454 fprintf( stderr, "%s: could not modify ctxcsn\n",
457 } else if ( verbose ) {
458 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
459 e->e_dn, (long) ctxcsn_id );
470 if ( enable_meter ) {
471 fprintf( stderr, "Closing DB..." );
473 if( be->be_entry_close( be ) ) {
480 if ( enable_meter ) {
481 fprintf( stderr, "\n" );
485 if ( slap_tool_destroy())