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] [-H url]\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] [-H url]\n";
104 if ( options != NULL ) {
105 fputs( options, stderr );
107 exit( EXIT_FAILURE );
111 parse_slapopt( int tool, int *mode )
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 */
195 } else if ( strncasecmp( optarg, "schema-check", len ) == 0 ) {
198 if ( strcasecmp( p, "yes" ) == 0 ) {
199 *mode &= ~SLAP_TOOL_NO_SCHEMA_CHECK;
200 } else if ( strcasecmp( p, "no" ) == 0 ) {
201 *mode |= SLAP_TOOL_NO_SCHEMA_CHECK;
203 Debug( LDAP_DEBUG_ANY, "unable to parse schema-check=\"%s\".\n", p, 0, 0 );
209 Debug( LDAP_DEBUG_ANY, "schema-check meaningless for tool.\n", 0, 0, 0 );
213 } else if ( strncasecmp( optarg, "value-check", len ) == 0 ) {
216 if ( strcasecmp( p, "yes" ) == 0 ) {
217 *mode |= SLAP_TOOL_VALUE_CHECK;
218 } else if ( strcasecmp( p, "no" ) == 0 ) {
219 *mode &= ~SLAP_TOOL_VALUE_CHECK;
221 Debug( LDAP_DEBUG_ANY, "unable to parse value-check=\"%s\".\n", p, 0, 0 );
227 Debug( LDAP_DEBUG_ANY, "value-check meaningless for tool.\n", 0, 0, 0 );
239 * slap_tool_init - initialize slap utility, handle program options.
243 * argc, argv command line arguments
246 static int need_shutdown;
250 const char* progname,
252 int argc, char **argv )
255 char *conffile = NULL;
256 char *confdir = NULL;
257 struct berval base = BER_BVNULL;
258 char *filterstr = NULL;
259 char *subtree = NULL;
260 char *ldiffile = NULL;
261 char **debug_unknowns = NULL;
263 int mode = SLAP_TOOL_MODE;
264 int truncatemode = 0;
269 /* tools default to "none", so that at least LDAP_DEBUG_ANY
270 * messages show up; use -d 0 to reset */
271 slap_debug = LDAP_DEBUG_NONE;
272 ldif_debug = slap_debug;
277 leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
278 sprintf( leakfilename, "%s.leak", progname );
279 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
282 free( leakfilename );
286 scope = LDAP_SCOPE_DEFAULT;
290 options = "b:cd:f:F:gj:l:n:o:qsS:uvw";
294 options = "a:b:cd:f:F:gH:l:n:o:s:v";
295 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
299 options = "d:f:F:No:Pv";
300 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
304 options = "b:cd:f:F:gj:l:n:o:qsS:uvw";
308 options = "a:b:cd:f:F:gH:l:n:o:s:v";
309 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
313 options = "d:f:F:n:o:Quv";
314 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
318 options = "d:f:F:M:o:R:U:vX:";
319 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
323 options = "b:cd:f:F:gn:o:qtv";
324 mode |= SLAP_TOOL_READMAIN;
328 options = "b:D:d:f:F:o:uU:vX:";
329 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
333 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
334 exit( EXIT_FAILURE );
338 while ( (i = getopt( argc, argv, options )) != EOF ) {
341 filterstr = ch_strdup( optarg );
345 ber_str2bv( optarg, 0, 1, &base );
348 case 'c': /* enable continue mode */
352 case 'd': { /* turn on debugging */
355 if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
356 usage( tool, progname );
360 /* allow to reset log level */
368 fputs( "must compile with LDAP_DEBUG for debugging\n",
374 ber_str2bv( optarg, 0, 1, &authcDN );
377 case 'f': /* specify a conf file */
378 conffile = ch_strdup( optarg );
381 case 'F': /* specify a conf dir */
382 confdir = ch_strdup( optarg );
385 case 'g': /* disable subordinate glue */
393 rc = ldap_url_parse_ext( optarg, &ludp,
394 LDAP_PVT_URL_PARSE_NOEMPTY_HOST | LDAP_PVT_URL_PARSE_NOEMPTY_DN );
395 if ( rc != LDAP_URL_SUCCESS ) {
396 usage( tool, progname );
399 /* don't accept host, port, attrs, extensions */
400 if ( ldap_pvt_url_scheme2proto( ludp->lud_scheme ) != LDAP_PROTO_TCP ) {
401 usage( tool, progname );
404 if ( ludp->lud_host != NULL ) {
405 usage( tool, progname );
408 if ( ludp->lud_port != 0 ) {
409 usage( tool, progname );
412 if ( ludp->lud_attrs != NULL ) {
413 usage( tool, progname );
416 if ( ludp->lud_exts != NULL ) {
417 usage( tool, progname );
420 if ( ludp->lud_dn != NULL && ludp->lud_dn[0] != '\0' ) {
421 subtree = ludp->lud_dn;
425 if ( ludp->lud_filter != NULL && ludp->lud_filter[0] != '\0' ) {
426 filterstr = ludp->lud_filter;
427 ludp->lud_filter = NULL;
430 scope = ludp->lud_scope;
432 ldap_free_urldesc( ludp );
435 case 'j': /* jump to linenumber */
436 if ( lutil_atoi( &jumpline, optarg ) ) {
437 usage( tool, progname );
441 case 'l': /* LDIF file */
442 ldiffile = ch_strdup( optarg );
446 ber_str2bv( optarg, 0, 0, &mech );
450 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
451 usage( tool, progname );
453 dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
456 case 'n': /* which config file db to index */
457 if ( lutil_atoi( &dbnum, optarg ) || dbnum < 0 ) {
458 usage( tool, progname );
463 if ( parse_slapopt( tool, &mode ) ) {
464 usage( tool, progname );
469 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
470 usage( tool, progname );
472 dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
480 case 'q': /* turn on quick */
481 mode |= SLAP_TOOL_QUICK;
489 if ( lutil_atou( &csnsid, optarg )
490 || csnsid > SLAP_SYNC_SID_MAX )
492 usage( tool, progname );
500 /* no schema check */
501 mode |= SLAP_TOOL_NO_SCHEMA_CHECK;
507 subtree = ch_strdup( optarg );
512 case 't': /* turn on truncate */
514 mode |= SLAP_TRUNCATE_MODE;
518 ber_str2bv( optarg, 0, 0, &authcID );
521 case 'u': /* dry run */
525 case 'v': /* turn on verbose */
529 case 'w': /* write context csn at the end */
534 ber_str2bv( optarg, 0, 0, &authzID );
538 usage( tool, progname );
543 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
544 if ( start_syslog ) {
547 logName = ch_strdup( progname );
550 logName = (char *)progname;
554 openlog( logName, OPENLOG_OPTIONS, syslogUser );
555 #elif defined LOG_DEBUG
556 openlog( logName, OPENLOG_OPTIONS );
563 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
581 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
582 usage( tool, progname );
588 if ( dbnum >= 0 && base.bv_val != NULL ) {
589 usage( tool, progname );
595 if ( argc == optind ) {
596 usage( tool, progname );
601 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
602 usage( tool, progname );
607 if ( argc != optind ) {
608 usage( tool, progname );
613 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
614 usage( tool, progname );
616 if ( BER_BVISNULL( &base ) ) {
617 usage( tool, progname );
619 ber_dupbv( &baseDN, &base );
626 if ( ldiffile == NULL ) {
627 dummy.fp = writer ? stdout : stdin;
630 } else if ((ldiffp = ldif_open( ldiffile, writer ? "w" : "r" ))
634 exit( EXIT_FAILURE );
638 * initialize stuff and figure out which backend we're dealing with
641 rc = slap_init( mode, progname );
643 fprintf( stderr, "%s: slap_init failed!\n", progname );
644 exit( EXIT_FAILURE );
647 rc = read_config( conffile, confdir );
650 fprintf( stderr, "%s: bad configuration %s!\n",
651 progname, confdir ? "directory" : "file" );
652 exit( EXIT_FAILURE );
655 if ( debug_unknowns ) {
656 rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
657 ldap_charray_free( debug_unknowns );
658 debug_unknowns = NULL;
660 exit( EXIT_FAILURE );
663 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
664 if ( syslog_unknowns ) {
665 rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
666 ldap_charray_free( syslog_unknowns );
667 syslog_unknowns = NULL;
669 exit( EXIT_FAILURE );
682 fprintf( stderr, "No databases found "
683 "in config file\n" );
684 exit( EXIT_FAILURE );
693 rc = glue_sub_attach( 0 );
697 "%s: subordinate configuration error\n", progname );
698 exit( EXIT_FAILURE );
702 rc = slap_schema_check();
705 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
706 exit( EXIT_FAILURE );
724 filter = str2filter( filterstr );
726 if( filter == NULL ) {
727 fprintf( stderr, "Invalid filter '%s'\n", filterstr );
728 exit( EXIT_FAILURE );
731 ch_free( filterstr );
737 ber_str2bv( subtree, 0, 0, &val );
738 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
739 if( rc != LDAP_SUCCESS ) {
740 fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
741 exit( EXIT_FAILURE );
744 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
752 if( base.bv_val != NULL ) {
755 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
756 if( rc != LDAP_SUCCESS ) {
757 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
758 progname, base.bv_val );
759 exit( EXIT_FAILURE );
762 be = select_backend( &nbase, 0 );
763 ber_memfree( nbase.bv_val );
764 BER_BVZERO( &nbase );
775 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
776 progname, base.bv_val );
777 exit( EXIT_FAILURE );
779 /* If the named base is a glue master, operate on the
782 if ( SLAP_GLUE_INSTANCE( be ) ) {
786 ch_free( base.bv_val );
789 } else if ( dbnum == -1 ) {
790 /* no suffix and no dbnum specified, just default to
791 * the first available database
793 if ( nbackends <= 0 ) {
794 fprintf( stderr, "No available databases\n" );
795 exit( EXIT_FAILURE );
797 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
800 /* db #0 is cn=config, don't select it as a default */
801 if ( dbnum < 1 ) continue;
803 if ( SLAP_MONITOR(be))
806 /* If just doing the first by default and it is a
807 * glue subordinate, find the master.
809 if ( SLAP_GLUE_SUBORDINATE(be) ) {
817 fprintf( stderr, "Available database(s) "
818 "do not allow %s\n", progname );
819 exit( EXIT_FAILURE );
822 if ( nosubordinates == 0 && dbnum > 1 ) {
823 Debug( LDAP_DEBUG_ANY,
824 "The first database does not allow %s;"
825 " using the first available one (%d)\n",
826 progname, dbnum, 0 );
829 } else if ( dbnum >= nbackends ) {
831 "Database number selected via -n is out of range\n"
832 "Must be in the range 0 to %d"
833 " (the number of configured databases)\n",
835 exit( EXIT_FAILURE );
839 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
840 if ( dbnum == 0 ) break;
845 if ( scope != LDAP_SCOPE_DEFAULT && BER_BVISNULL( &sub_ndn ) ) {
846 if ( be && be->be_nsuffix ) {
847 ber_dupbv( &sub_ndn, be->be_nsuffix );
851 "<scope> needs a DN or a valid database\n" );
852 exit( EXIT_FAILURE );
861 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
862 if ( bdtmp == be ) break;
871 if ( conffile != NULL ) {
876 if ( confdir != NULL ) {
881 if ( ldiffile != NULL ) {
886 /* slapdn doesn't specify a backend to startup */
887 if ( !dryrun && tool != SLAPDN ) {
890 if ( slap_startup( be ) ) {
893 fprintf( stderr, "slap_startup failed "
894 "(test would succeed using "
895 "the -u switch)\n" );
899 fprintf( stderr, "slap_startup failed\n" );
903 exit( EXIT_FAILURE );
908 int slap_tool_destroy( void )
912 if ( need_shutdown ) {
913 if ( slap_shutdown( be ))
920 if ( slapMode == SLAP_SERVER_MODE ) {
921 /* always false. just pulls in necessary symbol references. */
922 lutil_uuidstr(NULL, 0);
928 ldap_pvt_tls_destroy();
933 mal_dumpleaktrace( leakfile );
936 if ( !BER_BVISNULL( &authcDN ) ) {
937 ch_free( authcDN.bv_val );
938 BER_BVZERO( &authcDN );
941 if ( ldiffp && ldiffp != &dummy ) {
942 ldif_close( ldiffp );