2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2010 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;
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;
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;
130 /* tools default to "none" */
131 && slap_debug == LDAP_DEBUG_NONE
133 && !fstat ( fileno ( ldiffp->fp ), &stat_buf )
134 && S_ISREG(stat_buf.st_mode) ) {
135 enable_meter = !lutil_meter_open(
137 &lutil_meter_text_display,
138 &lutil_meter_linear_estimator,
144 /* nextline is the line number of the end of the current entry */
145 for( lineno=1; ( ldifrc = ldif_read_record( ldiffp, &nextline, &buf, &lmax )) > 0;
151 if ( lineno < jumpline )
154 e = str2entry2( buf, checkvals );
157 lutil_meter_update( &meter,
162 * Initialize text buffer
164 bvtext.bv_len = textlen;
165 bvtext.bv_val = textbuf;
166 bvtext.bv_val[0] = '\0';
169 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
172 if( continuemode ) continue;
176 /* make sure the DN is not empty */
177 if( BER_BVISEMPTY( &e->e_nname ) &&
178 !BER_BVISEMPTY( be->be_nsuffix ))
180 fprintf( stderr, "%s: line %d: "
181 "cannot add entry with empty dn=\"%s\"",
182 progname, lineno, e->e_dn );
183 bd = select_backend( &e->e_nname, nosubordinates );
187 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
188 if ( bdtmp == bd ) break;
192 assert( bdtmp != NULL );
194 fprintf( stderr, "; did you mean to use database #%d (%s)?",
196 bd->be_suffix[0].bv_val );
199 fprintf( stderr, "\n" );
202 if( continuemode ) continue;
207 bd = select_backend( &e->e_nname, nosubordinates );
209 fprintf( stderr, "%s: line %d: "
210 "database #%d (%s) not configured to hold \"%s\"",
213 be->be_suffix[0].bv_val,
218 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
219 if ( bdtmp == bd ) break;
223 assert( bdtmp != NULL );
225 fprintf( stderr, "; did you mean to use database #%d (%s)?",
227 bd->be_suffix[0].bv_val );
230 fprintf( stderr, "; no database configured for that naming context" );
232 fprintf( stderr, "\n" );
235 if( continuemode ) continue;
240 Attribute *oc = attr_find( e->e_attrs,
241 slap_schema.si_ad_objectClass );
244 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
245 progname, e->e_dn, lineno,
246 "no objectClass attribute");
249 if( continuemode ) continue;
256 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
257 rc = entry_schema_check( op, e, NULL, manage, 1, NULL,
258 &text, textbuf, textlen );
260 if( rc != LDAP_SUCCESS ) {
261 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
262 progname, e->e_dn, lineno, rc, text );
265 if( continuemode ) continue;
272 if ( SLAP_LASTMOD(be) ) {
273 time_t now = slap_get_time();
274 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
275 struct berval vals[ 2 ];
277 struct berval name, timestamp;
279 struct berval nvals[ 2 ];
281 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
287 GOT_ALL = (GOT_CSN|GOT_UUID)
291 vals[1].bv_val = NULL;
294 nvals[1].bv_val = NULL;
296 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
299 timestamp.bv_val = timebuf;
300 timestamp.bv_len = sizeof(timebuf);
302 slap_timestamp( &now, ×tamp );
304 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
305 BER_BVSTR( &name, SLAPD_ANONYMOUS );
308 name = be->be_rootdn;
309 nname = be->be_rootndn;
312 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
316 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
317 vals[0].bv_val = uuidbuf;
318 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
321 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
326 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
329 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
333 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
336 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
341 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
344 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
349 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
352 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
356 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
359 if ( SLAP_SINGLE_SHADOW(be) && got != GOT_ALL ) {
360 char buf[SLAP_TEXT_BUFLEN];
362 snprintf( buf, sizeof(buf),
364 ( !(got & GOT_UUID) ? slap_schema.si_ad_entryUUID->ad_cname.bv_val : "" ),
365 ( !(got & GOT_CSN) ? "," : "" ),
366 ( !(got & GOT_CSN) ? slap_schema.si_ad_entryCSN->ad_cname.bv_val : "" ) );
368 Debug( LDAP_DEBUG_ANY, "%s: warning, missing attrs %s from entry dn=\"%s\"\n",
369 progname, buf, e->e_name.bv_val );
372 if ( update_ctxcsn ) {
375 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
376 assert( attr != NULL );
378 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] );
380 Debug( LDAP_DEBUG_ANY, "%s: could not "
381 "extract SID from entryCSN=%s, entry dn=\"%s\"\n",
382 progname, attr->a_nvals[ 0 ].bv_val, e->e_name.bv_val );
385 assert( rc_sid <= SLAP_SYNC_SID_MAX );
387 sid = (unsigned)rc_sid;
388 if ( maxcsn[ sid ].bv_len != 0 ) {
390 value_match( &match, slap_schema.si_ad_entryCSN,
391 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
392 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
393 &maxcsn[ sid ], &attr->a_nvals[0], &text );
398 strcpy( maxcsn[ sid ].bv_val, attr->a_nvals[0].bv_val );
399 maxcsn[ sid ].bv_len = attr->a_nvals[0].bv_len;
406 id = be->be_entry_put( be, e, &bvtext );
408 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
409 "(line=%d): %s\n", progname, e->e_dn,
410 lineno, bvtext.bv_val );
413 if( continuemode ) continue;
417 fprintf( stderr, "added: \"%s\" (%08lx)\n",
418 e->e_dn, (long) id );
421 fprintf( stderr, "added: \"%s\"\n",
431 bvtext.bv_len = textlen;
432 bvtext.bv_val = textbuf;
433 bvtext.bv_val[0] = '\0';
435 if ( enable_meter ) {
436 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
437 lutil_meter_close( &meter );
440 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) {
442 if ( SLAP_SYNC_SUBENTRY( be )) {
443 build_new_dn( &ctxdn, &be->be_nsuffix[0],
444 (struct berval *)&slap_ldapsync_cn_bv, NULL );
446 ctxdn = be->be_nsuffix[0];
448 ctxcsn_id = be->be_dn2id_get( be, &ctxdn );
449 if ( ctxcsn_id == NOID ) {
450 if ( SLAP_SYNC_SUBENTRY( be )) {
451 ctxcsn_e = slap_create_context_csn_entry( be, NULL );
452 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
453 if ( maxcsn[ sid ].bv_len ) {
454 attr_merge_one( ctxcsn_e, slap_schema.si_ad_contextCSN,
455 &maxcsn[ sid ], NULL );
458 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
459 if ( ctxcsn_id == NOID ) {
460 fprintf( stderr, "%s: couldn't create context entry\n", progname );
464 fprintf( stderr, "%s: context entry is missing\n", progname );
468 ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
469 if ( ctxcsn_e != NULL ) {
470 Entry *e = entry_dup( ctxcsn_e );
472 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
478 for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) {
481 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] );
483 Debug( LDAP_DEBUG_ANY,
484 "%s: unable to extract SID "
485 "from #%d contextCSN=%s\n",
487 attr->a_nvals[ i ].bv_val );
491 assert( rc_sid <= SLAP_SYNC_SID_MAX );
493 sid = (unsigned)rc_sid;
495 if ( maxcsn[ sid ].bv_len == 0 ) {
499 value_match( &match, slap_schema.si_ad_entryCSN,
500 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
501 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
502 &maxcsn[ sid ], &attr->a_nvals[i], &text );
508 AC_MEMCPY( maxcsn[ sid ].bv_val,
509 attr->a_nvals[ i ].bv_val,
510 attr->a_nvals[ i ].bv_len );
511 maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0';
512 maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len;
517 if ( attr->a_nvals != attr->a_vals ) {
518 ber_bvarray_free( attr->a_nvals );
520 attr->a_nvals = NULL;
521 ber_bvarray_free( attr->a_vals );
530 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
531 if ( maxcsn[ sid ].bv_len ) {
532 attr_merge_one( e, slap_schema.si_ad_contextCSN,
533 &maxcsn[ sid], NULL );
537 ctxcsn_id = be->be_entry_modify( be, e, &bvtext );
538 if( ctxcsn_id == NOID ) {
539 fprintf( stderr, "%s: could not modify ctxcsn\n",
542 } else if ( verbose ) {
543 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
544 e->e_dn, (long) ctxcsn_id );
555 if ( enable_meter ) {
556 fprintf( stderr, "Closing DB..." );
558 if( be->be_entry_close( be ) ) {
565 if ( enable_meter ) {
566 fprintf( stderr, "\n" );
570 if ( slap_tool_destroy())