openldap/servers/slapd/slapcommon.c

776 lines
16 KiB
C
Raw Normal View History

/* slapcommon.c - common routine for the slap tools */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
2007-01-03 04:00:42 +08:00
* Copyright 1998-2007 The OpenLDAP Foundation.
* Portions Copyright 1998-2003 Kurt D. Zeilenga.
* Portions Copyright 2003 IBM Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
/* ACKNOWLEDGEMENTS:
* This work was initially developed by Kurt Zeilenga for inclusion
* in OpenLDAP Software. Additional signficant contributors include
* Jong Hyuk Choi
* Hallvard B. Furuseth
* Howard Chu
* Pierangelo Masarati
*/
#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"
#include "ldif.h"
tool_vars tool_globals;
#ifdef CSRIMALLOC
static char *leakfilename;
static FILE *leakfile;
#endif
static LDIFFP dummy;
2007-05-12 07:04:33 +08:00
#if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
2006-05-25 01:57:13 +08:00
int start_syslog;
static char **syslog_unknowns;
#ifdef LOG_LOCAL4
static int syslogUser = SLAP_DEFAULT_SYSLOG_USER;
#endif /* LOG_LOCAL4 */
2007-05-12 07:04:33 +08:00
#endif /* LDAP_DEBUG && LDAP_SYSLOG */
2006-05-25 01:57:13 +08:00
static void
usage( int tool, const char *progname )
{
char *options = NULL;
fprintf( stderr,
2006-05-25 01:57:13 +08:00
"usage: %s [-v] [-d debuglevel] [-f configfile] [-F configdir] [-o <name>[=<value>]]",
2004-03-18 07:25:09 +08:00
progname );
switch( tool ) {
case SLAPACL:
options = "\n\t[-U authcID | -D authcDN] [-X authzID | -o authzDN=<DN>]"
2006-05-25 01:57:13 +08:00
"\n\t-b DN [-u] [attr[/access][:value]] [...]\n";
break;
case SLAPADD:
options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]\n"
"\t[-l ldiffile] [-j linenumber] [-q] [-u] [-s] [-w]\n";
break;
case SLAPAUTH:
options = "\n\t[-U authcID] [-X authzID] [-R realm] [-M mech] ID [...]\n";
break;
case SLAPCAT:
options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]"
2005-01-15 00:23:50 +08:00
" [-l ldiffile] [-a filter]\n";
2004-03-18 07:25:09 +08:00
break;
case SLAPDN:
options = "\n\t[-N | -P] DN [...]\n";
break;
case SLAPINDEX:
options = " [-c]\n\t[-g] [-n databasenumber | -b suffix] [attr ...] [-q]\n";
2004-04-14 01:18:03 +08:00
break;
2004-04-20 08:08:44 +08:00
case SLAPTEST:
options = " [-u]\n";
2004-04-20 08:08:44 +08:00
break;
}
2004-03-18 07:25:09 +08:00
if ( options != NULL ) {
fputs( options, stderr );
}
exit( EXIT_FAILURE );
}
static int
2006-05-25 01:57:13 +08:00
parse_slapopt( void )
{
2006-05-25 01:57:13 +08:00
size_t len = 0;
char *p;
p = strchr( optarg, '=' );
2006-05-25 01:57:13 +08:00
if ( p != NULL ) {
len = p - optarg;
p++;
}
if ( strncasecmp( optarg, "sockurl", len ) == 0 ) {
if ( !BER_BVISNULL( &listener_url ) ) {
ber_memfree( listener_url.bv_val );
}
ber_str2bv( p, 0, 1, &listener_url );
} else if ( strncasecmp( optarg, "domain", len ) == 0 ) {
if ( !BER_BVISNULL( &peer_domain ) ) {
ber_memfree( peer_domain.bv_val );
}
ber_str2bv( p, 0, 1, &peer_domain );
} else if ( strncasecmp( optarg, "peername", len ) == 0 ) {
if ( !BER_BVISNULL( &peer_name ) ) {
ber_memfree( peer_name.bv_val );
}
ber_str2bv( p, 0, 1, &peer_name );
} else if ( strncasecmp( optarg, "sockname", len ) == 0 ) {
if ( !BER_BVISNULL( &sock_name ) ) {
ber_memfree( sock_name.bv_val );
}
ber_str2bv( p, 0, 1, &sock_name );
} else if ( strncasecmp( optarg, "ssf", len ) == 0 ) {
if ( lutil_atou( &ssf, p ) ) {
Debug( LDAP_DEBUG_ANY, "unable to parse ssf=\"%s\".\n", p, 0, 0 );
return -1;
}
} else if ( strncasecmp( optarg, "transport_ssf", len ) == 0 ) {
if ( lutil_atou( &transport_ssf, p ) ) {
Debug( LDAP_DEBUG_ANY, "unable to parse transport_ssf=\"%s\".\n", p, 0, 0 );
return -1;
}
} else if ( strncasecmp( optarg, "tls_ssf", len ) == 0 ) {
if ( lutil_atou( &tls_ssf, p ) ) {
Debug( LDAP_DEBUG_ANY, "unable to parse tls_ssf=\"%s\".\n", p, 0, 0 );
return -1;
}
} else if ( strncasecmp( optarg, "sasl_ssf", len ) == 0 ) {
if ( lutil_atou( &sasl_ssf, p ) ) {
Debug( LDAP_DEBUG_ANY, "unable to parse sasl_ssf=\"%s\".\n", p, 0, 0 );
return -1;
}
} else if ( strncasecmp( optarg, "authzDN", len ) == 0 ) {
ber_str2bv( p, 0, 1, &authzDN );
2007-05-12 07:04:33 +08:00
#if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
2006-05-25 01:57:13 +08:00
} else if ( strncasecmp( optarg, "syslog", len ) == 0 ) {
if ( parse_debug_level( p, &ldap_syslog, &syslog_unknowns ) ) {
return -1;
}
start_syslog = 1;
} else if ( strncasecmp( optarg, "syslog-level", len ) == 0 ) {
if ( parse_syslog_level( p, &ldap_syslog_level ) ) {
return -1;
}
start_syslog = 1;
#ifdef LOG_LOCAL4
} else if ( strncasecmp( optarg, "syslog-user", len ) == 0 ) {
if ( parse_syslog_user( p, &syslogUser ) ) {
return -1;
}
start_syslog = 1;
#endif /* LOG_LOCAL4 */
2007-05-12 07:04:33 +08:00
#endif /* LDAP_DEBUG && LDAP_SYSLOG */
2006-05-25 01:57:13 +08:00
} else {
return -1;
}
return 0;
}
/*
* slap_tool_init - initialize slap utility, handle program options.
* arguments:
* name program name
* tool tool code
* argc, argv command line arguments
*/
2005-12-17 06:24:53 +08:00
static int need_shutdown;
void
slap_tool_init(
const char* progname,
int tool,
int argc, char **argv )
{
char *options;
char *conffile = NULL;
char *confdir = NULL;
2004-04-07 12:11:43 +08:00
struct berval base = BER_BVNULL;
2004-09-09 05:44:28 +08:00
char *filterstr = NULL;
char *subtree = NULL;
char *ldiffile = NULL;
char **debug_unknowns = NULL;
int rc, i, dbnum;
int mode = SLAP_TOOL_MODE;
int truncatemode = 0;
int use_glue = 1;
#ifdef LDAP_DEBUG
/* tools default to "none", so that at least LDAP_DEBUG_ANY
* messages show up; use -d 0 to reset */
slap_debug = LDAP_DEBUG_NONE;
#endif
2006-05-25 01:57:13 +08:00
ldap_syslog = 0;
#ifdef CSRIMALLOC
2005-01-12 22:34:46 +08:00
leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
sprintf( leakfilename, "%s.leak", progname );
if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
leakfile = stderr;
}
free( leakfilename );
#endif
switch( tool ) {
case SLAPADD:
options = "b:cd:f:F:gj:l:n:o:qsS:tuvw";
break;
case SLAPCAT:
2006-05-25 01:57:13 +08:00
options = "a:b:cd:f:F:gl:n:o:s:v";
mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
break;
2004-03-18 07:25:09 +08:00
case SLAPDN:
2006-05-25 01:57:13 +08:00
options = "d:f:F:No:Pv";
mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
2004-03-18 07:25:09 +08:00
break;
case SLAPTEST:
options = "d:f:F:o:Quv";
mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
break;
2004-04-16 14:12:13 +08:00
case SLAPAUTH:
2006-05-25 01:57:13 +08:00
options = "d:f:F:M:o:R:U:vX:";
mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
2004-04-14 01:18:03 +08:00
break;
2004-03-18 07:25:09 +08:00
case SLAPINDEX:
2006-05-25 01:57:13 +08:00
options = "b:cd:f:F:gn:o:qv";
2004-03-18 07:25:09 +08:00
mode |= SLAP_TOOL_READMAIN;
break;
2004-04-20 08:08:44 +08:00
case SLAPACL:
options = "b:D:d:f:F:o:uU:vX:";
mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
2004-04-20 08:08:44 +08:00
break;
default:
2004-09-09 05:44:28 +08:00
fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
exit( EXIT_FAILURE );
}
dbnum = -1;
while ( (i = getopt( argc, argv, options )) != EOF ) {
switch ( i ) {
2004-09-09 05:44:28 +08:00
case 'a':
2006-08-29 09:36:52 +08:00
filterstr = ch_strdup( optarg );
2004-09-09 05:44:28 +08:00
break;
case 'b':
2004-04-14 01:18:03 +08:00
ber_str2bv( optarg, 0, 1, &base );
break;
case 'c': /* enable continue mode */
continuemode++;
break;
case 'd': { /* turn on debugging */
int level = 0;
if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
usage( tool, progname );
}
#ifdef LDAP_DEBUG
if ( level == 0 ) {
/* allow to reset log level */
slap_debug = 0;
} else {
slap_debug |= level;
}
#else
if ( level != 0 )
fputs( "must compile with LDAP_DEBUG for debugging\n",
stderr );
#endif
} break;
2004-04-20 08:08:44 +08:00
case 'D':
ber_str2bv( optarg, 0, 1, &authcDN );
2004-04-20 08:08:44 +08:00
break;
case 'f': /* specify a conf file */
2006-08-29 09:36:52 +08:00
conffile = ch_strdup( optarg );
break;
case 'F': /* specify a conf dir */
2006-08-29 09:36:52 +08:00
confdir = ch_strdup( optarg );
break;
case 'g': /* disable subordinate glue */
use_glue = 0;
break;
case 'j': /* jump to linenumber */
if ( lutil_atoi( &jumpline, optarg ) ) {
usage( tool, progname );
}
break;
case 'l': /* LDIF file */
2006-08-29 09:36:52 +08:00
ldiffile = ch_strdup( optarg );
break;
case 'M':
ber_str2bv( optarg, 0, 0, &mech );
break;
case 'N':
if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
usage( tool, progname );
}
dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
break;
case 'n': /* which config file db to index */
if ( lutil_atoi( &dbnum, optarg ) ) {
usage( tool, progname );
}
break;
case 'o':
2006-05-25 01:57:13 +08:00
if ( parse_slapopt() ) {
usage( tool, progname );
}
break;
case 'P':
if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
usage( tool, progname );
}
dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
break;
case 'Q':
quiet++;
slap_debug = 0;
break;
case 'q': /* turn on quick */
mode |= SLAP_TOOL_QUICK;
break;
case 'R':
realm = optarg;
break;
case 'S':
if ( lutil_atou( &csnsid, optarg )
|| csnsid > SLAP_SYNC_SID_MAX )
{
usage( tool, progname );
}
break;
case 's': /* dump subtree */
if ( tool == SLAPADD )
mode |= SLAP_TOOL_NO_SCHEMA_CHECK;
else if ( tool == SLAPCAT )
2006-08-29 09:36:52 +08:00
subtree = ch_strdup( optarg );
break;
case 't': /* turn on truncate */
truncatemode++;
mode |= SLAP_TRUNCATE_MODE;
break;
2004-04-14 01:18:03 +08:00
case 'U':
ber_str2bv( optarg, 0, 0, &authcID );
break;
case 'u': /* dry run */
dryrun++;
break;
case 'v': /* turn on verbose */
verbose++;
break;
2005-01-15 00:23:50 +08:00
case 'w': /* write context csn at the end */
update_ctxcsn++;
break;
2004-04-14 01:18:03 +08:00
case 'X':
ber_str2bv( optarg, 0, 0, &authzID );
break;
default:
usage( tool, progname );
break;
}
}
2007-05-12 07:04:33 +08:00
#if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
2006-05-25 01:57:13 +08:00
if ( start_syslog ) {
char *logName;
#ifdef HAVE_EBCDIC
logName = ch_strdup( progname );
__atoe( logName );
#else
logName = (char *)progname;
#endif
#ifdef LOG_LOCAL4
openlog( logName, OPENLOG_OPTIONS, syslogUser );
#elif defined LOG_DEBUG
2006-05-25 01:57:13 +08:00
openlog( logName, OPENLOG_OPTIONS );
#endif
#ifdef HAVE_EBCDIC
free( logName );
#endif
}
2007-05-12 07:04:33 +08:00
#endif /* LDAP_DEBUG && LDAP_SYSLOG */
2006-05-25 01:57:13 +08:00
2004-03-18 07:25:09 +08:00
switch ( tool ) {
case SLAPADD:
case SLAPCAT:
if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
usage( tool, progname );
}
break;
case SLAPINDEX:
if ( dbnum >= 0 && base.bv_val != NULL ) {
usage( tool, progname );
}
break;
2004-03-18 07:25:09 +08:00
case SLAPDN:
if ( argc == optind ) {
usage( tool, progname );
}
2004-03-18 07:25:09 +08:00
break;
2004-04-16 14:12:13 +08:00
case SLAPAUTH:
2004-04-14 01:18:03 +08:00
if ( argc == optind && BER_BVISNULL( &authcID ) ) {
usage( tool, progname );
}
break;
2004-03-18 07:25:09 +08:00
case SLAPTEST:
if ( argc != optind ) {
usage( tool, progname );
}
break;
2004-04-20 08:08:44 +08:00
case SLAPACL:
if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
usage( tool, progname );
}
if ( BER_BVISNULL( &base ) ) {
usage( tool, progname );
}
ber_dupbv( &baseDN, &base );
break;
2004-03-18 07:25:09 +08:00
default:
break;
}
if ( ldiffile == NULL ) {
dummy.fp = tool == SLAPCAT ? stdout : stdin;
ldiffp = &dummy;
} else if ((ldiffp = ldif_open( ldiffile, tool == SLAPCAT ? "w" : "r" ))
== NULL )
{
perror( ldiffile );
exit( EXIT_FAILURE );
}
/*
* initialize stuff and figure out which backend we're dealing with
*/
2005-03-04 17:26:50 +08:00
rc = slap_init( mode, progname );
if ( rc != 0 ) {
2005-03-04 17:26:50 +08:00
fprintf( stderr, "%s: slap_init failed!\n", progname );
exit( EXIT_FAILURE );
}
rc = read_config( conffile, confdir );
if ( rc != 0 ) {
2005-08-12 05:02:32 +08:00
fprintf( stderr, "%s: bad configuration %s!\n",
progname, confdir ? "directory" : "file" );
exit( EXIT_FAILURE );
}
if ( debug_unknowns ) {
rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
ldap_charray_free( debug_unknowns );
debug_unknowns = NULL;
if ( rc )
exit( EXIT_FAILURE );
}
2007-05-12 07:04:33 +08:00
#if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
2006-05-25 01:57:13 +08:00
if ( syslog_unknowns ) {
rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
ldap_charray_free( syslog_unknowns );
syslog_unknowns = NULL;
if ( rc )
exit( EXIT_FAILURE );
}
2006-09-14 15:29:46 +08:00
#endif
2006-05-25 01:57:13 +08:00
2005-01-23 04:18:38 +08:00
at_oc_cache = 1;
2004-03-10 10:44:38 +08:00
2004-03-18 07:25:09 +08:00
switch ( tool ) {
case SLAPADD:
case SLAPCAT:
case SLAPINDEX:
if ( !nbackends ) {
fprintf( stderr, "No databases found "
"in config file\n" );
exit( EXIT_FAILURE );
}
break;
default:
break;
}
if ( use_glue ) {
rc = glue_sub_attach();
if ( rc != 0 ) {
fprintf( stderr,
"%s: subordinate configuration error\n", progname );
exit( EXIT_FAILURE );
}
}
rc = slap_schema_check();
if ( rc != 0 ) {
fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
exit( EXIT_FAILURE );
}
2004-03-18 07:25:09 +08:00
switch ( tool ) {
case SLAPDN:
case SLAPTEST:
2004-04-16 14:12:13 +08:00
case SLAPAUTH:
2004-04-14 01:18:03 +08:00
be = NULL;
goto startup;
2004-03-18 07:25:09 +08:00
default:
break;
}
2004-09-09 05:44:28 +08:00
if( filterstr ) {
filter = str2filter( filterstr );
if( filter == NULL ) {
fprintf( stderr, "Invalid filter '%s'\n", filterstr );
exit( EXIT_FAILURE );
}
}
if( subtree ) {
struct berval val;
2004-04-14 01:18:03 +08:00
ber_str2bv( subtree, 0, 0, &val );
rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
if( rc != LDAP_SUCCESS ) {
2004-09-09 05:44:28 +08:00
fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
exit( EXIT_FAILURE );
}
2004-09-09 05:44:28 +08:00
if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
base = val;
2004-09-09 05:44:28 +08:00
} else {
free( subtree );
2004-09-09 05:44:28 +08:00
}
}
if( base.bv_val != NULL ) {
struct berval nbase;
rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
if( rc != LDAP_SUCCESS ) {
fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
progname, base.bv_val );
exit( EXIT_FAILURE );
}
be = select_backend( &nbase, 0 );
ber_memfree( nbase.bv_val );
switch ( tool ) {
case SLAPACL:
2004-04-20 22:55:02 +08:00
goto startup;
default:
break;
2004-04-20 22:55:02 +08:00
}
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
*/
2005-04-13 04:33:45 +08:00
if ( SLAP_GLUE_INSTANCE( be ) ) {
nosubordinates = 1;
}
} else if ( dbnum == -1 ) {
2005-08-10 19:26:32 +08:00
/* no suffix and no dbnum specified, just default to
* the first available database
*/
if ( nbackends <= 0 ) {
fprintf( stderr, "No available databases\n" );
exit( EXIT_FAILURE );
}
LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
dbnum++;
2005-08-10 19:26:32 +08:00
/* db #0 is cn=config, don't select it as a default */
if ( dbnum < 1 ) continue;
if ( SLAP_MONITOR(be))
continue;
/* If just doing the first by default and it is a
* glue subordinate, find the master.
*/
if ( SLAP_GLUE_SUBORDINATE(be) ) {
nosubordinates = 1;
continue;
}
break;
}
if ( !be ) {
fprintf( stderr, "Available database(s) "
"do not allow %s\n", progname );
exit( EXIT_FAILURE );
}
2005-08-10 19:26:32 +08:00
if ( nosubordinates == 0 && dbnum > 1 ) {
Debug( LDAP_DEBUG_ANY,
2004-09-09 05:44:28 +08:00
"The first database does not allow %s;"
" using the first available one (%d)\n",
2005-08-10 19:26:32 +08:00
progname, dbnum, 0 );
}
} else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
fprintf( stderr,
"Database number selected via -n is out of range\n"
2005-08-10 19:26:32 +08:00
"Must be in the range 0 to %d"
" (number of configured databases)\n",
nbackends-1 );
exit( EXIT_FAILURE );
} else {
LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
if ( dbnum == 0 ) break;
dbnum--;
}
}
2004-04-14 01:18:03 +08:00
startup:;
#ifdef CSRIMALLOC
mal_leaktrace(1);
#endif
2005-08-12 07:35:47 +08:00
if ( conffile != NULL ) {
ch_free( conffile );
}
if ( ldiffile != NULL ) {
ch_free( ldiffile );
}
/* slapdn doesn't specify a backend to startup */
if ( !dryrun && tool != SLAPDN ) {
2005-12-17 06:24:53 +08:00
need_shutdown = 1;
if ( slap_startup( be ) ) {
switch ( tool ) {
case SLAPTEST:
fprintf( stderr, "slap_startup failed "
"(test would succeed using "
"the -u switch)\n" );
break;
default:
fprintf( stderr, "slap_startup failed\n" );
break;
}
2005-01-12 22:31:03 +08:00
exit( EXIT_FAILURE );
2005-01-12 22:31:03 +08:00
}
}
}
void slap_tool_destroy( void )
{
2005-04-01 03:34:36 +08:00
if ( !dryrun ) {
2005-12-17 06:24:53 +08:00
if ( need_shutdown ) {
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
schema_destroy();
#ifdef HAVE_TLS
ldap_pvt_tls_destroy();
#endif
config_destroy();
#ifdef CSRIMALLOC
mal_dumpleaktrace( leakfile );
#endif
if ( !BER_BVISNULL( &authcDN ) ) {
ch_free( authcDN.bv_val );
}
2007-08-27 15:14:04 +08:00
if ( ldiffp && ldiffp != &dummy ) {
ldif_close( ldiffp );
}
}