3 * Copyright 1998-2002 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]\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 );
127 case 'c': /* enable continue mode */
131 case 'd': /* turn on debugging */
132 ldap_debug += atoi( optarg );
135 case 'f': /* specify a conf file */
136 conffile = strdup( optarg );
139 case 'l': /* LDIF file */
140 ldiffile = strdup( optarg );
143 case 'n': /* which config file db to index */
144 dbnum = atoi( optarg ) - 1;
147 case 't': /* turn on truncate */
149 mode |= SLAP_TRUNCATE_MODE;
152 case 'u': /* dry run */
156 case 'v': /* turn on verbose */
166 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
170 if ( ldiffile == NULL ) {
171 ldiffp = tool == SLAPCAT ? stdout : stdin;
173 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
177 exit( EXIT_FAILURE );
181 * initialize stuff and figure out which backend we're dealing with
185 if ( module_init() != 0 ) {
186 fprintf( stderr, "%s: module_init failed!\n", progname );
187 exit( EXIT_FAILURE );
191 rc = slap_init( mode, progname );
194 fprintf( stderr, "%s: slap_init failed!\n", progname );
195 exit( EXIT_FAILURE );
198 rc = slap_schema_init();
201 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
202 exit( EXIT_FAILURE );
205 rc = read_config( conffile, 0 );
208 fprintf( stderr, "%s: bad configuration file!\n", progname );
209 exit( EXIT_FAILURE );
213 fprintf( stderr, "No databases found in config file\n" );
214 exit( EXIT_FAILURE );
217 rc = glue_sub_init();
220 fprintf( stderr, "Subordinate configuration error\n" );
221 exit( EXIT_FAILURE );
224 rc = slap_schema_check();
227 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
228 exit( EXIT_FAILURE );
231 if( base.bv_val != NULL ) {
232 struct berval *nbase = NULL;
234 rc = dnNormalize( NULL, &base, &nbase );
235 if( rc != LDAP_SUCCESS ) {
236 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
237 progname, base.bv_val );
238 exit( EXIT_FAILURE );
241 be = select_backend( nbase, 0, 0 );
245 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
246 progname, base.bv_val );
247 exit( EXIT_FAILURE );
249 /* If the named base is a glue master, operate on the
252 if (be->be_flags & SLAP_BFLAG_GLUE_INSTANCE)
255 } else if ( dbnum == -1 ) {
256 be = &backends[dbnum=0];
257 /* If just doing the first by default and it is a
258 * glue subordinate, find the master.
260 while (be->be_flags & SLAP_BFLAG_GLUE_SUBORDINATE) {
265 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
267 "Database number selected via -n is out of range\n"
268 "Must be in the range 1 to %d"
269 " (number of databases in the config file)\n",
271 exit( EXIT_FAILURE );
274 be = &backends[dbnum];
284 void slap_tool_destroy( void )
289 if ( slapMode == SLAP_SERVER_MODE ) {
290 /* always false. just pulls in necessary symbol references. */
291 lutil_uuidstr(NULL, 0);
297 ldap_pvt_tls_destroy();
302 mal_dumpleaktrace( leakfile );