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 ];
45 slapadd( int argc, char **argv )
49 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
50 size_t textlen = sizeof textbuf;
51 const char *progname = "slapadd";
60 char opbuf[OPERATION_BUFFER_SIZE];
67 int rc = EXIT_SUCCESS;
70 slap_tool_init( progname, SLAPADD, argc, argv );
72 memset( opbuf, 0, sizeof(opbuf) );
73 op = (Operation *)opbuf;
75 if( !be->be_entry_open ||
76 !be->be_entry_close ||
80 !be->be_id2entry_get ||
81 !be->be_entry_modify)) )
83 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
86 fprintf( stderr, "\t(dry) continuing...\n" );
93 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
98 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
99 fprintf( stderr, "%s: could not open database.\n",
101 exit( EXIT_FAILURE );
104 if ( update_ctxcsn ) {
105 maxcsn.bv_val = maxcsnbuf;
109 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
110 Entry *e = str2entry2( buf, checkvals );
113 * Initialize text buffer
115 bvtext.bv_len = textlen;
116 bvtext.bv_val = textbuf;
117 bvtext.bv_val[0] = '\0';
120 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
123 if( continuemode ) continue;
127 /* make sure the DN is not empty */
128 if( BER_BVISEMPTY( &e->e_nname ) &&
129 !BER_BVISEMPTY( be->be_nsuffix )) {
130 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
131 progname, e->e_dn, lineno );
134 if( continuemode ) continue;
139 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
142 fprintf( stderr, "%s: line %d: "
143 "database (%s) not configured to hold \"%s\"\n",
145 be ? be->be_suffix[0].bv_val : "<none>",
147 fprintf( stderr, "%s: line %d: "
148 "database (%s) not configured to hold \"%s\"\n",
150 be ? be->be_nsuffix[0].bv_val : "<none>",
154 if( continuemode ) continue;
159 Attribute *sc = attr_find( e->e_attrs,
160 slap_schema.si_ad_structuralObjectClass );
161 Attribute *oc = attr_find( e->e_attrs,
162 slap_schema.si_ad_objectClass );
165 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
166 progname, e->e_dn, lineno,
167 "no objectClass attribute");
170 if( continuemode ) continue;
177 rc = structural_class( oc->a_vals, &val,
178 NULL, &text, textbuf, textlen );
180 if( rc != LDAP_SUCCESS ) {
181 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
182 progname, e->e_dn, lineno, rc, text );
185 if( continuemode ) continue;
189 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
196 rc = entry_schema_check( op, e, NULL, manage,
197 &text, textbuf, textlen );
199 if( rc != LDAP_SUCCESS ) {
200 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
201 progname, e->e_dn, lineno, rc, text );
204 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 ), 0, 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 ) {
288 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
289 if ( maxcsn.bv_len != 0 ) {
291 value_match( &match, slap_schema.si_ad_entryCSN,
292 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
293 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
294 &maxcsn, &attr->a_nvals[0], &text );
299 strcpy( maxcsn.bv_val, attr->a_nvals[0].bv_val );
300 maxcsn.bv_len = attr->a_nvals[0].bv_len;
306 id = be->be_entry_put( be, e, &bvtext );
308 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
309 "(line=%d): %s\n", progname, e->e_dn,
310 lineno, bvtext.bv_val );
313 if( continuemode ) continue;
317 fprintf( stderr, "added: \"%s\" (%08lx)\n",
318 e->e_dn, (long) id );
321 fprintf( stderr, "added: \"%s\"\n",
328 bvtext.bv_len = textlen;
329 bvtext.bv_val = textbuf;
330 bvtext.bv_val[0] = '\0';
332 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && maxcsn.bv_len ) {
333 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
334 if ( ctxcsn_id == NOID ) {
335 fprintf( stderr, "%s: context entry is missing\n", progname );
338 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
339 if ( ret == LDAP_SUCCESS ) {
340 attr = attr_find( ctxcsn_e->e_attrs,
341 slap_schema.si_ad_contextCSN );
343 value_match( &match, slap_schema.si_ad_entryCSN,
344 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
345 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
346 &maxcsn, &attr->a_nvals[0], &text );
348 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
349 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
350 attr->a_vals[0].bv_len = maxcsn.bv_len;
354 attr_merge_one( ctxcsn_e, slap_schema.si_ad_contextCSN, &maxcsn, NULL );
357 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
358 if( ctxcsn_id == NOID ) {
359 fprintf( stderr, "%s: could not modify ctxcsn\n",
362 } else if ( verbose ) {
363 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
364 ctxcsn_e->e_dn, (long) ctxcsn_id );
374 if( be->be_entry_close( be ) ) {