openldap/servers/slapd/tools/slapcommon.c

334 lines
6.4 KiB
C
Raw Normal View History

/* $OpenLDAP$ */
/*
2003-01-04 04:20:47 +08:00
* Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
* 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;
int continuemode = 0;
2001-12-27 10:03:57 +08:00
int nosubordinates = 0;
int dryrun = 0;
char *ldiffile = NULL;
FILE *ldiffp = NULL;
1999-08-20 02:13:36 +08:00
#ifdef CSRIMALLOC
char *leakfilename;
FILE *leakfile;
#endif
Backend *be = NULL;
static void
usage( int tool )
{
char *options = NULL;
fprintf( stderr,
"usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
"\t[-n databasenumber | -b suffix]", progname );
switch( tool ) {
case SLAPADD:
2003-03-18 18:38:23 +08:00
options = "\t[-l ldiffile] [-u]\n";
break;
case SLAPCAT:
options = "\t[-l ldiffile]\n";
break;
case SLAPINDEX:
2000-07-19 10:24:08 +08:00
options = "\n";
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 };
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
switch( tool ) {
case SLAPADD:
options = "b:cd:f:l:n:tuv";
break;
case SLAPINDEX:
options = "b:cd:f:n:v";
break;
case SLAPCAT:
options = "b:cd:f:l:n:v";
break;
default:
fprintf( stderr, "%s: unknown tool mode (%d)\n",
progname, tool );
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 );
base.bv_len = strlen( base.bv_val );
2003-01-21 03:13:00 +08:00
break;
case 'c': /* enable continue mode */
continuemode++;
break;
case 'd': /* turn on debugging */
1999-08-18 03:30:40 +08:00
ldap_debug += atoi( optarg );
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 'u': /* dry run */
dryrun++;
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 ) ) {
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
*/
#ifdef SLAPD_MODULES
if ( module_init() != 0 ) {
fprintf( stderr, "%s: module_init failed!\n", progname );
exit( EXIT_FAILURE );
}
#endif
rc = slap_init( mode, progname );
2003-01-21 04:17:12 +08:00
if ( rc != 0 ) {
fprintf( stderr, "%s: slap_init failed!\n", progname );
exit( EXIT_FAILURE );
}
rc = slap_schema_init();
2003-01-21 04:17:12 +08:00
if ( rc != 0 ) {
fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
exit( EXIT_FAILURE );
}
rc = read_config( conffile, 0 );
if ( rc != 0 ) {
fprintf( stderr, "%s: bad configuration file!\n", progname );
exit( EXIT_FAILURE );
}
if ( !nbackends ) {
fprintf( stderr, "No databases found in config file\n" );
exit( EXIT_FAILURE );
}
rc = glue_sub_init();
2003-01-21 04:17:12 +08:00
if ( rc != 0 ) {
fprintf( stderr, "Subordinate configuration error\n" );
exit( EXIT_FAILURE );
}
rc = slap_schema_check();
2003-01-21 04:17:12 +08:00
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;
rc = dnNormalize2( NULL, &base, &nbase );
2001-12-25 03:21:42 +08:00
if( rc != LDAP_SUCCESS ) {
fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
2002-01-06 15:21:06 +08:00
progname, base.bv_val );
exit( EXIT_FAILURE );
}
be = select_backend( &nbase, 0, 0 );
2003-03-01 20:28:45 +08:00
ber_memfree( nbase.bv_val );
if( be == NULL ) {
fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
progname, base.bv_val );
exit( EXIT_FAILURE );
}
/* If the named base is a glue master, operate on the
* entire context
*/
2003-03-04 06:56:21 +08:00
if (SLAP_GLUE_INSTANCE(be)) {
2001-12-27 10:03:57 +08:00
nosubordinates = 1;
2003-03-04 06:56:21 +08:00
}
} else if ( dbnum == -1 ) {
2003-03-04 17:46:48 +08:00
if ( nbackends <= 0 ) {
fprintf( stderr, "No available databases\n" );
exit( EXIT_FAILURE );
}
be = &backends[dbnum=0];
/* If just doing the first by default and it is a
* glue subordinate, find the master.
*/
2003-03-04 06:25:28 +08:00
while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
if (SLAP_GLUE_SUBORDINATE(be)) {
nosubordinates = 1;
}
be++;
2003-03-04 06:56:21 +08:00
dbnum++;
}
2003-03-04 17:46:48 +08:00
if ( dbnum >= nbackends ) {
2003-03-04 06:56:21 +08:00
fprintf( stderr, "Available database(s) "
"do not allow %s\n", name );
exit( EXIT_FAILURE );
}
2003-03-04 17:43:12 +08:00
if ( nosubordinates == 0 && dbnum > 0 ) {
2003-03-04 06:56:21 +08:00
#ifdef NEW_LOGGING
LDAP_LOG( BACKEND, ERR,
2003-03-04 17:43:12 +08:00
"The first database does not allow %s; using the first available one (%d)\n",
2003-03-04 17:46:48 +08:00
name, dbnum + 1, 0 );
2003-03-04 06:56:21 +08:00
#else
Debug( LDAP_DEBUG_ANY,
2003-03-04 17:43:12 +08:00
"The first database does not allow %s; using the first available one (%d)\n",
2003-03-04 17:46:48 +08:00
name, dbnum + 1, 0 );
2003-03-04 06:56:21 +08:00
#endif
}
} else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
fprintf( stderr,
"Database number selected via -n is out of range\n"
"Must be in the range 1 to %d"
" (number of databases in the config file)\n",
nbackends );
exit( EXIT_FAILURE );
} else {
be = &backends[dbnum];
}
1999-08-20 02:13:36 +08:00
#ifdef CSRIMALLOC
mal_leaktrace(1);
#endif
slap_startup( be );
}
void slap_tool_destroy( void )
{
slap_shutdown( be );
slap_destroy();
#ifdef SLAPD_MODULES
if ( slapMode == SLAP_SERVER_MODE ) {
/* always false. just pulls in necessary symbol references. */
lutil_uuidstr(NULL, 0);
}
module_kill();
#endif
2002-09-09 08:56:57 +08:00
schema_destroy();
#ifdef HAVE_TLS
ldap_pvt_tls_destroy();
#endif
config_destroy();
1999-08-20 02:13:36 +08:00
#ifdef CSRIMALLOC
mal_dumpleaktrace( leakfile );
#endif
}