3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* slapcommon.c - common routine for the slap tools */
12 #include <ac/stdlib.h>
14 #include <ac/string.h>
15 #include <ac/socket.h>
16 #include <ac/unistd.h>
18 #include "slapcommon.h"
22 char *progname = NULL;
23 char *conffile = SLAPD_DEFAULT_CONFIGFILE;
27 int nosubordinates = 0;
29 struct berval sub_ndn = { 0, NULL };
31 char *ldiffile = NULL;
46 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
47 "\t[-n databasenumber | -b suffix]", progname );
51 options = "\t[-l ldiffile] [-u]\n";
55 options = "\t[-l ldiffile]\n";
63 if( options != NULL ) {
64 fputs( options, stderr );
71 * slap_tool_init - initialize slap utility, handle program options.
75 * argc, argv command line arguments
82 int argc, char **argv )
85 struct berval base = { 0, NULL };
88 int mode = SLAP_TOOL_MODE;
90 progname = lutil_progname( name, argc, argv );
93 leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
94 sprintf( leakfilename, "%s.leak", progname );
95 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
103 options = "b:cd:f:l:n:tuv";
107 options = "b:cd:f:n:v";
111 options = "b:cd:f:l:n:s:v";
115 fprintf( stderr, "%s: unknown tool mode (%d)\n",
117 exit( EXIT_FAILURE );
121 conffile = SLAPD_DEFAULT_CONFIGFILE;
123 while ( (i = getopt( argc, argv, options )) != EOF ) {
126 base.bv_val = strdup( optarg );
127 base.bv_len = strlen( base.bv_val );
130 case 'c': /* enable continue mode */
134 case 'd': /* turn on debugging */
135 ldap_debug += atoi( optarg );
138 case 'f': /* specify a conf file */
139 conffile = strdup( optarg );
142 case 'l': /* LDIF file */
143 ldiffile = strdup( optarg );
146 case 'n': /* which config file db to index */
147 dbnum = atoi( optarg ) - 1;
150 case 's': /* dump subtree */
151 subtree = strdup( optarg );
154 case 't': /* turn on truncate */
156 mode |= SLAP_TRUNCATE_MODE;
159 case 'u': /* dry run */
163 case 'v': /* turn on verbose */
173 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
177 if ( ldiffile == NULL ) {
178 ldiffp = tool == SLAPCAT ? stdout : stdin;
180 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
184 exit( EXIT_FAILURE );
188 * initialize stuff and figure out which backend we're dealing with
192 if ( module_init() != 0 ) {
193 fprintf( stderr, "%s: module_init failed!\n", progname );
194 exit( EXIT_FAILURE );
198 rc = slap_init( mode, progname );
201 fprintf( stderr, "%s: slap_init failed!\n", progname );
202 exit( EXIT_FAILURE );
205 rc = slap_schema_init();
208 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
209 exit( EXIT_FAILURE );
212 rc = read_config( conffile, 0 );
215 fprintf( stderr, "%s: bad configuration file!\n", progname );
216 exit( EXIT_FAILURE );
220 fprintf( stderr, "No databases found in config file\n" );
221 exit( EXIT_FAILURE );
224 rc = glue_sub_init();
227 fprintf( stderr, "Subordinate configuration error\n" );
228 exit( EXIT_FAILURE );
231 rc = slap_schema_check();
234 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
235 exit( EXIT_FAILURE );
240 val.bv_val = subtree;
241 val.bv_len = strlen( subtree );
242 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
243 if( rc != LDAP_SUCCESS ) {
244 fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
245 exit( EXIT_FAILURE );
248 if( base.bv_val == NULL && dbnum == -1 )
254 if( base.bv_val != NULL ) {
257 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
258 if( rc != LDAP_SUCCESS ) {
259 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
260 progname, base.bv_val );
261 exit( EXIT_FAILURE );
264 be = select_backend( &nbase, 0, 0 );
265 ber_memfree( nbase.bv_val );
268 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
269 progname, base.bv_val );
270 exit( EXIT_FAILURE );
272 /* If the named base is a glue master, operate on the
275 if (SLAP_GLUE_INSTANCE(be)) {
279 } else if ( dbnum == -1 ) {
280 if ( nbackends <= 0 ) {
281 fprintf( stderr, "No available databases\n" );
282 exit( EXIT_FAILURE );
285 be = &backends[dbnum=0];
286 /* If just doing the first by default and it is a
287 * glue subordinate, find the master.
289 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
290 if (SLAP_GLUE_SUBORDINATE(be)) {
298 if ( dbnum >= nbackends ) {
299 fprintf( stderr, "Available database(s) "
300 "do not allow %s\n", name );
301 exit( EXIT_FAILURE );
304 if ( nosubordinates == 0 && dbnum > 0 ) {
306 LDAP_LOG( BACKEND, ERR,
307 "The first database does not allow %s; using the first available one (%d)\n",
308 name, dbnum + 1, 0 );
310 Debug( LDAP_DEBUG_ANY,
311 "The first database does not allow %s; using the first available one (%d)\n",
312 name, dbnum + 1, 0 );
316 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
318 "Database number selected via -n is out of range\n"
319 "Must be in the range 1 to %d"
320 " (number of databases in the config file)\n",
322 exit( EXIT_FAILURE );
325 be = &backends[dbnum];
335 void slap_tool_destroy( void )
340 if ( slapMode == SLAP_SERVER_MODE ) {
341 /* always false. just pulls in necessary symbol references. */
342 lutil_uuidstr(NULL, 0);
348 ldap_pvt_tls_destroy();
353 mal_dumpleaktrace( leakfile );