]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
add slapsaslauth test tool
[openldap] / servers / slapd / main.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29
30 #include <ac/socket.h>
31 #include <ac/string.h>
32 #include <ac/time.h>
33 #include <ac/unistd.h>
34 #include <ac/wait.h>
35 #include <ac/errno.h>
36
37 #include "ldap_pvt.h"
38
39 #include "slap.h"
40 #include "lutil.h"
41 #include "ldif.h"
42
43 #ifdef LDAP_SLAPI
44 #include "slapi/slapi.h"
45 #endif
46
47 #ifdef LDAP_SIGCHLD
48 static RETSIGTYPE wait4child( int sig );
49 #endif
50
51 #ifdef HAVE_NT_SERVICE_MANAGER
52 #define MAIN_RETURN(x) return
53 static struct sockaddr_in       bind_addr;
54
55 #define SERVICE_EXIT( e, n )    do { \
56         if ( is_NT_Service ) { \
57                 lutil_ServiceStatus.dwWin32ExitCode                             = (e); \
58                 lutil_ServiceStatus.dwServiceSpecificExitCode   = (n); \
59         } \
60 } while ( 0 )
61
62 #else
63 #define SERVICE_EXIT( e, n )
64 #define MAIN_RETURN(x) return(x)
65 #endif
66
67 typedef int (MainFunc) LDAP_P(( int argc, char *argv[] ));
68 extern MainFunc slapadd, slapcat, slapdn, slapindex, slappasswd, slaptest, slapsaslauth;
69
70 static struct {
71         char *name;
72         MainFunc *func;
73 } tools[] = {
74         {"slapadd", slapadd},
75         {"slapcat", slapcat},
76         {"slapdn", slapdn},
77         {"slapindex", slapindex},
78         {"slappasswd", slappasswd},
79         {"slaptest", slaptest},
80         {"slapsaslauth", slapsaslauth},
81         {NULL, NULL}
82 };
83
84 /*
85  * when more than one slapd is running on one machine, each one might have
86  * it's own LOCAL for syslogging and must have its own pid/args files
87  */
88
89 #ifndef HAVE_MKVERSION
90 const char Versionstr[] =
91         OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
92 #endif
93
94 #ifdef LOG_LOCAL4
95
96 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
97
98 typedef struct _str2intDispatch {
99         char    *stringVal;
100         int      abbr;
101         int      intVal;
102 } STRDISP, *STRDISP_P;
103
104
105 /* table to compute syslog-options to integer */
106 static STRDISP  syslog_types[] = {
107         { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
108         { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
109         { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
110         { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
111         { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
112         { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
113         { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
114         { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
115         { NULL, 0, 0 }
116 };
117
118 static int   cnvt_str2int( char *, STRDISP_P, int );
119
120 #endif  /* LOG_LOCAL4 */
121
122 #define CHECK_NONE      0x00
123 #define CHECK_CONFIG    0x01
124 static int check = CHECK_NONE;
125 static int version = 0;
126
127 static void
128 usage( char *name )
129 {
130         fprintf( stderr,
131                 "usage: %s options\n", name );
132         fprintf( stderr,
133                 "\t-4\t\tIPv4 only\n"
134                 "\t-6\t\tIPv6 only\n"
135                 "\t-T {acdipt}\tRun in Tool mode\n"
136                 "\t-c cookie\tSync cookie of consumer\n"
137                 "\t-d level\tDebug level" "\n"
138                 "\t-f filename\tConfiguration file\n"
139 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
140                 "\t-g group\tGroup (id or name) to run as\n"
141 #endif
142                 "\t-h URLs\t\tList of URLs to serve\n"
143 #ifdef LOG_LOCAL4
144                 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
145 #endif
146                 "\t-n serverName\tService name\n"
147 #ifdef HAVE_CHROOT
148                 "\t-r directory\tSandbox directory to chroot to\n"
149 #endif
150                 "\t-s level\tSyslog level\n"
151 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
152                 "\t-u user\t\tUser (id or name) to run as\n"
153                 "\t-V\t\tprint version info (-VV only)\n"
154 #endif
155     );
156 }
157
158 #ifdef HAVE_NT_SERVICE_MANAGER
159 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
160 #else
161 int main( int argc, char **argv )
162 #endif
163 {
164         int             i, no_detach = 0;
165         int             rc = 1;
166         char *urls = NULL;
167 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
168         char *username = NULL;
169         char *groupname = NULL;
170 #endif
171 #if defined(HAVE_CHROOT)
172         char *sandbox = NULL;
173 #endif
174 #ifdef LOG_LOCAL4
175     int     syslogUser = DEFAULT_SYSLOG_USER;
176 #endif
177         
178         int g_argc = argc;
179         char **g_argv = argv;
180
181 #ifdef HAVE_NT_SERVICE_MANAGER
182         char            *configfile = ".\\slapd.conf";
183 #else
184         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
185 #endif
186         char        *serverName;
187         int         serverMode = SLAP_SERVER_MODE;
188
189         struct berval cookie = BER_BVNULL;
190         struct sync_cookie *scp = NULL;
191         struct sync_cookie *scp_entry = NULL;
192
193 #ifdef CSRIMALLOC
194         FILE *leakfile;
195         if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
196                 leakfile = stderr;
197         }
198 #endif
199
200         sl_mem_init();
201
202         serverName = lutil_progname( "slapd", argc, argv );
203
204         if ( strcmp( serverName, "slapd" ) ) {
205                 for (i=0; tools[i].name; i++) {
206                         if ( !strcmp( serverName, tools[i].name ) ) {
207                                 rc = tools[i].func(argc, argv);
208                                 MAIN_RETURN(rc);
209                         }
210                 }
211         }
212
213 #ifdef HAVE_NT_SERVICE_MANAGER
214         {
215                 int *i;
216                 char *newConfigFile;
217                 char *newUrls;
218                 char *regService = NULL;
219
220                 if ( is_NT_Service ) {
221                         lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
222                         if ( strcmp(serverName, SERVICE_NAME) )
223                             regService = serverName;
224                 }
225
226                 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
227                 if ( i != NULL ) 
228                 {
229                         slap_debug = *i;
230 #ifdef NEW_LOGGING
231                         lutil_log_initialize( argc, argv );
232                         LDAP_LOG( SLAPD, INFO, 
233                                 "main: new debug level from registry is: %d\n", 
234                                 slap_debug, 0, 0 );
235 #else
236                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
237 #endif
238                 }
239
240                 newUrls = (char *) lutil_getRegParam(regService, "Urls");
241                 if (newUrls)
242                 {
243                     if (urls)
244                         ch_free(urls);
245
246                     urls = ch_strdup(newUrls);
247 #ifdef NEW_LOGGING
248                     LDAP_LOG( SLAPD, INFO, 
249                                 "main: new urls from registry: %s\n", urls, 0, 0 );
250 #else
251                     Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
252                           urls, 0, 0);
253 #endif
254
255                 }
256
257                 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
258                 if ( newConfigFile != NULL ) 
259                 {
260                         configfile = newConfigFile;
261 #ifdef NEW_LOGGING
262                         LDAP_LOG( SLAPD, INFO, 
263                                 "main: new config file from registry is: %s\n", configfile, 0, 0 );
264 #else
265                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
266 #endif
267
268                 }
269         }
270 #endif
271
272         while ( (i = getopt( argc, argv,
273                              "c:d:f:h:s:n:tT:V"
274 #if LDAP_PF_INET6
275                                 "46"
276 #endif
277 #ifdef HAVE_CHROOT
278                                 "r:"
279 #endif
280 #ifdef LOG_LOCAL4
281                              "l:"
282 #endif
283 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
284                              "u:g:"
285 #endif
286                              )) != EOF ) {
287                 switch ( i ) {
288 #ifdef LDAP_PF_INET6
289                 case '4':
290                         slap_inet4or6 = AF_INET;
291                         break;
292                 case '6':
293                         slap_inet4or6 = AF_INET6;
294                         break;
295 #endif
296
297                 case 'h':       /* listen URLs */
298                         if ( urls != NULL ) free( urls );
299                         urls = ch_strdup( optarg );
300                         break;
301
302                 case 'c':       /* provide sync cookie, override if exist in replica */
303                         scp = (struct sync_cookie *) ch_calloc( 1,
304                                                                                 sizeof( struct sync_cookie ));
305                         ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
306                         ber_bvarray_add( &scp->octet_str, &cookie );
307                         slap_parse_sync_cookie( scp );
308
309                         LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
310                                 if ( scp->rid == scp_entry->rid ) {
311 #ifdef NEW_LOGGING
312                                         LDAP_LOG( OPERATION, CRIT,
313                                                         "main: duplicated replica id in cookies\n",
314                                                         0, 0, 0 );
315 #else
316                                         Debug( LDAP_DEBUG_ANY,
317                                                     "main: duplicated replica id in cookies\n",
318                                                         0, 0, 0 );
319 #endif
320                                         slap_sync_cookie_free( scp, 1 );
321                                         goto destroy;
322                                 }
323                         }
324                         LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
325                         break;
326
327                 case 'd':       /* set debug level and 'do not detach' flag */
328                         no_detach = 1;
329 #ifdef LDAP_DEBUG
330                         slap_debug |= atoi( optarg );
331 #else
332                         if ( atoi( optarg ) != 0 )
333                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
334                                        stderr );
335 #endif
336                         break;
337
338                 case 'f':       /* read config file */
339                         configfile = ch_strdup( optarg );
340                         break;
341
342                 case 's':       /* set syslog level */
343                         ldap_syslog = atoi( optarg );
344                         break;
345
346 #ifdef LOG_LOCAL4
347                 case 'l':       /* set syslog local user */
348                         syslogUser = cnvt_str2int( optarg,
349                                 syslog_types, DEFAULT_SYSLOG_USER );
350                         break;
351 #endif
352
353 #ifdef HAVE_CHROOT
354                 case 'r':
355                         if( sandbox ) free(sandbox);
356                         sandbox = ch_strdup( optarg );
357                         break;
358 #endif
359
360 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
361                 case 'u':       /* user name */
362                         if( username ) free(username);
363                         username = ch_strdup( optarg );
364                         break;
365
366                 case 'g':       /* group name */
367                         if( groupname ) free(groupname);
368                         groupname = ch_strdup( optarg );
369                         break;
370 #endif /* SETUID && GETUID */
371
372                 case 'n':  /* NT service name */
373                         serverName = ch_strdup( optarg );
374                         break;
375
376                 case 't':
377                         /* deprecated; use slaptest instead */
378                         fprintf( stderr, "option -t deprecated; "
379                                 "use slaptest command instead\n" );
380                         check |= CHECK_CONFIG;
381                         break;
382
383                 case 'V':
384                         version++;
385                         break;
386
387                 case 'T':
388                         for (i=0; tools[i].name; i++) {
389                                 if ( optarg[0] == tools[i].name[4] ) {
390                                         rc = tools[i].func(argc, argv);
391                                         MAIN_RETURN(rc);
392                                 }
393                         }
394                         /* FALLTHRU */
395                 default:
396                         usage( argv[0] );
397                         rc = 1;
398                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
399                         goto stop;
400                 }
401         }
402
403 #ifdef NEW_LOGGING
404         lutil_log_initialize( argc, argv );
405 #else
406         lutil_set_debug_level( "slapd", slap_debug );
407         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
408         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
409         ldif_debug = slap_debug;
410 #endif
411
412         if ( version ) {
413                 fprintf( stderr, "%s\n", Versionstr );
414                 if ( version > 1 ) goto stop;
415         }
416
417         {
418                 char *logName;
419 #ifdef HAVE_EBCDIC
420                 logName = ch_strdup( serverName );
421                 __atoe( logName );
422 #else
423                 logName = serverName;
424 #endif
425
426 #ifdef LOG_LOCAL4
427                 openlog( logName, OPENLOG_OPTIONS, syslogUser );
428 #elif LOG_DEBUG
429                 openlog( logName, OPENLOG_OPTIONS );
430 #endif
431 #ifdef HAVE_EBCDIC
432                 free( logName );
433 #endif
434         }
435
436 #ifdef NEW_LOGGING
437         LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
438 #else
439         Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
440 #endif
441
442         if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
443                 rc = 1;
444                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
445                 goto stop;
446         }
447
448 #if defined(HAVE_CHROOT)
449         if ( sandbox ) {
450                 if ( chdir( sandbox ) ) {
451                         perror("chdir");
452                         rc = 1;
453                         goto stop;
454                 }
455                 if ( chroot( sandbox ) ) {
456                         perror("chroot");
457                         rc = 1;
458                         goto stop;
459                 }
460         }
461 #endif
462
463 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
464         if ( username != NULL || groupname != NULL ) {
465                 slap_init_user( username, groupname );
466         }
467 #endif
468
469         extops_init();
470         lutil_passwd_init();
471         slap_op_init();
472
473 #ifdef SLAPD_MODULES
474         if ( module_init() != 0 ) {
475                 rc = 1;
476                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
477                 goto destroy;
478         }
479 #endif
480
481         if ( slap_schema_init( ) != 0 ) {
482 #ifdef NEW_LOGGING
483                 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
484 #else
485                 Debug( LDAP_DEBUG_ANY,
486                     "schema initialization error\n",
487                     0, 0, 0 );
488 #endif
489
490                 goto destroy;
491         }
492
493         if ( slap_init( serverMode, serverName ) != 0 ) {
494                 rc = 1;
495                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
496                 goto destroy;
497         }
498
499         if ( slap_controls_init( ) != 0 ) {
500 #ifdef NEW_LOGGING
501                 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
502 #else
503                 Debug( LDAP_DEBUG_ANY,
504                     "controls initialization error\n",
505                     0, 0, 0 );
506 #endif
507
508                 goto destroy;
509         }
510
511 #ifdef HAVE_TLS
512         /* Library defaults to full certificate checking. This is correct when
513          * a client is verifying a server because all servers should have a
514          * valid cert. But few clients have valid certs, so we want our default
515          * to be no checking. The config file can override this as usual.
516          */
517         rc = 0;
518         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
519 #endif
520
521 #ifdef LDAP_SLAPI
522         if ( slapi_int_initialize() != 0 ) {
523 #ifdef NEW_LOGGING
524                 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
525 #else
526                 Debug( LDAP_DEBUG_ANY,
527                     "slapi initialization error\n",
528                     0, 0, 0 );
529 #endif
530
531                 goto destroy;
532         }
533 #endif /* LDAP_SLAPI */
534
535         if ( overlay_init() ) {
536                 goto destroy;
537         }
538
539         if ( read_config( configfile, 0 ) != 0 ) {
540                 rc = 1;
541                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
542
543                 if ( check & CHECK_CONFIG ) {
544                         fprintf( stderr, "config check failed\n" );
545                 }
546
547                 goto destroy;
548         }
549
550         if ( check & CHECK_CONFIG ) {
551                 fprintf( stderr, "config check succeeded\n" );
552
553                 check &= ~CHECK_CONFIG;
554                 if ( check == CHECK_NONE ) {
555                         rc = 0;
556                         goto destroy;
557                 }
558         }
559
560         if ( glue_sub_init( ) != 0 ) {
561 #ifdef NEW_LOGGING
562                 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
563 #else
564                 Debug( LDAP_DEBUG_ANY,
565                     "subordinate config error\n",
566                     0, 0, 0 );
567 #endif
568                 goto destroy;
569         }
570
571         if ( slap_schema_check( ) != 0 ) {
572 #ifdef NEW_LOGGING
573                 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
574 #else
575                 Debug( LDAP_DEBUG_ANY,
576                     "schema prep error\n",
577                     0, 0, 0 );
578 #endif
579
580                 goto destroy;
581         }
582
583 #ifdef HAVE_TLS
584         rc = ldap_pvt_tls_init();
585         if( rc != 0) {
586 #ifdef NEW_LOGGING
587                 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
588 #else
589                 Debug( LDAP_DEBUG_ANY,
590                     "main: TLS init failed: %d\n",
591                     0, 0, 0 );
592 #endif
593                 rc = 1;
594                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
595                 goto destroy;
596         }
597
598         rc = ldap_pvt_tls_init_def_ctx();
599         if( rc != 0) {
600 #ifdef NEW_LOGGING
601                 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
602 #else
603                 Debug( LDAP_DEBUG_ANY,
604                     "main: TLS init def ctx failed: %d\n",
605                     rc, 0, 0 );
606 #endif
607                 rc = 1;
608                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
609                 goto destroy;
610         }
611 #endif
612
613         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
614         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
615
616 #ifdef SIGPIPE
617         (void) SIGNAL( SIGPIPE, SIG_IGN );
618 #endif
619 #ifdef SIGHUP
620         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
621 #endif
622         (void) SIGNAL( SIGINT, slap_sig_shutdown );
623         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
624 #ifdef LDAP_SIGCHLD
625         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
626 #endif
627 #ifdef SIGBREAK
628         /* SIGBREAK is generated when Ctrl-Break is pressed. */
629         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
630 #endif
631
632 #ifndef HAVE_WINSOCK
633         lutil_detach( no_detach, 0 );
634 #endif /* HAVE_WINSOCK */
635
636 #ifdef CSRIMALLOC
637         mal_leaktrace(1);
638 #endif
639
640         /*
641          * FIXME: moved here from slapd_daemon_task()
642          * because back-monitor db_open() needs it
643          */
644         time( &starttime );
645
646         if ( slap_startup( NULL )  != 0 ) {
647                 rc = 1;
648                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
649                 goto shutdown;
650         }
651
652 #ifdef NEW_LOGGING
653         LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
654 #else
655         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
656 #endif
657
658
659         if ( slapd_pid_file != NULL ) {
660                 FILE *fp = fopen( slapd_pid_file, "w" );
661
662                 if( fp != NULL ) {
663                         fprintf( fp, "%d\n", (int) getpid() );
664                         fclose( fp );
665
666                 } else {
667                         free(slapd_pid_file);
668                         slapd_pid_file = NULL;
669                 }
670         }
671
672         if ( slapd_args_file != NULL ) {
673                 FILE *fp = fopen( slapd_args_file, "w" );
674
675                 if( fp != NULL ) {
676                         for ( i = 0; i < g_argc; i++ ) {
677                                 fprintf( fp, "%s ", g_argv[i] );
678                         }
679                         fprintf( fp, "\n" );
680                         fclose( fp );
681                 } else {
682                         free(slapd_args_file);
683                         slapd_args_file = NULL;
684                 }
685         }
686
687 #ifdef HAVE_NT_EVENT_LOG
688         if (is_NT_Service)
689         lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
690 #endif
691
692         rc = slapd_daemon();
693
694 #ifdef HAVE_NT_SERVICE_MANAGER
695         /* Throw away the event that we used during the startup process. */
696         if ( is_NT_Service )
697                 ldap_pvt_thread_cond_destroy( &started_event );
698 #endif
699
700 shutdown:
701         /* remember an error during shutdown */
702         rc |= slap_shutdown( NULL );
703
704 destroy:
705         /* remember an error during destroy */
706         rc |= slap_destroy();
707
708         while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
709                 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
710                 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
711                 ch_free( scp );
712         }
713
714 #ifdef SLAPD_MODULES
715         module_kill();
716 #endif
717
718         slap_op_destroy();
719
720         extops_kill();
721
722 stop:
723 #ifdef HAVE_NT_EVENT_LOG
724         if (is_NT_Service)
725         lutil_LogStoppedEvent( serverName );
726 #endif
727
728 #ifdef NEW_LOGGING
729         LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
730 #else
731         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
732 #endif
733
734
735 #ifdef HAVE_NT_SERVICE_MANAGER
736         lutil_ReportShutdownComplete();
737 #endif
738
739 #ifdef LOG_DEBUG
740     closelog();
741 #endif
742         slapd_daemon_destroy();
743
744         controls_destroy();
745
746         schema_destroy();
747
748         lutil_passwd_destroy();
749
750 #ifdef HAVE_TLS
751         ldap_pvt_tls_destroy();
752 #endif
753
754         if ( slapd_pid_file != NULL ) {
755                 unlink( slapd_pid_file );
756         }
757         if ( slapd_args_file != NULL ) {
758                 unlink( slapd_args_file );
759         }
760
761         config_destroy();
762
763 #ifdef CSRIMALLOC
764         mal_dumpleaktrace( leakfile );
765 #endif
766
767         MAIN_RETURN(rc);
768 }
769
770
771 #ifdef LDAP_SIGCHLD
772
773 /*
774  *  Catch and discard terminated child processes, to avoid zombies.
775  */
776
777 static RETSIGTYPE
778 wait4child( int sig )
779 {
780     int save_errno = errno;
781
782 #ifdef WNOHANG
783     errno = 0;
784 #ifdef HAVE_WAITPID
785     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
786         ;       /* NULL */
787 #else
788     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
789         ;       /* NULL */
790 #endif
791 #else
792     (void) wait( NULL );
793 #endif
794     (void) SIGNAL_REINSTALL( sig, wait4child );
795     errno = save_errno;
796 }
797
798 #endif /* LDAP_SIGCHLD */
799
800
801 #ifdef LOG_LOCAL4
802
803 /*
804  *  Convert a string to an integer by means of a dispatcher table
805  *  if the string is not in the table return the default
806  */
807
808 static int
809 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
810 {
811     int        retVal = defaultVal;
812     STRDISP_P  disp;
813
814     for (disp = dispatcher; disp->stringVal; disp++) {
815
816         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
817
818             retVal = disp->intVal;
819             break;
820
821         }
822     }
823
824     return (retVal);
825 }
826
827 #endif  /* LOG_LOCAL4 */