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] [-q] [-u] [-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]\n";
76 options = " DN [...]\n";
80 options = "\n\t[-n databasenumber | -b suffix] [-q]\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 char *confdir = SLAPD_DEFAULT_CONFIGDIR;
112 struct berval base = BER_BVNULL;
113 char *filterstr = NULL;
114 char *subtree = NULL;
115 char *ldiffile = NULL;
117 int mode = SLAP_TOOL_MODE;
118 int truncatemode = 0;
121 leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
122 sprintf( leakfilename, "%s.leak", progname );
123 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
126 free( leakfilename );
131 options = "b:cd:f:F:l:n:qtuvw";
135 options = "a:b:cd:f:F:l:n:s:v";
136 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
141 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
145 options = "d:f:F:uv";
146 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
150 options = "d:f:F:M:R:U:vX:";
151 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
155 options = "b:cd:f:F:n:qv";
156 mode |= SLAP_TOOL_READMAIN;
160 options = "b:D:d:f:F:U:v";
161 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
165 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
166 exit( EXIT_FAILURE );
170 while ( (i = getopt( argc, argv, options )) != EOF ) {
173 filterstr = strdup( optarg );
177 ber_str2bv( optarg, 0, 1, &base );
180 case 'c': /* enable continue mode */
184 case 'd': /* turn on debugging */
185 ldap_debug += atoi( optarg );
189 ber_str2bv( optarg, 0, 1, &authcDN );
192 case 'f': /* specify a conf file */
193 conffile = strdup( optarg );
196 case 'F': /* specify a conf dir */
197 confdir = strdup( optarg );
200 case 'l': /* LDIF file */
201 ldiffile = strdup( optarg );
205 ber_str2bv( optarg, 0, 0, &mech );
208 case 'n': /* which config file db to index */
209 dbnum = atoi( optarg );
212 case 'q': /* turn on quick */
213 mode |= SLAP_TOOL_QUICK;
220 case 's': /* dump subtree */
221 subtree = strdup( optarg );
224 case 't': /* turn on truncate */
226 mode |= SLAP_TRUNCATE_MODE;
230 ber_str2bv( optarg, 0, 0, &authcID );
233 case 'u': /* dry run */
237 case 'v': /* turn on verbose */
241 case 'w': /* write context csn at the end */
246 ber_str2bv( optarg, 0, 0, &authzID );
250 usage( tool, progname );
259 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
260 usage( tool, progname );
266 if ( argc == optind ) {
267 usage( tool, progname );
272 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
273 usage( tool, progname );
278 if ( argc != optind ) {
279 usage( tool, progname );
284 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
285 usage( tool, progname );
287 if ( BER_BVISNULL( &base ) ) {
288 usage( tool, progname );
290 ber_dupbv( &baseDN, &base );
299 if ( ldiffile == NULL ) {
300 ldiffp = tool == SLAPCAT ? stdout : stdin;
302 } else if ((ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
306 exit( EXIT_FAILURE );
310 * initialize stuff and figure out which backend we're dealing with
314 if ( module_init() != 0 ) {
315 fprintf( stderr, "%s: module_init failed!\n", progname );
316 exit( EXIT_FAILURE );
320 rc = slap_schema_init();
323 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
324 exit( EXIT_FAILURE );
327 rc = slap_init( mode, progname );
330 fprintf( stderr, "%s: slap_init failed!\n", progname );
331 exit( EXIT_FAILURE );
334 if ( frontend_init() ) {
335 fprintf( stderr, "%s: frontend_init failed!\n", progname );
336 exit( EXIT_FAILURE );
339 if ( overlay_init() ) {
340 fprintf( stderr, "%s: overlay_init failed!\n", progname );
341 exit( EXIT_FAILURE );
344 rc = read_config( conffile, confdir );
347 fprintf( stderr, "%s: bad configuration file!\n", progname );
348 exit( EXIT_FAILURE );
358 fprintf( stderr, "No databases found "
359 "in config file\n" );
360 exit( EXIT_FAILURE );
368 rc = slap_schema_check();
371 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
372 exit( EXIT_FAILURE );
387 filter = str2filter( filterstr );
389 if( filter == NULL ) {
390 fprintf( stderr, "Invalid filter '%s'\n", filterstr );
391 exit( EXIT_FAILURE );
397 ber_str2bv( subtree, 0, 0, &val );
398 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
399 if( rc != LDAP_SUCCESS ) {
400 fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
401 exit( EXIT_FAILURE );
404 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
411 if( base.bv_val != NULL ) {
414 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
415 if( rc != LDAP_SUCCESS ) {
416 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
417 progname, base.bv_val );
418 exit( EXIT_FAILURE );
421 be = select_backend( &nbase, 0, 0 );
422 ber_memfree( nbase.bv_val );
433 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
434 progname, base.bv_val );
435 exit( EXIT_FAILURE );
437 /* If the named base is a glue master, operate on the
440 if (SLAP_GLUE_INSTANCE(be)) {
444 } else if ( dbnum == -1 ) {
445 if ( nbackends <= 0 ) {
446 fprintf( stderr, "No available databases\n" );
447 exit( EXIT_FAILURE );
449 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
451 if ( dbnum < 1 ) continue;
453 if ( SLAP_MONITOR(be))
456 /* If just doing the first by default and it is a
457 * glue subordinate, find the master.
459 if ( SLAP_GLUE_SUBORDINATE(be) ) {
467 fprintf( stderr, "Available database(s) "
468 "do not allow %s\n", progname );
469 exit( EXIT_FAILURE );
472 if ( nosubordinates == 0 && dbnum > 0 ) {
473 Debug( LDAP_DEBUG_ANY,
474 "The first database does not allow %s;"
475 " using the first available one (%d)\n",
476 progname, dbnum + 1, 0 );
479 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
481 "Database number selected via -n is out of range\n"
482 "Must be in the range 1 to %d"
483 " (number of databases in the config file)\n",
485 exit( EXIT_FAILURE );
488 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
489 if ( dbnum == 0 ) break;
500 if ( !dryrun && slap_startup( be ) ) {
504 fprintf( stderr, "slap_startup failed "
505 "(test would succeed using "
506 "the -u switch)\n" );
510 fprintf( stderr, "slap_startup failed\n" );
514 exit( EXIT_FAILURE );
518 void slap_tool_destroy( void )
520 if ( !dryrun && be != NULL ) {
525 if ( slapMode == SLAP_SERVER_MODE ) {
526 /* always false. just pulls in necessary symbol references. */
527 lutil_uuidstr(NULL, 0);
533 ldap_pvt_tls_destroy();
538 mal_dumpleaktrace( leakfile );
541 if ( !BER_BVISNULL( &authcDN ) ) {
542 ch_free( authcDN.bv_val );