1 /* slapcommon.c - common routine for the slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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]\n",
57 options = "\t[-n databasenumber | -b suffix]\n"
58 "\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
62 options = "\t[-n databasenumber | -b suffix] [-l ldiffile] [-m] [-k]\n";
66 options = "\tDN [...]\n";
70 options = "\t[-n databasenumber | -b suffix]\n";
74 if ( options != NULL ) {
75 fputs( options, stderr );
82 * slap_tool_init - initialize slap utility, handle program options.
86 * argc, argv command line arguments
93 int argc, char **argv )
96 char *conffile = SLAPD_DEFAULT_CONFIGFILE;
97 struct berval base = BER_BVNULL;
99 char *ldiffile = NULL;
101 int mode = SLAP_TOOL_MODE;
102 int truncatemode = 0;
105 leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
106 sprintf( leakfilename, "%s.leak", progname );
107 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
110 free( leakfilename );
115 options = "b:cd:f:i:l:n:prtuvWw";
119 options = "b:cd:f:kl:mn:s:v";
120 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
129 options = "b:cd:f:n:v";
130 mode |= SLAP_TOOL_READMAIN;
134 fprintf( stderr, "%s: unknown tool mode (%d)\n",
136 exit( EXIT_FAILURE );
140 while ( (i = getopt( argc, argv, options )) != EOF ) {
143 base.bv_val = strdup( optarg );
144 base.bv_len = strlen( base.bv_val );
147 case 'c': /* enable continue mode */
151 case 'd': /* turn on debugging */
152 ldap_debug += atoi( optarg );
155 case 'f': /* specify a conf file */
156 conffile = strdup( optarg );
159 case 'i': /* specify syncrepl id list */
160 replica_id_string = strdup( optarg );
161 if ( !isdigit( (unsigned char) *replica_id_string )) {
162 usage( tool, progname );
163 exit( EXIT_FAILURE );
165 str2clist( &replica_id_strlist, replica_id_string, "," );
166 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
167 replica_id_list = ch_calloc( i + 1, sizeof( int ) );
168 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
169 replica_id_list[i] = atoi( replica_id_strlist[i] );
170 if ( replica_id_list[i] >= 1000 ) {
172 "%s: syncrepl id %d is out of range [0..999]\n",
173 progname, replica_id_list[i] );
174 exit( EXIT_FAILURE );
177 replica_id_list[i] = -1;
180 case 'k': /* Retrieve sync cookie entry */
181 retrieve_synccookie = 1;
184 case 'l': /* LDIF file */
185 ldiffile = strdup( optarg );
188 case 'm': /* Retrieve ldapsync entry */
192 case 'n': /* which config file db to index */
193 dbnum = atoi( optarg ) - 1;
196 case 'p': /* replica promotion */
197 replica_promotion = 1;
200 case 'r': /* replica demotion */
201 replica_demotion = 1;
204 case 's': /* dump subtree */
205 subtree = strdup( optarg );
208 case 't': /* turn on truncate */
210 mode |= SLAP_TRUNCATE_MODE;
213 case 'u': /* dry run */
217 case 'v': /* turn on verbose */
221 case 'W': /* write context csn on every entry add */
222 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
223 /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
226 case 'w': /* write context csn on at the end */
227 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
231 usage( tool, progname );
240 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
241 usage( tool, progname );
244 if ( replica_promotion && replica_demotion ) {
245 usage( tool, progname );
247 } else if ( !replica_promotion && !replica_demotion ) {
248 if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
249 usage( tool, progname );
255 if ( argc == optind ) {
256 usage( tool, progname );
261 if ( argc != optind ) {
262 usage( tool, progname );
270 if ( ldiffile == NULL ) {
271 ldiffp = tool == SLAPCAT ? stdout : stdin;
273 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
277 exit( EXIT_FAILURE );
281 * initialize stuff and figure out which backend we're dealing with
285 if ( module_init() != 0 ) {
286 fprintf( stderr, "%s: module_init failed!\n", progname );
287 exit( EXIT_FAILURE );
291 rc = slap_init( mode, progname );
294 fprintf( stderr, "%s: slap_init failed!\n", progname );
295 exit( EXIT_FAILURE );
298 rc = slap_schema_init();
301 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
302 exit( EXIT_FAILURE );
305 if ( overlay_init() ) {
306 fprintf( stderr, "%s: overlay_init failed!\n", progname );
307 exit( EXIT_FAILURE );
310 rc = read_config( conffile, 0 );
313 fprintf( stderr, "%s: bad configuration file!\n", progname );
314 exit( EXIT_FAILURE );
324 fprintf( stderr, "No databases found "
325 "in config file\n" );
326 exit( EXIT_FAILURE );
334 rc = glue_sub_init();
337 fprintf( stderr, "Subordinate configuration error\n" );
338 exit( EXIT_FAILURE );
341 rc = slap_schema_check();
344 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
345 exit( EXIT_FAILURE );
359 val.bv_val = subtree;
360 val.bv_len = strlen( subtree );
361 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
362 if( rc != LDAP_SUCCESS ) {
363 fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
364 exit( EXIT_FAILURE );
367 if( base.bv_val == NULL && dbnum == -1 )
373 if( base.bv_val != NULL ) {
376 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
377 if( rc != LDAP_SUCCESS ) {
378 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
379 progname, base.bv_val );
380 exit( EXIT_FAILURE );
383 be = select_backend( &nbase, 0, 0 );
384 ber_memfree( nbase.bv_val );
387 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
388 progname, base.bv_val );
389 exit( EXIT_FAILURE );
391 /* If the named base is a glue master, operate on the
394 if (SLAP_GLUE_INSTANCE(be)) {
398 } else if ( dbnum == -1 ) {
399 if ( nbackends <= 0 ) {
400 fprintf( stderr, "No available databases\n" );
401 exit( EXIT_FAILURE );
404 be = &backends[dbnum=0];
405 /* If just doing the first by default and it is a
406 * glue subordinate, find the master.
408 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
409 if (SLAP_GLUE_SUBORDINATE(be)) {
417 if ( dbnum >= nbackends ) {
418 fprintf( stderr, "Available database(s) "
419 "do not allow %s\n", progname );
420 exit( EXIT_FAILURE );
423 if ( nosubordinates == 0 && dbnum > 0 ) {
425 LDAP_LOG( BACKEND, ERR,
426 "The first database does not allow %s; using the first available one (%d)\n",
427 progname, dbnum + 1, 0 );
429 Debug( LDAP_DEBUG_ANY,
430 "The first database does not allow %s; using the first available one (%d)\n",
431 progname, dbnum + 1, 0 );
435 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
437 "Database number selected via -n is out of range\n"
438 "Must be in the range 1 to %d"
439 " (number of databases in the config file)\n",
441 exit( EXIT_FAILURE );
444 be = &backends[dbnum];
451 if ( slap_startup( be ) ) {
452 fprintf( stderr, "slap_startup failed\n" );
453 exit( EXIT_FAILURE );
457 void slap_tool_destroy( void )
462 if ( slapMode == SLAP_SERVER_MODE ) {
463 /* always false. just pulls in necessary symbol references. */
464 lutil_uuidstr(NULL, 0);
470 ldap_pvt_tls_destroy();
475 mal_dumpleaktrace( leakfile );