1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
1999-08-18 03:00:59 +08:00
|
|
|
/*
|
2002-01-05 05:17:25 +08:00
|
|
|
* Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
|
1999-08-18 03:00:59 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
|
|
|
|
|
|
|
#include <ac/ctype.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
2001-12-09 10:34:45 +08:00
|
|
|
#include <lber.h>
|
|
|
|
#include <ldif.h>
|
2002-08-24 08:55:24 +08:00
|
|
|
#include <lutil.h>
|
2001-12-09 10:34:45 +08:00
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
#include "slapcommon.h"
|
|
|
|
|
|
|
|
int
|
|
|
|
main( int argc, char **argv )
|
|
|
|
{
|
2002-01-13 13:00:59 +08:00
|
|
|
char *buf = NULL;
|
1999-08-18 03:00:59 +08:00
|
|
|
int lineno;
|
|
|
|
int lmax;
|
|
|
|
int rc = EXIT_SUCCESS;
|
|
|
|
|
2001-12-20 08:34:36 +08:00
|
|
|
const char *text;
|
2002-01-13 00:00:51 +08:00
|
|
|
char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
|
2001-12-20 08:34:36 +08:00
|
|
|
size_t textlen = sizeof textbuf;
|
|
|
|
|
2002-07-12 04:33:24 +08:00
|
|
|
#ifdef NEW_LOGGING
|
|
|
|
lutil_log_initialize(argc, argv );
|
|
|
|
#endif
|
1999-08-18 03:00:59 +08:00
|
|
|
slap_tool_init( "slapadd", SLAPADD, argc, argv );
|
|
|
|
|
2000-08-21 11:08:01 +08:00
|
|
|
if( !be->be_entry_open ||
|
|
|
|
!be->be_entry_close ||
|
1999-08-18 03:00:59 +08:00
|
|
|
!be->be_entry_put )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "%s: database doesn't support necessary operations.\n",
|
|
|
|
progname );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
lmax = 0;
|
1999-08-22 11:32:13 +08:00
|
|
|
lineno = 0;
|
1999-08-18 03:00:59 +08:00
|
|
|
|
|
|
|
if( be->be_entry_open( be, 1 ) != 0 ) {
|
|
|
|
fprintf( stderr, "%s: could not open database.\n",
|
|
|
|
progname );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
|
|
|
|
Entry *e = str2entry( buf );
|
2002-02-24 07:47:37 +08:00
|
|
|
struct berval bvtext;
|
|
|
|
|
|
|
|
bvtext.bv_len = textlen;
|
|
|
|
bvtext.bv_val = textbuf;
|
1999-08-18 03:00:59 +08:00
|
|
|
|
|
|
|
if( e == NULL ) {
|
1999-08-18 09:45:59 +08:00
|
|
|
fprintf( stderr, "%s: could not parse entry (line=%d)\n",
|
1999-08-18 03:00:59 +08:00
|
|
|
progname, lineno );
|
|
|
|
rc = EXIT_FAILURE;
|
1999-08-18 09:45:59 +08:00
|
|
|
if( continuemode ) continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-09-16 14:23:53 +08:00
|
|
|
/* make sure the DN is not empty */
|
2001-12-25 03:21:42 +08:00
|
|
|
if( !e->e_nname.bv_len ) {
|
2000-09-08 13:20:46 +08:00
|
|
|
fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
|
|
|
|
progname, e->e_dn, lineno );
|
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
entry_free( e );
|
|
|
|
if( continuemode ) continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-09-16 14:23:53 +08:00
|
|
|
/* check backend */
|
2001-12-27 10:03:57 +08:00
|
|
|
if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
|
|
|
|
!= be )
|
|
|
|
{
|
2001-12-25 04:07:19 +08:00
|
|
|
fprintf( stderr, "%s: line %d: "
|
|
|
|
"database (%s) not configured to hold \"%s\"\n",
|
|
|
|
progname, lineno,
|
2002-05-02 02:50:14 +08:00
|
|
|
be ? be->be_suffix[0].bv_val : "<none>",
|
2001-12-25 04:07:19 +08:00
|
|
|
e->e_dn );
|
|
|
|
fprintf( stderr, "%s: line %d: "
|
|
|
|
"database (%s) not configured to hold \"%s\"\n",
|
|
|
|
progname, lineno,
|
2002-05-02 02:50:14 +08:00
|
|
|
be ? be->be_nsuffix[0].bv_val : "<none>",
|
2001-12-25 04:07:19 +08:00
|
|
|
e->e_ndn );
|
2000-09-16 14:23:53 +08:00
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
entry_free( e );
|
|
|
|
if( continuemode ) continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-08-27 09:27:42 +08:00
|
|
|
if( global_schemacheck ) {
|
2001-12-20 06:41:00 +08:00
|
|
|
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 ) {
|
2002-01-02 19:00:36 +08:00
|
|
|
struct berval vals[2];
|
2001-12-20 08:34:36 +08:00
|
|
|
|
2002-04-08 17:43:22 +08:00
|
|
|
/* int ret = */
|
2002-08-27 09:27:42 +08:00
|
|
|
structural_class( oc->a_vals, vals,
|
2002-01-03 07:26:20 +08:00
|
|
|
NULL, &text, textbuf, textlen );
|
2001-12-20 06:41:00 +08:00
|
|
|
|
2002-01-02 19:00:36 +08:00
|
|
|
if( vals[0].bv_len == 0 ) {
|
2001-12-20 06:41:00 +08:00
|
|
|
fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
|
2002-08-27 09:27:42 +08:00
|
|
|
progname, e->e_dn, lineno, text );
|
2001-12-20 06:41:00 +08:00
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
entry_free( e );
|
|
|
|
if( continuemode ) continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-01-02 19:00:36 +08:00
|
|
|
vals[1].bv_val = NULL;
|
2001-12-20 06:41:00 +08:00
|
|
|
attr_merge( e, slap_schema.si_ad_structuralObjectClass,
|
|
|
|
vals );
|
|
|
|
}
|
2002-08-27 09:27:42 +08:00
|
|
|
|
|
|
|
/* check schema */
|
|
|
|
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;
|
|
|
|
}
|
2001-12-20 06:41:00 +08:00
|
|
|
}
|
|
|
|
|
2002-08-21 08:11:32 +08:00
|
|
|
if ( SLAP_LASTMOD(be) ) {
|
|
|
|
struct tm *ltm;
|
|
|
|
time_t now = slap_get_time();
|
2002-08-27 09:27:42 +08:00
|
|
|
char uuidbuf[40];
|
|
|
|
struct berval vals[2];
|
|
|
|
|
|
|
|
struct berval name, timestamp, csn;
|
|
|
|
char timebuf[22];
|
|
|
|
char csnbuf[64];
|
2002-08-21 08:11:32 +08:00
|
|
|
|
|
|
|
ltm = gmtime(&now);
|
|
|
|
lutil_gentime( timebuf, sizeof(timebuf), ltm );
|
|
|
|
|
|
|
|
csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
|
|
|
|
csn.bv_val = csnbuf;
|
|
|
|
|
|
|
|
timestamp.bv_val = timebuf;
|
|
|
|
timestamp.bv_len = strlen(timebuf);
|
|
|
|
|
|
|
|
if ( be->be_rootndn.bv_len == 0 ) {
|
|
|
|
name.bv_val = SLAPD_ANONYMOUS;
|
|
|
|
name.bv_len = sizeof(SLAPD_ANONYMOUS) - 1;
|
|
|
|
} else {
|
|
|
|
name = be->be_rootndn;
|
|
|
|
}
|
|
|
|
|
2002-08-27 09:27:42 +08:00
|
|
|
if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
|
|
|
|
== NULL )
|
|
|
|
{
|
|
|
|
vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
|
|
|
|
vals[0].bv_val = uuidbuf;
|
|
|
|
vals[1].bv_len = 0;
|
|
|
|
vals[1].bv_val = NULL;
|
|
|
|
attr_merge( e, slap_schema.si_ad_entryUUID, vals );
|
|
|
|
}
|
2001-06-06 08:23:56 +08:00
|
|
|
|
2002-08-27 09:27:42 +08:00
|
|
|
if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
|
|
|
|
== NULL )
|
|
|
|
{
|
|
|
|
ber_dupbv( &vals[0], &name );
|
|
|
|
vals[1].bv_len = 0;
|
|
|
|
vals[1].bv_val = NULL;
|
|
|
|
attr_merge( e, slap_schema.si_ad_creatorsName, vals);
|
|
|
|
}
|
2001-06-06 08:23:56 +08:00
|
|
|
|
2002-08-27 09:27:42 +08:00
|
|
|
if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
|
|
|
|
== NULL )
|
|
|
|
{
|
|
|
|
ber_dupbv( &vals[0], &name );
|
|
|
|
vals[1].bv_len = 0;
|
|
|
|
vals[1].bv_val = NULL;
|
|
|
|
attr_merge( e, slap_schema.si_ad_modifiersName, vals);
|
1999-08-18 09:45:59 +08:00
|
|
|
}
|
1999-08-18 03:00:59 +08:00
|
|
|
|
2002-08-27 09:27:42 +08:00
|
|
|
if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
|
|
|
|
== NULL )
|
|
|
|
{
|
|
|
|
ber_dupbv( &vals[0], ×tamp );
|
|
|
|
vals[1].bv_len = 0;
|
|
|
|
vals[1].bv_val = NULL;
|
|
|
|
attr_merge( e, slap_schema.si_ad_createTimestamp, vals );
|
|
|
|
}
|
2002-08-21 08:11:32 +08:00
|
|
|
|
2002-08-27 09:27:42 +08:00
|
|
|
if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
|
|
|
|
== NULL )
|
|
|
|
{
|
|
|
|
ber_dupbv( &vals[0], ×tamp );
|
|
|
|
vals[1].bv_len = 0;
|
|
|
|
vals[1].bv_val = NULL;
|
|
|
|
attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
|
|
|
|
== NULL )
|
|
|
|
{
|
|
|
|
ber_dupbv( &vals[0], &csn );
|
|
|
|
vals[1].bv_len = 0;
|
|
|
|
vals[1].bv_val = NULL;
|
|
|
|
attr_merge( e, slap_schema.si_ad_entryCSN, vals );
|
|
|
|
}
|
2002-08-21 08:11:32 +08:00
|
|
|
}
|
|
|
|
|
2002-01-29 04:36:29 +08:00
|
|
|
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;
|
|
|
|
}
|
2000-09-17 06:46:52 +08:00
|
|
|
|
2002-01-29 04:36:29 +08:00
|
|
|
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 );
|
|
|
|
}
|
1999-08-18 03:00:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
entry_free( e );
|
|
|
|
}
|
|
|
|
|
2000-05-28 01:06:11 +08:00
|
|
|
ch_free( buf );
|
1999-08-20 02:19:20 +08:00
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
be->be_entry_close( be );
|
|
|
|
|
|
|
|
if( be->be_sync ) {
|
|
|
|
be->be_sync( be );
|
|
|
|
}
|
|
|
|
|
1999-08-20 01:20:15 +08:00
|
|
|
slap_tool_destroy();
|
1999-08-18 03:00:59 +08:00
|
|
|
return rc;
|
|
|
|
}
|