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( (unsigned char) *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] );
163 if ( replica_id_list[i] >= 1000 ) {
165 "%s: syncrepl id %d is out of range [0..999]\n",
166 progname, replica_id_list[i] );
167 exit( EXIT_FAILURE );
170 replica_id_list[i] = -1;
173 case 'k': /* Retrieve sync cookie entry */
174 retrieve_synccookie = 1;
177 case 'l': /* LDIF file */
178 ldiffile = strdup( optarg );
181 case 'm': /* Retrieve ldapsync entry */
185 case 'n': /* which config file db to index */
186 dbnum = atoi( optarg ) - 1;
189 case 'p': /* replica promotion */
190 replica_promotion = 1;
193 case 'r': /* replica demotion */
194 replica_demotion = 1;
197 case 's': /* dump subtree */
198 subtree = strdup( optarg );
201 case 't': /* turn on truncate */
203 mode |= SLAP_TRUNCATE_MODE;
206 case 'u': /* dry run */
210 case 'v': /* turn on verbose */
214 case 'W': /* write context csn on every entry add */
215 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
216 /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
219 case 'w': /* write context csn on at the end */
220 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
229 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
233 if ( replica_promotion && replica_demotion ) {
235 } else if ( !replica_promotion && !replica_demotion ) {
236 if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
241 if ( ldiffile == NULL ) {
242 ldiffp = tool == SLAPCAT ? stdout : stdin;
244 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
248 exit( EXIT_FAILURE );
252 * initialize stuff and figure out which backend we're dealing with
256 if ( module_init() != 0 ) {
257 fprintf( stderr, "%s: module_init failed!\n", progname );
258 exit( EXIT_FAILURE );
262 rc = slap_init( mode, progname );
265 fprintf( stderr, "%s: slap_init failed!\n", progname );
266 exit( EXIT_FAILURE );
269 rc = slap_schema_init();
272 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
273 exit( EXIT_FAILURE );
276 rc = read_config( conffile, 0 );
279 fprintf( stderr, "%s: bad configuration file!\n", progname );
280 exit( EXIT_FAILURE );
284 fprintf( stderr, "No databases found in config file\n" );
285 exit( EXIT_FAILURE );
288 rc = glue_sub_init();
291 fprintf( stderr, "Subordinate configuration error\n" );
292 exit( EXIT_FAILURE );
295 rc = slap_schema_check();
298 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
299 exit( EXIT_FAILURE );
304 val.bv_val = subtree;
305 val.bv_len = strlen( subtree );
306 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
307 if( rc != LDAP_SUCCESS ) {
308 fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
309 exit( EXIT_FAILURE );
312 if( base.bv_val == NULL && dbnum == -1 )
318 if( base.bv_val != NULL ) {
321 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
322 if( rc != LDAP_SUCCESS ) {
323 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
324 progname, base.bv_val );
325 exit( EXIT_FAILURE );
328 be = select_backend( &nbase, 0, 0 );
329 ber_memfree( nbase.bv_val );
332 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
333 progname, base.bv_val );
334 exit( EXIT_FAILURE );
336 /* If the named base is a glue master, operate on the
339 if (SLAP_GLUE_INSTANCE(be)) {
343 } else if ( dbnum == -1 ) {
344 if ( nbackends <= 0 ) {
345 fprintf( stderr, "No available databases\n" );
346 exit( EXIT_FAILURE );
349 be = &backends[dbnum=0];
350 /* If just doing the first by default and it is a
351 * glue subordinate, find the master.
353 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
354 if (SLAP_GLUE_SUBORDINATE(be)) {
362 if ( dbnum >= nbackends ) {
363 fprintf( stderr, "Available database(s) "
364 "do not allow %s\n", name );
365 exit( EXIT_FAILURE );
368 if ( nosubordinates == 0 && dbnum > 0 ) {
370 LDAP_LOG( BACKEND, ERR,
371 "The first database does not allow %s; using the first available one (%d)\n",
372 name, dbnum + 1, 0 );
374 Debug( LDAP_DEBUG_ANY,
375 "The first database does not allow %s; using the first available one (%d)\n",
376 name, dbnum + 1, 0 );
380 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
382 "Database number selected via -n is out of range\n"
383 "Must be in the range 1 to %d"
384 " (number of databases in the config file)\n",
386 exit( EXIT_FAILURE );
389 be = &backends[dbnum];
399 void slap_tool_destroy( void )
404 if ( slapMode == SLAP_SERVER_MODE ) {
405 /* always false. just pulls in necessary symbol references. */
406 lutil_uuidstr(NULL, 0);
412 ldap_pvt_tls_destroy();
417 mal_dumpleaktrace( leakfile );