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
|
|
|
|
*/
|
|
|
|
/* slapcommon.c - common routine for the slap tools */
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
#include "slapcommon.h"
|
|
|
|
#include "lutil.h"
|
|
|
|
|
|
|
|
|
|
|
|
char *progname = NULL;
|
|
|
|
char *conffile = SLAPD_DEFAULT_CONFIGFILE;
|
|
|
|
int truncatemode = 0;
|
|
|
|
int verbose = 0;
|
1999-08-18 09:45:59 +08:00
|
|
|
int continuemode = 0;
|
2001-12-27 10:03:57 +08:00
|
|
|
int nosubordinates = 0;
|
1999-08-18 03:00:59 +08:00
|
|
|
|
|
|
|
char *ldiffile = NULL;
|
|
|
|
FILE *ldiffp = NULL;
|
|
|
|
|
1999-08-20 02:13:36 +08:00
|
|
|
#ifdef CSRIMALLOC
|
|
|
|
char *leakfilename;
|
|
|
|
FILE *leakfile;
|
|
|
|
#endif
|
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
Backend *be = NULL;
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage( int tool )
|
|
|
|
{
|
|
|
|
char *options = NULL;
|
|
|
|
fprintf( stderr,
|
1999-08-18 09:45:59 +08:00
|
|
|
"usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
|
2000-09-18 04:40:43 +08:00
|
|
|
"\t[-n databasenumber | -b suffix]", progname );
|
1999-08-18 03:00:59 +08:00
|
|
|
|
|
|
|
switch( tool ) {
|
|
|
|
case SLAPADD:
|
2000-09-18 04:40:43 +08:00
|
|
|
options = "\t[-l ldiffile]\n";
|
1999-08-18 03:00:59 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SLAPCAT:
|
|
|
|
options = "\t[-l ldiffile]\n";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SLAPINDEX:
|
2000-07-19 10:24:08 +08:00
|
|
|
options = "\n";
|
1999-08-18 03:00:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( options != NULL ) {
|
|
|
|
fputs( options, stderr );
|
|
|
|
}
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* slap_tool_init - initialize slap utility, handle program options.
|
|
|
|
* arguments:
|
|
|
|
* name program name
|
|
|
|
* tool tool code
|
|
|
|
* argc, argv command line arguments
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
slap_tool_init(
|
|
|
|
const char* name,
|
|
|
|
int tool,
|
|
|
|
int argc, char **argv )
|
|
|
|
{
|
|
|
|
char *options;
|
2001-12-25 03:21:42 +08:00
|
|
|
struct berval base = { 0, NULL };
|
1999-08-18 03:00:59 +08:00
|
|
|
int rc, i, dbnum;
|
|
|
|
int mode = SLAP_TOOL_MODE;
|
|
|
|
|
|
|
|
progname = lutil_progname( name, argc, argv );
|
|
|
|
|
1999-08-20 02:13:36 +08:00
|
|
|
#ifdef CSRIMALLOC
|
|
|
|
leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
|
|
|
|
sprintf( leakfilename, "%s.leak", progname );
|
|
|
|
if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
|
|
|
|
leakfile = stderr;
|
|
|
|
}
|
|
|
|
free( leakfilename );
|
|
|
|
#endif
|
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
switch( tool ) {
|
|
|
|
case SLAPADD:
|
2000-09-18 04:40:43 +08:00
|
|
|
options = "b:cd:f:l:n:tv";
|
1999-08-18 03:00:59 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SLAPINDEX:
|
1999-08-18 09:45:59 +08:00
|
|
|
options = "b:cd:f:n:v";
|
1999-08-18 03:00:59 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SLAPCAT:
|
1999-08-18 09:45:59 +08:00
|
|
|
options = "b:cd:f:l:n:v";
|
1999-08-18 03:00:59 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-08-18 05:10:48 +08:00
|
|
|
fprintf( stderr, "%s: unknown tool mode (%d)\n",
|
|
|
|
progname, tool );
|
1999-08-18 03:00:59 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
ldiffile = NULL;
|
|
|
|
conffile = SLAPD_DEFAULT_CONFIGFILE;
|
|
|
|
dbnum = -1;
|
|
|
|
while ( (i = getopt( argc, argv, options )) != EOF ) {
|
|
|
|
switch ( i ) {
|
|
|
|
case 'b':
|
2001-12-25 03:21:42 +08:00
|
|
|
base.bv_val = strdup( optarg );
|
2001-12-25 04:20:29 +08:00
|
|
|
base.bv_len = strlen( base.bv_val );
|
1999-08-18 03:00:59 +08:00
|
|
|
|
1999-08-18 09:45:59 +08:00
|
|
|
case 'c': /* enable continue mode */
|
|
|
|
continuemode++;
|
|
|
|
break;
|
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
case 'd': /* turn on debugging */
|
1999-08-18 03:30:40 +08:00
|
|
|
ldap_debug += atoi( optarg );
|
1999-08-18 03:00:59 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f': /* specify a conf file */
|
|
|
|
conffile = strdup( optarg );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l': /* LDIF file */
|
|
|
|
ldiffile = strdup( optarg );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n': /* which config file db to index */
|
|
|
|
dbnum = atoi( optarg ) - 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't': /* turn on truncate */
|
|
|
|
truncatemode++;
|
|
|
|
mode |= SLAP_TRUNCATE_MODE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v': /* turn on verbose */
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
usage( tool );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-25 03:21:42 +08:00
|
|
|
if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
|
1999-08-18 03:00:59 +08:00
|
|
|
usage( tool );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ldiffile == NULL ) {
|
|
|
|
ldiffp = tool == SLAPCAT ? stdout : stdin;
|
|
|
|
|
|
|
|
} else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
|
|
|
|
== NULL )
|
|
|
|
{
|
|
|
|
perror( ldiffile );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize stuff and figure out which backend we're dealing with
|
|
|
|
*/
|
|
|
|
|
|
|
|
rc = slap_init( mode, progname );
|
|
|
|
|
|
|
|
if (rc != 0 ) {
|
|
|
|
fprintf( stderr, "%s: slap_init failed!\n", progname );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
2000-05-16 04:04:36 +08:00
|
|
|
rc = schema_init();
|
|
|
|
|
|
|
|
if (rc != 0 ) {
|
|
|
|
fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
read_config( conffile );
|
|
|
|
|
|
|
|
if ( !nbackends ) {
|
|
|
|
fprintf( stderr, "No databases found in config file\n" );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
2001-12-10 20:09:40 +08:00
|
|
|
rc = glue_sub_init();
|
|
|
|
|
|
|
|
if (rc != 0 ) {
|
|
|
|
fprintf( stderr, "Subordinate configuration error\n" );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
2000-05-16 04:04:36 +08:00
|
|
|
rc = schema_prep();
|
|
|
|
|
|
|
|
if (rc != 0 ) {
|
|
|
|
fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
2001-12-25 03:21:42 +08:00
|
|
|
if( base.bv_val != NULL ) {
|
|
|
|
struct berval *nbase = NULL;
|
1999-08-18 03:00:59 +08:00
|
|
|
|
2001-12-25 03:21:42 +08:00
|
|
|
rc = dnNormalize( NULL, &base, &nbase );
|
|
|
|
if( rc != LDAP_SUCCESS ) {
|
1999-08-18 03:00:59 +08:00
|
|
|
fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
|
|
|
|
progname, base );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
2001-12-25 10:30:01 +08:00
|
|
|
be = select_backend( nbase, 0, 0 );
|
2001-12-25 03:21:42 +08:00
|
|
|
ber_bvfree( nbase );
|
1999-08-18 03:00:59 +08:00
|
|
|
|
|
|
|
if( be == NULL ) {
|
|
|
|
fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
|
2001-12-25 04:20:29 +08:00
|
|
|
progname, base.bv_val );
|
1999-08-18 03:00:59 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
2001-12-10 20:09:40 +08:00
|
|
|
/* If the named base is a glue master, operate on the
|
|
|
|
* entire context
|
|
|
|
*/
|
|
|
|
if (be->be_glueflags & SLAP_GLUE_INSTANCE)
|
2001-12-27 10:03:57 +08:00
|
|
|
nosubordinates = 1;
|
1999-08-18 03:00:59 +08:00
|
|
|
|
|
|
|
} else if ( dbnum == -1 ) {
|
|
|
|
be = &backends[dbnum=0];
|
2001-12-10 20:09:40 +08:00
|
|
|
/* If just doing the first by default and it is a
|
|
|
|
* glue subordinate, find the master.
|
|
|
|
*/
|
|
|
|
while (be->be_glueflags & SLAP_GLUE_SUBORDINATE) {
|
2001-12-27 10:03:57 +08:00
|
|
|
nosubordinates = 1;
|
2001-12-10 20:09:40 +08:00
|
|
|
be++;
|
|
|
|
}
|
1999-08-18 03:00:59 +08:00
|
|
|
|
|
|
|
} else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
|
|
|
|
fprintf( stderr,
|
|
|
|
"Database number selected via -n is out of range\n"
|
2001-12-25 04:20:29 +08:00
|
|
|
"Must be in the range 1 to %d"
|
|
|
|
" (number of databases in the config file)\n",
|
1999-08-18 03:00:59 +08:00
|
|
|
nbackends );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
be = &backends[dbnum];
|
|
|
|
}
|
1999-08-20 01:20:15 +08:00
|
|
|
|
1999-08-20 02:13:36 +08:00
|
|
|
#ifdef CSRIMALLOC
|
|
|
|
mal_leaktrace(1);
|
|
|
|
#endif
|
|
|
|
|
1999-08-20 01:20:15 +08:00
|
|
|
slap_startup( be );
|
|
|
|
}
|
|
|
|
|
|
|
|
void slap_tool_destroy( void )
|
|
|
|
{
|
|
|
|
slap_shutdown( be );
|
|
|
|
slap_destroy();
|
1999-08-20 02:13:36 +08:00
|
|
|
|
|
|
|
#ifdef CSRIMALLOC
|
|
|
|
mal_dumpleaktrace( leakfile );
|
|
|
|
#endif
|
1999-08-18 03:00:59 +08:00
|
|
|
}
|