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] [-d debuglevel] [-f configfile] [-F configdir]",
57 options = "\n\t[-U authcID | -D authcDN] [-X authzID | -o authzDN=<DN>]"
58 "\n\t-b DN -o <var>[=<val>] [-u] [attr[/access][:value]] [...]\n";
62 options = " [-c]\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 = " [-c]\n\t[-n databasenumber | -b suffix]"
72 " [-l ldiffile] [-a filter]\n";
76 options = "\n\t[-N | -P] DN [...]\n";
80 options = " [-c]\n\t[-n databasenumber | -b suffix] [-q]\n";
88 if ( options != NULL ) {
89 fputs( options, stderr );
100 p = strchr( optarg, '=' );
108 if ( strncasecmp( optarg, "sockurl", len ) == 0 ) {
109 if ( !BER_BVISNULL( &listener_url ) ) {
110 ber_memfree( listener_url.bv_val );
112 ber_str2bv( p, 0, 1, &listener_url );
114 } else if ( strncasecmp( optarg, "domain", len ) == 0 ) {
115 if ( !BER_BVISNULL( &peer_domain ) ) {
116 ber_memfree( peer_domain.bv_val );
118 ber_str2bv( p, 0, 1, &peer_domain );
120 } else if ( strncasecmp( optarg, "peername", len ) == 0 ) {
121 if ( !BER_BVISNULL( &peer_name ) ) {
122 ber_memfree( peer_name.bv_val );
124 ber_str2bv( p, 0, 1, &peer_name );
126 } else if ( strncasecmp( optarg, "sockname", len ) == 0 ) {
127 if ( !BER_BVISNULL( &sock_name ) ) {
128 ber_memfree( sock_name.bv_val );
130 ber_str2bv( p, 0, 1, &sock_name );
132 } else if ( strncasecmp( optarg, "ssf", len ) == 0 ) {
135 } else if ( strncasecmp( optarg, "transport_ssf", len ) == 0 ) {
136 transport_ssf = atoi( p );
138 } else if ( strncasecmp( optarg, "tls_ssf", len ) == 0 ) {
141 } else if ( strncasecmp( optarg, "sasl_ssf", len ) == 0 ) {
142 sasl_ssf = atoi( p );
144 } else if ( strncasecmp( optarg, "authzDN", len ) == 0 ) {
145 ber_str2bv( p, 0, 1, &authzDN );
155 * slap_tool_init - initialize slap utility, handle program options.
159 * argc, argv command line arguments
164 const char* progname,
166 int argc, char **argv )
169 char *conffile = SLAPD_DEFAULT_CONFIGFILE;
170 char *confdir = SLAPD_DEFAULT_CONFIGDIR;
171 struct berval base = BER_BVNULL;
172 char *filterstr = NULL;
173 char *subtree = NULL;
174 char *ldiffile = NULL;
176 int mode = SLAP_TOOL_MODE;
177 int truncatemode = 0;
180 leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
181 sprintf( leakfilename, "%s.leak", progname );
182 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
185 free( leakfilename );
190 options = "b:cd:f:F:l:n:qtuvw";
194 options = "a:b:cd:f:F:l:n:s:v";
195 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
199 options = "d:f:F:NPv";
200 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
204 options = "d:f:F:uv";
205 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
209 options = "d:f:F:M:R:U:vX:";
210 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
214 options = "b:cd:f:F:n:qv";
215 mode |= SLAP_TOOL_READMAIN;
219 options = "b:D:d:f:F:o:uU:vX:";
220 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
224 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
225 exit( EXIT_FAILURE );
229 while ( (i = getopt( argc, argv, options )) != EOF ) {
232 filterstr = strdup( optarg );
236 ber_str2bv( optarg, 0, 1, &base );
239 case 'c': /* enable continue mode */
243 case 'd': /* turn on debugging */
244 ldap_debug += atoi( optarg );
248 ber_str2bv( optarg, 0, 1, &authcDN );
251 case 'f': /* specify a conf file */
252 conffile = strdup( optarg );
255 case 'F': /* specify a conf dir */
256 confdir = strdup( optarg );
259 case 'l': /* LDIF file */
260 ldiffile = strdup( optarg );
264 ber_str2bv( optarg, 0, 0, &mech );
268 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
269 usage( tool, progname );
271 dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
274 case 'n': /* which config file db to index */
275 dbnum = atoi( optarg );
279 if ( parse_slapacl() ) {
280 usage( tool, progname );
285 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
286 usage( tool, progname );
288 dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
291 case 'q': /* turn on quick */
292 mode |= SLAP_TOOL_QUICK;
299 case 's': /* dump subtree */
300 subtree = strdup( optarg );
303 case 't': /* turn on truncate */
305 mode |= SLAP_TRUNCATE_MODE;
309 ber_str2bv( optarg, 0, 0, &authcID );
312 case 'u': /* dry run */
316 case 'v': /* turn on verbose */
320 case 'w': /* write context csn at the end */
325 ber_str2bv( optarg, 0, 0, &authzID );
329 usage( tool, progname );
338 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
339 usage( tool, progname );
345 if ( argc == optind ) {
346 usage( tool, progname );
351 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
352 usage( tool, progname );
357 if ( argc != optind ) {
358 usage( tool, progname );
363 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
364 usage( tool, progname );
366 if ( BER_BVISNULL( &base ) ) {
367 usage( tool, progname );
369 ber_dupbv( &baseDN, &base );
378 if ( ldiffile == NULL ) {
379 ldiffp = tool == SLAPCAT ? stdout : stdin;
381 } else if ((ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
385 exit( EXIT_FAILURE );
389 * initialize stuff and figure out which backend we're dealing with
393 if ( module_init() != 0 ) {
394 fprintf( stderr, "%s: module_init failed!\n", progname );
395 exit( EXIT_FAILURE );
399 rc = slap_schema_init();
402 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
403 exit( EXIT_FAILURE );
406 rc = slap_init( mode, progname );
409 fprintf( stderr, "%s: slap_init failed!\n", progname );
410 exit( EXIT_FAILURE );
413 if ( frontend_init() ) {
414 fprintf( stderr, "%s: frontend_init failed!\n", progname );
415 exit( EXIT_FAILURE );
418 if ( overlay_init() ) {
419 fprintf( stderr, "%s: overlay_init failed!\n", progname );
420 exit( EXIT_FAILURE );
423 rc = read_config( conffile, confdir );
426 fprintf( stderr, "%s: bad configuration file!\n", progname );
427 exit( EXIT_FAILURE );
437 fprintf( stderr, "No databases found "
438 "in config file\n" );
439 exit( EXIT_FAILURE );
447 rc = slap_schema_check();
450 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
451 exit( EXIT_FAILURE );
466 filter = str2filter( filterstr );
468 if( filter == NULL ) {
469 fprintf( stderr, "Invalid filter '%s'\n", filterstr );
470 exit( EXIT_FAILURE );
476 ber_str2bv( subtree, 0, 0, &val );
477 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
478 if( rc != LDAP_SUCCESS ) {
479 fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
480 exit( EXIT_FAILURE );
483 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
490 if( base.bv_val != NULL ) {
493 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
494 if( rc != LDAP_SUCCESS ) {
495 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
496 progname, base.bv_val );
497 exit( EXIT_FAILURE );
500 be = select_backend( &nbase, 0, 0 );
501 ber_memfree( nbase.bv_val );
512 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
513 progname, base.bv_val );
514 exit( EXIT_FAILURE );
516 /* If the named base is a glue master, operate on the
519 if ( SLAP_GLUE_INSTANCE( be ) ) {
523 } else if ( dbnum == -1 ) {
524 if ( nbackends <= 0 ) {
525 fprintf( stderr, "No available databases\n" );
526 exit( EXIT_FAILURE );
528 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
530 if ( dbnum < 1 ) continue;
532 if ( SLAP_MONITOR(be))
535 /* If just doing the first by default and it is a
536 * glue subordinate, find the master.
538 if ( SLAP_GLUE_SUBORDINATE(be) ) {
546 fprintf( stderr, "Available database(s) "
547 "do not allow %s\n", progname );
548 exit( EXIT_FAILURE );
551 if ( nosubordinates == 0 && dbnum > 0 ) {
552 Debug( LDAP_DEBUG_ANY,
553 "The first database does not allow %s;"
554 " using the first available one (%d)\n",
555 progname, dbnum + 1, 0 );
558 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
560 "Database number selected via -n is out of range\n"
561 "Must be in the range 1 to %d"
562 " (number of databases in the config file)\n",
564 exit( EXIT_FAILURE );
567 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
568 if ( dbnum == 0 ) break;
579 if ( !dryrun && be && slap_startup( be ) ) {
582 fprintf( stderr, "slap_startup failed "
583 "(test would succeed using "
584 "the -u switch)\n" );
588 fprintf( stderr, "slap_startup failed\n" );
592 exit( EXIT_FAILURE );
596 void slap_tool_destroy( void )
603 if ( slapMode == SLAP_SERVER_MODE ) {
604 /* always false. just pulls in necessary symbol references. */
605 lutil_uuidstr(NULL, 0);
611 ldap_pvt_tls_destroy();
616 mal_dumpleaktrace( leakfile );
619 if ( !BER_BVISNULL( &authcDN ) ) {
620 ch_free( authcDN.bv_val );