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";
108 mode |= SLAP_TOOL_READMAIN;
112 options = "b:cd:f:l:n:s:v";
113 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
117 fprintf( stderr, "%s: unknown tool mode (%d)\n",
119 exit( EXIT_FAILURE );
123 conffile = SLAPD_DEFAULT_CONFIGFILE;
125 while ( (i = getopt( argc, argv, options )) != EOF ) {
128 base.bv_val = strdup( optarg );
129 base.bv_len = strlen( base.bv_val );
132 case 'c': /* enable continue mode */
136 case 'd': /* turn on debugging */
137 ldap_debug += atoi( optarg );
140 case 'f': /* specify a conf file */
141 conffile = strdup( optarg );
144 case 'l': /* LDIF file */
145 ldiffile = strdup( optarg );
148 case 'n': /* which config file db to index */
149 dbnum = atoi( optarg ) - 1;
152 case 's': /* dump subtree */
153 subtree = strdup( optarg );
156 case 't': /* turn on truncate */
158 mode |= SLAP_TRUNCATE_MODE;
161 case 'u': /* dry run */
165 case 'v': /* turn on verbose */
175 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
179 if ( ldiffile == NULL ) {
180 ldiffp = tool == SLAPCAT ? stdout : stdin;
182 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
186 exit( EXIT_FAILURE );
190 * initialize stuff and figure out which backend we're dealing with
194 if ( module_init() != 0 ) {
195 fprintf( stderr, "%s: module_init failed!\n", progname );
196 exit( EXIT_FAILURE );
200 rc = slap_init( mode, progname );
203 fprintf( stderr, "%s: slap_init failed!\n", progname );
204 exit( EXIT_FAILURE );
207 rc = slap_schema_init();
210 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
211 exit( EXIT_FAILURE );
214 rc = read_config( conffile, 0 );
217 fprintf( stderr, "%s: bad configuration file!\n", progname );
218 exit( EXIT_FAILURE );
222 fprintf( stderr, "No databases found in config file\n" );
223 exit( EXIT_FAILURE );
226 rc = glue_sub_init();
229 fprintf( stderr, "Subordinate configuration error\n" );
230 exit( EXIT_FAILURE );
233 rc = slap_schema_check();
236 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
237 exit( EXIT_FAILURE );
242 val.bv_val = subtree;
243 val.bv_len = strlen( subtree );
244 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
245 if( rc != LDAP_SUCCESS ) {
246 fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
247 exit( EXIT_FAILURE );
250 if( base.bv_val == NULL && dbnum == -1 )
256 if( base.bv_val != NULL ) {
259 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
260 if( rc != LDAP_SUCCESS ) {
261 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
262 progname, base.bv_val );
263 exit( EXIT_FAILURE );
266 be = select_backend( &nbase, 0, 0 );
267 ber_memfree( nbase.bv_val );
270 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
271 progname, base.bv_val );
272 exit( EXIT_FAILURE );
274 /* If the named base is a glue master, operate on the
277 if (SLAP_GLUE_INSTANCE(be)) {
281 } else if ( dbnum == -1 ) {
282 if ( nbackends <= 0 ) {
283 fprintf( stderr, "No available databases\n" );
284 exit( EXIT_FAILURE );
287 be = &backends[dbnum=0];
288 /* If just doing the first by default and it is a
289 * glue subordinate, find the master.
291 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
292 if (SLAP_GLUE_SUBORDINATE(be)) {
300 if ( dbnum >= nbackends ) {
301 fprintf( stderr, "Available database(s) "
302 "do not allow %s\n", name );
303 exit( EXIT_FAILURE );
306 if ( nosubordinates == 0 && dbnum > 0 ) {
308 LDAP_LOG( BACKEND, ERR,
309 "The first database does not allow %s; using the first available one (%d)\n",
310 name, dbnum + 1, 0 );
312 Debug( LDAP_DEBUG_ANY,
313 "The first database does not allow %s; using the first available one (%d)\n",
314 name, dbnum + 1, 0 );
318 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
320 "Database number selected via -n is out of range\n"
321 "Must be in the range 1 to %d"
322 " (number of databases in the config file)\n",
324 exit( EXIT_FAILURE );
327 be = &backends[dbnum];
337 void slap_tool_destroy( void )
342 if ( slapMode == SLAP_SERVER_MODE ) {
343 /* always false. just pulls in necessary symbol references. */
344 lutil_uuidstr(NULL, 0);
350 ldap_pvt_tls_destroy();
355 mal_dumpleaktrace( leakfile );