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 ) {
138 } else if ( strncasecmp( optarg, "transport_ssf", len ) == 0 ) {
139 transport_ssf = atoi( p );
141 } else if ( strncasecmp( optarg, "tls_ssf", len ) == 0 ) {
144 } else if ( strncasecmp( optarg, "sasl_ssf", len ) == 0 ) {
145 sasl_ssf = atoi( p );
147 } else if ( strncasecmp( optarg, "authzDN", len ) == 0 ) {
148 ber_str2bv( p, 0, 1, &authzDN );
158 * slap_tool_init - initialize slap utility, handle program options.
162 * argc, argv command line arguments
167 const char* progname,
169 int argc, char **argv )
172 char *conffile = NULL;
173 char *confdir = NULL;
174 struct berval base = BER_BVNULL;
175 char *filterstr = NULL;
176 char *subtree = NULL;
177 char *ldiffile = NULL;
179 int mode = SLAP_TOOL_MODE;
180 int truncatemode = 0;
184 leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
185 sprintf( leakfilename, "%s.leak", progname );
186 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
189 free( leakfilename );
194 options = "b:cd:f:F:gl:n:qtuvw";
198 options = "a:b:cd:f:F:gl:n:s:v";
199 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
203 options = "d:f:F:NPv";
204 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
208 options = "d:f:F:uv";
209 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
213 options = "d:f:F:M:R:U:vX:";
214 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
218 options = "b:cd:f:F:gn:qv";
219 mode |= SLAP_TOOL_READMAIN;
223 options = "b:D:d:f:F:o:uU:vX:";
224 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
228 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
229 exit( EXIT_FAILURE );
233 while ( (i = getopt( argc, argv, options )) != EOF ) {
236 filterstr = strdup( optarg );
240 ber_str2bv( optarg, 0, 1, &base );
243 case 'c': /* enable continue mode */
247 case 'd': /* turn on debugging */
249 if ( optarg != NULL && optarg[ 0 ] != '-' && !isdigit( optarg[ 0 ] ) )
253 if ( str2loglevel( optarg, &level ) ) {
255 "unrecognized log level "
256 "\"%s\"\n", optarg );
257 exit( EXIT_FAILURE );
266 level = strtol( optarg, &next, 0 );
267 if ( next == NULL || next[ 0 ] != '\0' ) {
269 "unrecognized log level "
270 "\"%s\"\n", optarg );
271 exit( EXIT_FAILURE );
276 if ( atoi( optarg ) != 0 )
277 fputs( "must compile with LDAP_DEBUG for debugging\n",
283 ber_str2bv( optarg, 0, 1, &authcDN );
286 case 'f': /* specify a conf file */
287 conffile = strdup( optarg );
290 case 'F': /* specify a conf dir */
291 confdir = strdup( optarg );
294 case 'g': /* disable subordinate glue */
298 case 'l': /* LDIF file */
299 ldiffile = strdup( optarg );
303 ber_str2bv( optarg, 0, 0, &mech );
307 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
308 usage( tool, progname );
310 dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
313 case 'n': /* which config file db to index */
314 dbnum = atoi( optarg );
318 if ( parse_slapacl() ) {
319 usage( tool, progname );
324 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
325 usage( tool, progname );
327 dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
330 case 'q': /* turn on quick */
331 mode |= SLAP_TOOL_QUICK;
338 case 's': /* dump subtree */
339 subtree = strdup( optarg );
342 case 't': /* turn on truncate */
344 mode |= SLAP_TRUNCATE_MODE;
348 ber_str2bv( optarg, 0, 0, &authcID );
351 case 'u': /* dry run */
355 case 'v': /* turn on verbose */
359 case 'w': /* write context csn at the end */
364 ber_str2bv( optarg, 0, 0, &authzID );
368 usage( tool, progname );
377 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
378 usage( tool, progname );
384 if ( argc == optind ) {
385 usage( tool, progname );
390 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
391 usage( tool, progname );
396 if ( argc != optind ) {
397 usage( tool, progname );
402 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
403 usage( tool, progname );
405 if ( BER_BVISNULL( &base ) ) {
406 usage( tool, progname );
408 ber_dupbv( &baseDN, &base );
417 if ( ldiffile == NULL ) {
418 dummy.fp = tool == SLAPCAT ? stdout : stdin;
421 } else if ((ldiffp = ldif_open( ldiffile, tool == SLAPCAT ? "w" : "r" ))
425 exit( EXIT_FAILURE );
429 * initialize stuff and figure out which backend we're dealing with
432 rc = slap_init( mode, progname );
434 fprintf( stderr, "%s: slap_init failed!\n", progname );
435 exit( EXIT_FAILURE );
438 rc = read_config( conffile, confdir );
441 fprintf( stderr, "%s: bad configuration %s!\n",
442 progname, confdir ? "directory" : "file" );
443 exit( EXIT_FAILURE );
453 fprintf( stderr, "No databases found "
454 "in config file\n" );
455 exit( EXIT_FAILURE );
464 rc = glue_sub_attach();
468 "%s: subordinate configuration error\n", progname );
469 exit( EXIT_FAILURE );
473 rc = slap_schema_check();
476 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
477 exit( EXIT_FAILURE );
492 filter = str2filter( filterstr );
494 if( filter == NULL ) {
495 fprintf( stderr, "Invalid filter '%s'\n", filterstr );
496 exit( EXIT_FAILURE );
502 ber_str2bv( subtree, 0, 0, &val );
503 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
504 if( rc != LDAP_SUCCESS ) {
505 fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
506 exit( EXIT_FAILURE );
509 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
516 if( base.bv_val != NULL ) {
519 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
520 if( rc != LDAP_SUCCESS ) {
521 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
522 progname, base.bv_val );
523 exit( EXIT_FAILURE );
526 be = select_backend( &nbase, 0, 0 );
527 ber_memfree( nbase.bv_val );
538 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
539 progname, base.bv_val );
540 exit( EXIT_FAILURE );
542 /* If the named base is a glue master, operate on the
545 if ( SLAP_GLUE_INSTANCE( be ) ) {
549 } else if ( dbnum == -1 ) {
550 /* no suffix and no dbnum specified, just default to
551 * the first available database
553 if ( nbackends <= 0 ) {
554 fprintf( stderr, "No available databases\n" );
555 exit( EXIT_FAILURE );
557 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
560 /* db #0 is cn=config, don't select it as a default */
561 if ( dbnum < 1 ) continue;
563 if ( SLAP_MONITOR(be))
566 /* If just doing the first by default and it is a
567 * glue subordinate, find the master.
569 if ( SLAP_GLUE_SUBORDINATE(be) ) {
577 fprintf( stderr, "Available database(s) "
578 "do not allow %s\n", progname );
579 exit( EXIT_FAILURE );
582 if ( nosubordinates == 0 && dbnum > 1 ) {
583 Debug( LDAP_DEBUG_ANY,
584 "The first database does not allow %s;"
585 " using the first available one (%d)\n",
586 progname, dbnum, 0 );
589 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
591 "Database number selected via -n is out of range\n"
592 "Must be in the range 0 to %d"
593 " (number of configured databases)\n",
595 exit( EXIT_FAILURE );
598 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
599 if ( dbnum == 0 ) break;
610 if ( conffile != NULL ) {
614 if ( ldiffile != NULL ) {
618 /* slapdn doesn't specify a backend to startup */
619 if ( !dryrun && tool != SLAPDN && slap_startup( be ) ) {
622 fprintf( stderr, "slap_startup failed "
623 "(test would succeed using "
624 "the -u switch)\n" );
628 fprintf( stderr, "slap_startup failed\n" );
632 exit( EXIT_FAILURE );
636 void slap_tool_destroy( void )
643 if ( slapMode == SLAP_SERVER_MODE ) {
644 /* always false. just pulls in necessary symbol references. */
645 lutil_uuidstr(NULL, 0);
651 ldap_pvt_tls_destroy();
656 mal_dumpleaktrace( leakfile );
659 if ( !BER_BVISNULL( &authcDN ) ) {
660 ch_free( authcDN.bv_val );