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 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 ) + STRLENOF( ".leak" ) + 1 );
121 sprintf( leakfilename, "%s.leak", progname );
122 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
125 free( leakfilename );
130 options = "b:cd:f:l:n:qtuvw";
134 options = "a:b:cd:f:l:n: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:qv";
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 'l': /* LDIF file */
196 ldiffile = strdup( optarg );
200 ber_str2bv( optarg, 0, 0, &mech );
203 case 'n': /* which config file db to index */
204 dbnum = atoi( optarg );
207 case 'q': /* turn on quick */
208 mode |= SLAP_TOOL_QUICK;
215 case 's': /* dump subtree */
216 subtree = strdup( optarg );
219 case 't': /* turn on truncate */
221 mode |= SLAP_TRUNCATE_MODE;
225 ber_str2bv( optarg, 0, 0, &authcID );
228 case 'u': /* dry run */
232 case 'v': /* turn on verbose */
236 case 'w': /* write context csn at the end */
241 ber_str2bv( optarg, 0, 0, &authzID );
245 usage( tool, progname );
254 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
255 usage( tool, progname );
261 if ( argc == optind ) {
262 usage( tool, progname );
267 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
268 usage( tool, progname );
273 if ( argc != optind ) {
274 usage( tool, progname );
279 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
280 usage( tool, progname );
282 if ( BER_BVISNULL( &base ) ) {
283 usage( tool, progname );
285 ber_dupbv( &baseDN, &base );
294 if ( ldiffile == NULL ) {
295 ldiffp = tool == SLAPCAT ? stdout : stdin;
297 } else if ((ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
301 exit( EXIT_FAILURE );
305 * initialize stuff and figure out which backend we're dealing with
309 if ( module_init() != 0 ) {
310 fprintf( stderr, "%s: module_init failed!\n", progname );
311 exit( EXIT_FAILURE );
315 rc = slap_schema_init();
318 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
319 exit( EXIT_FAILURE );
322 rc = slap_init( mode, progname );
325 fprintf( stderr, "%s: slap_init failed!\n", progname );
326 exit( EXIT_FAILURE );
329 if ( frontend_init() ) {
330 fprintf( stderr, "%s: frontend_init failed!\n", progname );
331 exit( EXIT_FAILURE );
334 if ( overlay_init() ) {
335 fprintf( stderr, "%s: overlay_init failed!\n", progname );
336 exit( EXIT_FAILURE );
339 rc = read_config( conffile, 0 );
342 fprintf( stderr, "%s: bad configuration file!\n", progname );
343 exit( EXIT_FAILURE );
353 fprintf( stderr, "No databases found "
354 "in config file\n" );
355 exit( EXIT_FAILURE );
363 rc = slap_schema_check();
366 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
367 exit( EXIT_FAILURE );
382 filter = str2filter( filterstr );
384 if( filter == NULL ) {
385 fprintf( stderr, "Invalid filter '%s'\n", filterstr );
386 exit( EXIT_FAILURE );
392 ber_str2bv( subtree, 0, 0, &val );
393 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
394 if( rc != LDAP_SUCCESS ) {
395 fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
396 exit( EXIT_FAILURE );
399 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
406 if( base.bv_val != NULL ) {
409 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
410 if( rc != LDAP_SUCCESS ) {
411 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
412 progname, base.bv_val );
413 exit( EXIT_FAILURE );
416 be = select_backend( &nbase, 0, 0 );
417 ber_memfree( nbase.bv_val );
428 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
429 progname, base.bv_val );
430 exit( EXIT_FAILURE );
432 /* If the named base is a glue master, operate on the
435 if (SLAP_GLUE_INSTANCE(be)) {
439 } else if ( dbnum == -1 ) {
440 if ( nbackends <= 0 ) {
441 fprintf( stderr, "No available databases\n" );
442 exit( EXIT_FAILURE );
445 be = &backends[dbnum=1];
446 /* If just doing the first by default and it is a
447 * glue subordinate, find the master.
449 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
450 if (SLAP_GLUE_SUBORDINATE(be)) {
458 if ( dbnum >= nbackends ) {
459 fprintf( stderr, "Available database(s) "
460 "do not allow %s\n", progname );
461 exit( EXIT_FAILURE );
464 if ( nosubordinates == 0 && dbnum > 0 ) {
465 Debug( LDAP_DEBUG_ANY,
466 "The first database does not allow %s;"
467 " using the first available one (%d)\n",
468 progname, dbnum + 1, 0 );
471 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
473 "Database number selected via -n is out of range\n"
474 "Must be in the range 1 to %d"
475 " (number of databases in the config file)\n",
477 exit( EXIT_FAILURE );
480 be = &backends[dbnum];
489 if ( !dryrun && slap_startup( be ) ) {
493 fprintf( stderr, "slap_startup failed "
494 "(test would succeed using "
495 "the -u switch)\n" );
499 fprintf( stderr, "slap_startup failed\n" );
503 exit( EXIT_FAILURE );
507 void slap_tool_destroy( void )
509 if ( !dryrun && be != NULL ) {
514 if ( slapMode == SLAP_SERVER_MODE ) {
515 /* always false. just pulls in necessary symbol references. */
516 lutil_uuidstr(NULL, 0);
522 ldap_pvt_tls_destroy();
527 mal_dumpleaktrace( leakfile );
530 if ( !BER_BVISNULL( &authcDN ) ) {
531 ch_free( authcDN.bv_val );