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]"
63 " [-l ldiffile] [-a filter] [-m] [-k]\n";
67 options = "\tDN [...]\n";
71 options = "\t[-n databasenumber | -b suffix]\n";
75 options = "\t[-U authcID] [-X authzID] ID [...]\n";
79 options = "\t[-U authcID | -D authcDN]"
80 " -b DN attr[/level][:value] [...]\n";
84 if ( options != NULL ) {
85 fputs( options, stderr );
92 * slap_tool_init - initialize slap utility, handle program options.
96 * argc, argv command line arguments
101 const char* progname,
103 int argc, char **argv )
106 char *conffile = SLAPD_DEFAULT_CONFIGFILE;
107 struct berval base = BER_BVNULL;
108 char *filterstr = NULL;
109 char *subtree = NULL;
110 char *ldiffile = NULL;
112 int mode = SLAP_TOOL_MODE;
113 int truncatemode = 0;
116 leakfilename = malloc( strlen( progname ) + STRLEOF( ".leak" ) - 1 );
117 sprintf( leakfilename, "%s.leak", progname );
118 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
121 free( leakfilename );
124 config_init(); /* sets frontendDB */
128 options = "b:cd:f:i:l:n:prtuvWw";
132 options = "a:b:cd:f:kl:mn:s:v";
133 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
139 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
143 options = "d:f:U:vX:";
144 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
148 options = "b:cd:f:n:v";
149 mode |= SLAP_TOOL_READMAIN;
153 options = "b:D:d:f:U:v";
154 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
158 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
159 exit( EXIT_FAILURE );
163 while ( (i = getopt( argc, argv, options )) != EOF ) {
166 filterstr = strdup( optarg );
170 ber_str2bv( optarg, 0, 1, &base );
173 case 'c': /* enable continue mode */
177 case 'd': /* turn on debugging */
178 ldap_debug += atoi( optarg );
182 ber_str2bv( optarg, 0, 1, &authcDN );
185 case 'f': /* specify a conf file */
186 conffile = strdup( optarg );
189 case 'i': /* specify syncrepl id list */
190 replica_id_string = strdup( optarg );
191 if ( !isdigit( (unsigned char) *replica_id_string )) {
192 usage( tool, progname );
193 exit( EXIT_FAILURE );
195 slap_str2clist( &replica_id_strlist, replica_id_string, "," );
196 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
197 replica_id_list = ch_calloc( i + 1, sizeof( int ) );
198 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
199 replica_id_list[i] = atoi( replica_id_strlist[i] );
200 if ( replica_id_list[i] >= 1000 ) {
202 "%s: syncrepl id %d is out of range [0..999]\n",
203 progname, replica_id_list[i] );
204 exit( EXIT_FAILURE );
207 replica_id_list[i] = -1;
210 case 'k': /* Retrieve sync cookie entry */
211 retrieve_synccookie = 1;
214 case 'l': /* LDIF file */
215 ldiffile = strdup( optarg );
218 case 'm': /* Retrieve ldapsync entry */
222 case 'n': /* which config file db to index */
223 dbnum = atoi( optarg ) - 1;
226 case 'p': /* replica promotion */
227 replica_promotion = 1;
230 case 'r': /* replica demotion */
231 replica_demotion = 1;
234 case 's': /* dump subtree */
235 subtree = strdup( optarg );
238 case 't': /* turn on truncate */
240 mode |= SLAP_TRUNCATE_MODE;
244 ber_str2bv( optarg, 0, 0, &authcID );
247 case 'u': /* dry run */
251 case 'v': /* turn on verbose */
255 case 'W': /* write context csn on every entry add */
256 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
257 /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
260 case 'w': /* write context csn on at the end */
261 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
265 ber_str2bv( optarg, 0, 0, &authzID );
269 usage( tool, progname );
278 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
279 usage( tool, progname );
282 if ( replica_promotion && replica_demotion ) {
283 usage( tool, progname );
285 } else if ( !replica_promotion && !replica_demotion ) {
286 if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
287 usage( tool, progname );
293 if ( argc == optind ) {
294 usage( tool, progname );
299 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
300 usage( tool, progname );
305 if ( argc != optind ) {
306 usage( tool, progname );
311 if ( argc == optind ) {
312 usage( tool, progname );
314 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
315 usage( tool, progname );
317 if ( BER_BVISNULL( &base ) ) {
318 usage( tool, progname );
320 ber_dupbv( &baseDN, &base );
327 if ( ldiffile == NULL ) {
328 ldiffp = tool == SLAPCAT ? stdout : stdin;
330 } else if ((ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
334 exit( EXIT_FAILURE );
338 * initialize stuff and figure out which backend we're dealing with
342 if ( module_init() != 0 ) {
343 fprintf( stderr, "%s: module_init failed!\n", progname );
344 exit( EXIT_FAILURE );
348 rc = slap_init( mode, progname );
351 fprintf( stderr, "%s: slap_init failed!\n", progname );
352 exit( EXIT_FAILURE );
355 rc = slap_schema_init();
358 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
359 exit( EXIT_FAILURE );
362 if ( frontend_init() ) {
363 fprintf( stderr, "%s: frontend_init failed!\n", progname );
364 exit( EXIT_FAILURE );
367 if ( overlay_init() ) {
368 fprintf( stderr, "%s: overlay_init failed!\n", progname );
369 exit( EXIT_FAILURE );
372 rc = read_config( conffile, 0 );
375 fprintf( stderr, "%s: bad configuration file!\n", progname );
376 exit( EXIT_FAILURE );
386 fprintf( stderr, "No databases found "
387 "in config file\n" );
388 exit( EXIT_FAILURE );
396 rc = glue_sub_init();
399 fprintf( stderr, "Subordinate configuration error\n" );
400 exit( EXIT_FAILURE );
403 rc = slap_schema_check();
406 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
407 exit( EXIT_FAILURE );
422 filter = str2filter( filterstr );
424 if( filter == NULL ) {
425 fprintf( stderr, "Invalid filter '%s'\n", filterstr );
426 exit( EXIT_FAILURE );
432 ber_str2bv( subtree, 0, 0, &val );
433 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
434 if( rc != LDAP_SUCCESS ) {
435 fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
436 exit( EXIT_FAILURE );
439 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
446 if( base.bv_val != NULL ) {
449 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
450 if( rc != LDAP_SUCCESS ) {
451 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
452 progname, base.bv_val );
453 exit( EXIT_FAILURE );
456 be = select_backend( &nbase, 0, 0 );
457 ber_memfree( nbase.bv_val );
468 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
469 progname, base.bv_val );
470 exit( EXIT_FAILURE );
472 /* If the named base is a glue master, operate on the
475 if (SLAP_GLUE_INSTANCE(be)) {
479 } else if ( dbnum == -1 ) {
480 if ( nbackends <= 0 ) {
481 fprintf( stderr, "No available databases\n" );
482 exit( EXIT_FAILURE );
485 be = &backends[dbnum=0];
486 /* If just doing the first by default and it is a
487 * glue subordinate, find the master.
489 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
490 if (SLAP_GLUE_SUBORDINATE(be)) {
498 if ( dbnum >= nbackends ) {
499 fprintf( stderr, "Available database(s) "
500 "do not allow %s\n", progname );
501 exit( EXIT_FAILURE );
504 if ( nosubordinates == 0 && dbnum > 0 ) {
505 Debug( LDAP_DEBUG_ANY,
506 "The first database does not allow %s;"
507 " using the first available one (%d)\n",
508 progname, dbnum + 1, 0 );
511 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
513 "Database number selected via -n is out of range\n"
514 "Must be in the range 1 to %d"
515 " (number of databases in the config file)\n",
517 exit( EXIT_FAILURE );
520 be = &backends[dbnum];
529 if ( !dryrun && slap_startup( be ) ) {
530 fprintf( stderr, "slap_startup failed\n" );
531 exit( EXIT_FAILURE );
535 void slap_tool_destroy( void )
537 if ( !dryrun && be != NULL ) {
542 if ( slapMode == SLAP_SERVER_MODE ) {
543 /* always false. just pulls in necessary symbol references. */
544 lutil_uuidstr(NULL, 0);
550 ldap_pvt_tls_destroy();
555 mal_dumpleaktrace( leakfile );
558 if ( !BER_BVISNULL( &authcDN ) ) {
559 ch_free( authcDN.bv_val );