1 /* slapcommon.c - common routine for the slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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]",
57 options = "\n\t[-U authcID | -D authcDN]"
58 " -b DN [attr[/access][:value]] [...]\n";
62 options = "\n\t[-n databasenumber | -b suffix]\n"
63 "\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
67 options = "\n\t[-U authcID] [-X authzID] [-R realm] [-M mech] ID [...]\n";
71 options = "\n\t[-n databasenumber | -b suffix]"
72 " [-l ldiffile] [-a filter] [-m] [-k]\n";
76 options = " DN [...]\n";
80 options = "\n\t[-n databasenumber | -b suffix]\n";
88 if ( options != NULL ) {
89 fputs( options, stderr );
96 * slap_tool_init - initialize slap utility, handle program options.
100 * argc, argv command line arguments
105 const char* progname,
107 int argc, char **argv )
110 char *conffile = SLAPD_DEFAULT_CONFIGFILE;
111 struct berval base = BER_BVNULL;
112 char *filterstr = NULL;
113 char *subtree = NULL;
114 char *ldiffile = NULL;
116 int mode = SLAP_TOOL_MODE;
117 int truncatemode = 0;
120 leakfilename = malloc( strlen( progname ) + STRLEOF( ".leak" ) - 1 );
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 = "a:b:cd:f:kl:mn:s:v";
135 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
140 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
145 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
149 options = "d:f:M:R:U:vX:";
150 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
154 options = "b:cd:f:n:v";
155 mode |= SLAP_TOOL_READMAIN;
159 options = "b:D:d:f:U:v";
160 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
164 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
165 exit( EXIT_FAILURE );
169 while ( (i = getopt( argc, argv, options )) != EOF ) {
172 filterstr = strdup( optarg );
176 ber_str2bv( optarg, 0, 1, &base );
179 case 'c': /* enable continue mode */
183 case 'd': /* turn on debugging */
184 ldap_debug += atoi( optarg );
188 ber_str2bv( optarg, 0, 1, &authcDN );
191 case 'f': /* specify a conf file */
192 conffile = strdup( optarg );
195 case 'i': /* specify syncrepl id list */
196 replica_id_string = strdup( optarg );
197 if ( !isdigit( (unsigned char) *replica_id_string )) {
198 usage( tool, progname );
199 exit( EXIT_FAILURE );
201 slap_str2clist( &replica_id_strlist, replica_id_string, "," );
202 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
203 replica_id_list = ch_calloc( i + 1, sizeof( int ) );
204 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
205 replica_id_list[i] = atoi( replica_id_strlist[i] );
206 if ( replica_id_list[i] >= 1000 ) {
208 "%s: syncrepl id %d is out of range [0..999]\n",
209 progname, replica_id_list[i] );
210 exit( EXIT_FAILURE );
213 replica_id_list[i] = -1;
216 case 'k': /* Retrieve sync cookie entry */
217 retrieve_synccookie = 1;
220 case 'l': /* LDIF file */
221 ldiffile = strdup( optarg );
224 case 'm': /* Retrieve ldapsync entry */
229 ber_str2bv( optarg, 0, 0, &mech );
232 case 'n': /* which config file db to index */
233 dbnum = atoi( optarg ) - 1;
236 case 'p': /* replica promotion */
237 replica_promotion = 1;
240 case 'r': /* replica demotion */
241 replica_demotion = 1;
248 case 's': /* dump subtree */
249 subtree = strdup( optarg );
252 case 't': /* turn on truncate */
254 mode |= SLAP_TRUNCATE_MODE;
258 ber_str2bv( optarg, 0, 0, &authcID );
261 case 'u': /* dry run */
265 case 'v': /* turn on verbose */
269 case 'W': /* write context csn on every entry add */
270 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
271 /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
274 case 'w': /* write context csn on at the end */
275 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
279 ber_str2bv( optarg, 0, 0, &authzID );
283 usage( tool, progname );
292 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
293 usage( tool, progname );
296 if ( replica_promotion && replica_demotion ) {
297 usage( tool, progname );
299 } else if ( !replica_promotion && !replica_demotion ) {
300 if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
301 usage( tool, progname );
307 if ( argc == optind ) {
308 usage( tool, progname );
313 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
314 usage( tool, progname );
319 if ( argc != optind ) {
320 usage( tool, progname );
325 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
326 usage( tool, progname );
328 if ( BER_BVISNULL( &base ) ) {
329 usage( tool, progname );
331 ber_dupbv( &baseDN, &base );
338 if ( ldiffile == NULL ) {
339 ldiffp = tool == SLAPCAT ? stdout : stdin;
341 } else if ((ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
345 exit( EXIT_FAILURE );
349 * initialize stuff and figure out which backend we're dealing with
353 if ( module_init() != 0 ) {
354 fprintf( stderr, "%s: module_init failed!\n", progname );
355 exit( EXIT_FAILURE );
359 rc = slap_init( mode, progname );
362 fprintf( stderr, "%s: slap_init failed!\n", progname );
363 exit( EXIT_FAILURE );
366 rc = slap_schema_init();
369 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
370 exit( EXIT_FAILURE );
373 if ( frontend_init() ) {
374 fprintf( stderr, "%s: frontend_init failed!\n", progname );
375 exit( EXIT_FAILURE );
378 if ( overlay_init() ) {
379 fprintf( stderr, "%s: overlay_init failed!\n", progname );
380 exit( EXIT_FAILURE );
383 rc = read_config( conffile, 0 );
386 fprintf( stderr, "%s: bad configuration file!\n", progname );
387 exit( EXIT_FAILURE );
397 fprintf( stderr, "No databases found "
398 "in config file\n" );
399 exit( EXIT_FAILURE );
407 rc = slap_schema_check();
410 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
411 exit( EXIT_FAILURE );
426 filter = str2filter( filterstr );
428 if( filter == NULL ) {
429 fprintf( stderr, "Invalid filter '%s'\n", filterstr );
430 exit( EXIT_FAILURE );
436 ber_str2bv( subtree, 0, 0, &val );
437 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
438 if( rc != LDAP_SUCCESS ) {
439 fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
440 exit( EXIT_FAILURE );
443 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
450 if( base.bv_val != NULL ) {
453 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
454 if( rc != LDAP_SUCCESS ) {
455 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
456 progname, base.bv_val );
457 exit( EXIT_FAILURE );
460 be = select_backend( &nbase, 0, 0 );
461 ber_memfree( nbase.bv_val );
472 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
473 progname, base.bv_val );
474 exit( EXIT_FAILURE );
476 /* If the named base is a glue master, operate on the
479 if (SLAP_GLUE_INSTANCE(be)) {
483 } else if ( dbnum == -1 ) {
484 if ( nbackends <= 0 ) {
485 fprintf( stderr, "No available databases\n" );
486 exit( EXIT_FAILURE );
489 be = &backends[dbnum=0];
490 /* If just doing the first by default and it is a
491 * glue subordinate, find the master.
493 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
494 if (SLAP_GLUE_SUBORDINATE(be)) {
502 if ( dbnum >= nbackends ) {
503 fprintf( stderr, "Available database(s) "
504 "do not allow %s\n", progname );
505 exit( EXIT_FAILURE );
508 if ( nosubordinates == 0 && dbnum > 0 ) {
509 Debug( LDAP_DEBUG_ANY,
510 "The first database does not allow %s;"
511 " using the first available one (%d)\n",
512 progname, dbnum + 1, 0 );
515 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
517 "Database number selected via -n is out of range\n"
518 "Must be in the range 1 to %d"
519 " (number of databases in the config file)\n",
521 exit( EXIT_FAILURE );
524 be = &backends[dbnum];
533 if ( !dryrun && slap_startup( be ) ) {
537 fprintf( stderr, "slap_startup failed "
538 "(test would succeed using "
539 "the -u switch)\n" );
543 fprintf( stderr, "slap_startup failed\n" );
547 exit( EXIT_FAILURE );
551 void slap_tool_destroy( void )
553 if ( !dryrun && be != NULL ) {
558 if ( slapMode == SLAP_SERVER_MODE ) {
559 /* always false. just pulls in necessary symbol references. */
560 lutil_uuidstr(NULL, 0);
566 ldap_pvt_tls_destroy();
571 mal_dumpleaktrace( leakfile );
574 if ( !BER_BVISNULL( &authcDN ) ) {
575 ch_free( authcDN.bv_val );