2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 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 * ( SLAP_SYNC_SID_MAX + 1 ) ];
45 slapadd( int argc, char **argv )
49 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
50 size_t textlen = sizeof textbuf;
51 const char *progname = "slapadd";
54 struct berval maxcsn[ SLAP_SYNC_SID_MAX + 1 ];
60 OperationBuffer opbuf;
67 int rc = EXIT_SUCCESS;
73 slap_tool_init( progname, SLAPADD, argc, argv );
75 memset( &opbuf, 0, sizeof(opbuf) );
77 op->o_hdr = &opbuf.ob_hdr;
79 if( !be->be_entry_open ||
80 !be->be_entry_close ||
85 !be->be_entry_modify)) )
87 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
90 fprintf( stderr, "\t(dry) continuing...\n" );
97 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
102 /* enforce schema checking unless not disabled */
103 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
104 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
107 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
108 fprintf( stderr, "%s: could not open database.\n",
110 exit( EXIT_FAILURE );
113 if ( update_ctxcsn ) {
114 maxcsn[ 0 ].bv_val = maxcsnbuf;
115 for ( sid = 1; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
116 maxcsn[ sid ].bv_val = maxcsn[ sid - 1 ].bv_val + LDAP_LUTIL_CSNSTR_BUFSIZE;
117 maxcsn[ sid ].bv_len = 0;
121 /* nextline is the line number of the end of the current entry */
122 for( lineno=1; ldif_read_record( ldiffp, &nextline, &buf, &lmax );
123 lineno=nextline+1 ) {
126 if ( lineno < jumpline )
129 e = str2entry2( buf, checkvals );
132 * Initialize text buffer
134 bvtext.bv_len = textlen;
135 bvtext.bv_val = textbuf;
136 bvtext.bv_val[0] = '\0';
139 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
142 if( continuemode ) continue;
146 /* make sure the DN is not empty */
147 if( BER_BVISEMPTY( &e->e_nname ) &&
148 !BER_BVISEMPTY( be->be_nsuffix )) {
149 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
150 progname, e->e_dn, lineno );
153 if( continuemode ) continue;
158 if( select_backend( &e->e_nname, nosubordinates )
161 fprintf( stderr, "%s: line %d: "
162 "database (%s) not configured to hold \"%s\"\n",
164 be ? be->be_suffix[0].bv_val : "<none>",
166 fprintf( stderr, "%s: line %d: "
167 "database (%s) not configured to hold \"%s\"\n",
169 be ? be->be_nsuffix[0].bv_val : "<none>",
173 if( continuemode ) continue;
178 Attribute *oc = attr_find( e->e_attrs,
179 slap_schema.si_ad_objectClass );
182 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
183 progname, e->e_dn, lineno,
184 "no objectClass attribute");
187 if( continuemode ) continue;
194 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
195 rc = entry_schema_check( op, e, NULL, manage, 1,
196 &text, textbuf, textlen );
198 if( rc != LDAP_SUCCESS ) {
199 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
200 progname, e->e_dn, lineno, rc, text );
203 if( continuemode ) continue;
209 if ( SLAP_LASTMOD(be) ) {
210 time_t now = slap_get_time();
211 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
212 struct berval vals[ 2 ];
214 struct berval name, timestamp;
216 struct berval nvals[ 2 ];
218 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
221 vals[1].bv_val = NULL;
224 nvals[1].bv_val = NULL;
226 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
229 timestamp.bv_val = timebuf;
230 timestamp.bv_len = sizeof(timebuf);
232 slap_timestamp( &now, ×tamp );
234 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
235 BER_BVSTR( &name, SLAPD_ANONYMOUS );
238 name = be->be_rootdn;
239 nname = be->be_rootndn;
242 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
245 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
246 vals[0].bv_val = uuidbuf;
247 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
250 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
255 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
258 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
263 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
266 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
270 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
273 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
277 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
280 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
284 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
287 if ( update_ctxcsn ) {
290 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
291 assert( attr != NULL );
293 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] );
295 Debug( LDAP_DEBUG_ANY, "%s: could not "
296 "extract SID from entryCSN=%s\n",
297 progname, attr->a_nvals[ 0 ].bv_val, 0 );
300 assert( rc_sid <= SLAP_SYNC_SID_MAX );
302 sid = (unsigned)rc_sid;
303 if ( maxcsn[ sid ].bv_len != 0 ) {
305 value_match( &match, slap_schema.si_ad_entryCSN,
306 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
307 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
308 &maxcsn[ sid ], &attr->a_nvals[0], &text );
313 strcpy( maxcsn[ sid ].bv_val, attr->a_nvals[0].bv_val );
314 maxcsn[ sid ].bv_len = attr->a_nvals[0].bv_len;
321 id = be->be_entry_put( be, e, &bvtext );
323 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
324 "(line=%d): %s\n", progname, e->e_dn,
325 lineno, bvtext.bv_val );
328 if( continuemode ) continue;
332 fprintf( stderr, "added: \"%s\" (%08lx)\n",
333 e->e_dn, (long) id );
336 fprintf( stderr, "added: \"%s\"\n",
343 bvtext.bv_len = textlen;
344 bvtext.bv_val = textbuf;
345 bvtext.bv_val[0] = '\0';
347 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) {
348 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
349 if ( ctxcsn_id == NOID ) {
350 fprintf( stderr, "%s: context entry is missing\n", progname );
353 ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
354 if ( ctxcsn_e != NULL ) {
355 Entry *e = entry_dup( ctxcsn_e );
356 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
360 for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) {
363 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] );
365 Debug( LDAP_DEBUG_ANY,
366 "%s: unable to extract SID "
367 "from #%d contextCSN=%s\n",
369 attr->a_nvals[ i ].bv_val );
373 assert( rc_sid <= SLAP_SYNC_SID_MAX );
375 sid = (unsigned)rc_sid;
377 if ( maxcsn[ sid ].bv_len == 0 ) {
381 value_match( &match, slap_schema.si_ad_entryCSN,
382 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
383 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
384 &maxcsn[ sid ], &attr->a_nvals[i], &text );
388 AC_MEMCPY( maxcsn[ sid ].bv_val,
389 attr->a_nvals[ i ].bv_val,
390 attr->a_nvals[ i ].bv_len );
391 maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0';
392 maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len;
396 if ( attr->a_nvals != attr->a_nvals ) {
397 ber_bvarray_free( attr->a_nvals );
399 attr->a_nvals = NULL;
400 ber_bvarray_free( attr->a_vals );
404 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
405 if ( maxcsn[ sid ].bv_len ) {
406 attr_merge_one( e, slap_schema.si_ad_contextCSN,
407 &maxcsn[ sid], NULL );
411 ctxcsn_id = be->be_entry_modify( be, e, &bvtext );
412 if( ctxcsn_id == NOID ) {
413 fprintf( stderr, "%s: could not modify ctxcsn\n",
416 } else if ( verbose ) {
417 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
418 e->e_dn, (long) ctxcsn_id );
428 if( be->be_entry_close( be ) ) {