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_cond;
46 static ldap_pvt_thread_mutex_t put_mutex;
48 static struct berval bvtext;
49 static int put_lineno;
52 static int use_thread = 1; /*FIXME need a new switch for this */
54 static void *do_put(void *ptr)
60 ldap_pvt_thread_mutex_lock( &put_mutex );
62 ldap_pvt_thread_cond_wait( &put_cond, &put_mutex );
71 id = be->be_entry_put( be, e, &bvtext );
73 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
74 "(line=%d): %s\n", progname, e->e_dn,
75 lineno, bvtext.bv_val );
77 if ( continuemode ) continue;
78 put_rc = EXIT_FAILURE;
85 fprintf( stderr, "added: \"%s\"\n",
88 fprintf( stderr, "added: \"%s\" (%08lx)\n",
96 ldap_pvt_thread_mutex_unlock( &put_mutex );
100 slapadd( int argc, char **argv )
105 int rc = EXIT_SUCCESS;
108 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
109 size_t textlen = sizeof textbuf;
112 struct berval maxcsn;
120 ldap_pvt_thread_t put_tid;
122 slap_tool_init( progname, SLAPADD, argc, argv );
124 if( !be->be_entry_open ||
125 !be->be_entry_close ||
128 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
131 fprintf( stderr, "\t(dry) continuing...\n" );
134 exit( EXIT_FAILURE );
139 ldap_pvt_thread_initialize();
140 ldap_pvt_thread_cond_init( &put_cond );
141 ldap_pvt_thread_mutex_init( &put_mutex );
142 rc = ldap_pvt_thread_create( &put_tid, 0, do_put, NULL );
144 fprintf( stderr, "%s: could not create thread.\n",
146 exit( EXIT_FAILURE );
153 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
154 fprintf( stderr, "%s: could not open database.\n",
156 exit( EXIT_FAILURE );
159 if ( update_ctxcsn ) {
160 maxcsn.bv_val = maxcsnbuf;
164 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
165 Entry *e = str2entry( buf );
168 * Initialize text buffer
170 bvtext.bv_len = textlen;
171 bvtext.bv_val = textbuf;
172 bvtext.bv_val[0] = '\0';
175 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
178 if( continuemode ) continue;
182 /* make sure the DN is not empty */
183 if( !e->e_nname.bv_len ) {
184 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
185 progname, e->e_dn, lineno );
188 if( continuemode ) continue;
193 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
196 fprintf( stderr, "%s: line %d: "
197 "database (%s) not configured to hold \"%s\"\n",
199 be ? be->be_suffix[0].bv_val : "<none>",
201 fprintf( stderr, "%s: line %d: "
202 "database (%s) not configured to hold \"%s\"\n",
204 be ? be->be_nsuffix[0].bv_val : "<none>",
208 if( continuemode ) continue;
212 if( global_schemacheck ) {
213 Attribute *sc = attr_find( e->e_attrs,
214 slap_schema.si_ad_structuralObjectClass );
215 Attribute *oc = attr_find( e->e_attrs,
216 slap_schema.si_ad_objectClass );
219 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
220 progname, e->e_dn, lineno,
221 "no objectClass attribute");
224 if( continuemode ) continue;
229 struct berval vals[2];
231 rc = structural_class( oc->a_vals, vals,
232 NULL, &text, textbuf, textlen );
234 if( rc != LDAP_SUCCESS ) {
235 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
236 progname, e->e_dn, lineno, rc, text );
239 if( continuemode ) continue;
244 vals[1].bv_val = NULL;
246 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
247 vals, NULL /* FIXME */ );
251 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
253 if( rc != LDAP_SUCCESS ) {
254 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
255 progname, e->e_dn, lineno, rc, text );
258 if( continuemode ) continue;
263 if ( SLAP_LASTMOD(be) ) {
265 time_t now = slap_get_time();
266 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
267 struct berval vals[ 2 ];
269 struct berval name, timestamp;
271 struct berval nvals[ 2 ];
273 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
276 vals[1].bv_val = NULL;
279 nvals[1].bv_val = NULL;
282 lutil_gentime( timebuf, sizeof(timebuf), ltm );
284 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
287 timestamp.bv_val = timebuf;
288 timestamp.bv_len = strlen(timebuf);
290 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
291 BER_BVSTR( &name, SLAPD_ANONYMOUS );
294 name = be->be_rootdn;
295 nname = be->be_rootndn;
298 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
301 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
302 vals[0].bv_val = uuidbuf;
303 attr_merge_normalize_one( e,
304 slap_schema.si_ad_entryUUID, vals, NULL );
307 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
312 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
315 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
320 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
323 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
327 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
330 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
334 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
337 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
341 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
344 if ( update_ctxcsn ) {
345 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
346 if ( maxcsn.bv_len != 0 ) {
348 value_match( &match, slap_schema.si_ad_entryCSN,
349 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
350 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
351 &maxcsn, &attr->a_nvals[0], &text );
356 strcpy( maxcsn.bv_val, attr->a_nvals[0].bv_val );
357 maxcsn.bv_len = attr->a_nvals[0].bv_len;
363 ldap_pvt_thread_mutex_lock( &put_mutex );
370 ldap_pvt_thread_cond_signal( &put_cond );
371 ldap_pvt_thread_mutex_unlock( &put_mutex );
376 id = be->be_entry_put( be, e, &bvtext );
378 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
379 "(line=%d): %s\n", progname, e->e_dn,
380 lineno, bvtext.bv_val );
383 if( continuemode ) continue;
390 fprintf( stderr, "added: \"%s\"\n",
393 fprintf( stderr, "added: \"%s\" (%08lx)\n",
394 e->e_dn, (long) id );
402 ldap_pvt_thread_mutex_lock( &put_mutex );
403 /* Tell child thread to stop if it hasn't aborted */
405 put_rc = EXIT_FAILURE;
406 ldap_pvt_thread_cond_signal( &put_cond );
408 ldap_pvt_thread_mutex_unlock( &put_mutex );
409 ldap_pvt_thread_join( put_tid, NULL );
410 ldap_pvt_thread_mutex_destroy( &put_mutex );
411 ldap_pvt_thread_cond_destroy( &put_cond );
414 bvtext.bv_len = textlen;
415 bvtext.bv_val = textbuf;
416 bvtext.bv_val[0] = '\0';
418 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && maxcsn.bv_len ) {
419 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
420 if ( ctxcsn_id == NOID ) {
421 fprintf( stderr, "%s: context entry is missing\n", progname );
424 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
425 if ( ret == LDAP_SUCCESS ) {
426 attr = attr_find( ctxcsn_e->e_attrs,
427 slap_schema.si_ad_contextCSN );
428 value_match( &match, slap_schema.si_ad_entryCSN,
429 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
430 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
431 &maxcsn, &attr->a_nvals[0], &text );
433 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
434 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
435 attr->a_vals[0].bv_len = maxcsn.bv_len;
437 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
438 if( ctxcsn_id == NOID ) {
439 fprintf( stderr, "%s: could not modify ctxcsn\n",
442 } else if ( verbose ) {
443 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
444 ctxcsn_e->e_dn, (long) ctxcsn_id );
454 if( be->be_entry_close( be ) ) {