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 = 0; /*FIXME need a new switch for this */
55 static void *do_put(void *ptr)
63 ldap_pvt_thread_cond_wait( &put_cond, &put_mutex );
70 id = be->be_entry_put( be, e, &bvtext );
72 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
73 "(line=%d): %s\n", progname, e->e_dn,
74 lineno, bvtext.bv_val );
76 if ( continuemode ) continue;
77 put_rc = EXIT_FAILURE;
84 fprintf( stderr, "added: \"%s\"\n",
87 fprintf( stderr, "added: \"%s\" (%08lx)\n",
93 ldap_pvt_thread_cond_signal( &put_cond );
99 slapadd( int argc, char **argv )
104 int rc = EXIT_SUCCESS;
107 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
108 size_t textlen = sizeof textbuf;
111 struct berval maxcsn;
119 ldap_pvt_thread_t put_tid;
121 slap_tool_init( progname, SLAPADD, argc, argv );
123 if( !be->be_entry_open ||
124 !be->be_entry_close ||
127 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
130 fprintf( stderr, "\t(dry) continuing...\n" );
133 exit( EXIT_FAILURE );
138 ldap_pvt_thread_initialize();
139 ldap_pvt_thread_cond_init( &put_cond );
140 ldap_pvt_thread_mutex_init( &put_mutex );
141 rc = ldap_pvt_thread_create( &put_tid, 0, do_put, NULL );
143 fprintf( stderr, "%s: could not create thread.\n",
145 exit( EXIT_FAILURE );
152 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
153 fprintf( stderr, "%s: could not open database.\n",
155 exit( EXIT_FAILURE );
158 if ( update_ctxcsn ) {
159 maxcsn.bv_val = maxcsnbuf;
163 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
164 Entry *e = str2entry( buf );
167 * Initialize text buffer
169 bvtext.bv_len = textlen;
170 bvtext.bv_val = textbuf;
171 bvtext.bv_val[0] = '\0';
174 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
177 if( continuemode ) continue;
181 /* make sure the DN is not empty */
182 if( !e->e_nname.bv_len ) {
183 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
184 progname, e->e_dn, lineno );
187 if( continuemode ) continue;
192 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
195 fprintf( stderr, "%s: line %d: "
196 "database (%s) not configured to hold \"%s\"\n",
198 be ? be->be_suffix[0].bv_val : "<none>",
200 fprintf( stderr, "%s: line %d: "
201 "database (%s) not configured to hold \"%s\"\n",
203 be ? be->be_nsuffix[0].bv_val : "<none>",
207 if( continuemode ) continue;
211 if( global_schemacheck ) {
212 Attribute *sc = attr_find( e->e_attrs,
213 slap_schema.si_ad_structuralObjectClass );
214 Attribute *oc = attr_find( e->e_attrs,
215 slap_schema.si_ad_objectClass );
218 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
219 progname, e->e_dn, lineno,
220 "no objectClass attribute");
223 if( continuemode ) continue;
228 struct berval vals[2];
230 rc = structural_class( oc->a_vals, vals,
231 NULL, &text, textbuf, textlen );
233 if( rc != LDAP_SUCCESS ) {
234 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
235 progname, e->e_dn, lineno, rc, text );
238 if( continuemode ) continue;
243 vals[1].bv_val = NULL;
245 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
246 vals, NULL /* FIXME */ );
250 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
252 if( rc != LDAP_SUCCESS ) {
253 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
254 progname, e->e_dn, lineno, rc, text );
257 if( continuemode ) continue;
262 if ( SLAP_LASTMOD(be) ) {
264 time_t now = slap_get_time();
265 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
266 struct berval vals[ 2 ];
268 struct berval name, timestamp;
270 struct berval nvals[ 2 ];
272 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
275 vals[1].bv_val = NULL;
278 nvals[1].bv_val = NULL;
281 lutil_gentime( timebuf, sizeof(timebuf), ltm );
283 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
286 timestamp.bv_val = timebuf;
287 timestamp.bv_len = strlen(timebuf);
289 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
290 BER_BVSTR( &name, SLAPD_ANONYMOUS );
293 name = be->be_rootdn;
294 nname = be->be_rootndn;
297 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
300 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
301 vals[0].bv_val = uuidbuf;
302 attr_merge_normalize_one( e,
303 slap_schema.si_ad_entryUUID, vals, NULL );
306 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
311 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
314 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
319 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
322 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
326 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
329 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
333 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
336 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
340 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
343 if ( update_ctxcsn ) {
344 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
345 if ( maxcsn.bv_len != 0 ) {
347 value_match( &match, slap_schema.si_ad_entryCSN,
348 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
349 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
350 &maxcsn, &attr->a_nvals[0], &text );
355 strcpy( maxcsn.bv_val, attr->a_nvals[0].bv_val );
356 maxcsn.bv_len = attr->a_nvals[0].bv_len;
363 ldap_pvt_thread_cond_wait( &put_cond, &put_mutex );
372 ldap_pvt_thread_cond_signal( &put_cond );
377 id = be->be_entry_put( be, e, &bvtext );
379 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
380 "(line=%d): %s\n", progname, e->e_dn,
381 lineno, bvtext.bv_val );
384 if( continuemode ) continue;
391 fprintf( stderr, "added: \"%s\"\n",
394 fprintf( stderr, "added: \"%s\" (%08lx)\n",
395 e->e_dn, (long) id );
403 put_rc = EXIT_FAILURE;
404 ldap_pvt_thread_cond_signal( &put_cond );
405 ldap_pvt_thread_join( put_tid, NULL );
408 bvtext.bv_len = textlen;
409 bvtext.bv_val = textbuf;
410 bvtext.bv_val[0] = '\0';
412 if ( update_ctxcsn && !dryrun && maxcsn.bv_len ) {
413 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
414 if ( ctxcsn_id == NOID ) {
415 fprintf( stderr, "%s: context entry is missing\n", progname );
418 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
419 if ( ret == LDAP_SUCCESS ) {
420 attr = attr_find( ctxcsn_e->e_attrs,
421 slap_schema.si_ad_contextCSN );
422 value_match( &match, slap_schema.si_ad_entryCSN,
423 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
424 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
425 &maxcsn, &attr->a_nvals[0], &text );
427 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
428 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
429 attr->a_vals[0].bv_len = maxcsn.bv_len;
431 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
432 if( ctxcsn_id == NOID ) {
433 fprintf( stderr, "%s: could not modify ctxcsn\n",
436 } else if ( verbose ) {
437 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
438 ctxcsn_e->e_dn, (long) ctxcsn_id );
448 if( be->be_entry_close( be ) ) {