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 ];
45 slapadd( int argc, char **argv )
49 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
50 size_t textlen = sizeof textbuf;
51 const char *progname = "slapadd";
59 OperationBuffer opbuf;
67 int rc = EXIT_SUCCESS;
70 slap_tool_init( progname, SLAPADD, argc, argv );
72 memset( &opbuf, 0, sizeof(opbuf) );
73 op = (Operation *) &opbuf;
74 op->o_hdr = (Opheader *)(op+1);
76 if( !be->be_entry_open ||
77 !be->be_entry_close ||
81 !be->be_id2entry_get ||
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.bv_val = maxcsnbuf;
110 /* nextline is the line number of the end of the current entry */
111 for( lineno=1; ldif_read_record( ldiffp, &nextline, &buf, &lmax );
112 lineno=nextline+1 ) {
115 if ( lineno < jumpline )
118 e = str2entry2( buf, checkvals );
121 * Initialize text buffer
123 bvtext.bv_len = textlen;
124 bvtext.bv_val = textbuf;
125 bvtext.bv_val[0] = '\0';
128 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
131 if( continuemode ) continue;
135 /* make sure the DN is not empty */
136 if( BER_BVISEMPTY( &e->e_nname ) &&
137 !BER_BVISEMPTY( be->be_nsuffix )) {
138 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
139 progname, e->e_dn, lineno );
142 if( continuemode ) continue;
147 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
150 fprintf( stderr, "%s: line %d: "
151 "database (%s) not configured to hold \"%s\"\n",
153 be ? be->be_suffix[0].bv_val : "<none>",
155 fprintf( stderr, "%s: line %d: "
156 "database (%s) not configured to hold \"%s\"\n",
158 be ? be->be_nsuffix[0].bv_val : "<none>",
162 if( continuemode ) continue;
167 Attribute *oc = attr_find( e->e_attrs,
168 slap_schema.si_ad_objectClass );
171 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
172 progname, e->e_dn, lineno,
173 "no objectClass attribute");
176 if( continuemode ) continue;
183 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
184 rc = entry_schema_check( op, e, NULL, manage, 1,
185 &text, textbuf, textlen );
187 if( rc != LDAP_SUCCESS ) {
188 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
189 progname, e->e_dn, lineno, rc, text );
192 if( continuemode ) continue;
198 if ( SLAP_LASTMOD(be) ) {
199 time_t now = slap_get_time();
200 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
201 struct berval vals[ 2 ];
203 struct berval name, timestamp;
205 struct berval nvals[ 2 ];
207 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
210 vals[1].bv_val = NULL;
213 nvals[1].bv_val = NULL;
215 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
218 timestamp.bv_val = timebuf;
219 timestamp.bv_len = sizeof(timebuf);
221 slap_timestamp( &now, ×tamp );
223 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
224 BER_BVSTR( &name, SLAPD_ANONYMOUS );
227 name = be->be_rootdn;
228 nname = be->be_rootndn;
231 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
234 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
235 vals[0].bv_val = uuidbuf;
236 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
239 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
244 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
247 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
252 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
255 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
259 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
262 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
266 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
269 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
273 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
276 if ( update_ctxcsn ) {
277 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
278 if ( maxcsn.bv_len != 0 ) {
280 value_match( &match, slap_schema.si_ad_entryCSN,
281 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
282 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
283 &maxcsn, &attr->a_nvals[0], &text );
288 strcpy( maxcsn.bv_val, attr->a_nvals[0].bv_val );
289 maxcsn.bv_len = attr->a_nvals[0].bv_len;
295 id = be->be_entry_put( be, e, &bvtext );
297 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
298 "(line=%d): %s\n", progname, e->e_dn,
299 lineno, bvtext.bv_val );
302 if( continuemode ) continue;
306 fprintf( stderr, "added: \"%s\" (%08lx)\n",
307 e->e_dn, (long) id );
310 fprintf( stderr, "added: \"%s\"\n",
317 bvtext.bv_len = textlen;
318 bvtext.bv_val = textbuf;
319 bvtext.bv_val[0] = '\0';
321 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && maxcsn.bv_len ) {
322 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
323 if ( ctxcsn_id == NOID ) {
324 fprintf( stderr, "%s: context entry is missing\n", progname );
327 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
328 if ( ret == LDAP_SUCCESS ) {
329 attr = attr_find( ctxcsn_e->e_attrs,
330 slap_schema.si_ad_contextCSN );
332 value_match( &match, slap_schema.si_ad_entryCSN,
333 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
334 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
335 &maxcsn, &attr->a_nvals[0], &text );
337 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
338 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
339 attr->a_vals[0].bv_len = maxcsn.bv_len;
343 attr_merge_one( ctxcsn_e, slap_schema.si_ad_contextCSN, &maxcsn, NULL );
346 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
347 if( ctxcsn_id == NOID ) {
348 fprintf( stderr, "%s: could not modify ctxcsn\n",
351 } else if ( verbose ) {
352 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
353 ctxcsn_e->e_dn, (long) ctxcsn_id );
363 if( be->be_entry_close( be ) ) {