3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* slapcommon.c - common routine for the slap tools */
12 #include <ac/stdlib.h>
14 #include <ac/string.h>
15 #include <ac/socket.h>
16 #include <ac/unistd.h>
18 #include "slapcommon.h"
22 char *progname = NULL;
23 char *conffile = SLAPD_DEFAULT_CONFIGFILE;
26 int update_ctxcsn = SLAP_TOOL_CTXCSN_KEEP;
27 int retrieve_ctxcsn = 0;
28 int retrieve_synccookie = 0;
29 int replica_promotion = 0;
30 int replica_demotion = 0;
31 char *replica_id_string = NULL;
32 char **replica_id_strlist = NULL;
33 int *replica_id_list = NULL;
35 int nosubordinates = 0;
37 struct berval sub_ndn = { 0, NULL };
39 char *ldiffile = NULL;
54 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
55 "\t[-n databasenumber | -b suffix]", progname );
59 options = "\n\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
63 options = "\t[-l ldiffile] [-m] [-k]\n";
71 if( options != NULL ) {
72 fputs( options, stderr );
79 * slap_tool_init - initialize slap utility, handle program options.
83 * argc, argv command line arguments
90 int argc, char **argv )
93 struct berval base = { 0, NULL };
96 int mode = SLAP_TOOL_MODE;
98 progname = lutil_progname( name, argc, argv );
101 leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
102 sprintf( leakfilename, "%s.leak", progname );
103 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
106 free( leakfilename );
111 options = "b:cd:f:i:l:n:prtuvWw";
115 options = "b:cd:f:n:v";
116 mode |= SLAP_TOOL_READMAIN;
120 options = "b:cd:f:kl:mn:s:v";
121 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
125 fprintf( stderr, "%s: unknown tool mode (%d)\n",
127 exit( EXIT_FAILURE );
131 conffile = SLAPD_DEFAULT_CONFIGFILE;
133 while ( (i = getopt( argc, argv, options )) != EOF ) {
136 base.bv_val = strdup( optarg );
137 base.bv_len = strlen( base.bv_val );
140 case 'c': /* enable continue mode */
144 case 'd': /* turn on debugging */
145 ldap_debug += atoi( optarg );
148 case 'f': /* specify a conf file */
149 conffile = strdup( optarg );
152 case 'i': /* specify syncrepl id list */
153 replica_id_string = strdup( optarg );
154 if ( !isdigit( *replica_id_string )) {
156 exit( EXIT_FAILURE );
158 str2clist( &replica_id_strlist, replica_id_string, "," );
159 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
160 replica_id_list = ch_calloc( i + 1, sizeof( int ) );
161 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
162 replica_id_list[i] = atoi( replica_id_strlist[i] );
164 replica_id_list[i] = -1;
167 case 'k': /* Retrieve sync cookie entry */
168 retrieve_synccookie = 1;
171 case 'l': /* LDIF file */
172 ldiffile = strdup( optarg );
175 case 'm': /* Retrieve ldapsync entry */
179 case 'n': /* which config file db to index */
180 dbnum = atoi( optarg ) - 1;
183 case 'p': /* replica promotion */
184 replica_promotion = 1;
187 case 'r': /* replica demotion */
188 replica_demotion = 1;
191 case 's': /* dump subtree */
192 subtree = strdup( optarg );
195 case 't': /* turn on truncate */
197 mode |= SLAP_TRUNCATE_MODE;
200 case 'u': /* dry run */
204 case 'v': /* turn on verbose */
208 case 'W': /* write context csn on every entry add */
209 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
210 /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
213 case 'w': /* write context csn on at the end */
214 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
223 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
227 if ( replica_promotion && replica_demotion ) {
229 } else if ( !replica_promotion && !replica_demotion ) {
230 if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
235 if ( ldiffile == NULL ) {
236 ldiffp = tool == SLAPCAT ? stdout : stdin;
238 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
242 exit( EXIT_FAILURE );
246 * initialize stuff and figure out which backend we're dealing with
250 if ( module_init() != 0 ) {
251 fprintf( stderr, "%s: module_init failed!\n", progname );
252 exit( EXIT_FAILURE );
256 rc = slap_init( mode, progname );
259 fprintf( stderr, "%s: slap_init failed!\n", progname );
260 exit( EXIT_FAILURE );
263 rc = slap_schema_init();
266 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
267 exit( EXIT_FAILURE );
270 rc = read_config( conffile, 0 );
273 fprintf( stderr, "%s: bad configuration file!\n", progname );
274 exit( EXIT_FAILURE );
278 fprintf( stderr, "No databases found in config file\n" );
279 exit( EXIT_FAILURE );
282 rc = glue_sub_init();
285 fprintf( stderr, "Subordinate configuration error\n" );
286 exit( EXIT_FAILURE );
289 rc = slap_schema_check();
292 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
293 exit( EXIT_FAILURE );
298 val.bv_val = subtree;
299 val.bv_len = strlen( subtree );
300 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
301 if( rc != LDAP_SUCCESS ) {
302 fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
303 exit( EXIT_FAILURE );
306 if( base.bv_val == NULL && dbnum == -1 )
312 if( base.bv_val != NULL ) {
315 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
316 if( rc != LDAP_SUCCESS ) {
317 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
318 progname, base.bv_val );
319 exit( EXIT_FAILURE );
322 be = select_backend( &nbase, 0, 0 );
323 ber_memfree( nbase.bv_val );
326 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
327 progname, base.bv_val );
328 exit( EXIT_FAILURE );
330 /* If the named base is a glue master, operate on the
333 if (SLAP_GLUE_INSTANCE(be)) {
337 } else if ( dbnum == -1 ) {
338 if ( nbackends <= 0 ) {
339 fprintf( stderr, "No available databases\n" );
340 exit( EXIT_FAILURE );
343 be = &backends[dbnum=0];
344 /* If just doing the first by default and it is a
345 * glue subordinate, find the master.
347 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
348 if (SLAP_GLUE_SUBORDINATE(be)) {
356 if ( dbnum >= nbackends ) {
357 fprintf( stderr, "Available database(s) "
358 "do not allow %s\n", name );
359 exit( EXIT_FAILURE );
362 if ( nosubordinates == 0 && dbnum > 0 ) {
364 LDAP_LOG( BACKEND, ERR,
365 "The first database does not allow %s; using the first available one (%d)\n",
366 name, dbnum + 1, 0 );
368 Debug( LDAP_DEBUG_ANY,
369 "The first database does not allow %s; using the first available one (%d)\n",
370 name, dbnum + 1, 0 );
374 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
376 "Database number selected via -n is out of range\n"
377 "Must be in the range 1 to %d"
378 " (number of databases in the config file)\n",
380 exit( EXIT_FAILURE );
383 be = &backends[dbnum];
393 void slap_tool_destroy( void )
398 if ( slapMode == SLAP_SERVER_MODE ) {
399 /* always false. just pulls in necessary symbol references. */
400 lutil_uuidstr(NULL, 0);
406 ldap_pvt_tls_destroy();
411 mal_dumpleaktrace( leakfile );