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;
137 options = "d:f:U:vX:";
141 options = "b:cd:f:n:v";
142 mode |= SLAP_TOOL_READMAIN;
146 options = "b:D:d:f:U:v";
150 fprintf( stderr, "%s: unknown tool mode (%d)\n",
152 exit( EXIT_FAILURE );
156 while ( (i = getopt( argc, argv, options )) != EOF ) {
159 ber_str2bv( optarg, 0, 1, &base );
162 case 'c': /* enable continue mode */
166 case 'd': /* turn on debugging */
167 ldap_debug += atoi( optarg );
171 ber_str2bv( optarg, 0, 0, &authcDN );
174 case 'f': /* specify a conf file */
175 conffile = strdup( optarg );
178 case 'i': /* specify syncrepl id list */
179 replica_id_string = strdup( optarg );
180 if ( !isdigit( (unsigned char) *replica_id_string )) {
181 usage( tool, progname );
182 exit( EXIT_FAILURE );
184 str2clist( &replica_id_strlist, replica_id_string, "," );
185 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
186 replica_id_list = ch_calloc( i + 1, sizeof( int ) );
187 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
188 replica_id_list[i] = atoi( replica_id_strlist[i] );
189 if ( replica_id_list[i] >= 1000 ) {
191 "%s: syncrepl id %d is out of range [0..999]\n",
192 progname, replica_id_list[i] );
193 exit( EXIT_FAILURE );
196 replica_id_list[i] = -1;
199 case 'k': /* Retrieve sync cookie entry */
200 retrieve_synccookie = 1;
203 case 'l': /* LDIF file */
204 ldiffile = strdup( optarg );
207 case 'm': /* Retrieve ldapsync entry */
211 case 'n': /* which config file db to index */
212 dbnum = atoi( optarg ) - 1;
215 case 'p': /* replica promotion */
216 replica_promotion = 1;
219 case 'r': /* replica demotion */
220 replica_demotion = 1;
223 case 's': /* dump subtree */
224 subtree = strdup( optarg );
227 case 't': /* turn on truncate */
229 mode |= SLAP_TRUNCATE_MODE;
233 ber_str2bv( optarg, 0, 0, &authcID );
236 case 'u': /* dry run */
240 case 'v': /* turn on verbose */
244 case 'W': /* write context csn on every entry add */
245 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
246 /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
249 case 'w': /* write context csn on at the end */
250 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
254 ber_str2bv( optarg, 0, 0, &authzID );
258 usage( tool, progname );
267 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
268 usage( tool, progname );
271 if ( replica_promotion && replica_demotion ) {
272 usage( tool, progname );
274 } else if ( !replica_promotion && !replica_demotion ) {
275 if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
276 usage( tool, progname );
282 if ( argc == optind ) {
283 usage( tool, progname );
288 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
289 usage( tool, progname );
294 if ( argc != optind ) {
295 usage( tool, progname );
300 if ( argc == optind ) {
301 usage( tool, progname );
303 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
304 usage( tool, progname );
306 if ( BER_BVISNULL( &base ) ) {
307 usage( tool, progname );
309 ber_dupbv( &baseDN, &base );
316 if ( ldiffile == NULL ) {
317 ldiffp = tool == SLAPCAT ? stdout : stdin;
319 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
323 exit( EXIT_FAILURE );
327 * initialize stuff and figure out which backend we're dealing with
331 if ( module_init() != 0 ) {
332 fprintf( stderr, "%s: module_init failed!\n", progname );
333 exit( EXIT_FAILURE );
337 rc = slap_init( mode, progname );
340 fprintf( stderr, "%s: slap_init failed!\n", progname );
341 exit( EXIT_FAILURE );
344 rc = slap_schema_init();
347 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
348 exit( EXIT_FAILURE );
351 if ( overlay_init() ) {
352 fprintf( stderr, "%s: overlay_init failed!\n", progname );
353 exit( EXIT_FAILURE );
356 rc = read_config( conffile, 0 );
359 fprintf( stderr, "%s: bad configuration file!\n", progname );
360 exit( EXIT_FAILURE );
370 fprintf( stderr, "No databases found "
371 "in config file\n" );
372 exit( EXIT_FAILURE );
380 rc = glue_sub_init();
383 fprintf( stderr, "Subordinate configuration error\n" );
384 exit( EXIT_FAILURE );
387 rc = slap_schema_check();
390 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
391 exit( EXIT_FAILURE );
409 ber_str2bv( subtree, 0, 0, &val );
410 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
411 if( rc != LDAP_SUCCESS ) {
412 fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
413 exit( EXIT_FAILURE );
416 if ( BER_BVISNULL( &base ) && dbnum == -1 )
422 if( base.bv_val != NULL ) {
425 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
426 if( rc != LDAP_SUCCESS ) {
427 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
428 progname, base.bv_val );
429 exit( EXIT_FAILURE );
432 be = select_backend( &nbase, 0, 0 );
433 ber_memfree( nbase.bv_val );
435 if ( tool == SLAPACL ) {
440 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
441 progname, base.bv_val );
442 exit( EXIT_FAILURE );
444 /* If the named base is a glue master, operate on the
447 if (SLAP_GLUE_INSTANCE(be)) {
451 } else if ( dbnum == -1 ) {
452 if ( nbackends <= 0 ) {
453 fprintf( stderr, "No available databases\n" );
454 exit( EXIT_FAILURE );
457 be = &backends[dbnum=0];
458 /* If just doing the first by default and it is a
459 * glue subordinate, find the master.
461 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
462 if (SLAP_GLUE_SUBORDINATE(be)) {
470 if ( dbnum >= nbackends ) {
471 fprintf( stderr, "Available database(s) "
472 "do not allow %s\n", progname );
473 exit( EXIT_FAILURE );
476 if ( nosubordinates == 0 && dbnum > 0 ) {
478 LDAP_LOG( BACKEND, ERR,
479 "The first database does not allow %s; using the first available one (%d)\n",
480 progname, dbnum + 1, 0 );
482 Debug( LDAP_DEBUG_ANY,
483 "The first database does not allow %s; using the first available one (%d)\n",
484 progname, dbnum + 1, 0 );
488 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
490 "Database number selected via -n is out of range\n"
491 "Must be in the range 1 to %d"
492 " (number of databases in the config file)\n",
494 exit( EXIT_FAILURE );
497 be = &backends[dbnum];
506 if ( slap_startup( be ) ) {
507 fprintf( stderr, "slap_startup failed\n" );
508 exit( EXIT_FAILURE );
512 void slap_tool_destroy( void )
517 if ( slapMode == SLAP_SERVER_MODE ) {
518 /* always false. just pulls in necessary symbol references. */
519 lutil_uuidstr(NULL, 0);
525 ldap_pvt_tls_destroy();
530 mal_dumpleaktrace( leakfile );