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"
41 tool_vars tool_globals;
44 static char *leakfilename;
45 static FILE *leakfile;
51 usage( int tool, const char *progname )
55 "usage: %s [-v] [-d debuglevel] [-f configfile] [-F configdir]",
60 options = "\n\t[-U authcID | -D authcDN] [-X authzID | -o authzDN=<DN>]"
61 "\n\t-b DN -o <var>[=<val>] [-u] [attr[/access][:value]] [...]\n";
65 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]\n"
66 "\t[-l ldiffile] [-q] [-u] [-w]\n";
70 options = "\n\t[-U authcID] [-X authzID] [-R realm] [-M mech] ID [...]\n";
74 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]"
75 " [-l ldiffile] [-a filter]\n";
79 options = "\n\t[-N | -P] DN [...]\n";
83 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix] [-q]\n";
91 if ( options != NULL ) {
92 fputs( options, stderr );
103 p = strchr( optarg, '=' );
111 if ( strncasecmp( optarg, "sockurl", len ) == 0 ) {
112 if ( !BER_BVISNULL( &listener_url ) ) {
113 ber_memfree( listener_url.bv_val );
115 ber_str2bv( p, 0, 1, &listener_url );
117 } else if ( strncasecmp( optarg, "domain", len ) == 0 ) {
118 if ( !BER_BVISNULL( &peer_domain ) ) {
119 ber_memfree( peer_domain.bv_val );
121 ber_str2bv( p, 0, 1, &peer_domain );
123 } else if ( strncasecmp( optarg, "peername", len ) == 0 ) {
124 if ( !BER_BVISNULL( &peer_name ) ) {
125 ber_memfree( peer_name.bv_val );
127 ber_str2bv( p, 0, 1, &peer_name );
129 } else if ( strncasecmp( optarg, "sockname", len ) == 0 ) {
130 if ( !BER_BVISNULL( &sock_name ) ) {
131 ber_memfree( sock_name.bv_val );
133 ber_str2bv( p, 0, 1, &sock_name );
135 } else if ( strncasecmp( optarg, "ssf", len ) == 0 ) {
136 if ( lutil_atou( &ssf, p ) ) {
137 Debug( LDAP_DEBUG_ANY, "unable to parse ssf=\"%s\".\n", p, 0, 0 );
141 } else if ( strncasecmp( optarg, "transport_ssf", len ) == 0 ) {
142 if ( lutil_atou( &transport_ssf, p ) ) {
143 Debug( LDAP_DEBUG_ANY, "unable to parse transport_ssf=\"%s\".\n", p, 0, 0 );
147 } else if ( strncasecmp( optarg, "tls_ssf", len ) == 0 ) {
148 if ( lutil_atou( &tls_ssf, p ) ) {
149 Debug( LDAP_DEBUG_ANY, "unable to parse tls_ssf=\"%s\".\n", p, 0, 0 );
153 } else if ( strncasecmp( optarg, "sasl_ssf", len ) == 0 ) {
154 if ( lutil_atou( &sasl_ssf, p ) ) {
155 Debug( LDAP_DEBUG_ANY, "unable to parse sasl_ssf=\"%s\".\n", p, 0, 0 );
159 } else if ( strncasecmp( optarg, "authzDN", len ) == 0 ) {
160 ber_str2bv( p, 0, 1, &authzDN );
170 * slap_tool_init - initialize slap utility, handle program options.
174 * argc, argv command line arguments
177 static int need_shutdown;
181 const char* progname,
183 int argc, char **argv )
186 char *conffile = NULL;
187 char *confdir = NULL;
188 struct berval base = BER_BVNULL;
189 char *filterstr = NULL;
190 char *subtree = NULL;
191 char *ldiffile = NULL;
193 int mode = SLAP_TOOL_MODE;
194 int truncatemode = 0;
198 /* tools default to "none", so that at least LDAP_DEBUG_ANY
199 * messages show up; use -d 0 to reset */
200 slap_debug = LDAP_DEBUG_NONE;
204 leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
205 sprintf( leakfilename, "%s.leak", progname );
206 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
209 free( leakfilename );
214 options = "b:cd:f:F:gl:n:qtuvw";
218 options = "a:b:cd:f:F:gl:n:s:v";
219 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
223 options = "d:f:F:NPv";
224 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
228 options = "d:f:F:uv";
229 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
233 options = "d:f:F:M:R:U:vX:";
234 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
238 options = "b:cd:f:F:gn:qv";
239 mode |= SLAP_TOOL_READMAIN;
243 options = "b:D:d:f:F:o:uU:vX:";
244 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
248 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
249 exit( EXIT_FAILURE );
253 while ( (i = getopt( argc, argv, options )) != EOF ) {
256 filterstr = strdup( optarg );
260 ber_str2bv( optarg, 0, 1, &base );
263 case 'c': /* enable continue mode */
267 case 'd': { /* turn on debugging */
270 if ( parse_debug_level( optarg, &level ) ) {
271 usage( tool, progname );
275 /* allow to reset log level */
283 fputs( "must compile with LDAP_DEBUG for debugging\n",
289 ber_str2bv( optarg, 0, 1, &authcDN );
292 case 'f': /* specify a conf file */
293 conffile = strdup( optarg );
296 case 'F': /* specify a conf dir */
297 confdir = strdup( optarg );
300 case 'g': /* disable subordinate glue */
304 case 'l': /* LDIF file */
305 ldiffile = strdup( optarg );
309 ber_str2bv( optarg, 0, 0, &mech );
313 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
314 usage( tool, progname );
316 dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
319 case 'n': /* which config file db to index */
320 if ( lutil_atoi( &dbnum, optarg ) ) {
321 usage( tool, progname );
326 if ( parse_slapacl() ) {
327 usage( tool, progname );
332 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
333 usage( tool, progname );
335 dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
338 case 'q': /* turn on quick */
339 mode |= SLAP_TOOL_QUICK;
346 case 's': /* dump subtree */
347 subtree = strdup( optarg );
350 case 't': /* turn on truncate */
352 mode |= SLAP_TRUNCATE_MODE;
356 ber_str2bv( optarg, 0, 0, &authcID );
359 case 'u': /* dry run */
363 case 'v': /* turn on verbose */
367 case 'w': /* write context csn at the end */
372 ber_str2bv( optarg, 0, 0, &authzID );
376 usage( tool, progname );
385 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
386 usage( tool, progname );
392 if ( argc == optind ) {
393 usage( tool, progname );
398 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
399 usage( tool, progname );
404 if ( argc != optind ) {
405 usage( tool, progname );
410 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
411 usage( tool, progname );
413 if ( BER_BVISNULL( &base ) ) {
414 usage( tool, progname );
416 ber_dupbv( &baseDN, &base );
425 if ( ldiffile == NULL ) {
426 dummy.fp = tool == SLAPCAT ? stdout : stdin;
429 } else if ((ldiffp = ldif_open( ldiffile, tool == SLAPCAT ? "w" : "r" ))
433 exit( EXIT_FAILURE );
437 * initialize stuff and figure out which backend we're dealing with
440 rc = slap_init( mode, progname );
442 fprintf( stderr, "%s: slap_init failed!\n", progname );
443 exit( EXIT_FAILURE );
446 rc = read_config( conffile, confdir );
449 fprintf( stderr, "%s: bad configuration %s!\n",
450 progname, confdir ? "directory" : "file" );
451 exit( EXIT_FAILURE );
461 fprintf( stderr, "No databases found "
462 "in config file\n" );
463 exit( EXIT_FAILURE );
472 rc = glue_sub_attach();
476 "%s: subordinate configuration error\n", progname );
477 exit( EXIT_FAILURE );
481 rc = slap_schema_check();
484 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
485 exit( EXIT_FAILURE );
500 filter = str2filter( filterstr );
502 if( filter == NULL ) {
503 fprintf( stderr, "Invalid filter '%s'\n", filterstr );
504 exit( EXIT_FAILURE );
510 ber_str2bv( subtree, 0, 0, &val );
511 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
512 if( rc != LDAP_SUCCESS ) {
513 fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
514 exit( EXIT_FAILURE );
517 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
524 if( base.bv_val != NULL ) {
527 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
528 if( rc != LDAP_SUCCESS ) {
529 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
530 progname, base.bv_val );
531 exit( EXIT_FAILURE );
534 be = select_backend( &nbase, 0, 0 );
535 ber_memfree( nbase.bv_val );
546 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
547 progname, base.bv_val );
548 exit( EXIT_FAILURE );
550 /* If the named base is a glue master, operate on the
553 if ( SLAP_GLUE_INSTANCE( be ) ) {
557 } else if ( dbnum == -1 ) {
558 /* no suffix and no dbnum specified, just default to
559 * the first available database
561 if ( nbackends <= 0 ) {
562 fprintf( stderr, "No available databases\n" );
563 exit( EXIT_FAILURE );
565 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
568 /* db #0 is cn=config, don't select it as a default */
569 if ( dbnum < 1 ) continue;
571 if ( SLAP_MONITOR(be))
574 /* If just doing the first by default and it is a
575 * glue subordinate, find the master.
577 if ( SLAP_GLUE_SUBORDINATE(be) ) {
585 fprintf( stderr, "Available database(s) "
586 "do not allow %s\n", progname );
587 exit( EXIT_FAILURE );
590 if ( nosubordinates == 0 && dbnum > 1 ) {
591 Debug( LDAP_DEBUG_ANY,
592 "The first database does not allow %s;"
593 " using the first available one (%d)\n",
594 progname, dbnum, 0 );
597 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
599 "Database number selected via -n is out of range\n"
600 "Must be in the range 0 to %d"
601 " (number of configured databases)\n",
603 exit( EXIT_FAILURE );
606 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
607 if ( dbnum == 0 ) break;
618 if ( conffile != NULL ) {
622 if ( ldiffile != NULL ) {
626 /* slapdn doesn't specify a backend to startup */
627 if ( !dryrun && tool != SLAPDN && slap_startup( be ) ) {
632 fprintf( stderr, "slap_startup failed "
633 "(test would succeed using "
634 "the -u switch)\n" );
638 fprintf( stderr, "slap_startup failed\n" );
642 exit( EXIT_FAILURE );
646 void slap_tool_destroy( void )
649 if ( need_shutdown ) {
655 if ( slapMode == SLAP_SERVER_MODE ) {
656 /* always false. just pulls in necessary symbol references. */
657 lutil_uuidstr(NULL, 0);
663 ldap_pvt_tls_destroy();
668 mal_dumpleaktrace( leakfile );
671 if ( !BER_BVISNULL( &authcDN ) ) {
672 ch_free( authcDN.bv_val );