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 ];
44 static char maxcsnbuf[ LDAP_PVT_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;
67 int lineno, nextline, ldifrc;
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;
106 /* do not check values in quick mode */
107 if ( slapMode & SLAP_TOOL_QUICK ) {
108 if ( slapMode & SLAP_TOOL_VALUE_CHECK ) {
109 fprintf( stderr, "%s: value-check incompatible with quick mode; disabled.\n", progname );
110 slapMode &= ~SLAP_TOOL_VALUE_CHECK;
117 /* enforce schema checking unless not disabled */
118 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
119 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
122 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
123 fprintf( stderr, "%s: could not open database.\n",
125 exit( EXIT_FAILURE );
128 if ( update_ctxcsn ) {
129 maxcsn[ 0 ].bv_val = maxcsnbuf;
130 for ( sid = 1; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
131 maxcsn[ sid ].bv_val = maxcsn[ sid - 1 ].bv_val + LDAP_PVT_CSNSTR_BUFSIZE;
132 maxcsn[ sid ].bv_len = 0;
138 /* tools default to "none" */
139 && slap_debug == LDAP_DEBUG_NONE
141 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
142 && S_ISREG(stat_buf.st_mode) ) {
143 enable_meter = !lutil_meter_open(
145 &lutil_meter_text_display,
146 &lutil_meter_linear_estimator,
152 /* nextline is the line number of the end of the current entry */
153 for( lineno=1; ( ldifrc = ldif_read_record( ldiffp, &nextline, &buf, &lmax )) > 0;
159 if ( lineno < jumpline )
162 e = str2entry2( buf, checkvals );
165 lutil_meter_update( &meter,
170 * Initialize text buffer
172 bvtext.bv_len = textlen;
173 bvtext.bv_val = textbuf;
174 bvtext.bv_val[0] = '\0';
177 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
180 if( continuemode ) continue;
184 /* make sure the DN is not empty */
185 if( BER_BVISEMPTY( &e->e_nname ) &&
186 !BER_BVISEMPTY( be->be_nsuffix ))
188 fprintf( stderr, "%s: line %d: "
189 "cannot add entry with empty dn=\"%s\"",
190 progname, lineno, e->e_dn );
191 bd = select_backend( &e->e_nname, nosubordinates );
195 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
196 if ( bdtmp == bd ) break;
200 assert( bdtmp != NULL );
202 fprintf( stderr, "; did you mean to use database #%d (%s)?",
204 bd->be_suffix[0].bv_val );
207 fprintf( stderr, "\n" );
210 if( continuemode ) continue;
215 bd = select_backend( &e->e_nname, nosubordinates );
217 fprintf( stderr, "%s: line %d: "
218 "database #%d (%s) not configured to hold \"%s\"",
221 be->be_suffix[0].bv_val,
226 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
227 if ( bdtmp == bd ) break;
231 assert( bdtmp != NULL );
233 fprintf( stderr, "; did you mean to use database #%d (%s)?",
235 bd->be_suffix[0].bv_val );
238 fprintf( stderr, "; no database configured for that naming context" );
240 fprintf( stderr, "\n" );
243 if( continuemode ) continue;
248 Attribute *oc = attr_find( e->e_attrs,
249 slap_schema.si_ad_objectClass );
252 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
253 progname, e->e_dn, lineno,
254 "no objectClass attribute");
257 if( continuemode ) continue;
264 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
265 rc = entry_schema_check( op, e, NULL, manage, 1, NULL,
266 &text, textbuf, textlen );
268 if( rc != LDAP_SUCCESS ) {
269 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
270 progname, e->e_dn, lineno, rc, text );
273 if( continuemode ) continue;
279 if ( (slapMode & SLAP_TOOL_VALUE_CHECK) != 0) {
280 Modifications *ml = NULL;
282 if ( slap_entry2mods( e, &ml, &text, textbuf, textlen )
285 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
286 progname, e->e_dn, lineno, rc, text );
289 if( continuemode ) continue;
294 rc = slap_mods_check( op, ml, &text, textbuf, textlen, NULL );
295 slap_mods_free( ml, 1 );
296 if ( rc != LDAP_SUCCESS ) {
297 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
298 progname, e->e_dn, lineno, rc, text );
301 if( continuemode ) continue;
308 if ( SLAP_LASTMOD(be) ) {
309 time_t now = slap_get_time();
310 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
311 struct berval vals[ 2 ];
313 struct berval name, timestamp;
315 struct berval nvals[ 2 ];
317 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
323 GOT_ALL = (GOT_CSN|GOT_UUID)
327 vals[1].bv_val = NULL;
330 nvals[1].bv_val = NULL;
332 csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
335 timestamp.bv_val = timebuf;
336 timestamp.bv_len = sizeof(timebuf);
338 slap_timestamp( &now, ×tamp );
340 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
341 BER_BVSTR( &name, SLAPD_ANONYMOUS );
344 name = be->be_rootdn;
345 nname = be->be_rootndn;
348 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
352 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
353 vals[0].bv_val = uuidbuf;
354 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
357 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
362 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
365 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
369 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
372 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
377 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
380 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
385 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
388 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
392 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
395 if ( SLAP_SINGLE_SHADOW(be) && got != GOT_ALL ) {
396 char buf[SLAP_TEXT_BUFLEN];
398 snprintf( buf, sizeof(buf),
400 ( !(got & GOT_UUID) ? slap_schema.si_ad_entryUUID->ad_cname.bv_val : "" ),
401 ( !(got & GOT_CSN) ? "," : "" ),
402 ( !(got & GOT_CSN) ? slap_schema.si_ad_entryCSN->ad_cname.bv_val : "" ) );
404 Debug( LDAP_DEBUG_ANY, "%s: warning, missing attrs %s from entry dn=\"%s\"\n",
405 progname, buf, e->e_name.bv_val );
408 if ( update_ctxcsn ) {
411 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
412 assert( attr != NULL );
414 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] );
416 Debug( LDAP_DEBUG_ANY, "%s: could not "
417 "extract SID from entryCSN=%s, entry dn=\"%s\"\n",
418 progname, attr->a_nvals[ 0 ].bv_val, e->e_name.bv_val );
421 assert( rc_sid <= SLAP_SYNC_SID_MAX );
423 sid = (unsigned)rc_sid;
424 if ( maxcsn[ sid ].bv_len != 0 ) {
426 value_match( &match, slap_schema.si_ad_entryCSN,
427 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
428 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
429 &maxcsn[ sid ], &attr->a_nvals[0], &text );
434 strcpy( maxcsn[ sid ].bv_val, attr->a_nvals[0].bv_val );
435 maxcsn[ sid ].bv_len = attr->a_nvals[0].bv_len;
442 id = be->be_entry_put( be, e, &bvtext );
444 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
445 "(line=%d): %s\n", progname, e->e_dn,
446 lineno, bvtext.bv_val );
449 if( continuemode ) continue;
453 fprintf( stderr, "added: \"%s\" (%08lx)\n",
454 e->e_dn, (long) id );
457 fprintf( stderr, "added: \"%s\"\n",
467 bvtext.bv_len = textlen;
468 bvtext.bv_val = textbuf;
469 bvtext.bv_val[0] = '\0';
471 if ( enable_meter ) {
472 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
473 lutil_meter_close( &meter );
476 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) {
478 if ( SLAP_SYNC_SUBENTRY( be )) {
479 build_new_dn( &ctxdn, &be->be_nsuffix[0],
480 (struct berval *)&slap_ldapsync_cn_bv, NULL );
482 ctxdn = be->be_nsuffix[0];
484 ctxcsn_id = be->be_dn2id_get( be, &ctxdn );
485 if ( ctxcsn_id == NOID ) {
486 if ( SLAP_SYNC_SUBENTRY( be )) {
487 ctxcsn_e = slap_create_context_csn_entry( be, NULL );
488 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
489 if ( maxcsn[ sid ].bv_len ) {
490 attr_merge_one( ctxcsn_e, slap_schema.si_ad_contextCSN,
491 &maxcsn[ sid ], NULL );
494 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
495 if ( ctxcsn_id == NOID ) {
496 fprintf( stderr, "%s: couldn't create context entry\n", progname );
500 fprintf( stderr, "%s: context entry is missing\n", progname );
504 ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
505 if ( ctxcsn_e != NULL ) {
506 Entry *e = entry_dup( ctxcsn_e );
508 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
514 for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) {
517 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] );
519 Debug( LDAP_DEBUG_ANY,
520 "%s: unable to extract SID "
521 "from #%d contextCSN=%s\n",
523 attr->a_nvals[ i ].bv_val );
527 assert( rc_sid <= SLAP_SYNC_SID_MAX );
529 sid = (unsigned)rc_sid;
531 if ( maxcsn[ sid ].bv_len == 0 ) {
535 value_match( &match, slap_schema.si_ad_entryCSN,
536 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
537 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
538 &maxcsn[ sid ], &attr->a_nvals[i], &text );
544 AC_MEMCPY( maxcsn[ sid ].bv_val,
545 attr->a_nvals[ i ].bv_val,
546 attr->a_nvals[ i ].bv_len );
547 maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0';
548 maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len;
553 if ( attr->a_nvals != attr->a_vals ) {
554 ber_bvarray_free( attr->a_nvals );
556 attr->a_nvals = NULL;
557 ber_bvarray_free( attr->a_vals );
566 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
567 if ( maxcsn[ sid ].bv_len ) {
568 attr_merge_one( e, slap_schema.si_ad_contextCSN,
569 &maxcsn[ sid], NULL );
573 ctxcsn_id = be->be_entry_modify( be, e, &bvtext );
574 if( ctxcsn_id == NOID ) {
575 fprintf( stderr, "%s: could not modify ctxcsn\n",
578 } else if ( verbose ) {
579 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
580 e->e_dn, (long) ctxcsn_id );
591 if ( enable_meter ) {
592 fprintf( stderr, "Closing DB..." );
594 if( be->be_entry_close( be ) ) {
601 if ( enable_meter ) {
602 fprintf( stderr, "\n" );
606 if ( slap_tool_destroy())