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