1 /* slapcommon.c - common routine for the slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
6 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7 * Portions Copyright 2003 IBM Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by Kurt Zeilenga for inclusion
20 * in OpenLDAP Software. Additional signficant contributors include
22 * Hallvard B. Furuseth
31 #include <ac/stdlib.h>
33 #include <ac/string.h>
34 #include <ac/socket.h>
35 #include <ac/unistd.h>
37 #include "slapcommon.h"
40 tool_vars tool_globals;
43 static char *leakfilename;
44 static FILE *leakfile;
48 usage( int tool, const char *progname )
52 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]",
57 options = "\n\t[-n databasenumber | -b suffix]\n"
58 "\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
62 options = "\n\t[-n databasenumber | -b suffix] [-l ldiffile] [-m] [-k]\n";
66 options = " DN [...]\n";
70 options = "\n\t[-n databasenumber | -b suffix]\n";
78 if ( options != NULL ) {
79 fputs( options, stderr );
86 * slap_tool_init - initialize slap utility, handle program options.
90 * argc, argv command line arguments
97 int argc, char **argv )
100 char *conffile = SLAPD_DEFAULT_CONFIGFILE;
101 struct berval base = BER_BVNULL;
102 char *subtree = NULL;
103 char *ldiffile = NULL;
105 int mode = SLAP_TOOL_MODE;
106 int truncatemode = 0;
109 leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
110 sprintf( leakfilename, "%s.leak", progname );
111 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
114 free( leakfilename );
119 options = "b:cd:f:i:l:n:prtuvWw";
123 options = "b:cd:f:kl:mn:s:v";
124 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
129 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
134 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
139 options = "b:cd:f:n:v";
140 mode |= SLAP_TOOL_READMAIN;
144 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
145 exit( EXIT_FAILURE );
149 while ( (i = getopt( argc, argv, options )) != EOF ) {
152 ber_str2bv( optarg, 0, 1, &base );
155 case 'c': /* enable continue mode */
159 case 'd': /* turn on debugging */
160 ldap_debug += atoi( optarg );
163 case 'f': /* specify a conf file */
164 conffile = strdup( optarg );
167 case 'i': /* specify syncrepl id list */
168 replica_id_string = strdup( optarg );
169 if ( !isdigit( (unsigned char) *replica_id_string )) {
170 usage( tool, progname );
171 exit( EXIT_FAILURE );
173 str2clist( &replica_id_strlist, replica_id_string, "," );
174 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
175 replica_id_list = ch_calloc( i + 1, sizeof( int ) );
176 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
177 replica_id_list[i] = atoi( replica_id_strlist[i] );
178 if ( replica_id_list[i] >= 1000 ) {
180 "%s: syncrepl id %d is out of range [0..999]\n",
181 progname, replica_id_list[i] );
182 exit( EXIT_FAILURE );
185 replica_id_list[i] = -1;
188 case 'k': /* Retrieve sync cookie entry */
189 retrieve_synccookie = 1;
192 case 'l': /* LDIF file */
193 ldiffile = strdup( optarg );
196 case 'm': /* Retrieve ldapsync entry */
200 case 'n': /* which config file db to index */
201 dbnum = atoi( optarg ) - 1;
204 case 'p': /* replica promotion */
205 replica_promotion = 1;
208 case 'r': /* replica demotion */
209 replica_demotion = 1;
212 case 's': /* dump subtree */
213 subtree = strdup( optarg );
216 case 't': /* turn on truncate */
218 mode |= SLAP_TRUNCATE_MODE;
221 case 'u': /* dry run */
225 case 'v': /* turn on verbose */
229 case 'W': /* write context csn on every entry add */
230 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
231 /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
234 case 'w': /* write context csn on at the end */
235 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
239 usage( tool, progname );
248 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
249 usage( tool, progname );
252 if ( replica_promotion && replica_demotion ) {
253 usage( tool, progname );
255 } else if ( !replica_promotion && !replica_demotion ) {
256 if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
257 usage( tool, progname );
263 if ( argc == optind ) {
264 usage( tool, progname );
269 if ( argc != optind ) {
270 usage( tool, progname );
280 if ( ldiffile == NULL ) {
281 ldiffp = tool == SLAPCAT ? stdout : stdin;
283 } else if ( ( ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ) )
287 exit( EXIT_FAILURE );
291 * initialize stuff and figure out which backend we're dealing with
295 if ( module_init() != 0 ) {
296 fprintf( stderr, "%s: module_init failed!\n", progname );
297 exit( EXIT_FAILURE );
301 rc = slap_schema_init();
304 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
305 exit( EXIT_FAILURE );
308 rc = slap_init( mode, progname );
311 fprintf( stderr, "%s: slap_init failed!\n", progname );
312 exit( EXIT_FAILURE );
315 if ( overlay_init() ) {
316 fprintf( stderr, "%s: overlay_init failed!\n", progname );
317 exit( EXIT_FAILURE );
320 rc = read_config( conffile, 0 );
323 fprintf( stderr, "%s: bad configuration file!\n", progname );
324 exit( EXIT_FAILURE );
332 fprintf( stderr, "No databases found "
333 "in config file\n" );
334 exit( EXIT_FAILURE );
342 rc = glue_sub_init();
345 fprintf( stderr, "Subordinate configuration error\n" );
346 exit( EXIT_FAILURE );
349 rc = slap_schema_check();
352 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
353 exit( EXIT_FAILURE );
368 val.bv_val = subtree;
369 val.bv_len = strlen( subtree );
370 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
371 if( rc != LDAP_SUCCESS ) {
372 fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
373 exit( EXIT_FAILURE );
376 if( base.bv_val == NULL && dbnum == -1 )
382 if( base.bv_val != NULL ) {
385 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
386 if( rc != LDAP_SUCCESS ) {
387 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
388 progname, base.bv_val );
389 exit( EXIT_FAILURE );
392 be = select_backend( &nbase, 0, 0 );
393 ber_memfree( nbase.bv_val );
396 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
397 progname, base.bv_val );
398 exit( EXIT_FAILURE );
400 /* If the named base is a glue master, operate on the
403 if (SLAP_GLUE_INSTANCE(be)) {
407 } else if ( dbnum == -1 ) {
408 if ( nbackends <= 0 ) {
409 fprintf( stderr, "No available databases\n" );
410 exit( EXIT_FAILURE );
413 be = &backends[dbnum=0];
414 /* If just doing the first by default and it is a
415 * glue subordinate, find the master.
417 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
418 if (SLAP_GLUE_SUBORDINATE(be)) {
426 if ( dbnum >= nbackends ) {
427 fprintf( stderr, "Available database(s) "
428 "do not allow %s\n", progname );
429 exit( EXIT_FAILURE );
432 if ( nosubordinates == 0 && dbnum > 0 ) {
434 LDAP_LOG( BACKEND, ERR,
435 "The first database does not allow %s; using the first available one (%d)\n",
436 progname, dbnum + 1, 0 );
438 Debug( LDAP_DEBUG_ANY,
439 "The first database does not allow %s; using the first available one (%d)\n",
440 progname, dbnum + 1, 0 );
444 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
446 "Database number selected via -n is out of range\n"
447 "Must be in the range 1 to %d"
448 " (number of databases in the config file)\n",
450 exit( EXIT_FAILURE );
453 be = &backends[dbnum];
462 if ( !dryrun && slap_startup( be ) ) {
466 fprintf( stderr, "slap_startup failed "
467 "(test would succeed using "
468 "the -u switch)\n" );
472 fprintf( stderr, "slap_startup failed\n" );
476 exit( EXIT_FAILURE );
480 void slap_tool_destroy( void )
482 if ( !dryrun && be != NULL ) {
487 if ( slapMode == SLAP_SERVER_MODE ) {
488 /* always false. just pulls in necessary symbol references. */
489 lutil_uuidstr(NULL, 0);
495 ldap_pvt_tls_destroy();
500 mal_dumpleaktrace( leakfile );