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 );
281 fprintf( stderr, "No databases found in config file\n" );
282 exit( EXIT_FAILURE );
285 rc = glue_sub_init();
288 fprintf( stderr, "Subordinate configuration error\n" );
289 exit( EXIT_FAILURE );
292 rc = slap_schema_check();
295 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
296 exit( EXIT_FAILURE );
301 val.bv_val = subtree;
302 val.bv_len = strlen( subtree );
303 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
304 if( rc != LDAP_SUCCESS ) {
305 fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
306 exit( EXIT_FAILURE );
309 if( base.bv_val == NULL && dbnum == -1 )
315 if( base.bv_val != NULL ) {
318 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
319 if( rc != LDAP_SUCCESS ) {
320 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
321 progname, base.bv_val );
322 exit( EXIT_FAILURE );
325 be = select_backend( &nbase, 0, 0 );
326 ber_memfree( nbase.bv_val );
329 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
330 progname, base.bv_val );
331 exit( EXIT_FAILURE );
333 /* If the named base is a glue master, operate on the
336 if (SLAP_GLUE_INSTANCE(be)) {
340 } else if ( dbnum == -1 ) {
341 if ( nbackends <= 0 ) {
342 fprintf( stderr, "No available databases\n" );
343 exit( EXIT_FAILURE );
346 be = &backends[dbnum=0];
347 /* If just doing the first by default and it is a
348 * glue subordinate, find the master.
350 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
351 if (SLAP_GLUE_SUBORDINATE(be)) {
359 if ( dbnum >= nbackends ) {
360 fprintf( stderr, "Available database(s) "
361 "do not allow %s\n", progname );
362 exit( EXIT_FAILURE );
365 if ( nosubordinates == 0 && dbnum > 0 ) {
367 LDAP_LOG( BACKEND, ERR,
368 "The first database does not allow %s; using the first available one (%d)\n",
369 progname, dbnum + 1, 0 );
371 Debug( LDAP_DEBUG_ANY,
372 "The first database does not allow %s; using the first available one (%d)\n",
373 progname, dbnum + 1, 0 );
377 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
379 "Database number selected via -n is out of range\n"
380 "Must be in the range 1 to %d"
381 " (number of databases in the config file)\n",
383 exit( EXIT_FAILURE );
386 be = &backends[dbnum];
393 if ( slap_startup( be ) ) {
394 fprintf( stderr, "slap_startup failed\n" );
395 exit( EXIT_FAILURE );
399 void slap_tool_destroy( void )
404 if ( slapMode == SLAP_SERVER_MODE ) {
405 /* always false. just pulls in necessary symbol references. */
406 lutil_uuidstr(NULL, 0);
412 ldap_pvt_tls_destroy();
417 mal_dumpleaktrace( leakfile );