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