+/* $OpenLDAP$ */
/*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "portable.h"
#include <ac/socket.h>
#include <ac/unistd.h>
+#include <lber.h>
+#include <ldif.h>
+
#include "slapcommon.h"
int
main( int argc, char **argv )
{
- char *buf;
+ char *buf = NULL;
int lineno;
int lmax;
int rc = EXIT_SUCCESS;
+ const char *text;
+ char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
+ size_t textlen = sizeof textbuf;
+
slap_tool_init( "slapadd", SLAPADD, argc, argv );
- if( !be->be_entry_open &&
- !be->be_entry_close &&
+ if( !be->be_entry_open ||
+ !be->be_entry_close ||
!be->be_entry_put )
{
fprintf( stderr, "%s: database doesn't support necessary operations.\n",
exit( EXIT_FAILURE );
}
- buf = NULL;
lmax = 0;
lineno = 0;
}
while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
- ID id;
Entry *e = str2entry( buf );
+ struct berval bvtext;
+
+ bvtext.bv_len = textlen;
+ bvtext.bv_val = textbuf;
if( e == NULL ) {
fprintf( stderr, "%s: could not parse entry (line=%d)\n",
break;
}
- if( !noschemacheck ) {
- /* make sure the DN is valid */
- if( dn_normalize_case( e->e_ndn ) == NULL ) {
- fprintf( stderr, "%s: bad dn=\"%s\" (line=%d)\n",
- progname, e->e_dn, lineno );
+ /* make sure the DN is not empty */
+ if( !e->e_nname.bv_len ) {
+ fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
+ progname, e->e_dn, lineno );
+ rc = EXIT_FAILURE;
+ entry_free( e );
+ if( continuemode ) continue;
+ break;
+ }
+
+ /* check backend */
+ if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
+ != be )
+ {
+ fprintf( stderr, "%s: line %d: "
+ "database (%s) not configured to hold \"%s\"\n",
+ progname, lineno,
+ be ? be->be_suffix[0].bv_val : "<none>",
+ e->e_dn );
+ fprintf( stderr, "%s: line %d: "
+ "database (%s) not configured to hold \"%s\"\n",
+ progname, lineno,
+ be ? be->be_nsuffix[0].bv_val : "<none>",
+ e->e_ndn );
+ rc = EXIT_FAILURE;
+ entry_free( e );
+ if( continuemode ) continue;
+ break;
+ }
+
+ {
+ Attribute *sc = attr_find( e->e_attrs,
+ slap_schema.si_ad_structuralObjectClass );
+ Attribute *oc = attr_find( e->e_attrs,
+ slap_schema.si_ad_objectClass );
+
+ if( oc == NULL ) {
+ fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
+ progname, e->e_dn, lineno,
+ "no objectClass attribute");
rc = EXIT_FAILURE;
entry_free( e );
if( continuemode ) continue;
break;
}
+ if( sc == NULL ) {
+ struct berval vals[2];
+
+ /* int ret = */
+ structural_class( oc->a_vals, vals,
+ NULL, &text, textbuf, textlen );
+
+ if( vals[0].bv_len == 0 ) {
+ fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
+ progname, e->e_dn, lineno, text );
+ rc = EXIT_FAILURE;
+ entry_free( e );
+ if( continuemode ) continue;
+ break;
+ }
+
+ vals[1].bv_val = NULL;
+ attr_merge( e, slap_schema.si_ad_structuralObjectClass,
+ vals );
+ }
+ }
+
+ if( global_schemacheck ) {
/* check schema */
- if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
- fprintf( stderr, "%s: entry dn=\"%s\" violates schema violation (line=%d)\n",
- progname, e->e_dn, lineno );
+
+ rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
+
+ if( rc != LDAP_SUCCESS ) {
+ fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
+ progname, e->e_dn, lineno, text );
rc = EXIT_FAILURE;
entry_free( e );
if( continuemode ) continue;
break;
}
+ }
- /* check backend */
- if( select_backend( e->e_ndn ) != be ) {
- fprintf( stderr, "%s: database not configured to hold dn=\"%s\" (line=%d)\n",
- progname, e->e_dn, lineno );
+ if (!dryrun) {
+ ID id = be->be_entry_put( be, e, &bvtext );
+ if( id == NOID ) {
+ fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d): %s\n",
+ progname, e->e_dn, lineno, bvtext.bv_val );
rc = EXIT_FAILURE;
entry_free( e );
if( continuemode ) continue;
break;
}
- }
-
- id = be->be_entry_put( be, e );
-
- if( id == NOID ) {
- fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d)\n",
- progname, e->e_dn, lineno );
- rc = EXIT_FAILURE;
- entry_free( e );
- if( continuemode ) continue;
- break;
-
- } else if ( verbose ) {
- fprintf( stderr, "added: \"%s\" (%08lx)\n",
- e->e_dn, (long) id );
+
+ if ( verbose ) {
+ fprintf( stderr, "added: \"%s\" (%08lx)\n",
+ e->e_dn, (long) id );
+ }
+ } else {
+ if ( verbose ) {
+ fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
+ }
}
entry_free( e );
}
- free( buf );
+ ch_free( buf );
be->be_entry_close( be );