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"
53 "\t[-n databasenumber | -b suffix]", progname );
57 options = "\n\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
61 options = "\t[-l ldiffile] [-m] [-k]\n";
69 if( options != NULL ) {
70 fputs( options, stderr );
77 * slap_tool_init - initialize slap utility, handle program options.
81 * argc, argv command line arguments
88 int argc, char **argv )
91 char *conffile = SLAPD_DEFAULT_CONFIGFILE;
92 struct berval base = { 0, NULL };
94 char *ldiffile = NULL;
96 int mode = SLAP_TOOL_MODE;
100 leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
101 sprintf( leakfilename, "%s.leak", progname );
102 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
105 free( leakfilename );
110 options = "b:cd:f:i:l:n:prtuvWw";
114 options = "b:cd:f:n:v";
115 mode |= SLAP_TOOL_READMAIN;
119 options = "b:cd:f:kl:mn:s:v";
120 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
124 fprintf( stderr, "%s: unknown tool mode (%d)\n",
126 exit( EXIT_FAILURE );
130 while ( (i = getopt( argc, argv, options )) != EOF ) {
133 base.bv_val = strdup( optarg );
134 base.bv_len = strlen( base.bv_val );
137 case 'c': /* enable continue mode */
141 case 'd': /* turn on debugging */
142 ldap_debug += atoi( optarg );
145 case 'f': /* specify a conf file */
146 conffile = strdup( optarg );
149 case 'i': /* specify syncrepl id list */
150 replica_id_string = strdup( optarg );
151 if ( !isdigit( (unsigned char) *replica_id_string )) {
152 usage( tool, progname );
153 exit( EXIT_FAILURE );
155 str2clist( &replica_id_strlist, replica_id_string, "," );
156 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
157 replica_id_list = ch_calloc( i + 1, sizeof( int ) );
158 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
159 replica_id_list[i] = atoi( replica_id_strlist[i] );
160 if ( replica_id_list[i] >= 1000 ) {
162 "%s: syncrepl id %d is out of range [0..999]\n",
163 progname, replica_id_list[i] );
164 exit( EXIT_FAILURE );
167 replica_id_list[i] = -1;
170 case 'k': /* Retrieve sync cookie entry */
171 retrieve_synccookie = 1;
174 case 'l': /* LDIF file */
175 ldiffile = strdup( optarg );
178 case 'm': /* Retrieve ldapsync entry */
182 case 'n': /* which config file db to index */
183 dbnum = atoi( optarg ) - 1;
186 case 'p': /* replica promotion */
187 replica_promotion = 1;
190 case 'r': /* replica demotion */
191 replica_demotion = 1;
194 case 's': /* dump subtree */
195 subtree = strdup( optarg );
198 case 't': /* turn on truncate */
200 mode |= SLAP_TRUNCATE_MODE;
203 case 'u': /* dry run */
207 case 'v': /* turn on verbose */
211 case 'W': /* write context csn on every entry add */
212 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
213 /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
216 case 'w': /* write context csn on at the end */
217 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
221 usage( tool, progname );
226 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
227 usage( tool, progname );
230 if ( replica_promotion && replica_demotion ) {
231 usage( tool, progname );
232 } else if ( !replica_promotion && !replica_demotion ) {
233 if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
234 usage( tool, progname );
238 if ( ldiffile == NULL ) {
239 ldiffp = tool == SLAPCAT ? stdout : stdin;
241 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
245 exit( EXIT_FAILURE );
249 * initialize stuff and figure out which backend we're dealing with
253 if ( module_init() != 0 ) {
254 fprintf( stderr, "%s: module_init failed!\n", progname );
255 exit( EXIT_FAILURE );
259 rc = slap_init( mode, progname );
262 fprintf( stderr, "%s: slap_init failed!\n", progname );
263 exit( EXIT_FAILURE );
266 rc = slap_schema_init();
269 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
270 exit( EXIT_FAILURE );
273 rc = read_config( conffile, 0 );
276 fprintf( stderr, "%s: bad configuration file!\n", progname );
277 exit( EXIT_FAILURE );
283 fprintf( stderr, "No databases found in config file\n" );
284 exit( EXIT_FAILURE );
287 rc = glue_sub_init();
290 fprintf( stderr, "Subordinate configuration error\n" );
291 exit( EXIT_FAILURE );
294 rc = slap_schema_check();
297 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
298 exit( EXIT_FAILURE );
303 val.bv_val = subtree;
304 val.bv_len = strlen( subtree );
305 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
306 if( rc != LDAP_SUCCESS ) {
307 fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
308 exit( EXIT_FAILURE );
311 if( base.bv_val == NULL && dbnum == -1 )
317 if( base.bv_val != NULL ) {
320 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
321 if( rc != LDAP_SUCCESS ) {
322 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
323 progname, base.bv_val );
324 exit( EXIT_FAILURE );
327 be = select_backend( &nbase, 0, 0 );
328 ber_memfree( nbase.bv_val );
331 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
332 progname, base.bv_val );
333 exit( EXIT_FAILURE );
335 /* If the named base is a glue master, operate on the
338 if (SLAP_GLUE_INSTANCE(be)) {
342 } else if ( dbnum == -1 ) {
343 if ( nbackends <= 0 ) {
344 fprintf( stderr, "No available databases\n" );
345 exit( EXIT_FAILURE );
348 be = &backends[dbnum=0];
349 /* If just doing the first by default and it is a
350 * glue subordinate, find the master.
352 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
353 if (SLAP_GLUE_SUBORDINATE(be)) {
361 if ( dbnum >= nbackends ) {
362 fprintf( stderr, "Available database(s) "
363 "do not allow %s\n", progname );
364 exit( EXIT_FAILURE );
367 if ( nosubordinates == 0 && dbnum > 0 ) {
369 LDAP_LOG( BACKEND, ERR,
370 "The first database does not allow %s; using the first available one (%d)\n",
371 progname, dbnum + 1, 0 );
373 Debug( LDAP_DEBUG_ANY,
374 "The first database does not allow %s; using the first available one (%d)\n",
375 progname, dbnum + 1, 0 );
379 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
381 "Database number selected via -n is out of range\n"
382 "Must be in the range 1 to %d"
383 " (number of databases in the config file)\n",
385 exit( EXIT_FAILURE );
388 be = &backends[dbnum];
395 if ( slap_startup( be ) ) {
396 fprintf( stderr, "slap_startup failed\n" );
397 exit( EXIT_FAILURE );
401 void slap_tool_destroy( void )
406 if ( slapMode == SLAP_SERVER_MODE ) {
407 /* always false. just pulls in necessary symbol references. */
408 lutil_uuidstr(NULL, 0);
414 ldap_pvt_tls_destroy();
419 mal_dumpleaktrace( leakfile );