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"
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 );
63 * Initialize text buffer
65 bvtext.bv_len = textlen;
66 bvtext.bv_val = textbuf;
67 bvtext.bv_val[0] = '\0';
70 fprintf( stderr, "%s: could not parse entry (line=%d)\n",
73 if( continuemode ) continue;
77 /* make sure the DN is not empty */
78 if( !e->e_nname.bv_len ) {
79 fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
80 progname, e->e_dn, lineno );
83 if( continuemode ) continue;
88 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
91 fprintf( stderr, "%s: line %d: "
92 "database (%s) not configured to hold \"%s\"\n",
94 be ? be->be_suffix[0].bv_val : "<none>",
96 fprintf( stderr, "%s: line %d: "
97 "database (%s) not configured to hold \"%s\"\n",
99 be ? be->be_nsuffix[0].bv_val : "<none>",
103 if( continuemode ) continue;
107 if( global_schemacheck ) {
108 Attribute *sc = attr_find( e->e_attrs,
109 slap_schema.si_ad_structuralObjectClass );
110 Attribute *oc = attr_find( e->e_attrs,
111 slap_schema.si_ad_objectClass );
114 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
115 progname, e->e_dn, lineno,
116 "no objectClass attribute");
119 if( continuemode ) continue;
124 struct berval vals[2];
126 rc = structural_class( oc->a_vals, vals,
127 NULL, &text, textbuf, textlen );
129 if( rc != LDAP_SUCCESS ) {
130 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
131 progname, e->e_dn, lineno, rc, text );
134 if( continuemode ) continue;
139 vals[1].bv_val = NULL;
142 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
143 vals, NULL /* FIXME */ );
145 attr_merge( e, slap_schema.si_ad_structuralObjectClass, vals );
150 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
152 if( rc != LDAP_SUCCESS ) {
153 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
154 progname, e->e_dn, lineno, rc, text );
157 if( continuemode ) continue;
162 if ( SLAP_LASTMOD(be) ) {
164 time_t now = slap_get_time();
165 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
166 struct berval vals[ 2 ];
168 struct berval name, timestamp, csn;
171 struct berval nvals[ 2 ];
174 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
175 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
178 vals[1].bv_val = NULL;
182 nvals[1].bv_val = NULL;
186 lutil_gentime( timebuf, sizeof(timebuf), ltm );
188 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
191 timestamp.bv_val = timebuf;
192 timestamp.bv_len = strlen(timebuf);
194 if ( be->be_rootndn.bv_len == 0 ) {
195 name.bv_val = SLAPD_ANONYMOUS;
196 name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
198 nname.bv_val = SLAPD_ANONYMOUS;
199 nname.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
202 name = be->be_rootdn;
204 nname = be->be_rootndn;
208 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
211 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
212 vals[0].bv_val = uuidbuf;
214 attr_merge( e, slap_schema.si_ad_entryUUID, vals, NULL );
216 attr_merge( e, slap_schema.si_ad_entryUUID, vals );
220 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
226 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
228 attr_merge( e, slap_schema.si_ad_creatorsName, vals );
232 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
238 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
240 attr_merge( e, slap_schema.si_ad_modifiersName, vals );
244 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
249 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
251 attr_merge( e, slap_schema.si_ad_createTimestamp, vals );
255 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
260 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
262 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals );
266 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
271 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
273 attr_merge( e, slap_schema.si_ad_entryCSN, vals );
279 ID id = be->be_entry_put( be, e, &bvtext );
281 fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d): %s\n",
282 progname, e->e_dn, lineno, bvtext.bv_val );
285 if( continuemode ) continue;
290 fprintf( stderr, "added: \"%s\" (%08lx)\n",
291 e->e_dn, (long) id );
295 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
304 if( be->be_entry_close( be )) rc = EXIT_FAILURE;