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";
68 slap_tool_init( progname, SLAPADD, argc, argv );
70 if( !be->be_entry_open ||
71 !be->be_entry_close ||
74 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
77 fprintf( stderr, "\t(dry) continuing...\n" );
84 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;
89 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
90 fprintf( stderr, "%s: could not open database.\n",
95 if ( update_ctxcsn ) {
96 maxcsn.bv_val = maxcsnbuf;
100 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
101 Entry *e = str2entry2( buf, checkvals );
104 * Initialize text buffer
106 bvtext.bv_len = textlen;
107 bvtext.bv_val = textbuf;
108 bvtext.bv_val[0] = '\0';
111 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
114 if( continuemode ) continue;
118 /* make sure the DN is not empty */
119 if( BER_BVISEMPTY( &e->e_nname ) &&
120 !BER_BVISEMPTY( be->be_nsuffix )) {
121 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
122 progname, e->e_dn, lineno );
125 if( continuemode ) continue;
130 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
133 fprintf( stderr, "%s: line %d: "
134 "database (%s) not configured to hold \"%s\"\n",
136 be ? be->be_suffix[0].bv_val : "<none>",
138 fprintf( stderr, "%s: line %d: "
139 "database (%s) not configured to hold \"%s\"\n",
141 be ? be->be_nsuffix[0].bv_val : "<none>",
145 if( continuemode ) continue;
150 Attribute *sc = attr_find( e->e_attrs,
151 slap_schema.si_ad_structuralObjectClass );
152 Attribute *oc = attr_find( e->e_attrs,
153 slap_schema.si_ad_objectClass );
156 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
157 progname, e->e_dn, lineno,
158 "no objectClass attribute");
161 if( continuemode ) continue;
168 rc = structural_class( oc->a_vals, &val,
169 NULL, &text, textbuf, textlen );
171 if( rc != LDAP_SUCCESS ) {
172 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
173 progname, e->e_dn, lineno, rc, text );
176 if( continuemode ) continue;
180 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
185 rc = entry_schema_check( be, e, NULL, manage,
186 &text, textbuf, textlen );
188 if( rc != LDAP_SUCCESS ) {
189 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
190 progname, e->e_dn, lineno, rc, text );
193 if( continuemode ) continue;
198 if ( SLAP_LASTMOD(be) ) {
200 time_t now = slap_get_time();
201 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
202 struct berval vals[ 2 ];
204 struct berval name, timestamp;
206 struct berval nvals[ 2 ];
208 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
211 vals[1].bv_val = NULL;
214 nvals[1].bv_val = NULL;
217 lutil_gentime( timebuf, sizeof(timebuf), ltm );
219 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
222 timestamp.bv_val = timebuf;
223 timestamp.bv_len = strlen(timebuf);
225 if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
226 BER_BVSTR( &name, SLAPD_ANONYMOUS );
229 name = be->be_rootdn;
230 nname = be->be_rootndn;
233 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
236 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
237 vals[0].bv_val = uuidbuf;
238 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
241 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
246 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
249 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
254 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
257 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
261 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
264 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
268 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
271 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
275 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
278 if ( update_ctxcsn ) {
279 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
280 if ( maxcsn.bv_len != 0 ) {
282 value_match( &match, slap_schema.si_ad_entryCSN,
283 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
284 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
285 &maxcsn, &attr->a_nvals[0], &text );
290 strcpy( maxcsn.bv_val, attr->a_nvals[0].bv_val );
291 maxcsn.bv_len = attr->a_nvals[0].bv_len;
297 id = be->be_entry_put( be, e, &bvtext );
299 fprintf( stderr, "%s: could not add entry dn=\"%s\" "
300 "(line=%d): %s\n", progname, e->e_dn,
301 lineno, bvtext.bv_val );
304 if( continuemode ) continue;
311 fprintf( stderr, "added: \"%s\"\n",
314 fprintf( stderr, "added: \"%s\" (%08lx)\n",
315 e->e_dn, (long) id );
323 bvtext.bv_len = textlen;
324 bvtext.bv_val = textbuf;
325 bvtext.bv_val[0] = '\0';
327 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && maxcsn.bv_len ) {
328 ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
329 if ( ctxcsn_id == NOID ) {
330 fprintf( stderr, "%s: context entry is missing\n", progname );
333 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
334 if ( ret == LDAP_SUCCESS ) {
335 attr = attr_find( ctxcsn_e->e_attrs,
336 slap_schema.si_ad_contextCSN );
338 value_match( &match, slap_schema.si_ad_entryCSN,
339 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
340 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
341 &maxcsn, &attr->a_nvals[0], &text );
343 AC_MEMCPY( attr->a_vals[0].bv_val, maxcsn.bv_val, maxcsn.bv_len );
344 attr->a_vals[0].bv_val[maxcsn.bv_len] = '\0';
345 attr->a_vals[0].bv_len = maxcsn.bv_len;
349 attr_merge_one( ctxcsn_e, slap_schema.si_ad_contextCSN, &maxcsn, NULL );
352 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
353 if( ctxcsn_id == NOID ) {
354 fprintf( stderr, "%s: could not modify ctxcsn\n",
357 } else if ( verbose ) {
358 fprintf( stderr, "modified: \"%s\" (%08lx)\n",
359 ctxcsn_e->e_dn, (long) ctxcsn_id );
369 if( be->be_entry_close( be ) ) {