3 * Copyright 1998-2000 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;
29 char *ldiffile = NULL;
44 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
45 "\t[-n databasenumber | -b suffix]", progname );
49 options = "\t[-l ldiffile]\n";
53 options = "\t[-l ldiffile]\n";
61 if( options != NULL ) {
62 fputs( options, stderr );
69 * slap_tool_init - initialize slap utility, handle program options.
73 * argc, argv command line arguments
80 int argc, char **argv )
83 struct berval base = { 0, NULL };
85 int mode = SLAP_TOOL_MODE;
87 progname = lutil_progname( name, argc, argv );
90 leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
91 sprintf( leakfilename, "%s.leak", progname );
92 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
100 options = "b:cd:f:l:n:tv";
104 options = "b:cd:f:n:v";
108 options = "b:cd:f:l:n:v";
112 fprintf( stderr, "%s: unknown tool mode (%d)\n",
114 exit( EXIT_FAILURE );
118 conffile = SLAPD_DEFAULT_CONFIGFILE;
120 while ( (i = getopt( argc, argv, options )) != EOF ) {
123 base.bv_val = strdup( optarg );
124 base.bv_len = strlen( base.bv_val );
126 case 'c': /* enable continue mode */
130 case 'd': /* turn on debugging */
131 ldap_debug += atoi( optarg );
134 case 'f': /* specify a conf file */
135 conffile = strdup( optarg );
138 case 'l': /* LDIF file */
139 ldiffile = strdup( optarg );
142 case 'n': /* which config file db to index */
143 dbnum = atoi( optarg ) - 1;
146 case 't': /* turn on truncate */
148 mode |= SLAP_TRUNCATE_MODE;
151 case 'v': /* turn on verbose */
161 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
165 if ( ldiffile == NULL ) {
166 ldiffp = tool == SLAPCAT ? stdout : stdin;
168 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
172 exit( EXIT_FAILURE );
176 * initialize stuff and figure out which backend we're dealing with
179 rc = slap_init( mode, progname );
182 fprintf( stderr, "%s: slap_init failed!\n", progname );
183 exit( EXIT_FAILURE );
189 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
190 exit( EXIT_FAILURE );
193 read_config( conffile );
196 fprintf( stderr, "No databases found in config file\n" );
197 exit( EXIT_FAILURE );
200 rc = glue_sub_init();
203 fprintf( stderr, "Subordinate configuration error\n" );
204 exit( EXIT_FAILURE );
210 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
211 exit( EXIT_FAILURE );
214 if( base.bv_val != NULL ) {
215 struct berval *nbase = NULL;
217 rc = dnNormalize( NULL, &base, &nbase );
218 if( rc != LDAP_SUCCESS ) {
219 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
221 exit( EXIT_FAILURE );
224 be = select_backend( nbase->bv_val, 0, 0 );
228 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
229 progname, base.bv_val );
230 exit( EXIT_FAILURE );
232 /* If the named base is a glue master, operate on the
235 if (be->be_glueflags & SLAP_GLUE_INSTANCE)
238 } else if ( dbnum == -1 ) {
239 be = &backends[dbnum=0];
240 /* If just doing the first by default and it is a
241 * glue subordinate, find the master.
243 while (be->be_glueflags & SLAP_GLUE_SUBORDINATE) {
248 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
250 "Database number selected via -n is out of range\n"
251 "Must be in the range 1 to %d"
252 " (number of databases in the config file)\n",
254 exit( EXIT_FAILURE );
257 be = &backends[dbnum];
267 void slap_tool_destroy( void )
273 mal_dumpleaktrace( leakfile );