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 options = "\t[-U authcID] [-X authzID] ID [...]\n";
78 options = "\t[-U authcID | -D authcDN] -b DN attr[/level][:value] [...]\n";
82 if ( options != NULL ) {
83 fputs( options, stderr );
90 * slap_tool_init - initialize slap utility, handle program options.
94 * argc, argv command line arguments
101 int argc, char **argv )
104 char *conffile = SLAPD_DEFAULT_CONFIGFILE;
105 struct berval base = BER_BVNULL;
106 char *subtree = NULL;
107 char *ldiffile = NULL;
109 int mode = SLAP_TOOL_MODE;
110 int truncatemode = 0;
113 leakfilename = malloc( strlen( progname ) + STRLEOF( ".leak" ) - 1 );
114 sprintf( leakfilename, "%s.leak", progname );
115 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
118 free( leakfilename );
123 options = "b:cd:f:i:l:n:prtuvWw";
127 options = "b:cd:f:kl:mn:s:v";
128 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
134 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
138 options = "d:f:U:vX:";
139 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
143 options = "b:cd:f:n:v";
144 mode |= SLAP_TOOL_READMAIN;
148 options = "b:D:d:f:U:v";
149 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
153 fprintf( stderr, "%s: unknown tool mode (%d)\n",
155 exit( EXIT_FAILURE );
159 while ( (i = getopt( argc, argv, options )) != EOF ) {
162 ber_str2bv( optarg, 0, 1, &base );
165 case 'c': /* enable continue mode */
169 case 'd': /* turn on debugging */
170 ldap_debug += atoi( optarg );
174 ber_str2bv( optarg, 0, 1, &authcDN );
177 case 'f': /* specify a conf file */
178 conffile = strdup( optarg );
181 case 'i': /* specify syncrepl id list */
182 replica_id_string = strdup( optarg );
183 if ( !isdigit( (unsigned char) *replica_id_string )) {
184 usage( tool, progname );
185 exit( EXIT_FAILURE );
187 str2clist( &replica_id_strlist, replica_id_string, "," );
188 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
189 replica_id_list = ch_calloc( i + 1, sizeof( int ) );
190 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
191 replica_id_list[i] = atoi( replica_id_strlist[i] );
192 if ( replica_id_list[i] >= 1000 ) {
194 "%s: syncrepl id %d is out of range [0..999]\n",
195 progname, replica_id_list[i] );
196 exit( EXIT_FAILURE );
199 replica_id_list[i] = -1;
202 case 'k': /* Retrieve sync cookie entry */
203 retrieve_synccookie = 1;
206 case 'l': /* LDIF file */
207 ldiffile = strdup( optarg );
210 case 'm': /* Retrieve ldapsync entry */
214 case 'n': /* which config file db to index */
215 dbnum = atoi( optarg ) - 1;
218 case 'p': /* replica promotion */
219 replica_promotion = 1;
222 case 'r': /* replica demotion */
223 replica_demotion = 1;
226 case 's': /* dump subtree */
227 subtree = strdup( optarg );
230 case 't': /* turn on truncate */
232 mode |= SLAP_TRUNCATE_MODE;
236 ber_str2bv( optarg, 0, 0, &authcID );
239 case 'u': /* dry run */
243 case 'v': /* turn on verbose */
247 case 'W': /* write context csn on every entry add */
248 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
249 /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
252 case 'w': /* write context csn on at the end */
253 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
257 ber_str2bv( optarg, 0, 0, &authzID );
261 usage( tool, progname );
270 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
271 usage( tool, progname );
274 if ( replica_promotion && replica_demotion ) {
275 usage( tool, progname );
277 } else if ( !replica_promotion && !replica_demotion ) {
278 if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
279 usage( tool, progname );
285 if ( argc == optind ) {
286 usage( tool, progname );
291 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
292 usage( tool, progname );
297 if ( argc != optind ) {
298 usage( tool, progname );
303 if ( argc == optind ) {
304 usage( tool, progname );
306 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
307 usage( tool, progname );
309 if ( BER_BVISNULL( &base ) ) {
310 usage( tool, progname );
312 ber_dupbv( &baseDN, &base );
319 if ( ldiffile == NULL ) {
320 ldiffp = tool == SLAPCAT ? stdout : stdin;
322 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
326 exit( EXIT_FAILURE );
330 * initialize stuff and figure out which backend we're dealing with
334 if ( module_init() != 0 ) {
335 fprintf( stderr, "%s: module_init failed!\n", progname );
336 exit( EXIT_FAILURE );
340 rc = slap_init( mode, progname );
343 fprintf( stderr, "%s: slap_init failed!\n", progname );
344 exit( EXIT_FAILURE );
347 rc = slap_schema_init();
350 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
351 exit( EXIT_FAILURE );
354 if ( frontend_init() ) {
355 fprintf( stderr, "%s: frontend_init failed!\n", progname );
356 exit( EXIT_FAILURE );
359 if ( overlay_init() ) {
360 fprintf( stderr, "%s: overlay_init failed!\n", progname );
361 exit( EXIT_FAILURE );
364 rc = read_config( conffile, 0 );
367 fprintf( stderr, "%s: bad configuration file!\n", progname );
368 exit( EXIT_FAILURE );
378 fprintf( stderr, "No databases found "
379 "in config file\n" );
380 exit( EXIT_FAILURE );
388 rc = glue_sub_init();
391 fprintf( stderr, "Subordinate configuration error\n" );
392 exit( EXIT_FAILURE );
395 rc = slap_schema_check();
398 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
399 exit( EXIT_FAILURE );
415 ber_str2bv( subtree, 0, 0, &val );
416 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
417 if( rc != LDAP_SUCCESS ) {
418 fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
419 exit( EXIT_FAILURE );
422 if ( BER_BVISNULL( &base ) && dbnum == -1 )
428 if( base.bv_val != NULL ) {
431 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
432 if( rc != LDAP_SUCCESS ) {
433 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
434 progname, base.bv_val );
435 exit( EXIT_FAILURE );
438 be = select_backend( &nbase, 0, 0 );
439 ber_memfree( nbase.bv_val );
450 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
451 progname, base.bv_val );
452 exit( EXIT_FAILURE );
454 /* If the named base is a glue master, operate on the
457 if (SLAP_GLUE_INSTANCE(be)) {
461 } else if ( dbnum == -1 ) {
462 if ( nbackends <= 0 ) {
463 fprintf( stderr, "No available databases\n" );
464 exit( EXIT_FAILURE );
467 be = &backends[dbnum=0];
468 /* If just doing the first by default and it is a
469 * glue subordinate, find the master.
471 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
472 if (SLAP_GLUE_SUBORDINATE(be)) {
480 if ( dbnum >= nbackends ) {
481 fprintf( stderr, "Available database(s) "
482 "do not allow %s\n", progname );
483 exit( EXIT_FAILURE );
486 if ( nosubordinates == 0 && dbnum > 0 ) {
488 LDAP_LOG( BACKEND, ERR,
489 "The first database does not allow %s; using the first available one (%d)\n",
490 progname, dbnum + 1, 0 );
492 Debug( LDAP_DEBUG_ANY,
493 "The first database does not allow %s; using the first available one (%d)\n",
494 progname, dbnum + 1, 0 );
498 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
500 "Database number selected via -n is out of range\n"
501 "Must be in the range 1 to %d"
502 " (number of databases in the config file)\n",
504 exit( EXIT_FAILURE );
507 be = &backends[dbnum];
516 if ( !dryrun && slap_startup( be ) ) {
517 fprintf( stderr, "slap_startup failed\n" );
518 exit( EXIT_FAILURE );
522 void slap_tool_destroy( void )
529 if ( slapMode == SLAP_SERVER_MODE ) {
530 /* always false. just pulls in necessary symbol references. */
531 lutil_uuidstr(NULL, 0);
537 ldap_pvt_tls_destroy();
542 mal_dumpleaktrace( leakfile );
545 if ( !BER_BVISNULL( &authcDN ) ) {
546 ch_free( authcDN.bv_val );