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