3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdlib.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15 #include <ac/unistd.h>
21 #include "slapcommon.h"
23 static char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
26 main( int argc, char **argv )
31 int rc = EXIT_SUCCESS;
34 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
35 size_t textlen = sizeof textbuf;
39 lutil_log_initialize(argc, argv );
41 slap_tool_init( "slapadd", SLAPADD, argc, argv );
43 if( !be->be_entry_open ||
44 !be->be_entry_close ||
47 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
55 if( be->be_entry_open( be, 1 ) != 0 ) {
56 fprintf( stderr, "%s: could not open database.\n",
61 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
62 Entry *e = str2entry( buf );
66 * Initialize text buffer
68 bvtext.bv_len = textlen;
69 bvtext.bv_val = textbuf;
70 bvtext.bv_val[0] = '\0';
73 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
76 if( continuemode ) continue;
80 /* make sure the DN is not empty */
81 if( !e->e_nname.bv_len ) {
82 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
83 progname, e->e_dn, lineno );
86 if( continuemode ) continue;
91 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
94 fprintf( stderr, "%s: line %d: "
95 "database (%s) not configured to hold \"%s\"\n",
97 be ? be->be_suffix[0].bv_val : "<none>",
99 fprintf( stderr, "%s: line %d: "
100 "database (%s) not configured to hold \"%s\"\n",
102 be ? be->be_nsuffix[0].bv_val : "<none>",
106 if( continuemode ) continue;
110 if( global_schemacheck ) {
111 Attribute *sc = attr_find( e->e_attrs,
112 slap_schema.si_ad_structuralObjectClass );
113 Attribute *oc = attr_find( e->e_attrs,
114 slap_schema.si_ad_objectClass );
117 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
118 progname, e->e_dn, lineno,
119 "no objectClass attribute");
122 if( continuemode ) continue;
127 struct berval vals[2];
129 rc = structural_class( oc->a_vals, vals,
130 NULL, &text, textbuf, textlen );
132 if( rc != LDAP_SUCCESS ) {
133 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
134 progname, e->e_dn, lineno, rc, text );
137 if( continuemode ) continue;
142 vals[1].bv_val = NULL;
144 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
145 vals, NULL /* FIXME */ );
149 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
151 if( rc != LDAP_SUCCESS ) {
152 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
153 progname, e->e_dn, lineno, rc, text );
156 if( continuemode ) continue;
161 if ( SLAP_LASTMOD(be) ) {
163 time_t now = slap_get_time();
164 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
165 struct berval vals[ 2 ];
167 struct berval name, timestamp;
169 struct berval nvals[ 2 ];
171 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
174 vals[1].bv_val = NULL;
177 nvals[1].bv_val = NULL;
180 lutil_gentime( timebuf, sizeof(timebuf), ltm );
182 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
185 timestamp.bv_val = timebuf;
186 timestamp.bv_len = strlen(timebuf);
188 if ( be->be_rootndn.bv_len == 0 ) {
189 name.bv_val = SLAPD_ANONYMOUS;
190 name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
191 nname.bv_val = SLAPD_ANONYMOUS;
192 nname.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
194 name = be->be_rootdn;
195 nname = be->be_rootndn;
198 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
201 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
202 vals[0].bv_val = uuidbuf;
203 attr_merge( e, slap_schema.si_ad_entryUUID, vals, NULL );
206 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
211 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
214 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
219 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
222 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
226 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
229 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
233 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
236 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
240 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
245 ID id = be->be_entry_put( be, e, &bvtext );
247 fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d): %s\n",
248 progname, e->e_dn, lineno, bvtext.bv_val );
251 if( continuemode ) continue;
256 fprintf( stderr, "added: \"%s\" (%08lx)\n",
257 e->e_dn, (long) id );
261 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
268 if ( SLAP_LASTMOD(be) && update_ctxcsn == SLAP_TOOL_CTXCSN_BATCH && csn.bv_len > 0 ) {
271 struct berval ctxcsn_ndn = { 0, NULL };
273 struct berval bvtext;
276 bvtext.bv_len = textlen;
277 bvtext.bv_val = textbuf;
278 bvtext.bv_val[0] = '\0';
280 build_new_dn( &ctxcsn_ndn, &be->be_nsuffix[0], (struct berval *)&slap_ldapsync_cn_bv, NULL );
281 ctxcsn_id = be->be_dn2id_get( be, &ctxcsn_ndn );
283 if ( ctxcsn_id == NOID ) {
284 ctxcsn_e = slap_create_context_csn_entry( be, &csn );
285 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext );
286 if( ctxcsn_id == NOID ) {
287 fprintf( stderr, "%s: could not add ctxcsn subentry\n", progname);
291 fprintf( stderr, "added: \"%s\" (%08lx)\n", ctxcsn_e->e_dn, (long) ctxcsn_id );
293 entry_free( ctxcsn_e );
295 ret = be->be_id2entry_get( be, ctxcsn_id, &ctxcsn_e );
296 if ( ret == LDAP_SUCCESS ) {
297 attr = attr_find( ctxcsn_e->e_attrs, slap_schema.si_ad_contextCSN );
298 attr->a_vals[0] = csn;
299 ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext );
300 if( ctxcsn_id == NOID ) {
301 fprintf( stderr, "%s: could not modify ctxcsn subentry\n", progname);
305 fprintf( stderr, "modified: \"%s\" (%08lx)\n", ctxcsn_e->e_dn, (long) ctxcsn_id );
308 fprintf( stderr, "%s: could not modify ctxcsn subentry\n", progname);
316 if( be->be_entry_close( be )) rc = EXIT_FAILURE;