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;
70 slap_tool_init( progname, SLAPADD, argc, argv );
72 memset( &opbuf, 0, sizeof(opbuf) );
74 op->o_hdr = &opbuf.ob_hdr;
76 if( !be->be_entry_open ||
77 !be->be_entry_close ||
82 !be->be_entry_modify)) )
84 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
87 fprintf( stderr, "\t(dry) continuing...\n" );
94 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
99 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
100 fprintf( stderr, "%s: could not open database.\n",
102 exit( EXIT_FAILURE );
105 if ( update_ctxcsn ) {
106 maxcsn[ 0 ].bv_val = maxcsnbuf;
107 for ( sid = 1; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
108 maxcsn[ sid ].bv_val = maxcsn[ sid - 1 ].bv_val + LDAP_LUTIL_CSNSTR_BUFSIZE;
109 maxcsn[ sid ].bv_len = 0;
113 /* nextline is the line number of the end of the current entry */
114 for( lineno=1; ldif_read_record( ldiffp, &nextline, &buf, &lmax );
115 lineno=nextline+1 ) {
118 if ( lineno < jumpline )
121 e = str2entry2( buf, checkvals );
124 * Initialize text buffer
126 bvtext.bv_len = textlen;
127 bvtext.bv_val = textbuf;
128 bvtext.bv_val[0] = '\0';
131 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
134 if( continuemode ) continue;
138 /* make sure the DN is not empty */
139 if( BER_BVISEMPTY( &e->e_nname ) &&
140 !BER_BVISEMPTY( be->be_nsuffix )) {
141 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
142 progname, e->e_dn, lineno );
145 if( continuemode ) continue;
150 if( select_backend( &e->e_nname, nosubordinates )
153 fprintf( stderr, "%s: line %d: "
154 "database (%s) not configured to hold \"%s\"\n",
156 be ? be->be_suffix[0].bv_val : "<none>",
158 fprintf( stderr, "%s: line %d: "
159 "database (%s) not configured to hold \"%s\"\n",
161 be ? be->be_nsuffix[0].bv_val : "<none>",
165 if( continuemode ) continue;
170 Attribute *oc = attr_find( e->e_attrs,
171 slap_schema.si_ad_objectClass );
174 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
175 progname, e->e_dn, lineno,
176 "no objectClass attribute");
179 if( continuemode ) continue;
186 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
187 rc = entry_schema_check( op, e, NULL, manage, 1,
188 &text, textbuf, textlen );
190 if( rc != LDAP_SUCCESS ) {
191 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
192 progname, e->e_dn, lineno, rc, text );
195 if( continuemode ) continue;
201 if ( SLAP_LASTMOD(be) ) {
202 time_t now = slap_get_time();
203 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
204 struct berval vals[ 2 ];
206 struct berval name, timestamp;
208 struct berval nvals[ 2 ];
210 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
213 vals[1].bv_val = NULL;
216 nvals[1].bv_val = NULL;
218 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
221 timestamp.bv_val = timebuf;
222 timestamp.bv_len = sizeof(timebuf);
224 slap_timestamp( &now, ×tamp );
226 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
227 BER_BVSTR( &name, SLAPD_ANONYMOUS );
230 name = be->be_rootdn;
231 nname = be->be_rootndn;
234 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
237 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
238 vals[0].bv_val = uuidbuf;
239 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
242 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
247 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
250 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
255 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
258 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
262 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
265 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
269 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
272 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
276 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
279 if ( update_ctxcsn ) {
282 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
283 assert( attr != NULL );
285 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] );
287 Debug( LDAP_DEBUG_ANY, "%s: could not "
288 "extract SID from entryCSN=%s\n",
289 progname, attr->a_nvals[ 0 ].bv_val, 0 );
292 assert( rc_sid <= SLAP_SYNC_SID_MAX );
294 sid = (unsigned)rc_sid;
295 if ( maxcsn[ sid ].bv_len != 0 ) {
297 value_match( &match, slap_schema.si_ad_entryCSN,
298 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
299 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
300 &maxcsn[ sid ], &attr->a_nvals[0], &text );
305 strcpy( maxcsn[ sid ].bv_val, attr->a_nvals[0].bv_val );
306 maxcsn[ sid ].bv_len = attr->a_nvals[0].bv_len;
313 id = be->be_entry_put( be, e, &bvtext );
315 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
316 "(line=%d): %s\n", progname, e->e_dn,
317 lineno, bvtext.bv_val );
320 if( continuemode ) continue;
324 fprintf( stderr, "added: \"%s\" (%08lx)\n",
325 e->e_dn, (long) id );
328 fprintf( stderr, "added: \"%s\"\n",
335 bvtext.bv_len = textlen;
336 bvtext.bv_val = textbuf;
337 bvtext.bv_val[0] = '\0';
339 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) {
340 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
341 if ( ctxcsn_id == NOID ) {
342 fprintf( stderr, "%s: context entry is missing\n", progname );
345 ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
346 if ( ctxcsn_e != NULL ) {
347 Entry *e = entry_dup( ctxcsn_e );
348 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
352 for ( i = 0; !BER_BVISNULL( &attr->a_vals[ i ] ); i++ ) {
355 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] );
357 Debug( LDAP_DEBUG_ANY,
358 "%s: unable to extract SID "
359 "from #%d contextCSN=%s\n",
361 attr->a_nvals[ i ].bv_val );
365 assert( rc_sid <= SLAP_SYNC_SID_MAX );
367 sid = (unsigned)rc_sid;
369 if ( maxcsn[ sid ].bv_len == 0 ) {
373 value_match( &match, slap_schema.si_ad_entryCSN,
374 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
375 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
376 &maxcsn[ sid ], &attr->a_nvals[i], &text );
380 AC_MEMCPY( maxcsn[ sid ].bv_val,
381 attr->a_nvals[ i ].bv_val,
382 attr->a_nvals[ i ].bv_len );
383 maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0';
384 maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len;
388 if ( attr->a_nvals != attr->a_vals ) {
389 ber_bvarray_free( attr->a_vals );
391 attr->a_nvals = NULL;
392 ber_bvarray_free( attr->a_vals );
396 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
397 if ( maxcsn[ sid ].bv_len ) {
398 attr_merge_one( e, slap_schema.si_ad_contextCSN,
399 &maxcsn[ sid], NULL );
403 ctxcsn_id = be->be_entry_modify( be, e, &bvtext );
404 if( ctxcsn_id == NOID ) {
405 fprintf( stderr, "%s: could not modify ctxcsn\n",
408 } else if ( verbose ) {
409 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
410 e->e_dn, (long) ctxcsn_id );
420 if( be->be_entry_close( be ) ) {