2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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>
39 #include "slapcommon.h"
41 static char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
42 static char maxcsnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
43 static const char *progname = "slapadd";
45 static ldap_pvt_thread_cond_t put_cond1;
46 static ldap_pvt_thread_cond_t put_cond2;
47 static ldap_pvt_thread_mutex_t put_mutex1;
48 static ldap_pvt_thread_mutex_t put_mutex2;
50 static struct berval bvtext;
51 static int put_lineno;
54 static int use_thread = 0; /*FIXME need a new switch for this */
56 static void *do_put(void *ptr)
62 ldap_pvt_thread_mutex_lock( &put_mutex1 );
64 ldap_pvt_thread_cond_wait( &put_cond1, &put_mutex1 );
68 ldap_pvt_thread_mutex_lock( &put_mutex2 );
69 ldap_pvt_thread_cond_signal( &put_cond2 );
70 ldap_pvt_thread_mutex_unlock( &put_mutex2 );
76 id = be->be_entry_put( be, e, &bvtext );
78 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
79 "(line=%d): %s\n", progname, e->e_dn,
80 lineno, bvtext.bv_val );
82 if ( continuemode ) continue;
83 put_rc = EXIT_FAILURE;
90 fprintf( stderr, "added: \"%s\"\n",
93 fprintf( stderr, "added: \"%s\" (%08lx)\n",
101 ldap_pvt_thread_mutex_unlock( &put_mutex1 );
105 slapadd( int argc, char **argv )
110 int rc = EXIT_SUCCESS;
113 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
114 size_t textlen = sizeof textbuf;
117 struct berval maxcsn;
125 ldap_pvt_thread_t put_tid;
127 slap_tool_init( progname, SLAPADD, argc, argv );
129 if( !be->be_entry_open ||
130 !be->be_entry_close ||
133 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
136 fprintf( stderr, "\t(dry) continuing...\n" );
139 exit( EXIT_FAILURE );
143 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
146 ldap_pvt_thread_initialize();
147 ldap_pvt_thread_cond_init( &put_cond1 );
148 ldap_pvt_thread_cond_init( &put_cond2 );
149 ldap_pvt_thread_mutex_init( &put_mutex1 );
150 ldap_pvt_thread_mutex_init( &put_mutex2 );
151 rc = ldap_pvt_thread_create( &put_tid, 0, do_put, NULL );
153 fprintf( stderr, "%s: could not create thread.\n",
155 exit( EXIT_FAILURE );
157 ldap_pvt_thread_mutex_lock( &put_mutex2 );
163 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
164 fprintf( stderr, "%s: could not open database.\n",
166 exit( EXIT_FAILURE );
169 if ( update_ctxcsn ) {
170 maxcsn.bv_val = maxcsnbuf;
174 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
175 Entry *e = str2entry2( buf, checkvals );
178 * Initialize text buffer
180 bvtext.bv_len = textlen;
181 bvtext.bv_val = textbuf;
182 bvtext.bv_val[0] = '\0';
185 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
188 if( continuemode ) continue;
192 /* make sure the DN is not empty */
193 if( !e->e_nname.bv_len ) {
194 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
195 progname, e->e_dn, lineno );
198 if( continuemode ) continue;
203 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
206 fprintf( stderr, "%s: line %d: "
207 "database (%s) not configured to hold \"%s\"\n",
209 be ? be->be_suffix[0].bv_val : "<none>",
211 fprintf( stderr, "%s: line %d: "
212 "database (%s) not configured to hold \"%s\"\n",
214 be ? be->be_nsuffix[0].bv_val : "<none>",
218 if( continuemode ) continue;
222 if( global_schemacheck ) {
223 Attribute *sc = attr_find( e->e_attrs,
224 slap_schema.si_ad_structuralObjectClass );
225 Attribute *oc = attr_find( e->e_attrs,
226 slap_schema.si_ad_objectClass );
229 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
230 progname, e->e_dn, lineno,
231 "no objectClass attribute");
234 if( continuemode ) continue;
239 struct berval vals[2];
241 rc = structural_class( oc->a_vals, vals,
242 NULL, &text, textbuf, textlen );
244 if( rc != LDAP_SUCCESS ) {
245 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
246 progname, e->e_dn, lineno, rc, text );
249 if( continuemode ) continue;
254 vals[1].bv_val = NULL;
256 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
257 vals, NULL /* FIXME */ );
261 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
263 if( rc != LDAP_SUCCESS ) {
264 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
265 progname, e->e_dn, lineno, rc, text );
268 if( continuemode ) continue;
273 if ( SLAP_LASTMOD(be) ) {
275 time_t now = slap_get_time();
276 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
277 struct berval vals[ 2 ];
279 struct berval name, timestamp;
281 struct berval nvals[ 2 ];
283 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
286 vals[1].bv_val = NULL;
289 nvals[1].bv_val = NULL;
292 lutil_gentime( timebuf, sizeof(timebuf), ltm );
294 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
297 timestamp.bv_val = timebuf;
298 timestamp.bv_len = strlen(timebuf);
300 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
301 BER_BVSTR( &name, SLAPD_ANONYMOUS );
304 name = be->be_rootdn;
305 nname = be->be_rootndn;
308 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
311 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
312 vals[0].bv_val = uuidbuf;
313 attr_merge_normalize_one( e,
314 slap_schema.si_ad_entryUUID, vals, NULL );
317 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
322 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
325 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
330 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
333 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
337 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
340 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
344 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
347 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
351 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
354 if ( update_ctxcsn ) {
355 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
356 if ( maxcsn.bv_len != 0 ) {
358 value_match( &match, slap_schema.si_ad_entryCSN,
359 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
360 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
361 &maxcsn, &attr->a_nvals[0], &text );
366 strcpy( maxcsn.bv_val, attr->a_nvals[0].bv_val );
367 maxcsn.bv_len = attr->a_nvals[0].bv_len;
373 ldap_pvt_thread_mutex_lock( &put_mutex1 );
376 ldap_pvt_thread_mutex_unlock( &put_mutex1 );
381 ldap_pvt_thread_cond_signal( &put_cond1 );
382 ldap_pvt_thread_mutex_unlock( &put_mutex1 );
383 /* Make sure writer wakes up */
384 ldap_pvt_thread_cond_wait( &put_cond2, &put_mutex2 );
389 id = be->be_entry_put( be, e, &bvtext );
391 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
392 "(line=%d): %s\n", progname, e->e_dn,
393 lineno, bvtext.bv_val );
396 if( continuemode ) continue;
403 fprintf( stderr, "added: \"%s\"\n",
406 fprintf( stderr, "added: \"%s\" (%08lx)\n",
407 e->e_dn, (long) id );
415 ldap_pvt_thread_mutex_unlock( &put_mutex2 );
416 ldap_pvt_thread_mutex_lock( &put_mutex1 );
417 /* Tell child thread to stop if it hasn't aborted */
419 put_rc = EXIT_FAILURE;
420 ldap_pvt_thread_cond_signal( &put_cond1 );
422 ldap_pvt_thread_mutex_unlock( &put_mutex1 );
423 ldap_pvt_thread_join( put_tid, NULL );
424 ldap_pvt_thread_mutex_destroy( &put_mutex2 );
425 ldap_pvt_thread_mutex_destroy( &put_mutex1 );
426 ldap_pvt_thread_cond_destroy( &put_cond2 );
427 ldap_pvt_thread_cond_destroy( &put_cond1 );
430 bvtext.bv_len = textlen;
431 bvtext.bv_val = textbuf;
432 bvtext.bv_val[0] = '\0';
434 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && maxcsn.bv_len ) {
435 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
436 if ( ctxcsn_id == NOID ) {
437 fprintf( stderr, "%s: context entry is missing\n", progname );
440 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
441 if ( ret == LDAP_SUCCESS ) {
442 attr = attr_find( ctxcsn_e->e_attrs,
443 slap_schema.si_ad_contextCSN );
444 value_match( &match, slap_schema.si_ad_entryCSN,
445 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
446 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
447 &maxcsn, &attr->a_nvals[0], &text );
449 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
450 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
451 attr->a_vals[0].bv_len = maxcsn.bv_len;
453 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
454 if( ctxcsn_id == NOID ) {
455 fprintf( stderr, "%s: could not modify ctxcsn\n",
458 } else if ( verbose ) {
459 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
460 ctxcsn_e->e_dn, (long) ctxcsn_id );
470 if( be->be_entry_close( be ) ) {