1 /* slapcommon.c - common routine for the slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2010 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;
50 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
52 static char **syslog_unknowns;
54 static int syslogUser = SLAP_DEFAULT_SYSLOG_USER;
55 #endif /* LOG_LOCAL4 */
56 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
59 usage( int tool, const char *progname )
63 "usage: %s [-v] [-d debuglevel] [-f configfile] [-F configdir] [-o <name>[=<value>]]",
68 options = "\n\t[-U authcID | -D authcDN] [-X authzID | -o authzDN=<DN>]"
69 "\n\t-b DN [-u] [attr[/access][:value]] [...]\n";
73 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]\n"
74 "\t[-l ldiffile] [-j linenumber] [-q] [-u] [-s] [-w]\n";
78 options = "\n\t[-U authcID] [-X authzID] [-R realm] [-M mech] ID [...]\n";
82 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]"
83 " [-l ldiffile] [-a filter] [-s subtree]\n";
87 options = "\n\t[-N | -P] DN [...]\n";
91 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix] [attr ...] [-q] [-t]\n";
95 options = " [-n databasenumber] [-u]\n";
99 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]"
100 " [-l errorfile] [-a filter] [-s subtree]\n";
104 if ( options != NULL ) {
105 fputs( options, stderr );
107 exit( EXIT_FAILURE );
111 parse_slapopt( void )
116 p = strchr( optarg, '=' );
122 if ( strncasecmp( optarg, "sockurl", len ) == 0 ) {
123 if ( !BER_BVISNULL( &listener_url ) ) {
124 ber_memfree( listener_url.bv_val );
126 ber_str2bv( p, 0, 1, &listener_url );
128 } else if ( strncasecmp( optarg, "domain", len ) == 0 ) {
129 if ( !BER_BVISNULL( &peer_domain ) ) {
130 ber_memfree( peer_domain.bv_val );
132 ber_str2bv( p, 0, 1, &peer_domain );
134 } else if ( strncasecmp( optarg, "peername", len ) == 0 ) {
135 if ( !BER_BVISNULL( &peer_name ) ) {
136 ber_memfree( peer_name.bv_val );
138 ber_str2bv( p, 0, 1, &peer_name );
140 } else if ( strncasecmp( optarg, "sockname", len ) == 0 ) {
141 if ( !BER_BVISNULL( &sock_name ) ) {
142 ber_memfree( sock_name.bv_val );
144 ber_str2bv( p, 0, 1, &sock_name );
146 } else if ( strncasecmp( optarg, "ssf", len ) == 0 ) {
147 if ( lutil_atou( &ssf, p ) ) {
148 Debug( LDAP_DEBUG_ANY, "unable to parse ssf=\"%s\".\n", p, 0, 0 );
152 } else if ( strncasecmp( optarg, "transport_ssf", len ) == 0 ) {
153 if ( lutil_atou( &transport_ssf, p ) ) {
154 Debug( LDAP_DEBUG_ANY, "unable to parse transport_ssf=\"%s\".\n", p, 0, 0 );
158 } else if ( strncasecmp( optarg, "tls_ssf", len ) == 0 ) {
159 if ( lutil_atou( &tls_ssf, p ) ) {
160 Debug( LDAP_DEBUG_ANY, "unable to parse tls_ssf=\"%s\".\n", p, 0, 0 );
164 } else if ( strncasecmp( optarg, "sasl_ssf", len ) == 0 ) {
165 if ( lutil_atou( &sasl_ssf, p ) ) {
166 Debug( LDAP_DEBUG_ANY, "unable to parse sasl_ssf=\"%s\".\n", p, 0, 0 );
170 } else if ( strncasecmp( optarg, "authzDN", len ) == 0 ) {
171 ber_str2bv( p, 0, 1, &authzDN );
173 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
174 } else if ( strncasecmp( optarg, "syslog", len ) == 0 ) {
175 if ( parse_debug_level( p, &ldap_syslog, &syslog_unknowns ) ) {
180 } else if ( strncasecmp( optarg, "syslog-level", len ) == 0 ) {
181 if ( parse_syslog_level( p, &ldap_syslog_level ) ) {
187 } else if ( strncasecmp( optarg, "syslog-user", len ) == 0 ) {
188 if ( parse_syslog_user( p, &syslogUser ) ) {
192 #endif /* LOG_LOCAL4 */
193 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
203 * slap_tool_init - initialize slap utility, handle program options.
207 * argc, argv command line arguments
210 static int need_shutdown;
214 const char* progname,
216 int argc, char **argv )
219 char *conffile = NULL;
220 char *confdir = NULL;
221 struct berval base = BER_BVNULL;
222 char *filterstr = NULL;
223 char *subtree = NULL;
224 char *ldiffile = NULL;
225 char **debug_unknowns = NULL;
227 int mode = SLAP_TOOL_MODE;
228 int truncatemode = 0;
233 /* tools default to "none", so that at least LDAP_DEBUG_ANY
234 * messages show up; use -d 0 to reset */
235 slap_debug = LDAP_DEBUG_NONE;
236 ldif_debug = slap_debug;
241 leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
242 sprintf( leakfilename, "%s.leak", progname );
243 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
246 free( leakfilename );
252 options = "b:cd:f:F:gj:l:n:o:qsS:uvw";
256 options = "a:b:cd:f:F:gl:n:o:s:v";
257 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
261 options = "d:f:F:No:Pv";
262 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
266 options = "a:b:cd:f:F:gl:n:o:s:v";
267 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
271 options = "d:f:F:n:o:Quv";
272 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
276 options = "d:f:F:M:o:R:U:vX:";
277 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
281 options = "b:cd:f:F:gn:o:qtv";
282 mode |= SLAP_TOOL_READMAIN;
286 options = "b:D:d:f:F:o:uU:vX:";
287 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
291 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
292 exit( EXIT_FAILURE );
296 while ( (i = getopt( argc, argv, options )) != EOF ) {
299 filterstr = ch_strdup( optarg );
303 ber_str2bv( optarg, 0, 1, &base );
306 case 'c': /* enable continue mode */
310 case 'd': { /* turn on debugging */
313 if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
314 usage( tool, progname );
318 /* allow to reset log level */
326 fputs( "must compile with LDAP_DEBUG for debugging\n",
332 ber_str2bv( optarg, 0, 1, &authcDN );
335 case 'f': /* specify a conf file */
336 conffile = ch_strdup( optarg );
339 case 'F': /* specify a conf dir */
340 confdir = ch_strdup( optarg );
343 case 'g': /* disable subordinate glue */
347 case 'j': /* jump to linenumber */
348 if ( lutil_atoi( &jumpline, optarg ) ) {
349 usage( tool, progname );
353 case 'l': /* LDIF file */
354 ldiffile = ch_strdup( optarg );
358 ber_str2bv( optarg, 0, 0, &mech );
362 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
363 usage( tool, progname );
365 dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
368 case 'n': /* which config file db to index */
369 if ( lutil_atoi( &dbnum, optarg ) || dbnum < 0 ) {
370 usage( tool, progname );
375 if ( parse_slapopt() ) {
376 usage( tool, progname );
381 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
382 usage( tool, progname );
384 dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
392 case 'q': /* turn on quick */
393 mode |= SLAP_TOOL_QUICK;
401 if ( lutil_atou( &csnsid, optarg )
402 || csnsid > SLAP_SYNC_SID_MAX )
404 usage( tool, progname );
408 case 's': /* dump subtree */
409 if ( tool == SLAPADD )
410 mode |= SLAP_TOOL_NO_SCHEMA_CHECK;
411 else if ( tool == SLAPCAT || tool == SLAPSCHEMA )
412 subtree = ch_strdup( optarg );
415 case 't': /* turn on truncate */
417 mode |= SLAP_TRUNCATE_MODE;
421 ber_str2bv( optarg, 0, 0, &authcID );
424 case 'u': /* dry run */
428 case 'v': /* turn on verbose */
432 case 'w': /* write context csn at the end */
437 ber_str2bv( optarg, 0, 0, &authzID );
441 usage( tool, progname );
446 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
447 if ( start_syslog ) {
450 logName = ch_strdup( progname );
453 logName = (char *)progname;
457 openlog( logName, OPENLOG_OPTIONS, syslogUser );
458 #elif defined LOG_DEBUG
459 openlog( logName, OPENLOG_OPTIONS );
466 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
483 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
484 usage( tool, progname );
490 if ( dbnum >= 0 && base.bv_val != NULL ) {
491 usage( tool, progname );
497 if ( argc == optind ) {
498 usage( tool, progname );
503 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
504 usage( tool, progname );
509 if ( argc != optind ) {
510 usage( tool, progname );
515 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
516 usage( tool, progname );
518 if ( BER_BVISNULL( &base ) ) {
519 usage( tool, progname );
521 ber_dupbv( &baseDN, &base );
528 if ( ldiffile == NULL ) {
529 dummy.fp = writer ? stdout : stdin;
532 } else if ((ldiffp = ldif_open( ldiffile, writer ? "w" : "r" ))
536 exit( EXIT_FAILURE );
540 * initialize stuff and figure out which backend we're dealing with
543 rc = slap_init( mode, progname );
545 fprintf( stderr, "%s: slap_init failed!\n", progname );
546 exit( EXIT_FAILURE );
549 rc = read_config( conffile, confdir );
552 fprintf( stderr, "%s: bad configuration %s!\n",
553 progname, confdir ? "directory" : "file" );
554 exit( EXIT_FAILURE );
557 if ( debug_unknowns ) {
558 rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
559 ldap_charray_free( debug_unknowns );
560 debug_unknowns = NULL;
562 exit( EXIT_FAILURE );
565 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
566 if ( syslog_unknowns ) {
567 rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
568 ldap_charray_free( syslog_unknowns );
569 syslog_unknowns = NULL;
571 exit( EXIT_FAILURE );
583 fprintf( stderr, "No databases found "
584 "in config file\n" );
585 exit( EXIT_FAILURE );
594 rc = glue_sub_attach( 0 );
598 "%s: subordinate configuration error\n", progname );
599 exit( EXIT_FAILURE );
603 rc = slap_schema_check();
606 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
607 exit( EXIT_FAILURE );
625 filter = str2filter( filterstr );
627 if( filter == NULL ) {
628 fprintf( stderr, "Invalid filter '%s'\n", filterstr );
629 exit( EXIT_FAILURE );
632 ch_free( filterstr );
638 ber_str2bv( subtree, 0, 0, &val );
639 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
640 if( rc != LDAP_SUCCESS ) {
641 fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
642 exit( EXIT_FAILURE );
645 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
653 if( base.bv_val != NULL ) {
656 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
657 if( rc != LDAP_SUCCESS ) {
658 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
659 progname, base.bv_val );
660 exit( EXIT_FAILURE );
663 be = select_backend( &nbase, 0 );
664 ber_memfree( nbase.bv_val );
665 BER_BVZERO( &nbase );
676 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
677 progname, base.bv_val );
678 exit( EXIT_FAILURE );
680 /* If the named base is a glue master, operate on the
683 if ( SLAP_GLUE_INSTANCE( be ) ) {
687 ch_free( base.bv_val );
690 } else if ( dbnum == -1 ) {
691 /* no suffix and no dbnum specified, just default to
692 * the first available database
694 if ( nbackends <= 0 ) {
695 fprintf( stderr, "No available databases\n" );
696 exit( EXIT_FAILURE );
698 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
701 /* db #0 is cn=config, don't select it as a default */
702 if ( dbnum < 1 ) continue;
704 if ( SLAP_MONITOR(be))
707 /* If just doing the first by default and it is a
708 * glue subordinate, find the master.
710 if ( SLAP_GLUE_SUBORDINATE(be) ) {
718 fprintf( stderr, "Available database(s) "
719 "do not allow %s\n", progname );
720 exit( EXIT_FAILURE );
723 if ( nosubordinates == 0 && dbnum > 1 ) {
724 Debug( LDAP_DEBUG_ANY,
725 "The first database does not allow %s;"
726 " using the first available one (%d)\n",
727 progname, dbnum, 0 );
730 } else if ( dbnum >= nbackends ) {
732 "Database number selected via -n is out of range\n"
733 "Must be in the range 0 to %d"
734 " (the number of configured databases)\n",
736 exit( EXIT_FAILURE );
740 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
741 if ( dbnum == 0 ) break;
751 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
752 if ( bdtmp == be ) break;
761 if ( conffile != NULL ) {
766 if ( confdir != NULL ) {
771 if ( ldiffile != NULL ) {
776 /* slapdn doesn't specify a backend to startup */
777 if ( !dryrun && tool != SLAPDN ) {
780 if ( slap_startup( be ) ) {
783 fprintf( stderr, "slap_startup failed "
784 "(test would succeed using "
785 "the -u switch)\n" );
789 fprintf( stderr, "slap_startup failed\n" );
793 exit( EXIT_FAILURE );
798 int slap_tool_destroy( void )
802 if ( need_shutdown ) {
803 if ( slap_shutdown( be ))
810 if ( slapMode == SLAP_SERVER_MODE ) {
811 /* always false. just pulls in necessary symbol references. */
812 lutil_uuidstr(NULL, 0);
818 ldap_pvt_tls_destroy();
823 mal_dumpleaktrace( leakfile );
826 if ( !BER_BVISNULL( &authcDN ) ) {
827 ch_free( authcDN.bv_val );
828 BER_BVZERO( &authcDN );
831 if ( ldiffp && ldiffp != &dummy ) {
832 ldif_close( ldiffp );