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 )
50 int rc = EXIT_SUCCESS;
53 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
54 size_t textlen = sizeof textbuf;
55 const char *progname = "slapadd";
67 slap_tool_init( progname, SLAPADD, argc, argv );
69 if( !be->be_entry_open ||
70 !be->be_entry_close ||
73 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
76 fprintf( stderr, "\t(dry) continuing...\n" );
83 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
88 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
89 fprintf( stderr, "%s: could not open database.\n",
94 if ( update_ctxcsn ) {
95 maxcsn.bv_val = maxcsnbuf;
99 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
100 Entry *e = str2entry2( buf, checkvals );
103 * Initialize text buffer
105 bvtext.bv_len = textlen;
106 bvtext.bv_val = textbuf;
107 bvtext.bv_val[0] = '\0';
110 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
113 if( continuemode ) continue;
117 /* make sure the DN is not empty */
118 if( BER_BVISEMPTY( &e->e_nname ) &&
119 !BER_BVISEMPTY( be->be_nsuffix )) {
120 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
121 progname, e->e_dn, lineno );
124 if( continuemode ) continue;
129 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
132 fprintf( stderr, "%s: line %d: "
133 "database (%s) not configured to hold \"%s\"\n",
135 be ? be->be_suffix[0].bv_val : "<none>",
137 fprintf( stderr, "%s: line %d: "
138 "database (%s) not configured to hold \"%s\"\n",
140 be ? be->be_nsuffix[0].bv_val : "<none>",
144 if( continuemode ) continue;
149 Attribute *sc = attr_find( e->e_attrs,
150 slap_schema.si_ad_structuralObjectClass );
151 Attribute *oc = attr_find( e->e_attrs,
152 slap_schema.si_ad_objectClass );
155 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
156 progname, e->e_dn, lineno,
157 "no objectClass attribute");
160 if( continuemode ) continue;
167 rc = structural_class( oc->a_vals, &val,
168 NULL, &text, textbuf, textlen );
170 if( rc != LDAP_SUCCESS ) {
171 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
172 progname, e->e_dn, lineno, rc, text );
175 if( continuemode ) continue;
179 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
184 rc = entry_schema_check( be, e, NULL, manage,
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;
197 if ( SLAP_LASTMOD(be) ) {
198 time_t now = slap_get_time();
199 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
200 struct berval vals[ 2 ];
202 struct berval name, timestamp;
204 struct berval nvals[ 2 ];
206 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
209 vals[1].bv_val = NULL;
212 nvals[1].bv_val = NULL;
214 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
217 timestamp.bv_val = timebuf;
218 timestamp.bv_len = sizeof(timebuf);
220 slap_timestamp( &now, ×tamp );
222 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
223 BER_BVSTR( &name, SLAPD_ANONYMOUS );
226 name = be->be_rootdn;
227 nname = be->be_rootndn;
230 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
233 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
234 vals[0].bv_val = uuidbuf;
235 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
238 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
243 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
246 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
251 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
254 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
258 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
261 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
265 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
268 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
272 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
275 if ( update_ctxcsn ) {
276 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
277 if ( maxcsn.bv_len != 0 ) {
279 value_match( &match, slap_schema.si_ad_entryCSN,
280 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
281 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
282 &maxcsn, &attr->a_nvals[0], &text );
287 strcpy( maxcsn.bv_val, attr->a_nvals[0].bv_val );
288 maxcsn.bv_len = attr->a_nvals[0].bv_len;
294 id = be->be_entry_put( be, e, &bvtext );
296 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
297 "(line=%d): %s\n", progname, e->e_dn,
298 lineno, bvtext.bv_val );
301 if( continuemode ) continue;
308 fprintf( stderr, "added: \"%s\"\n",
311 fprintf( stderr, "added: \"%s\" (%08lx)\n",
312 e->e_dn, (long) id );
319 bvtext.bv_len = textlen;
320 bvtext.bv_val = textbuf;
321 bvtext.bv_val[0] = '\0';
323 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && maxcsn.bv_len ) {
324 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
325 if ( ctxcsn_id == NOID ) {
326 fprintf( stderr, "%s: context entry is missing\n", progname );
329 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
330 if ( ret == LDAP_SUCCESS ) {
331 attr = attr_find( ctxcsn_e->e_attrs,
332 slap_schema.si_ad_contextCSN );
334 value_match( &match, slap_schema.si_ad_entryCSN,
335 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
336 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
337 &maxcsn, &attr->a_nvals[0], &text );
339 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
340 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
341 attr->a_vals[0].bv_len = maxcsn.bv_len;
345 attr_merge_one( ctxcsn_e, slap_schema.si_ad_contextCSN, &maxcsn, NULL );
348 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
349 if( ctxcsn_id == NOID ) {
350 fprintf( stderr, "%s: could not modify ctxcsn\n",
353 } else if ( verbose ) {
354 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
355 ctxcsn_e->e_dn, (long) ctxcsn_id );
365 if( be->be_entry_close( be ) ) {