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 = { 0, NULL };
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 rc = read_config( conffile, 0 );
308 fprintf( stderr, "%s: bad configuration file!\n", progname );
309 exit( EXIT_FAILURE );
319 fprintf( stderr, "No databases found "
320 "in config file\n" );
321 exit( EXIT_FAILURE );
329 rc = glue_sub_init();
332 fprintf( stderr, "Subordinate configuration error\n" );
333 exit( EXIT_FAILURE );
336 rc = slap_schema_check();
339 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
340 exit( EXIT_FAILURE );
354 val.bv_val = subtree;
355 val.bv_len = strlen( subtree );
356 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
357 if( rc != LDAP_SUCCESS ) {
358 fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
359 exit( EXIT_FAILURE );
362 if( base.bv_val == NULL && dbnum == -1 )
368 if( base.bv_val != NULL ) {
371 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
372 if( rc != LDAP_SUCCESS ) {
373 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
374 progname, base.bv_val );
375 exit( EXIT_FAILURE );
378 be = select_backend( &nbase, 0, 0 );
379 ber_memfree( nbase.bv_val );
382 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
383 progname, base.bv_val );
384 exit( EXIT_FAILURE );
386 /* If the named base is a glue master, operate on the
389 if (SLAP_GLUE_INSTANCE(be)) {
393 } else if ( dbnum == -1 ) {
394 if ( nbackends <= 0 ) {
395 fprintf( stderr, "No available databases\n" );
396 exit( EXIT_FAILURE );
399 be = &backends[dbnum=0];
400 /* If just doing the first by default and it is a
401 * glue subordinate, find the master.
403 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
404 if (SLAP_GLUE_SUBORDINATE(be)) {
412 if ( dbnum >= nbackends ) {
413 fprintf( stderr, "Available database(s) "
414 "do not allow %s\n", progname );
415 exit( EXIT_FAILURE );
418 if ( nosubordinates == 0 && dbnum > 0 ) {
420 LDAP_LOG( BACKEND, ERR,
421 "The first database does not allow %s; using the first available one (%d)\n",
422 progname, dbnum + 1, 0 );
424 Debug( LDAP_DEBUG_ANY,
425 "The first database does not allow %s; using the first available one (%d)\n",
426 progname, dbnum + 1, 0 );
430 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
432 "Database number selected via -n is out of range\n"
433 "Must be in the range 1 to %d"
434 " (number of databases in the config file)\n",
436 exit( EXIT_FAILURE );
439 be = &backends[dbnum];
446 if ( slap_startup( be ) ) {
447 fprintf( stderr, "slap_startup failed\n" );
448 exit( EXIT_FAILURE );
452 void slap_tool_destroy( void )
457 if ( slapMode == SLAP_SERVER_MODE ) {
458 /* always false. just pulls in necessary symbol references. */
459 lutil_uuidstr(NULL, 0);
465 ldap_pvt_tls_destroy();
470 mal_dumpleaktrace( leakfile );