1 /* slapcommon.c - common routine for the slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 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"
41 char *progname = NULL;
42 char *conffile = SLAPD_DEFAULT_CONFIGFILE;
45 int update_ctxcsn = SLAP_TOOL_CTXCSN_KEEP;
46 int retrieve_ctxcsn = 0;
47 int retrieve_synccookie = 0;
48 int replica_promotion = 0;
49 int replica_demotion = 0;
50 char *replica_id_string = NULL;
51 char **replica_id_strlist = NULL;
52 int *replica_id_list = NULL;
54 int nosubordinates = 0;
56 struct berval sub_ndn = { 0, NULL };
58 char *ldiffile = NULL;
73 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
74 "\t[-n databasenumber | -b suffix]", progname );
78 options = "\n\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
82 options = "\t[-l ldiffile] [-m] [-k]\n";
90 if( options != NULL ) {
91 fputs( options, stderr );
98 * slap_tool_init - initialize slap utility, handle program options.
102 * argc, argv command line arguments
109 int argc, char **argv )
112 struct berval base = { 0, NULL };
113 char *subtree = NULL;
115 int mode = SLAP_TOOL_MODE;
117 progname = lutil_progname( name, argc, argv );
120 leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
121 sprintf( leakfilename, "%s.leak", progname );
122 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
125 free( leakfilename );
130 options = "b:cd:f:i:l:n:prtuvWw";
134 options = "b:cd:f:n:v";
135 mode |= SLAP_TOOL_READMAIN;
139 options = "b:cd:f:kl:mn:s:v";
140 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
144 fprintf( stderr, "%s: unknown tool mode (%d)\n",
146 exit( EXIT_FAILURE );
150 conffile = SLAPD_DEFAULT_CONFIGFILE;
152 while ( (i = getopt( argc, argv, options )) != EOF ) {
155 base.bv_val = strdup( optarg );
156 base.bv_len = strlen( base.bv_val );
159 case 'c': /* enable continue mode */
163 case 'd': /* turn on debugging */
164 ldap_debug += atoi( optarg );
167 case 'f': /* specify a conf file */
168 conffile = strdup( optarg );
171 case 'i': /* specify syncrepl id list */
172 replica_id_string = strdup( optarg );
173 if ( !isdigit( (unsigned char) *replica_id_string )) {
175 exit( EXIT_FAILURE );
177 str2clist( &replica_id_strlist, replica_id_string, "," );
178 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
179 replica_id_list = ch_calloc( i + 1, sizeof( int ) );
180 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
181 replica_id_list[i] = atoi( replica_id_strlist[i] );
182 if ( replica_id_list[i] >= 1000 ) {
184 "%s: syncrepl id %d is out of range [0..999]\n",
185 progname, replica_id_list[i] );
186 exit( EXIT_FAILURE );
189 replica_id_list[i] = -1;
192 case 'k': /* Retrieve sync cookie entry */
193 retrieve_synccookie = 1;
196 case 'l': /* LDIF file */
197 ldiffile = strdup( optarg );
200 case 'm': /* Retrieve ldapsync entry */
204 case 'n': /* which config file db to index */
205 dbnum = atoi( optarg ) - 1;
208 case 'p': /* replica promotion */
209 replica_promotion = 1;
212 case 'r': /* replica demotion */
213 replica_demotion = 1;
216 case 's': /* dump subtree */
217 subtree = strdup( optarg );
220 case 't': /* turn on truncate */
222 mode |= SLAP_TRUNCATE_MODE;
225 case 'u': /* dry run */
229 case 'v': /* turn on verbose */
233 case 'W': /* write context csn on every entry add */
234 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
235 /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
238 case 'w': /* write context csn on at the end */
239 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
248 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
252 if ( replica_promotion && replica_demotion ) {
254 } else if ( !replica_promotion && !replica_demotion ) {
255 if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
260 if ( ldiffile == NULL ) {
261 ldiffp = tool == SLAPCAT ? stdout : stdin;
263 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
267 exit( EXIT_FAILURE );
271 * initialize stuff and figure out which backend we're dealing with
275 if ( module_init() != 0 ) {
276 fprintf( stderr, "%s: module_init failed!\n", progname );
277 exit( EXIT_FAILURE );
281 rc = slap_init( mode, progname );
284 fprintf( stderr, "%s: slap_init failed!\n", progname );
285 exit( EXIT_FAILURE );
288 rc = slap_schema_init();
291 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
292 exit( EXIT_FAILURE );
295 rc = read_config( conffile, 0 );
298 fprintf( stderr, "%s: bad configuration file!\n", progname );
299 exit( EXIT_FAILURE );
303 fprintf( stderr, "No databases found in config file\n" );
304 exit( EXIT_FAILURE );
307 rc = glue_sub_init();
310 fprintf( stderr, "Subordinate configuration error\n" );
311 exit( EXIT_FAILURE );
314 rc = slap_schema_check();
317 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
318 exit( EXIT_FAILURE );
323 val.bv_val = subtree;
324 val.bv_len = strlen( subtree );
325 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
326 if( rc != LDAP_SUCCESS ) {
327 fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
328 exit( EXIT_FAILURE );
331 if( base.bv_val == NULL && dbnum == -1 )
337 if( base.bv_val != NULL ) {
340 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
341 if( rc != LDAP_SUCCESS ) {
342 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
343 progname, base.bv_val );
344 exit( EXIT_FAILURE );
347 be = select_backend( &nbase, 0, 0 );
348 ber_memfree( nbase.bv_val );
351 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
352 progname, base.bv_val );
353 exit( EXIT_FAILURE );
355 /* If the named base is a glue master, operate on the
358 if (SLAP_GLUE_INSTANCE(be)) {
362 } else if ( dbnum == -1 ) {
363 if ( nbackends <= 0 ) {
364 fprintf( stderr, "No available databases\n" );
365 exit( EXIT_FAILURE );
368 be = &backends[dbnum=0];
369 /* If just doing the first by default and it is a
370 * glue subordinate, find the master.
372 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
373 if (SLAP_GLUE_SUBORDINATE(be)) {
381 if ( dbnum >= nbackends ) {
382 fprintf( stderr, "Available database(s) "
383 "do not allow %s\n", name );
384 exit( EXIT_FAILURE );
387 if ( nosubordinates == 0 && dbnum > 0 ) {
389 LDAP_LOG( BACKEND, ERR,
390 "The first database does not allow %s; using the first available one (%d)\n",
391 name, dbnum + 1, 0 );
393 Debug( LDAP_DEBUG_ANY,
394 "The first database does not allow %s; using the first available one (%d)\n",
395 name, dbnum + 1, 0 );
399 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
401 "Database number selected via -n is out of range\n"
402 "Must be in the range 1 to %d"
403 " (number of databases in the config file)\n",
405 exit( EXIT_FAILURE );
408 be = &backends[dbnum];
415 if ( slap_startup( be ) ) {
416 fprintf( stderr, "slap_startup failed\n" );
417 exit( EXIT_FAILURE );
421 void slap_tool_destroy( void )
426 if ( slapMode == SLAP_SERVER_MODE ) {
427 /* always false. just pulls in necessary symbol references. */
428 lutil_uuidstr(NULL, 0);
434 ldap_pvt_tls_destroy();
439 mal_dumpleaktrace( leakfile );