1 /* slapcommon.c - common routine for the slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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
179 const char* progname,
181 int argc, char **argv )
184 char *conffile = NULL;
185 char *confdir = NULL;
186 struct berval base = BER_BVNULL;
187 char *filterstr = NULL;
188 char *subtree = NULL;
189 char *ldiffile = NULL;
191 int mode = SLAP_TOOL_MODE;
192 int truncatemode = 0;
196 /* tools default to "none", so that at least LDAP_DEBUG_ANY
197 * messages show up; use -d 0 to reset */
198 ldap_debug = LDAP_DEBUG_NONE;
202 leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
203 sprintf( leakfilename, "%s.leak", progname );
204 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
207 free( leakfilename );
212 options = "b:cd:f:F:gl:n:qtuvw";
216 options = "a:b:cd:f:F:gl:n:s:v";
217 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
221 options = "d:f:F:NPv";
222 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
226 options = "d:f:F:uv";
227 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
231 options = "d:f:F:M:R:U:vX:";
232 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
236 options = "b:cd:f:F:gn:qv";
237 mode |= SLAP_TOOL_READMAIN;
241 options = "b:D:d:f:F:o:uU:vX:";
242 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
246 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
247 exit( EXIT_FAILURE );
251 while ( (i = getopt( argc, argv, options )) != EOF ) {
254 filterstr = strdup( optarg );
258 ber_str2bv( optarg, 0, 1, &base );
261 case 'c': /* enable continue mode */
265 case 'd': { /* turn on debugging */
269 if ( optarg != NULL && optarg[ 0 ] != '-' && !isdigit( optarg[ 0 ] ) )
274 levels = ldap_str2charray( optarg, "," );
276 for ( i = 0; levels[ i ] != NULL; i++ ) {
279 if ( str2loglevel( levels[ i ], &level ) ) {
281 "unrecognized log level "
282 "\"%s\"\n", levels[ i ] );
284 /* but keep parsing... */
291 /* allow to reset log level */
298 if ( lutil_atoix( &level, optarg, 0 ) != 0 ) {
300 "unrecognized log level "
301 "\"%s\"\n", optarg );
302 exit( EXIT_FAILURE );
309 /* allow to reset log level */
314 if ( lutil_atoi( &level, optarg ) != 0 || level != 0 )
315 fputs( "must compile with LDAP_DEBUG for debugging\n",
321 ber_str2bv( optarg, 0, 1, &authcDN );
324 case 'f': /* specify a conf file */
325 conffile = strdup( optarg );
328 case 'F': /* specify a conf dir */
329 confdir = strdup( optarg );
332 case 'g': /* disable subordinate glue */
336 case 'l': /* LDIF file */
337 ldiffile = strdup( optarg );
341 ber_str2bv( optarg, 0, 0, &mech );
345 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
346 usage( tool, progname );
348 dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
351 case 'n': /* which config file db to index */
352 if ( lutil_atoi( &dbnum, optarg ) ) {
353 usage( tool, progname );
358 if ( parse_slapacl() ) {
359 usage( tool, progname );
364 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
365 usage( tool, progname );
367 dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
370 case 'q': /* turn on quick */
371 mode |= SLAP_TOOL_QUICK;
378 case 's': /* dump subtree */
379 subtree = strdup( optarg );
382 case 't': /* turn on truncate */
384 mode |= SLAP_TRUNCATE_MODE;
388 ber_str2bv( optarg, 0, 0, &authcID );
391 case 'u': /* dry run */
395 case 'v': /* turn on verbose */
399 case 'w': /* write context csn at the end */
404 ber_str2bv( optarg, 0, 0, &authzID );
408 usage( tool, progname );
417 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
418 usage( tool, progname );
424 if ( argc == optind ) {
425 usage( tool, progname );
430 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
431 usage( tool, progname );
436 if ( argc != optind ) {
437 usage( tool, progname );
442 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
443 usage( tool, progname );
445 if ( BER_BVISNULL( &base ) ) {
446 usage( tool, progname );
448 ber_dupbv( &baseDN, &base );
457 if ( ldiffile == NULL ) {
458 dummy.fp = tool == SLAPCAT ? stdout : stdin;
461 } else if ((ldiffp = ldif_open( ldiffile, tool == SLAPCAT ? "w" : "r" ))
465 exit( EXIT_FAILURE );
469 * initialize stuff and figure out which backend we're dealing with
472 rc = slap_init( mode, progname );
474 fprintf( stderr, "%s: slap_init failed!\n", progname );
475 exit( EXIT_FAILURE );
478 rc = read_config( conffile, confdir );
481 fprintf( stderr, "%s: bad configuration %s!\n",
482 progname, confdir ? "directory" : "file" );
483 exit( EXIT_FAILURE );
493 fprintf( stderr, "No databases found "
494 "in config file\n" );
495 exit( EXIT_FAILURE );
504 rc = glue_sub_attach();
508 "%s: subordinate configuration error\n", progname );
509 exit( EXIT_FAILURE );
513 rc = slap_schema_check();
516 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
517 exit( EXIT_FAILURE );
532 filter = str2filter( filterstr );
534 if( filter == NULL ) {
535 fprintf( stderr, "Invalid filter '%s'\n", filterstr );
536 exit( EXIT_FAILURE );
542 ber_str2bv( subtree, 0, 0, &val );
543 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
544 if( rc != LDAP_SUCCESS ) {
545 fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
546 exit( EXIT_FAILURE );
549 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
556 if( base.bv_val != NULL ) {
559 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
560 if( rc != LDAP_SUCCESS ) {
561 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
562 progname, base.bv_val );
563 exit( EXIT_FAILURE );
566 be = select_backend( &nbase, 0, 0 );
567 ber_memfree( nbase.bv_val );
578 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
579 progname, base.bv_val );
580 exit( EXIT_FAILURE );
582 /* If the named base is a glue master, operate on the
585 if ( SLAP_GLUE_INSTANCE( be ) ) {
589 } else if ( dbnum == -1 ) {
590 /* no suffix and no dbnum specified, just default to
591 * the first available database
593 if ( nbackends <= 0 ) {
594 fprintf( stderr, "No available databases\n" );
595 exit( EXIT_FAILURE );
597 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
600 /* db #0 is cn=config, don't select it as a default */
601 if ( dbnum < 1 ) continue;
603 if ( SLAP_MONITOR(be))
606 /* If just doing the first by default and it is a
607 * glue subordinate, find the master.
609 if ( SLAP_GLUE_SUBORDINATE(be) ) {
617 fprintf( stderr, "Available database(s) "
618 "do not allow %s\n", progname );
619 exit( EXIT_FAILURE );
622 if ( nosubordinates == 0 && dbnum > 1 ) {
623 Debug( LDAP_DEBUG_ANY,
624 "The first database does not allow %s;"
625 " using the first available one (%d)\n",
626 progname, dbnum, 0 );
629 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
631 "Database number selected via -n is out of range\n"
632 "Must be in the range 0 to %d"
633 " (number of configured databases)\n",
635 exit( EXIT_FAILURE );
638 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
639 if ( dbnum == 0 ) break;
650 if ( conffile != NULL ) {
654 if ( ldiffile != NULL ) {
658 /* slapdn doesn't specify a backend to startup */
659 if ( !dryrun && tool != SLAPDN && slap_startup( be ) ) {
662 fprintf( stderr, "slap_startup failed "
663 "(test would succeed using "
664 "the -u switch)\n" );
668 fprintf( stderr, "slap_startup failed\n" );
672 exit( EXIT_FAILURE );
676 void slap_tool_destroy( void )
683 if ( slapMode == SLAP_SERVER_MODE ) {
684 /* always false. just pulls in necessary symbol references. */
685 lutil_uuidstr(NULL, 0);
691 ldap_pvt_tls_destroy();
696 mal_dumpleaktrace( leakfile );
699 if ( !BER_BVISNULL( &authcDN ) ) {
700 ch_free( authcDN.bv_val );