3 * Copyright 1998-2002 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>
20 #include "slapcommon.h"
23 main( int argc, char **argv )
28 int rc = EXIT_SUCCESS;
31 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
32 size_t textlen = sizeof textbuf;
34 struct berval name, timestamp, csn;
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 );
65 bvtext.bv_len = textlen;
66 bvtext.bv_val = textbuf;
69 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
72 if( continuemode ) continue;
76 /* make sure the DN is not empty */
77 if( !e->e_nname.bv_len ) {
78 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
79 progname, e->e_dn, lineno );
82 if( continuemode ) continue;
87 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
90 fprintf( stderr, "%s: line %d: "
91 "database (%s) not configured to hold \"%s\"\n",
93 be ? be->be_suffix[0].bv_val : "<none>",
95 fprintf( stderr, "%s: line %d: "
96 "database (%s) not configured to hold \"%s\"\n",
98 be ? be->be_nsuffix[0].bv_val : "<none>",
102 if( continuemode ) continue;
107 Attribute *sc = attr_find( e->e_attrs,
108 slap_schema.si_ad_structuralObjectClass );
109 Attribute *oc = attr_find( e->e_attrs,
110 slap_schema.si_ad_objectClass );
113 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
114 progname, e->e_dn, lineno,
115 "no objectClass attribute");
118 if( continuemode ) continue;
123 struct berval vals[2];
126 structural_class( oc->a_vals, vals,
127 NULL, &text, textbuf, textlen );
129 if( vals[0].bv_len == 0 ) {
130 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
131 progname, e->e_dn, lineno, text );
134 if( continuemode ) continue;
138 vals[1].bv_val = NULL;
139 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
144 if ( SLAP_LASTMOD(be) ) {
146 time_t now = slap_get_time();
149 lutil_gentime( timebuf, sizeof(timebuf), ltm );
151 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
154 timestamp.bv_val = timebuf;
155 timestamp.bv_len = strlen(timebuf);
157 if ( be->be_rootndn.bv_len == 0 ) {
158 name.bv_val = SLAPD_ANONYMOUS;
159 name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
161 name = be->be_rootndn;
165 if( global_schemacheck ) {
168 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
170 if( rc != LDAP_SUCCESS ) {
171 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
172 progname, e->e_dn, lineno, text );
175 if( continuemode ) continue;
180 if ( SLAP_LASTMOD(be) ) {
182 struct berval vals[2];
184 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
185 vals[0].bv_val = uuidbuf;
187 vals[1].bv_val = NULL;
188 attr_merge( e, slap_schema.si_ad_entryUUID, vals );
190 ber_dupbv( &vals[0], &name );
192 vals[1].bv_val = NULL;
193 attr_merge( e, slap_schema.si_ad_creatorsName, vals);
195 ber_dupbv( &vals[0], &name );
197 vals[1].bv_val = NULL;
198 attr_merge( e, slap_schema.si_ad_modifiersName, vals);
200 ber_dupbv( &vals[0], ×tamp );
202 vals[1].bv_val = NULL;
203 attr_merge( e, slap_schema.si_ad_createTimestamp, vals );
205 ber_dupbv( &vals[0], ×tamp );
207 vals[1].bv_val = NULL;
208 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals );
210 ber_dupbv( &vals[0], &csn );
212 vals[1].bv_val = NULL;
213 attr_merge( e, slap_schema.si_ad_entryCSN, vals );
217 ID id = be->be_entry_put( be, e, &bvtext );
219 fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d): %s\n",
220 progname, e->e_dn, lineno, bvtext.bv_val );
223 if( continuemode ) continue;
228 fprintf( stderr, "added: \"%s\" (%08lx)\n",
229 e->e_dn, (long) id );
233 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
242 be->be_entry_close( be );