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 );
106 slapadd( int argc, char **argv )
111 int rc = EXIT_SUCCESS;
114 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
115 size_t textlen = sizeof textbuf;
118 struct berval maxcsn;
126 ldap_pvt_thread_t put_tid;
128 slap_tool_init( progname, SLAPADD, argc, argv );
130 if( !be->be_entry_open ||
131 !be->be_entry_close ||
134 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
137 fprintf( stderr, "\t(dry) continuing...\n" );
140 exit( EXIT_FAILURE );
144 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
147 ldap_pvt_thread_initialize();
148 ldap_pvt_thread_cond_init( &put_cond1 );
149 ldap_pvt_thread_cond_init( &put_cond2 );
150 ldap_pvt_thread_mutex_init( &put_mutex1 );
151 ldap_pvt_thread_mutex_init( &put_mutex2 );
152 rc = ldap_pvt_thread_create( &put_tid, 0, do_put, NULL );
154 fprintf( stderr, "%s: could not create thread.\n",
156 exit( EXIT_FAILURE );
158 ldap_pvt_thread_mutex_lock( &put_mutex2 );
164 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
165 fprintf( stderr, "%s: could not open database.\n",
167 exit( EXIT_FAILURE );
170 if ( update_ctxcsn ) {
171 maxcsn.bv_val = maxcsnbuf;
175 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
176 Entry *e = str2entry2( buf, checkvals );
179 * Initialize text buffer
181 bvtext.bv_len = textlen;
182 bvtext.bv_val = textbuf;
183 bvtext.bv_val[0] = '\0';
186 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
189 if( continuemode ) continue;
193 /* make sure the DN is not empty */
194 if( !e->e_nname.bv_len ) {
195 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
196 progname, e->e_dn, lineno );
199 if( continuemode ) continue;
204 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
207 fprintf( stderr, "%s: line %d: "
208 "database (%s) not configured to hold \"%s\"\n",
210 be ? be->be_suffix[0].bv_val : "<none>",
212 fprintf( stderr, "%s: line %d: "
213 "database (%s) not configured to hold \"%s\"\n",
215 be ? be->be_nsuffix[0].bv_val : "<none>",
219 if( continuemode ) continue;
223 if( global_schemacheck ) {
224 Attribute *sc = attr_find( e->e_attrs,
225 slap_schema.si_ad_structuralObjectClass );
226 Attribute *oc = attr_find( e->e_attrs,
227 slap_schema.si_ad_objectClass );
230 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
231 progname, e->e_dn, lineno,
232 "no objectClass attribute");
235 if( continuemode ) continue;
240 struct berval vals[2];
242 rc = structural_class( oc->a_vals, vals,
243 NULL, &text, textbuf, textlen );
245 if( rc != LDAP_SUCCESS ) {
246 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
247 progname, e->e_dn, lineno, rc, text );
250 if( continuemode ) continue;
255 vals[1].bv_val = NULL;
257 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
258 vals, NULL /* FIXME */ );
262 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
264 if( rc != LDAP_SUCCESS ) {
265 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
266 progname, e->e_dn, lineno, rc, text );
269 if( continuemode ) continue;
274 if ( SLAP_LASTMOD(be) ) {
276 time_t now = slap_get_time();
277 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
278 struct berval vals[ 2 ];
280 struct berval name, timestamp;
282 struct berval nvals[ 2 ];
284 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
287 vals[1].bv_val = NULL;
290 nvals[1].bv_val = NULL;
293 lutil_gentime( timebuf, sizeof(timebuf), ltm );
295 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
298 timestamp.bv_val = timebuf;
299 timestamp.bv_len = strlen(timebuf);
301 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
302 BER_BVSTR( &name, SLAPD_ANONYMOUS );
305 name = be->be_rootdn;
306 nname = be->be_rootndn;
309 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
312 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
313 vals[0].bv_val = uuidbuf;
314 attr_merge_normalize_one( e,
315 slap_schema.si_ad_entryUUID, vals, NULL );
318 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
323 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
326 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
331 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
334 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
338 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
341 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
345 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
348 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
352 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
355 if ( update_ctxcsn ) {
356 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
357 if ( maxcsn.bv_len != 0 ) {
359 value_match( &match, slap_schema.si_ad_entryCSN,
360 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
361 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
362 &maxcsn, &attr->a_nvals[0], &text );
367 strcpy( maxcsn.bv_val, attr->a_nvals[0].bv_val );
368 maxcsn.bv_len = attr->a_nvals[0].bv_len;
374 ldap_pvt_thread_mutex_lock( &put_mutex1 );
377 ldap_pvt_thread_mutex_unlock( &put_mutex1 );
382 ldap_pvt_thread_cond_signal( &put_cond1 );
383 ldap_pvt_thread_mutex_unlock( &put_mutex1 );
384 /* Make sure writer wakes up */
385 ldap_pvt_thread_cond_wait( &put_cond2, &put_mutex2 );
390 id = be->be_entry_put( be, e, &bvtext );
392 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
393 "(line=%d): %s\n", progname, e->e_dn,
394 lineno, bvtext.bv_val );
397 if( continuemode ) continue;
404 fprintf( stderr, "added: \"%s\"\n",
407 fprintf( stderr, "added: \"%s\" (%08lx)\n",
408 e->e_dn, (long) id );
416 ldap_pvt_thread_mutex_unlock( &put_mutex2 );
417 ldap_pvt_thread_mutex_lock( &put_mutex1 );
418 /* Tell child thread to stop if it hasn't aborted */
420 put_rc = EXIT_FAILURE;
421 ldap_pvt_thread_cond_signal( &put_cond1 );
423 ldap_pvt_thread_mutex_unlock( &put_mutex1 );
424 ldap_pvt_thread_join( put_tid, NULL );
425 ldap_pvt_thread_mutex_destroy( &put_mutex2 );
426 ldap_pvt_thread_mutex_destroy( &put_mutex1 );
427 ldap_pvt_thread_cond_destroy( &put_cond2 );
428 ldap_pvt_thread_cond_destroy( &put_cond1 );
431 bvtext.bv_len = textlen;
432 bvtext.bv_val = textbuf;
433 bvtext.bv_val[0] = '\0';
435 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && maxcsn.bv_len ) {
436 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
437 if ( ctxcsn_id == NOID ) {
438 fprintf( stderr, "%s: context entry is missing\n", progname );
441 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
442 if ( ret == LDAP_SUCCESS ) {
443 attr = attr_find( ctxcsn_e->e_attrs,
444 slap_schema.si_ad_contextCSN );
445 value_match( &match, slap_schema.si_ad_entryCSN,
446 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
447 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
448 &maxcsn, &attr->a_nvals[0], &text );
450 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
451 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
452 attr->a_vals[0].bv_len = maxcsn.bv_len;
454 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
455 if( ctxcsn_id == NOID ) {
456 fprintf( stderr, "%s: could not modify ctxcsn\n",
459 } else if ( verbose ) {
460 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
461 ctxcsn_e->e_dn, (long) ctxcsn_id );
471 if( be->be_entry_close( be ) ) {