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 ];
55 MatchingRule *mr_csnsid;
61 OperationBuffer opbuf;
68 int rc = EXIT_SUCCESS;
71 slap_tool_init( progname, SLAPADD, argc, argv );
73 memset( &opbuf, 0, sizeof(opbuf) );
75 op->o_hdr = &opbuf.ob_hdr;
77 if( !be->be_entry_open ||
78 !be->be_entry_close ||
83 !be->be_entry_modify)) )
85 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
88 fprintf( stderr, "\t(dry) continuing...\n" );
95 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
100 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
101 fprintf( stderr, "%s: could not open database.\n",
103 exit( EXIT_FAILURE );
106 if ( update_ctxcsn ) {
107 mr_csnsid = mr_find( "CSNSIDMatch" );
108 assert( mr_csnsid != NULL );
109 maxcsn[ 0 ].bv_val = maxcsnbuf;
110 for ( sid = 1; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
111 maxcsn[ sid ].bv_val = maxcsn[ sid - 1 ].bv_val + LDAP_LUTIL_CSNSTR_BUFSIZE;
112 maxcsn[ sid ].bv_len = 0;
116 /* nextline is the line number of the end of the current entry */
117 for( lineno=1; ldif_read_record( ldiffp, &nextline, &buf, &lmax );
118 lineno=nextline+1 ) {
121 if ( lineno < jumpline )
124 e = str2entry2( buf, checkvals );
127 * Initialize text buffer
129 bvtext.bv_len = textlen;
130 bvtext.bv_val = textbuf;
131 bvtext.bv_val[0] = '\0';
134 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
137 if( continuemode ) continue;
141 /* make sure the DN is not empty */
142 if( BER_BVISEMPTY( &e->e_nname ) &&
143 !BER_BVISEMPTY( be->be_nsuffix )) {
144 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
145 progname, e->e_dn, lineno );
148 if( continuemode ) continue;
153 if( select_backend( &e->e_nname, nosubordinates )
156 fprintf( stderr, "%s: line %d: "
157 "database (%s) not configured to hold \"%s\"\n",
159 be ? be->be_suffix[0].bv_val : "<none>",
161 fprintf( stderr, "%s: line %d: "
162 "database (%s) not configured to hold \"%s\"\n",
164 be ? be->be_nsuffix[0].bv_val : "<none>",
168 if( continuemode ) continue;
173 Attribute *oc = attr_find( e->e_attrs,
174 slap_schema.si_ad_objectClass );
177 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
178 progname, e->e_dn, lineno,
179 "no objectClass attribute");
182 if( continuemode ) continue;
189 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
190 rc = entry_schema_check( op, e, NULL, manage, 1,
191 &text, textbuf, textlen );
193 if( rc != LDAP_SUCCESS ) {
194 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
195 progname, e->e_dn, lineno, rc, text );
198 if( continuemode ) continue;
204 if ( SLAP_LASTMOD(be) ) {
205 time_t now = slap_get_time();
206 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
207 struct berval vals[ 2 ];
209 struct berval name, timestamp;
211 struct berval nvals[ 2 ];
213 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
216 vals[1].bv_val = NULL;
219 nvals[1].bv_val = NULL;
221 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
224 timestamp.bv_val = timebuf;
225 timestamp.bv_len = sizeof(timebuf);
227 slap_timestamp( &now, ×tamp );
229 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
230 BER_BVSTR( &name, SLAPD_ANONYMOUS );
233 name = be->be_rootdn;
234 nname = be->be_rootndn;
237 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
240 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
241 vals[0].bv_val = uuidbuf;
242 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
245 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
250 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
253 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
258 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
261 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
265 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
268 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
272 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
275 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
279 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
282 if ( update_ctxcsn ) {
285 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
286 assert( attr != NULL );
288 rc = mr_csnsid->smr_normalize( SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
289 NULL, NULL, &attr->a_nvals[ 0 ], &nsid, NULL );
290 assert( rc == LDAP_SUCCESS );
291 rc = lutil_atoulx( &sid, nsid.bv_val, 16 );
292 ber_memfree( nsid.bv_val );
294 Debug( LDAP_DEBUG_ANY, "%s: could not "
295 "extract SID from entryCSN=%s\n",
296 progname, attr->a_nvals[ 0 ].bv_val, 0 );
299 assert( sid <= SLAP_SYNC_SID_MAX );
301 if ( maxcsn[ sid ].bv_len != 0 ) {
303 value_match( &match, slap_schema.si_ad_entryCSN,
304 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
305 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
306 &maxcsn[ sid ], &attr->a_nvals[0], &text );
311 strcpy( maxcsn[ sid ].bv_val, attr->a_nvals[0].bv_val );
312 maxcsn[ sid ].bv_len = attr->a_nvals[0].bv_len;
319 id = be->be_entry_put( be, e, &bvtext );
321 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
322 "(line=%d): %s\n", progname, e->e_dn,
323 lineno, bvtext.bv_val );
326 if( continuemode ) continue;
330 fprintf( stderr, "added: \"%s\" (%08lx)\n",
331 e->e_dn, (long) id );
334 fprintf( stderr, "added: \"%s\"\n",
341 bvtext.bv_len = textlen;
342 bvtext.bv_val = textbuf;
343 bvtext.bv_val[0] = '\0';
345 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) {
346 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
347 if ( ctxcsn_id == NOID ) {
348 fprintf( stderr, "%s: context entry is missing\n", progname );
351 ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
352 if ( ctxcsn_e != NULL ) {
353 attr = attr_find( ctxcsn_e->e_attrs, slap_schema.si_ad_contextCSN );
357 for ( i = 0; !BER_BVISNULL( &attr->a_vals[ i ] ); i++ ) {
360 rc = mr_csnsid->smr_normalize( SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
361 NULL, NULL, &attr->a_nvals[ i ], &nsid, NULL );
363 /* must succeed, since it passed
364 * validation/normalization */
365 assert( rc == LDAP_SUCCESS );
367 rc = lutil_atoulx( &sid, nsid.bv_val, 16 );
368 ber_memfree( nsid.bv_val );
371 Debug( LDAP_DEBUG_ANY,
372 "%s: unable to extract SID "
373 "from #%d contextCSN=%s\n",
375 attr->a_nvals[ i ].bv_val );
379 if ( maxcsn[ sid ].bv_len == 0 ) {
383 value_match( &match, slap_schema.si_ad_entryCSN,
384 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
385 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
386 &maxcsn[ sid ], &attr->a_nvals[i], &text );
390 AC_MEMCPY( maxcsn[ sid ].bv_val,
391 attr->a_nvals[ i ].bv_val,
392 attr->a_nvals[ i ].bv_len );
393 maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0';
394 maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len;
399 if ( attr->a_nvals && attr->a_nvals != attr->a_vals ) {
400 ber_bvarray_free( attr->a_nvals );
402 ber_bvarray_free( attr->a_vals );
406 attr->a_nvals = NULL;
409 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
410 if ( maxcsn[ sid ].bv_len ) {
411 attr_merge_one( ctxcsn_e, slap_schema.si_ad_contextCSN,
412 &maxcsn[ sid], NULL );
416 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
417 if( ctxcsn_id == NOID ) {
418 fprintf( stderr, "%s: could not modify ctxcsn\n",
421 } else if ( verbose ) {
422 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
423 ctxcsn_e->e_dn, (long) ctxcsn_id );
432 if( be->be_entry_close( be ) ) {