]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
Check for SLAP_SYNTAX_DN flag instead of distinguishedName syntax
[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 SLAPMODIFY:
268                 options = "b:cd:f:F:gj:l:n:o:qsS:uvw";
269                 break;
270
271         case SLAPSCHEMA:
272                 options = "a:b:cd:f:F:gH:l:n:o:s:v";
273                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
274                 break;
275
276         case SLAPTEST:
277                 options = "d:f:F:n:o:Quv";
278                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
279                 break;
280
281         case SLAPAUTH:
282                 options = "d:f:F:M:o:R:U:vX:";
283                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
284                 break;
285
286         case SLAPINDEX:
287                 options = "b:cd:f:F:gn:o:qtv";
288                 mode |= SLAP_TOOL_READMAIN;
289                 break;
290
291         case SLAPACL:
292                 options = "b:D:d:f:F:o:uU:vX:";
293                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
294                 break;
295
296         default:
297                 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
298                 exit( EXIT_FAILURE );
299         }
300
301         dbnum = -1;
302         while ( (i = getopt( argc, argv, options )) != EOF ) {
303                 switch ( i ) {
304                 case 'a':
305                         filterstr = ch_strdup( optarg );
306                         break;
307
308                 case 'b':
309                         ber_str2bv( optarg, 0, 1, &base );
310                         break;
311
312                 case 'c':       /* enable continue mode */
313                         continuemode++;
314                         break;
315
316                 case 'd': {     /* turn on debugging */
317                         int     level = 0;
318
319                         if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
320                                 usage( tool, progname );
321                         }
322 #ifdef LDAP_DEBUG
323                         if ( level == 0 ) {
324                                 /* allow to reset log level */
325                                 slap_debug = 0;
326
327                         } else {
328                                 slap_debug |= level;
329                         }
330 #else
331                         if ( level != 0 )
332                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
333                                        stderr );
334 #endif
335                         } break;
336
337                 case 'D':
338                         ber_str2bv( optarg, 0, 1, &authcDN );
339                         break;
340
341                 case 'f':       /* specify a conf file */
342                         conffile = ch_strdup( optarg );
343                         break;
344
345                 case 'F':       /* specify a conf dir */
346                         confdir = ch_strdup( optarg );
347                         break;
348
349                 case 'g':       /* disable subordinate glue */
350                         use_glue = 0;
351                         break;
352
353                 case 'H': {
354                         LDAPURLDesc *ludp;
355                         int rc;
356
357                         rc = ldap_url_parse_ext( optarg, &ludp,
358                                 LDAP_PVT_URL_PARSE_NOEMPTY_HOST | LDAP_PVT_URL_PARSE_NOEMPTY_DN );
359                         if ( rc != LDAP_URL_SUCCESS ) {
360                                 usage( tool, progname );
361                         }
362
363                         /* don't accept host, port, attrs, extensions */
364                         if ( ldap_pvt_url_scheme2proto( ludp->lud_scheme ) != LDAP_PROTO_TCP ) {
365                                 usage( tool, progname );
366                         }
367
368                         if ( ludp->lud_host != NULL ) {
369                                 usage( tool, progname );
370                         }
371
372                         if ( ludp->lud_port != 0 ) {
373                                 usage( tool, progname );
374                         }
375
376                         if ( ludp->lud_attrs != NULL ) {
377                                 usage( tool, progname );
378                         }
379
380                         if ( ludp->lud_exts != NULL ) {
381                                 usage( tool, progname );
382                         }
383
384                         if ( ludp->lud_dn != NULL && ludp->lud_dn[0] != '\0' ) {
385                                 subtree = ludp->lud_dn;
386                                 ludp->lud_dn = NULL;
387                         }
388
389                         if ( ludp->lud_filter != NULL && ludp->lud_filter[0] != '\0' ) {
390                                 filterstr = ludp->lud_filter;
391                                 ludp->lud_filter = NULL;
392                         }
393
394                         scope = ludp->lud_scope;
395
396                         ldap_free_urldesc( ludp );
397                         } break;
398
399                 case 'j':       /* jump to linenumber */
400                         if ( lutil_atoi( &jumpline, optarg ) ) {
401                                 usage( tool, progname );
402                         }
403                         break;
404
405                 case 'l':       /* LDIF file */
406                         ldiffile = ch_strdup( optarg );
407                         break;
408
409                 case 'M':
410                         ber_str2bv( optarg, 0, 0, &mech );
411                         break;
412
413                 case 'N':
414                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
415                                 usage( tool, progname );
416                         }
417                         dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
418                         break;
419
420                 case 'n':       /* which config file db to index */
421                         if ( lutil_atoi( &dbnum, optarg ) || dbnum < 0 ) {
422                                 usage( tool, progname );
423                         }
424                         break;
425
426                 case 'o':
427                         if ( parse_slapopt() ) {
428                                 usage( tool, progname );
429                         }
430                         break;
431
432                 case 'P':
433                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
434                                 usage( tool, progname );
435                         }
436                         dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
437                         break;
438
439                 case 'Q':
440                         quiet++;
441                         slap_debug = 0;
442                         break;
443
444                 case 'q':       /* turn on quick */
445                         mode |= SLAP_TOOL_QUICK;
446                         break;
447
448                 case 'R':
449                         realm = optarg;
450                         break;
451
452                 case 'S':
453                         if ( lutil_atou( &csnsid, optarg )
454                                 || csnsid > SLAP_SYNC_SID_MAX )
455                         {
456                                 usage( tool, progname );
457                         }
458                         break;
459
460                 case 's':       /* dump subtree */
461                         if ( tool == SLAPADD )
462                                 mode |= SLAP_TOOL_NO_SCHEMA_CHECK;
463                         else if ( tool == SLAPCAT || tool == SLAPSCHEMA )
464                                 subtree = ch_strdup( optarg );
465                         break;
466
467                 case 't':       /* turn on truncate */
468                         truncatemode++;
469                         mode |= SLAP_TRUNCATE_MODE;
470                         break;
471
472                 case 'U':
473                         ber_str2bv( optarg, 0, 0, &authcID );
474                         break;
475
476                 case 'u':       /* dry run */
477                         dryrun++;
478                         break;
479
480                 case 'v':       /* turn on verbose */
481                         verbose++;
482                         break;
483
484                 case 'w':       /* write context csn at the end */
485                         update_ctxcsn++;
486                         break;
487
488                 case 'X':
489                         ber_str2bv( optarg, 0, 0, &authzID );
490                         break;
491
492                 default:
493                         usage( tool, progname );
494                         break;
495                 }
496         }
497
498 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
499         if ( start_syslog ) {
500                 char *logName;
501 #ifdef HAVE_EBCDIC
502                 logName = ch_strdup( progname );
503                 __atoe( logName );
504 #else
505                 logName = (char *)progname;
506 #endif
507
508 #ifdef LOG_LOCAL4
509                 openlog( logName, OPENLOG_OPTIONS, syslogUser );
510 #elif defined LOG_DEBUG
511                 openlog( logName, OPENLOG_OPTIONS );
512 #endif
513 #ifdef HAVE_EBCDIC
514                 free( logName );
515                 logName = NULL;
516 #endif
517         }
518 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
519
520         switch ( tool ) {
521         case SLAPCAT:
522         case SLAPSCHEMA:
523                 writer = 1;
524                 break;
525
526         default:
527                 writer = 0;
528                 break;
529         }
530
531         switch ( tool ) {
532         case SLAPADD:
533         case SLAPCAT:
534         case SLAPMODIFY:
535         case SLAPSCHEMA:
536                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
537                         usage( tool, progname );
538                 }
539
540                 break;
541
542         case SLAPINDEX:
543                 if ( dbnum >= 0 && base.bv_val != NULL ) {
544                         usage( tool, progname );
545                 }
546
547                 break;
548
549         case SLAPDN:
550                 if ( argc == optind ) {
551                         usage( tool, progname );
552                 }
553                 break;
554
555         case SLAPAUTH:
556                 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
557                         usage( tool, progname );
558                 }
559                 break;
560
561         case SLAPTEST:
562                 if ( argc != optind ) {
563                         usage( tool, progname );
564                 }
565                 break;
566
567         case SLAPACL:
568                 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
569                         usage( tool, progname );
570                 }
571                 if ( BER_BVISNULL( &base ) ) {
572                         usage( tool, progname );
573                 }
574                 ber_dupbv( &baseDN, &base );
575                 break;
576
577         default:
578                 break;
579         }
580
581         if ( ldiffile == NULL ) {
582                 dummy.fp = writer ? stdout : stdin;
583                 ldiffp = &dummy;
584
585         } else if ((ldiffp = ldif_open( ldiffile, writer ? "w" : "r" ))
586                 == NULL )
587         {
588                 perror( ldiffile );
589                 exit( EXIT_FAILURE );
590         }
591
592         /*
593          * initialize stuff and figure out which backend we're dealing with
594          */
595
596         rc = slap_init( mode, progname );
597         if ( rc != 0 ) {
598                 fprintf( stderr, "%s: slap_init failed!\n", progname );
599                 exit( EXIT_FAILURE );
600         }
601
602         rc = read_config( conffile, confdir );
603
604         if ( rc != 0 ) {
605                 fprintf( stderr, "%s: bad configuration %s!\n",
606                         progname, confdir ? "directory" : "file" );
607                 exit( EXIT_FAILURE );
608         }
609
610         if ( debug_unknowns ) {
611                 rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
612                 ldap_charray_free( debug_unknowns );
613                 debug_unknowns = NULL;
614                 if ( rc )
615                         exit( EXIT_FAILURE );
616         }
617
618 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
619         if ( syslog_unknowns ) {
620                 rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
621                 ldap_charray_free( syslog_unknowns );
622                 syslog_unknowns = NULL;
623                 if ( rc )
624                         exit( EXIT_FAILURE );
625         }
626 #endif
627
628         at_oc_cache = 1;
629
630         switch ( tool ) {
631         case SLAPADD:
632         case SLAPCAT:
633         case SLAPINDEX:
634         case SLAPMODIFY:
635         case SLAPSCHEMA:
636                 if ( !nbackends ) {
637                         fprintf( stderr, "No databases found "
638                                         "in config file\n" );
639                         exit( EXIT_FAILURE );
640                 }
641                 break;
642
643         default:
644                 break;
645         }
646
647         if ( use_glue ) {
648                 rc = glue_sub_attach( 0 );
649
650                 if ( rc != 0 ) {
651                         fprintf( stderr,
652                                 "%s: subordinate configuration error\n", progname );
653                         exit( EXIT_FAILURE );
654                 }
655         }
656
657         rc = slap_schema_check();
658
659         if ( rc != 0 ) {
660                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
661                 exit( EXIT_FAILURE );
662         }
663
664         switch ( tool ) {
665         case SLAPTEST:
666                 if ( dbnum >= 0 )
667                         goto get_db;
668                 /* FALLTHRU */
669         case SLAPDN:
670         case SLAPAUTH:
671                 be = NULL;
672                 goto startup;
673
674         default:
675                 break;
676         }
677
678         if( filterstr ) {
679                 filter = str2filter( filterstr );
680
681                 if( filter == NULL ) {
682                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
683                         exit( EXIT_FAILURE );
684                 }
685
686                 ch_free( filterstr );
687                 filterstr = NULL;
688         }
689
690         if( subtree ) {
691                 struct berval val;
692                 ber_str2bv( subtree, 0, 0, &val );
693                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
694                 if( rc != LDAP_SUCCESS ) {
695                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
696                         exit( EXIT_FAILURE );
697                 }
698
699                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
700                         base = val;
701                 } else {
702                         free( subtree );
703                         subtree = NULL;
704                 }
705         }
706
707         if( base.bv_val != NULL ) {
708                 struct berval nbase;
709
710                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
711                 if( rc != LDAP_SUCCESS ) {
712                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
713                                 progname, base.bv_val );
714                         exit( EXIT_FAILURE );
715                 }
716
717                 be = select_backend( &nbase, 0 );
718                 ber_memfree( nbase.bv_val );
719                 BER_BVZERO( &nbase );
720
721                 switch ( tool ) {
722                 case SLAPACL:
723                         goto startup;
724
725                 default:
726                         break;
727                 }
728
729                 if( be == NULL ) {
730                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
731                                 progname, base.bv_val );
732                         exit( EXIT_FAILURE );
733                 }
734                 /* If the named base is a glue master, operate on the
735                  * entire context
736                  */
737                 if ( SLAP_GLUE_INSTANCE( be ) ) {
738                         nosubordinates = 1;
739                 }
740
741                 ch_free( base.bv_val );
742                 BER_BVZERO( &base );
743
744         } else if ( dbnum == -1 ) {
745                 /* no suffix and no dbnum specified, just default to
746                  * the first available database
747                  */
748                 if ( nbackends <= 0 ) {
749                         fprintf( stderr, "No available databases\n" );
750                         exit( EXIT_FAILURE );
751                 }
752                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
753                         dbnum++;
754
755                         /* db #0 is cn=config, don't select it as a default */
756                         if ( dbnum < 1 ) continue;
757                 
758                         if ( SLAP_MONITOR(be))
759                                 continue;
760
761                 /* If just doing the first by default and it is a
762                  * glue subordinate, find the master.
763                  */
764                         if ( SLAP_GLUE_SUBORDINATE(be) ) {
765                                 nosubordinates = 1;
766                                 continue;
767                         }
768                         break;
769                 }
770
771                 if ( !be ) {
772                         fprintf( stderr, "Available database(s) "
773                                         "do not allow %s\n", progname );
774                         exit( EXIT_FAILURE );
775                 }
776                 
777                 if ( nosubordinates == 0 && dbnum > 1 ) {
778                         Debug( LDAP_DEBUG_ANY,
779                                 "The first database does not allow %s;"
780                                 " using the first available one (%d)\n",
781                                 progname, dbnum, 0 );
782                 }
783
784         } else if ( dbnum >= nbackends ) {
785                 fprintf( stderr,
786                         "Database number selected via -n is out of range\n"
787                         "Must be in the range 0 to %d"
788                         " (the number of configured databases)\n",
789                         nbackends - 1 );
790                 exit( EXIT_FAILURE );
791
792         } else {
793 get_db:
794                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
795                         if ( dbnum == 0 ) break;
796                         dbnum--;
797                 }
798         }
799
800         if ( scope != LDAP_SCOPE_DEFAULT && BER_BVISNULL( &sub_ndn ) ) {
801                 if ( be && be->be_nsuffix ) {
802                         ber_dupbv( &sub_ndn, be->be_nsuffix );
803
804                 } else {
805                         fprintf( stderr,
806                                 "<scope> needs a DN or a valid database\n" );
807                         exit( EXIT_FAILURE );
808                 }
809         }
810
811 startup:;
812         if ( be ) {
813                 BackendDB *bdtmp;
814
815                 dbnum = 0;
816                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
817                         if ( bdtmp == be ) break;
818                         dbnum++;
819                 }
820         }
821
822 #ifdef CSRIMALLOC
823         mal_leaktrace(1);
824 #endif
825
826         if ( conffile != NULL ) {
827                 ch_free( conffile );
828                 conffile = NULL;
829         }
830
831         if ( confdir != NULL ) {
832                 ch_free( confdir );
833                 confdir = NULL;
834         }
835
836         if ( ldiffile != NULL ) {
837                 ch_free( ldiffile );
838                 ldiffile = NULL;
839         }
840
841         /* slapdn doesn't specify a backend to startup */
842         if ( !dryrun && tool != SLAPDN ) {
843                 need_shutdown = 1;
844
845                 if ( slap_startup( be ) ) {
846                         switch ( tool ) {
847                         case SLAPTEST:
848                                 fprintf( stderr, "slap_startup failed "
849                                                 "(test would succeed using "
850                                                 "the -u switch)\n" );
851                                 break;
852
853                         default:
854                                 fprintf( stderr, "slap_startup failed\n" );
855                                 break;
856                         }
857
858                         exit( EXIT_FAILURE );
859                 }
860         }
861 }
862
863 int slap_tool_destroy( void )
864 {
865         int rc = 0;
866         if ( !dryrun ) {
867                 if ( need_shutdown ) {
868                         if ( slap_shutdown( be ))
869                                 rc = EXIT_FAILURE;
870                 }
871                 if ( slap_destroy())
872                         rc = EXIT_FAILURE;
873         }
874 #ifdef SLAPD_MODULES
875         if ( slapMode == SLAP_SERVER_MODE ) {
876         /* always false. just pulls in necessary symbol references. */
877                 lutil_uuidstr(NULL, 0);
878         }
879         module_kill();
880 #endif
881         schema_destroy();
882 #ifdef HAVE_TLS
883         ldap_pvt_tls_destroy();
884 #endif
885         config_destroy();
886
887 #ifdef CSRIMALLOC
888         mal_dumpleaktrace( leakfile );
889 #endif
890
891         if ( !BER_BVISNULL( &authcDN ) ) {
892                 ch_free( authcDN.bv_val );
893                 BER_BVZERO( &authcDN );
894         }
895
896         if ( ldiffp && ldiffp != &dummy ) {
897                 ldif_close( ldiffp );
898         }
899         return rc;
900 }
901
902