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>
21 #include "slapcommon.h"
24 main( int argc, char **argv )
29 int rc = EXIT_SUCCESS;
32 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
33 size_t textlen = sizeof textbuf;
36 lutil_log_initialize(argc, argv );
38 slap_tool_init( "slapadd", SLAPADD, argc, argv );
40 if( !be->be_entry_open ||
41 !be->be_entry_close ||
44 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
52 if( be->be_entry_open( be, 1 ) != 0 ) {
53 fprintf( stderr, "%s: could not open database.\n",
58 while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
59 Entry *e = str2entry( buf );
62 bvtext.bv_len = textlen;
63 bvtext.bv_val = textbuf;
66 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
69 if( continuemode ) continue;
73 /* make sure the DN is not empty */
74 if( !e->e_nname.bv_len ) {
75 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
76 progname, e->e_dn, lineno );
79 if( continuemode ) continue;
84 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
87 fprintf( stderr, "%s: line %d: "
88 "database (%s) not configured to hold \"%s\"\n",
90 be ? be->be_suffix[0].bv_val : "<none>",
92 fprintf( stderr, "%s: line %d: "
93 "database (%s) not configured to hold \"%s\"\n",
95 be ? be->be_nsuffix[0].bv_val : "<none>",
99 if( continuemode ) continue;
103 if( global_schemacheck ) {
104 Attribute *sc = attr_find( e->e_attrs,
105 slap_schema.si_ad_structuralObjectClass );
106 Attribute *oc = attr_find( e->e_attrs,
107 slap_schema.si_ad_objectClass );
110 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
111 progname, e->e_dn, lineno,
112 "no objectClass attribute");
115 if( continuemode ) continue;
120 struct berval vals[2];
122 rc = structural_class( oc->a_vals, vals,
123 NULL, &text, textbuf, textlen );
125 if( rc != LDAP_SUCCESS ) {
126 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
127 progname, e->e_dn, lineno, rc, text );
130 if( continuemode ) continue;
134 vals[1].bv_val = NULL;
135 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
140 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
142 if( rc != LDAP_SUCCESS ) {
143 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
144 progname, e->e_dn, lineno, rc, text );
147 if( continuemode ) continue;
152 if ( SLAP_LASTMOD(be) ) {
154 time_t now = slap_get_time();
155 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
156 struct berval vals[ 2 ];
158 struct berval name, timestamp, csn;
159 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
160 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
163 lutil_gentime( timebuf, sizeof(timebuf), ltm );
165 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
168 timestamp.bv_val = timebuf;
169 timestamp.bv_len = strlen(timebuf);
171 if ( be->be_rootndn.bv_len == 0 ) {
172 name.bv_val = SLAPD_ANONYMOUS;
173 name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
175 name = be->be_rootndn;
178 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
181 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
182 vals[0].bv_val = uuidbuf;
184 vals[1].bv_val = NULL;
185 attr_merge( e, slap_schema.si_ad_entryUUID, vals );
188 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
193 vals[1].bv_val = NULL;
194 attr_merge( e, slap_schema.si_ad_creatorsName, vals);
197 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
202 vals[1].bv_val = NULL;
203 attr_merge( e, slap_schema.si_ad_modifiersName, vals);
206 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
211 vals[1].bv_val = NULL;
212 attr_merge( e, slap_schema.si_ad_createTimestamp, vals );
215 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
220 vals[1].bv_val = NULL;
221 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals );
224 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
229 vals[1].bv_val = NULL;
230 attr_merge( e, slap_schema.si_ad_entryCSN, vals );
235 ID id = be->be_entry_put( be, e, &bvtext );
237 fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d): %s\n",
238 progname, e->e_dn, lineno, bvtext.bv_val );
241 if( continuemode ) continue;
246 fprintf( stderr, "added: \"%s\" (%08lx)\n",
247 e->e_dn, (long) id );
251 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
260 be->be_entry_close( be );