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_NONE;
27 int retrieve_ctxcsn = 0;
28 int retrieve_synccookie = 0;
30 int nosubordinates = 0;
32 struct berval sub_ndn = { 0, NULL };
34 char *ldiffile = NULL;
49 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
50 "\t[-n databasenumber | -b suffix]", progname );
54 options = "\t[-l ldiffile] [-u] [-W] [-w]\n";
58 options = "\t[-l ldiffile] [-m] [-k]\n";
66 if( options != NULL ) {
67 fputs( options, stderr );
74 * slap_tool_init - initialize slap utility, handle program options.
78 * argc, argv command line arguments
85 int argc, char **argv )
88 struct berval base = { 0, NULL };
91 int mode = SLAP_TOOL_MODE;
93 progname = lutil_progname( name, argc, argv );
96 leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
97 sprintf( leakfilename, "%s.leak", progname );
98 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
101 free( leakfilename );
106 options = "b:cd:f:l:n:tuvWw";
110 options = "b:cd:f:n:v";
111 mode |= SLAP_TOOL_READMAIN;
115 options = "b:cd:f:kl:mn:s:v";
116 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
120 fprintf( stderr, "%s: unknown tool mode (%d)\n",
122 exit( EXIT_FAILURE );
126 conffile = SLAPD_DEFAULT_CONFIGFILE;
128 while ( (i = getopt( argc, argv, options )) != EOF ) {
131 base.bv_val = strdup( optarg );
132 base.bv_len = strlen( base.bv_val );
135 case 'c': /* enable continue mode */
139 case 'd': /* turn on debugging */
140 ldap_debug += atoi( optarg );
143 case 'f': /* specify a conf file */
144 conffile = strdup( optarg );
147 case 'k': /* Retrieve sync cookie entry */
148 retrieve_synccookie = 1;
151 case 'l': /* LDIF file */
152 ldiffile = strdup( optarg );
155 case 'm': /* Retrieve ldapsync entry */
159 case 'n': /* which config file db to index */
160 dbnum = atoi( optarg ) - 1;
163 case 's': /* dump subtree */
164 subtree = strdup( optarg );
167 case 't': /* turn on truncate */
169 mode |= SLAP_TRUNCATE_MODE;
172 case 'u': /* dry run */
176 case 'v': /* turn on verbose */
180 case 'W': /* write context csn on every entry add */
181 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
182 /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
185 case 'w': /* write context csn on at the end */
186 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
195 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
199 if ( ldiffile == NULL ) {
200 ldiffp = tool == SLAPCAT ? stdout : stdin;
202 } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
206 exit( EXIT_FAILURE );
210 * initialize stuff and figure out which backend we're dealing with
214 if ( module_init() != 0 ) {
215 fprintf( stderr, "%s: module_init failed!\n", progname );
216 exit( EXIT_FAILURE );
220 rc = slap_init( mode, progname );
223 fprintf( stderr, "%s: slap_init failed!\n", progname );
224 exit( EXIT_FAILURE );
227 rc = slap_schema_init();
230 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
231 exit( EXIT_FAILURE );
234 rc = read_config( conffile, 0 );
237 fprintf( stderr, "%s: bad configuration file!\n", progname );
238 exit( EXIT_FAILURE );
242 fprintf( stderr, "No databases found in config file\n" );
243 exit( EXIT_FAILURE );
246 rc = glue_sub_init();
249 fprintf( stderr, "Subordinate configuration error\n" );
250 exit( EXIT_FAILURE );
253 rc = slap_schema_check();
256 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
257 exit( EXIT_FAILURE );
262 val.bv_val = subtree;
263 val.bv_len = strlen( subtree );
264 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
265 if( rc != LDAP_SUCCESS ) {
266 fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
267 exit( EXIT_FAILURE );
270 if( base.bv_val == NULL && dbnum == -1 )
276 if( base.bv_val != NULL ) {
279 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
280 if( rc != LDAP_SUCCESS ) {
281 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
282 progname, base.bv_val );
283 exit( EXIT_FAILURE );
286 be = select_backend( &nbase, 0, 0 );
287 ber_memfree( nbase.bv_val );
290 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
291 progname, base.bv_val );
292 exit( EXIT_FAILURE );
294 /* If the named base is a glue master, operate on the
297 if (SLAP_GLUE_INSTANCE(be)) {
301 } else if ( dbnum == -1 ) {
302 if ( nbackends <= 0 ) {
303 fprintf( stderr, "No available databases\n" );
304 exit( EXIT_FAILURE );
307 be = &backends[dbnum=0];
308 /* If just doing the first by default and it is a
309 * glue subordinate, find the master.
311 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
312 if (SLAP_GLUE_SUBORDINATE(be)) {
320 if ( dbnum >= nbackends ) {
321 fprintf( stderr, "Available database(s) "
322 "do not allow %s\n", name );
323 exit( EXIT_FAILURE );
326 if ( nosubordinates == 0 && dbnum > 0 ) {
328 LDAP_LOG( BACKEND, ERR,
329 "The first database does not allow %s; using the first available one (%d)\n",
330 name, dbnum + 1, 0 );
332 Debug( LDAP_DEBUG_ANY,
333 "The first database does not allow %s; using the first available one (%d)\n",
334 name, dbnum + 1, 0 );
338 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
340 "Database number selected via -n is out of range\n"
341 "Must be in the range 1 to %d"
342 " (number of databases in the config file)\n",
344 exit( EXIT_FAILURE );
347 be = &backends[dbnum];
357 void slap_tool_destroy( void )
362 if ( slapMode == SLAP_SERVER_MODE ) {
363 /* always false. just pulls in necessary symbol references. */
364 lutil_uuidstr(NULL, 0);
370 ldap_pvt_tls_destroy();
375 mal_dumpleaktrace( leakfile );