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;
30 char *ldiffile = NULL;
45 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
46 "\t[-n databasenumber | -b suffix]", progname );
50 options = "\t[-l ldiffile] [-u]\n";
54 options = "\t[-l ldiffile]\n";
62 if( options != NULL ) {
63 fputs( options, stderr );
70 * slap_tool_init - initialize slap utility, handle program options.
74 * argc, argv command line arguments
81 int argc, char **argv )
84 struct berval base = { 0, NULL };
86 int mode = SLAP_TOOL_MODE;
88 progname = lutil_progname( name, argc, argv );
91 leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
92 sprintf( leakfilename, "%s.leak", progname );
93 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
101 options = "b:cd:f:l:n:tuv";
105 options = "b:cd:f:n:v";
109 options = "b:cd:f:l:n:v";
113 fprintf( stderr, "%s: unknown tool mode (%d)\n",
115 exit( EXIT_FAILURE );
119 conffile = SLAPD_DEFAULT_CONFIGFILE;
121 while ( (i = getopt( argc, argv, options )) != EOF ) {
124 base.bv_val = strdup( optarg );
125 base.bv_len = strlen( base.bv_val );
128 case 'c': /* enable continue mode */
132 case 'd': /* turn on debugging */
133 ldap_debug += atoi( optarg );
136 case 'f': /* specify a conf file */
137 conffile = strdup( optarg );
140 case 'l': /* LDIF file */
141 ldiffile = strdup( optarg );
144 case 'n': /* which config file db to index */
145 dbnum = atoi( optarg ) - 1;
148 case 't': /* turn on truncate */
150 mode |= SLAP_TRUNCATE_MODE;
153 case 'u': /* dry run */
157 case 'v': /* turn on verbose */
167 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
171 if ( ldiffile == NULL ) {
172 ldiffp = tool == SLAPCAT ? stdout : stdin;
174 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
178 exit( EXIT_FAILURE );
182 * initialize stuff and figure out which backend we're dealing with
186 if ( module_init() != 0 ) {
187 fprintf( stderr, "%s: module_init failed!\n", progname );
188 exit( EXIT_FAILURE );
192 rc = slap_init( mode, progname );
195 fprintf( stderr, "%s: slap_init failed!\n", progname );
196 exit( EXIT_FAILURE );
199 rc = slap_schema_init();
202 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
203 exit( EXIT_FAILURE );
206 rc = read_config( conffile, 0 );
209 fprintf( stderr, "%s: bad configuration file!\n", progname );
210 exit( EXIT_FAILURE );
214 fprintf( stderr, "No databases found in config file\n" );
215 exit( EXIT_FAILURE );
218 rc = glue_sub_init();
221 fprintf( stderr, "Subordinate configuration error\n" );
222 exit( EXIT_FAILURE );
225 rc = slap_schema_check();
228 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
229 exit( EXIT_FAILURE );
232 if( base.bv_val != NULL ) {
235 rc = dnNormalize2( NULL, &base, &nbase, NULL );
236 if( rc != LDAP_SUCCESS ) {
237 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
238 progname, base.bv_val );
239 exit( EXIT_FAILURE );
242 be = select_backend( &nbase, 0, 0 );
243 ber_memfree( nbase.bv_val );
246 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
247 progname, base.bv_val );
248 exit( EXIT_FAILURE );
250 /* If the named base is a glue master, operate on the
253 if (SLAP_GLUE_INSTANCE(be)) {
257 } else if ( dbnum == -1 ) {
258 if ( nbackends <= 0 ) {
259 fprintf( stderr, "No available databases\n" );
260 exit( EXIT_FAILURE );
263 be = &backends[dbnum=0];
264 /* If just doing the first by default and it is a
265 * glue subordinate, find the master.
267 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
268 if (SLAP_GLUE_SUBORDINATE(be)) {
276 if ( dbnum >= nbackends ) {
277 fprintf( stderr, "Available database(s) "
278 "do not allow %s\n", name );
279 exit( EXIT_FAILURE );
282 if ( nosubordinates == 0 && dbnum > 0 ) {
284 LDAP_LOG( BACKEND, ERR,
285 "The first database does not allow %s; using the first available one (%d)\n",
286 name, dbnum + 1, 0 );
288 Debug( LDAP_DEBUG_ANY,
289 "The first database does not allow %s; using the first available one (%d)\n",
290 name, dbnum + 1, 0 );
294 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
296 "Database number selected via -n is out of range\n"
297 "Must be in the range 1 to %d"
298 " (number of databases in the config file)\n",
300 exit( EXIT_FAILURE );
303 be = &backends[dbnum];
313 void slap_tool_destroy( void )
318 if ( slapMode == SLAP_SERVER_MODE ) {
319 /* always false. just pulls in necessary symbol references. */
320 lutil_uuidstr(NULL, 0);
326 ldap_pvt_tls_destroy();
331 mal_dumpleaktrace( leakfile );