]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
ITS#6649
[openldap] / servers / slapd / slapcommon.c
1 /* slapcommon.c - common routine for the slap tools */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2010 The OpenLDAP Foundation.
6  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7  * Portions Copyright 2003 IBM Corporation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
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>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by Kurt Zeilenga for inclusion
20  * in OpenLDAP Software.  Additional signficant contributors include
21  *    Jong Hyuk Choi
22  *    Hallvard B. Furuseth
23  *    Howard Chu
24  *    Pierangelo Masarati
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/stdlib.h>
32 #include <ac/ctype.h>
33 #include <ac/string.h>
34 #include <ac/socket.h>
35 #include <ac/unistd.h>
36
37 #include "slapcommon.h"
38 #include "lutil.h"
39 #include "ldif.h"
40
41 tool_vars tool_globals;
42
43 #ifdef CSRIMALLOC
44 static char *leakfilename;
45 static FILE *leakfile;
46 #endif
47
48 static LDIFFP dummy;
49
50 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
51 int start_syslog;
52 static char **syslog_unknowns;
53 #ifdef LOG_LOCAL4
54 static int syslogUser = SLAP_DEFAULT_SYSLOG_USER;
55 #endif /* LOG_LOCAL4 */
56 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
57
58 static void
59 usage( int tool, const char *progname )
60 {
61         char *options = NULL;
62         fprintf( stderr,
63                 "usage: %s [-v] [-d debuglevel] [-f configfile] [-F configdir] [-o <name>[=<value>]]",
64                 progname );
65
66         switch( tool ) {
67         case SLAPACL:
68                 options = "\n\t[-U authcID | -D authcDN] [-X authzID | -o authzDN=<DN>]"
69                         "\n\t-b DN [-u] [attr[/access][:value]] [...]\n";
70                 break;
71
72         case SLAPADD:
73                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]\n"
74                         "\t[-l ldiffile] [-j linenumber] [-q] [-u] [-s] [-w]\n";
75                 break;
76
77         case SLAPAUTH:
78                 options = "\n\t[-U authcID] [-X authzID] [-R realm] [-M mech] ID [...]\n";
79                 break;
80
81         case SLAPCAT:
82                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]"
83                         " [-l ldiffile] [-a filter] [-s subtree] [-H url]\n";
84                 break;
85
86         case SLAPDN:
87                 options = "\n\t[-N | -P] DN [...]\n";
88                 break;
89
90         case SLAPINDEX:
91                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix] [attr ...] [-q] [-t]\n";
92                 break;
93
94         case SLAPTEST:
95                 options = " [-n databasenumber] [-u]\n";
96                 break;
97
98         case SLAPSCHEMA:
99                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]"
100                         " [-l errorfile] [-a filter] [-s subtree] [-H url]\n";
101                 break;
102         }
103
104         if ( options != NULL ) {
105                 fputs( options, stderr );
106         }
107         exit( EXIT_FAILURE );
108 }
109
110 static int
111 parse_slapopt( void )
112 {
113         size_t  len = 0;
114         char    *p;
115
116         p = strchr( optarg, '=' );
117         if ( p != NULL ) {
118                 len = p - optarg;
119                 p++;
120         }
121
122         if ( strncasecmp( optarg, "sockurl", len ) == 0 ) {
123                 if ( !BER_BVISNULL( &listener_url ) ) {
124                         ber_memfree( listener_url.bv_val );
125                 }
126                 ber_str2bv( p, 0, 1, &listener_url );
127
128         } else if ( strncasecmp( optarg, "domain", len ) == 0 ) {
129                 if ( !BER_BVISNULL( &peer_domain ) ) {
130                         ber_memfree( peer_domain.bv_val );
131                 }
132                 ber_str2bv( p, 0, 1, &peer_domain );
133
134         } else if ( strncasecmp( optarg, "peername", len ) == 0 ) {
135                 if ( !BER_BVISNULL( &peer_name ) ) {
136                         ber_memfree( peer_name.bv_val );
137                 }
138                 ber_str2bv( p, 0, 1, &peer_name );
139
140         } else if ( strncasecmp( optarg, "sockname", len ) == 0 ) {
141                 if ( !BER_BVISNULL( &sock_name ) ) {
142                         ber_memfree( sock_name.bv_val );
143                 }
144                 ber_str2bv( p, 0, 1, &sock_name );
145
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 );
149                         return -1;
150                 }
151
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 );
155                         return -1;
156                 }
157
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 );
161                         return -1;
162                 }
163
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 );
167                         return -1;
168                 }
169
170         } else if ( strncasecmp( optarg, "authzDN", len ) == 0 ) {
171                 ber_str2bv( p, 0, 1, &authzDN );
172
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 ) ) {
176                         return -1;
177                 }
178                 start_syslog = 1;
179
180         } else if ( strncasecmp( optarg, "syslog-level", len ) == 0 ) {
181                 if ( parse_syslog_level( p, &ldap_syslog_level ) ) {
182                         return -1;
183                 }
184                 start_syslog = 1;
185
186 #ifdef LOG_LOCAL4
187         } else if ( strncasecmp( optarg, "syslog-user", len ) == 0 ) {
188                 if ( parse_syslog_user( p, &syslogUser ) ) {
189                         return -1;
190                 }
191                 start_syslog = 1;
192 #endif /* LOG_LOCAL4 */
193 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
194
195         } else {
196                 return -1;
197         }
198
199         return 0;
200 }
201
202 /*
203  * slap_tool_init - initialize slap utility, handle program options.
204  * arguments:
205  *      name            program name
206  *      tool            tool code
207  *      argc, argv      command line arguments
208  */
209
210 static int need_shutdown;
211
212 void
213 slap_tool_init(
214         const char* progname,
215         int tool,
216         int argc, char **argv )
217 {
218         char *options;
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;
226         int rc, i;
227         int mode = SLAP_TOOL_MODE;
228         int truncatemode = 0;
229         int use_glue = 1;
230         int writer;
231
232 #ifdef LDAP_DEBUG
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;
237 #endif
238         ldap_syslog = 0;
239
240 #ifdef CSRIMALLOC
241         leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
242         sprintf( leakfilename, "%s.leak", progname );
243         if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
244                 leakfile = stderr;
245         }
246         free( leakfilename );
247         leakfilename = NULL;
248 #endif
249
250         scope = LDAP_SCOPE_DEFAULT;
251
252         switch( tool ) {
253         case SLAPADD:
254                 options = "b:cd:f:F:gj:l:n:o:qsS:uvw";
255                 break;
256
257         case SLAPCAT:
258                 options = "a:b:cd:f:F:gH:l:n:o:s:v";
259                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
260                 break;
261
262         case SLAPDN:
263                 options = "d:f:F:No:Pv";
264                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
265                 break;
266
267         case SLAPSCHEMA:
268                 options = "a:b:cd:f:F:gH:l:n:o:s:v";
269                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
270                 break;
271
272         case SLAPTEST:
273                 options = "d:f:F:n:o:Quv";
274                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
275                 break;
276
277         case SLAPAUTH:
278                 options = "d:f:F:M:o:R:U:vX:";
279                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
280                 break;
281
282         case SLAPINDEX:
283                 options = "b:cd:f:F:gn:o:qtv";
284                 mode |= SLAP_TOOL_READMAIN;
285                 break;
286
287         case SLAPACL:
288                 options = "b:D:d:f:F:o:uU:vX:";
289                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
290                 break;
291
292         default:
293                 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
294                 exit( EXIT_FAILURE );
295         }
296
297         dbnum = -1;
298         while ( (i = getopt( argc, argv, options )) != EOF ) {
299                 switch ( i ) {
300                 case 'a':
301                         filterstr = ch_strdup( optarg );
302                         break;
303
304                 case 'b':
305                         ber_str2bv( optarg, 0, 1, &base );
306                         break;
307
308                 case 'c':       /* enable continue mode */
309                         continuemode++;
310                         break;
311
312                 case 'd': {     /* turn on debugging */
313                         int     level = 0;
314
315                         if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
316                                 usage( tool, progname );
317                         }
318 #ifdef LDAP_DEBUG
319                         if ( level == 0 ) {
320                                 /* allow to reset log level */
321                                 slap_debug = 0;
322
323                         } else {
324                                 slap_debug |= level;
325                         }
326 #else
327                         if ( level != 0 )
328                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
329                                        stderr );
330 #endif
331                         } break;
332
333                 case 'D':
334                         ber_str2bv( optarg, 0, 1, &authcDN );
335                         break;
336
337                 case 'f':       /* specify a conf file */
338                         conffile = ch_strdup( optarg );
339                         break;
340
341                 case 'F':       /* specify a conf dir */
342                         confdir = ch_strdup( optarg );
343                         break;
344
345                 case 'g':       /* disable subordinate glue */
346                         use_glue = 0;
347                         break;
348
349                 case 'H': {
350                         LDAPURLDesc *ludp;
351                         int rc;
352
353                         rc = ldap_url_parse_ext( optarg, &ludp,
354                                 LDAP_PVT_URL_PARSE_NOEMPTY_HOST | LDAP_PVT_URL_PARSE_NOEMPTY_DN );
355                         if ( rc != LDAP_URL_SUCCESS ) {
356                                 usage( tool, progname );
357                         }
358
359                         /* don't accept host, port, attrs, extensions */
360                         if ( ldap_pvt_url_scheme2proto( ludp->lud_scheme ) != LDAP_PROTO_TCP ) {
361                                 usage( tool, progname );
362                         }
363
364                         if ( ludp->lud_host != NULL ) {
365                                 usage( tool, progname );
366                         }
367
368                         if ( ludp->lud_port != 0 ) {
369                                 usage( tool, progname );
370                         }
371
372                         if ( ludp->lud_attrs != NULL ) {
373                                 usage( tool, progname );
374                         }
375
376                         if ( ludp->lud_exts != NULL ) {
377                                 usage( tool, progname );
378                         }
379
380                         if ( ludp->lud_dn != NULL && ludp->lud_dn[0] != '\0' ) {
381                                 subtree = ludp->lud_dn;
382                                 ludp->lud_dn = NULL;
383                         }
384
385                         if ( ludp->lud_filter != NULL && ludp->lud_filter[0] != '\0' ) {
386                                 filterstr = ludp->lud_filter;
387                                 ludp->lud_filter = NULL;
388                         }
389
390                         scope = ludp->lud_scope;
391
392                         ldap_free_urldesc( ludp );
393                         } break;
394
395                 case 'j':       /* jump to linenumber */
396                         if ( lutil_atoi( &jumpline, optarg ) ) {
397                                 usage( tool, progname );
398                         }
399                         break;
400
401                 case 'l':       /* LDIF file */
402                         ldiffile = ch_strdup( optarg );
403                         break;
404
405                 case 'M':
406                         ber_str2bv( optarg, 0, 0, &mech );
407                         break;
408
409                 case 'N':
410                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
411                                 usage( tool, progname );
412                         }
413                         dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
414                         break;
415
416                 case 'n':       /* which config file db to index */
417                         if ( lutil_atoi( &dbnum, optarg ) || dbnum < 0 ) {
418                                 usage( tool, progname );
419                         }
420                         break;
421
422                 case 'o':
423                         if ( parse_slapopt() ) {
424                                 usage( tool, progname );
425                         }
426                         break;
427
428                 case 'P':
429                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
430                                 usage( tool, progname );
431                         }
432                         dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
433                         break;
434
435                 case 'Q':
436                         quiet++;
437                         slap_debug = 0;
438                         break;
439
440                 case 'q':       /* turn on quick */
441                         mode |= SLAP_TOOL_QUICK;
442                         break;
443
444                 case 'R':
445                         realm = optarg;
446                         break;
447
448                 case 'S':
449                         if ( lutil_atou( &csnsid, optarg )
450                                 || csnsid > SLAP_SYNC_SID_MAX )
451                         {
452                                 usage( tool, progname );
453                         }
454                         break;
455
456                 case 's':       /* dump subtree */
457                         if ( tool == SLAPADD )
458                                 mode |= SLAP_TOOL_NO_SCHEMA_CHECK;
459                         else if ( tool == SLAPCAT || tool == SLAPSCHEMA )
460                                 subtree = ch_strdup( optarg );
461                         break;
462
463                 case 't':       /* turn on truncate */
464                         truncatemode++;
465                         mode |= SLAP_TRUNCATE_MODE;
466                         break;
467
468                 case 'U':
469                         ber_str2bv( optarg, 0, 0, &authcID );
470                         break;
471
472                 case 'u':       /* dry run */
473                         dryrun++;
474                         break;
475
476                 case 'v':       /* turn on verbose */
477                         verbose++;
478                         break;
479
480                 case 'w':       /* write context csn at the end */
481                         update_ctxcsn++;
482                         break;
483
484                 case 'X':
485                         ber_str2bv( optarg, 0, 0, &authzID );
486                         break;
487
488                 default:
489                         usage( tool, progname );
490                         break;
491                 }
492         }
493
494 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
495         if ( start_syslog ) {
496                 char *logName;
497 #ifdef HAVE_EBCDIC
498                 logName = ch_strdup( progname );
499                 __atoe( logName );
500 #else
501                 logName = (char *)progname;
502 #endif
503
504 #ifdef LOG_LOCAL4
505                 openlog( logName, OPENLOG_OPTIONS, syslogUser );
506 #elif defined LOG_DEBUG
507                 openlog( logName, OPENLOG_OPTIONS );
508 #endif
509 #ifdef HAVE_EBCDIC
510                 free( logName );
511                 logName = NULL;
512 #endif
513         }
514 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
515
516         switch ( tool ) {
517         case SLAPCAT:
518         case SLAPSCHEMA:
519                 writer = 1;
520                 break;
521
522         default:
523                 writer = 0;
524                 break;
525         }
526
527         switch ( tool ) {
528         case SLAPADD:
529         case SLAPCAT:
530         case SLAPSCHEMA:
531                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
532                         usage( tool, progname );
533                 }
534
535                 break;
536
537         case SLAPINDEX:
538                 if ( dbnum >= 0 && base.bv_val != NULL ) {
539                         usage( tool, progname );
540                 }
541
542                 break;
543
544         case SLAPDN:
545                 if ( argc == optind ) {
546                         usage( tool, progname );
547                 }
548                 break;
549
550         case SLAPAUTH:
551                 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
552                         usage( tool, progname );
553                 }
554                 break;
555
556         case SLAPTEST:
557                 if ( argc != optind ) {
558                         usage( tool, progname );
559                 }
560                 break;
561
562         case SLAPACL:
563                 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
564                         usage( tool, progname );
565                 }
566                 if ( BER_BVISNULL( &base ) ) {
567                         usage( tool, progname );
568                 }
569                 ber_dupbv( &baseDN, &base );
570                 break;
571
572         default:
573                 break;
574         }
575
576         if ( ldiffile == NULL ) {
577                 dummy.fp = writer ? stdout : stdin;
578                 ldiffp = &dummy;
579
580         } else if ((ldiffp = ldif_open( ldiffile, writer ? "w" : "r" ))
581                 == NULL )
582         {
583                 perror( ldiffile );
584                 exit( EXIT_FAILURE );
585         }
586
587         /*
588          * initialize stuff and figure out which backend we're dealing with
589          */
590
591         rc = slap_init( mode, progname );
592         if ( rc != 0 ) {
593                 fprintf( stderr, "%s: slap_init failed!\n", progname );
594                 exit( EXIT_FAILURE );
595         }
596
597         rc = read_config( conffile, confdir );
598
599         if ( rc != 0 ) {
600                 fprintf( stderr, "%s: bad configuration %s!\n",
601                         progname, confdir ? "directory" : "file" );
602                 exit( EXIT_FAILURE );
603         }
604
605         if ( debug_unknowns ) {
606                 rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
607                 ldap_charray_free( debug_unknowns );
608                 debug_unknowns = NULL;
609                 if ( rc )
610                         exit( EXIT_FAILURE );
611         }
612
613 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
614         if ( syslog_unknowns ) {
615                 rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
616                 ldap_charray_free( syslog_unknowns );
617                 syslog_unknowns = NULL;
618                 if ( rc )
619                         exit( EXIT_FAILURE );
620         }
621 #endif
622
623         at_oc_cache = 1;
624
625         switch ( tool ) {
626         case SLAPADD:
627         case SLAPCAT:
628         case SLAPINDEX:
629         case SLAPSCHEMA:
630                 if ( !nbackends ) {
631                         fprintf( stderr, "No databases found "
632                                         "in config file\n" );
633                         exit( EXIT_FAILURE );
634                 }
635                 break;
636
637         default:
638                 break;
639         }
640
641         if ( use_glue ) {
642                 rc = glue_sub_attach( 0 );
643
644                 if ( rc != 0 ) {
645                         fprintf( stderr,
646                                 "%s: subordinate configuration error\n", progname );
647                         exit( EXIT_FAILURE );
648                 }
649         }
650
651         rc = slap_schema_check();
652
653         if ( rc != 0 ) {
654                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
655                 exit( EXIT_FAILURE );
656         }
657
658         switch ( tool ) {
659         case SLAPTEST:
660                 if ( dbnum >= 0 )
661                         goto get_db;
662                 /* FALLTHRU */
663         case SLAPDN:
664         case SLAPAUTH:
665                 be = NULL;
666                 goto startup;
667
668         default:
669                 break;
670         }
671
672         if( filterstr ) {
673                 filter = str2filter( filterstr );
674
675                 if( filter == NULL ) {
676                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
677                         exit( EXIT_FAILURE );
678                 }
679
680                 ch_free( filterstr );
681                 filterstr = NULL;
682         }
683
684         if( subtree ) {
685                 struct berval val;
686                 ber_str2bv( subtree, 0, 0, &val );
687                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
688                 if( rc != LDAP_SUCCESS ) {
689                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
690                         exit( EXIT_FAILURE );
691                 }
692
693                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
694                         base = val;
695                 } else {
696                         free( subtree );
697                         subtree = NULL;
698                 }
699         }
700
701         if( base.bv_val != NULL ) {
702                 struct berval nbase;
703
704                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
705                 if( rc != LDAP_SUCCESS ) {
706                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
707                                 progname, base.bv_val );
708                         exit( EXIT_FAILURE );
709                 }
710
711                 be = select_backend( &nbase, 0 );
712                 ber_memfree( nbase.bv_val );
713                 BER_BVZERO( &nbase );
714
715                 switch ( tool ) {
716                 case SLAPACL:
717                         goto startup;
718
719                 default:
720                         break;
721                 }
722
723                 if( be == NULL ) {
724                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
725                                 progname, base.bv_val );
726                         exit( EXIT_FAILURE );
727                 }
728                 /* If the named base is a glue master, operate on the
729                  * entire context
730                  */
731                 if ( SLAP_GLUE_INSTANCE( be ) ) {
732                         nosubordinates = 1;
733                 }
734
735                 ch_free( base.bv_val );
736                 BER_BVZERO( &base );
737
738         } else if ( dbnum == -1 ) {
739                 /* no suffix and no dbnum specified, just default to
740                  * the first available database
741                  */
742                 if ( nbackends <= 0 ) {
743                         fprintf( stderr, "No available databases\n" );
744                         exit( EXIT_FAILURE );
745                 }
746                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
747                         dbnum++;
748
749                         /* db #0 is cn=config, don't select it as a default */
750                         if ( dbnum < 1 ) continue;
751                 
752                         if ( SLAP_MONITOR(be))
753                                 continue;
754
755                 /* If just doing the first by default and it is a
756                  * glue subordinate, find the master.
757                  */
758                         if ( SLAP_GLUE_SUBORDINATE(be) ) {
759                                 nosubordinates = 1;
760                                 continue;
761                         }
762                         break;
763                 }
764
765                 if ( !be ) {
766                         fprintf( stderr, "Available database(s) "
767                                         "do not allow %s\n", progname );
768                         exit( EXIT_FAILURE );
769                 }
770                 
771                 if ( nosubordinates == 0 && dbnum > 1 ) {
772                         Debug( LDAP_DEBUG_ANY,
773                                 "The first database does not allow %s;"
774                                 " using the first available one (%d)\n",
775                                 progname, dbnum, 0 );
776                 }
777
778         } else if ( dbnum >= nbackends ) {
779                 fprintf( stderr,
780                         "Database number selected via -n is out of range\n"
781                         "Must be in the range 0 to %d"
782                         " (the number of configured databases)\n",
783                         nbackends - 1 );
784                 exit( EXIT_FAILURE );
785
786         } else {
787 get_db:
788                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
789                         if ( dbnum == 0 ) break;
790                         dbnum--;
791                 }
792         }
793
794         if ( scope != LDAP_SCOPE_DEFAULT && BER_BVISNULL( &sub_ndn ) ) {
795                 if ( be && be->be_nsuffix ) {
796                         ber_dupbv( &sub_ndn, be->be_nsuffix );
797
798                 } else {
799                         fprintf( stderr,
800                                 "<scope> needs a DN or a valid database\n" );
801                         exit( EXIT_FAILURE );
802                 }
803         }
804
805 startup:;
806         if ( be ) {
807                 BackendDB *bdtmp;
808
809                 dbnum = 0;
810                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
811                         if ( bdtmp == be ) break;
812                         dbnum++;
813                 }
814         }
815
816 #ifdef CSRIMALLOC
817         mal_leaktrace(1);
818 #endif
819
820         if ( conffile != NULL ) {
821                 ch_free( conffile );
822                 conffile = NULL;
823         }
824
825         if ( confdir != NULL ) {
826                 ch_free( confdir );
827                 confdir = NULL;
828         }
829
830         if ( ldiffile != NULL ) {
831                 ch_free( ldiffile );
832                 ldiffile = NULL;
833         }
834
835         /* slapdn doesn't specify a backend to startup */
836         if ( !dryrun && tool != SLAPDN ) {
837                 need_shutdown = 1;
838
839                 if ( slap_startup( be ) ) {
840                         switch ( tool ) {
841                         case SLAPTEST:
842                                 fprintf( stderr, "slap_startup failed "
843                                                 "(test would succeed using "
844                                                 "the -u switch)\n" );
845                                 break;
846
847                         default:
848                                 fprintf( stderr, "slap_startup failed\n" );
849                                 break;
850                         }
851
852                         exit( EXIT_FAILURE );
853                 }
854         }
855 }
856
857 int slap_tool_destroy( void )
858 {
859         int rc = 0;
860         if ( !dryrun ) {
861                 if ( need_shutdown ) {
862                         if ( slap_shutdown( be ))
863                                 rc = EXIT_FAILURE;
864                 }
865                 if ( slap_destroy())
866                         rc = EXIT_FAILURE;
867         }
868 #ifdef SLAPD_MODULES
869         if ( slapMode == SLAP_SERVER_MODE ) {
870         /* always false. just pulls in necessary symbol references. */
871                 lutil_uuidstr(NULL, 0);
872         }
873         module_kill();
874 #endif
875         schema_destroy();
876 #ifdef HAVE_TLS
877         ldap_pvt_tls_destroy();
878 #endif
879         config_destroy();
880
881 #ifdef CSRIMALLOC
882         mal_dumpleaktrace( leakfile );
883 #endif
884
885         if ( !BER_BVISNULL( &authcDN ) ) {
886                 ch_free( authcDN.bv_val );
887                 BER_BVZERO( &authcDN );
888         }
889
890         if ( ldiffp && ldiffp != &dummy ) {
891                 ldif_close( ldiffp );
892         }
893         return rc;
894 }
895
896