1 /* slapcommon.c - common routine for the slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2009 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";
99 if ( options != NULL ) {
100 fputs( options, stderr );
102 exit( EXIT_FAILURE );
106 parse_slapopt( void )
111 p = strchr( optarg, '=' );
117 if ( strncasecmp( optarg, "sockurl", len ) == 0 ) {
118 if ( !BER_BVISNULL( &listener_url ) ) {
119 ber_memfree( listener_url.bv_val );
121 ber_str2bv( p, 0, 1, &listener_url );
123 } else if ( strncasecmp( optarg, "domain", len ) == 0 ) {
124 if ( !BER_BVISNULL( &peer_domain ) ) {
125 ber_memfree( peer_domain.bv_val );
127 ber_str2bv( p, 0, 1, &peer_domain );
129 } else if ( strncasecmp( optarg, "peername", len ) == 0 ) {
130 if ( !BER_BVISNULL( &peer_name ) ) {
131 ber_memfree( peer_name.bv_val );
133 ber_str2bv( p, 0, 1, &peer_name );
135 } else if ( strncasecmp( optarg, "sockname", len ) == 0 ) {
136 if ( !BER_BVISNULL( &sock_name ) ) {
137 ber_memfree( sock_name.bv_val );
139 ber_str2bv( p, 0, 1, &sock_name );
141 } else if ( strncasecmp( optarg, "ssf", len ) == 0 ) {
142 if ( lutil_atou( &ssf, p ) ) {
143 Debug( LDAP_DEBUG_ANY, "unable to parse ssf=\"%s\".\n", p, 0, 0 );
147 } else if ( strncasecmp( optarg, "transport_ssf", len ) == 0 ) {
148 if ( lutil_atou( &transport_ssf, p ) ) {
149 Debug( LDAP_DEBUG_ANY, "unable to parse transport_ssf=\"%s\".\n", p, 0, 0 );
153 } else if ( strncasecmp( optarg, "tls_ssf", len ) == 0 ) {
154 if ( lutil_atou( &tls_ssf, p ) ) {
155 Debug( LDAP_DEBUG_ANY, "unable to parse tls_ssf=\"%s\".\n", p, 0, 0 );
159 } else if ( strncasecmp( optarg, "sasl_ssf", len ) == 0 ) {
160 if ( lutil_atou( &sasl_ssf, p ) ) {
161 Debug( LDAP_DEBUG_ANY, "unable to parse sasl_ssf=\"%s\".\n", p, 0, 0 );
165 } else if ( strncasecmp( optarg, "authzDN", len ) == 0 ) {
166 ber_str2bv( p, 0, 1, &authzDN );
168 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
169 } else if ( strncasecmp( optarg, "syslog", len ) == 0 ) {
170 if ( parse_debug_level( p, &ldap_syslog, &syslog_unknowns ) ) {
175 } else if ( strncasecmp( optarg, "syslog-level", len ) == 0 ) {
176 if ( parse_syslog_level( p, &ldap_syslog_level ) ) {
182 } else if ( strncasecmp( optarg, "syslog-user", len ) == 0 ) {
183 if ( parse_syslog_user( p, &syslogUser ) ) {
187 #endif /* LOG_LOCAL4 */
188 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
198 * slap_tool_init - initialize slap utility, handle program options.
202 * argc, argv command line arguments
205 static int need_shutdown;
209 const char* progname,
211 int argc, char **argv )
214 char *conffile = NULL;
215 char *confdir = NULL;
216 struct berval base = BER_BVNULL;
217 char *filterstr = NULL;
218 char *subtree = NULL;
219 char *ldiffile = NULL;
220 char **debug_unknowns = NULL;
222 int mode = SLAP_TOOL_MODE;
223 int truncatemode = 0;
227 /* tools default to "none", so that at least LDAP_DEBUG_ANY
228 * messages show up; use -d 0 to reset */
229 slap_debug = LDAP_DEBUG_NONE;
234 leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
235 sprintf( leakfilename, "%s.leak", progname );
236 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
239 free( leakfilename );
245 options = "b:cd:f:F:gj:l:n:o:qsS:uvw";
249 options = "a:b:cd:f:F:gl:n:o:s:v";
250 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
254 options = "d:f:F:No:Pv";
255 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
259 options = "d:f:F:o:Quv";
260 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
264 options = "d:f:F:M:o:R:U:vX:";
265 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
269 options = "b:cd:f:F:gn:o:qtv";
270 mode |= SLAP_TOOL_READMAIN;
274 options = "b:D:d:f:F:o:uU:vX:";
275 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
279 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
280 exit( EXIT_FAILURE );
284 while ( (i = getopt( argc, argv, options )) != EOF ) {
287 filterstr = ch_strdup( optarg );
291 ber_str2bv( optarg, 0, 1, &base );
294 case 'c': /* enable continue mode */
298 case 'd': { /* turn on debugging */
301 if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
302 usage( tool, progname );
306 /* allow to reset log level */
314 fputs( "must compile with LDAP_DEBUG for debugging\n",
320 ber_str2bv( optarg, 0, 1, &authcDN );
323 case 'f': /* specify a conf file */
324 conffile = ch_strdup( optarg );
327 case 'F': /* specify a conf dir */
328 confdir = ch_strdup( optarg );
331 case 'g': /* disable subordinate glue */
335 case 'j': /* jump to linenumber */
336 if ( lutil_atoi( &jumpline, optarg ) ) {
337 usage( tool, progname );
341 case 'l': /* LDIF file */
342 ldiffile = ch_strdup( optarg );
346 ber_str2bv( optarg, 0, 0, &mech );
350 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
351 usage( tool, progname );
353 dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
356 case 'n': /* which config file db to index */
357 if ( lutil_atoi( &dbnum, optarg ) || dbnum < 0 ) {
358 usage( tool, progname );
363 if ( parse_slapopt() ) {
364 usage( tool, progname );
369 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
370 usage( tool, progname );
372 dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
380 case 'q': /* turn on quick */
381 mode |= SLAP_TOOL_QUICK;
389 if ( lutil_atou( &csnsid, optarg )
390 || csnsid > SLAP_SYNC_SID_MAX )
392 usage( tool, progname );
396 case 's': /* dump subtree */
397 if ( tool == SLAPADD )
398 mode |= SLAP_TOOL_NO_SCHEMA_CHECK;
399 else if ( tool == SLAPCAT )
400 subtree = ch_strdup( optarg );
403 case 't': /* turn on truncate */
405 mode |= SLAP_TRUNCATE_MODE;
409 ber_str2bv( optarg, 0, 0, &authcID );
412 case 'u': /* dry run */
416 case 'v': /* turn on verbose */
420 case 'w': /* write context csn at the end */
425 ber_str2bv( optarg, 0, 0, &authzID );
429 usage( tool, progname );
434 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
435 if ( start_syslog ) {
438 logName = ch_strdup( progname );
441 logName = (char *)progname;
445 openlog( logName, OPENLOG_OPTIONS, syslogUser );
446 #elif defined LOG_DEBUG
447 openlog( logName, OPENLOG_OPTIONS );
454 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
459 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
460 usage( tool, progname );
466 if ( dbnum >= 0 && base.bv_val != NULL ) {
467 usage( tool, progname );
473 if ( argc == optind ) {
474 usage( tool, progname );
479 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
480 usage( tool, progname );
485 if ( argc != optind ) {
486 usage( tool, progname );
491 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
492 usage( tool, progname );
494 if ( BER_BVISNULL( &base ) ) {
495 usage( tool, progname );
497 ber_dupbv( &baseDN, &base );
504 if ( ldiffile == NULL ) {
505 dummy.fp = tool == SLAPCAT ? stdout : stdin;
508 } else if ((ldiffp = ldif_open( ldiffile, tool == SLAPCAT ? "w" : "r" ))
512 exit( EXIT_FAILURE );
516 * initialize stuff and figure out which backend we're dealing with
519 rc = slap_init( mode, progname );
521 fprintf( stderr, "%s: slap_init failed!\n", progname );
522 exit( EXIT_FAILURE );
525 rc = read_config( conffile, confdir );
528 fprintf( stderr, "%s: bad configuration %s!\n",
529 progname, confdir ? "directory" : "file" );
530 exit( EXIT_FAILURE );
533 if ( debug_unknowns ) {
534 rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
535 ldap_charray_free( debug_unknowns );
536 debug_unknowns = NULL;
538 exit( EXIT_FAILURE );
541 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
542 if ( syslog_unknowns ) {
543 rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
544 ldap_charray_free( syslog_unknowns );
545 syslog_unknowns = NULL;
547 exit( EXIT_FAILURE );
558 fprintf( stderr, "No databases found "
559 "in config file\n" );
560 exit( EXIT_FAILURE );
569 rc = glue_sub_attach( 0 );
573 "%s: subordinate configuration error\n", progname );
574 exit( EXIT_FAILURE );
578 rc = slap_schema_check();
581 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
582 exit( EXIT_FAILURE );
597 filter = str2filter( filterstr );
599 if( filter == NULL ) {
600 fprintf( stderr, "Invalid filter '%s'\n", filterstr );
601 exit( EXIT_FAILURE );
604 ch_free( filterstr );
610 ber_str2bv( subtree, 0, 0, &val );
611 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
612 if( rc != LDAP_SUCCESS ) {
613 fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
614 exit( EXIT_FAILURE );
617 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
625 if( base.bv_val != NULL ) {
628 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
629 if( rc != LDAP_SUCCESS ) {
630 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
631 progname, base.bv_val );
632 exit( EXIT_FAILURE );
635 be = select_backend( &nbase, 0 );
636 ber_memfree( nbase.bv_val );
637 BER_BVZERO( &nbase );
648 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
649 progname, base.bv_val );
650 exit( EXIT_FAILURE );
652 /* If the named base is a glue master, operate on the
655 if ( SLAP_GLUE_INSTANCE( be ) ) {
659 ch_free( base.bv_val );
662 } else if ( dbnum == -1 ) {
663 /* no suffix and no dbnum specified, just default to
664 * the first available database
666 if ( nbackends <= 0 ) {
667 fprintf( stderr, "No available databases\n" );
668 exit( EXIT_FAILURE );
670 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
673 /* db #0 is cn=config, don't select it as a default */
674 if ( dbnum < 1 ) continue;
676 if ( SLAP_MONITOR(be))
679 /* If just doing the first by default and it is a
680 * glue subordinate, find the master.
682 if ( SLAP_GLUE_SUBORDINATE(be) ) {
690 fprintf( stderr, "Available database(s) "
691 "do not allow %s\n", progname );
692 exit( EXIT_FAILURE );
695 if ( nosubordinates == 0 && dbnum > 1 ) {
696 Debug( LDAP_DEBUG_ANY,
697 "The first database does not allow %s;"
698 " using the first available one (%d)\n",
699 progname, dbnum, 0 );
702 } else if ( dbnum >= nbackends ) {
704 "Database number selected via -n is out of range\n"
705 "Must be in the range 0 to %d"
706 " (the number of configured databases)\n",
708 exit( EXIT_FAILURE );
711 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
712 if ( dbnum == 0 ) break;
722 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
723 if ( bdtmp == be ) break;
732 if ( conffile != NULL ) {
737 if ( ldiffile != NULL ) {
742 /* slapdn doesn't specify a backend to startup */
743 if ( !dryrun && tool != SLAPDN ) {
746 if ( slap_startup( be ) ) {
749 fprintf( stderr, "slap_startup failed "
750 "(test would succeed using "
751 "the -u switch)\n" );
755 fprintf( stderr, "slap_startup failed\n" );
759 exit( EXIT_FAILURE );
764 int slap_tool_destroy( void )
768 if ( need_shutdown ) {
769 if ( slap_shutdown( be ))
776 if ( slapMode == SLAP_SERVER_MODE ) {
777 /* always false. just pulls in necessary symbol references. */
778 lutil_uuidstr(NULL, 0);
784 ldap_pvt_tls_destroy();
789 mal_dumpleaktrace( leakfile );
792 if ( !BER_BVISNULL( &authcDN ) ) {
793 ch_free( authcDN.bv_val );
794 BER_BVZERO( &authcDN );
797 if ( ldiffp && ldiffp != &dummy ) {
798 ldif_close( ldiffp );