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