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