2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
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.
30 #include <ac/socket.h>
31 #include <ac/string.h>
33 #include <ac/unistd.h>
44 #include "slapi/slapi.h"
48 static RETSIGTYPE wait4child( int sig );
51 #ifdef HAVE_NT_SERVICE_MANAGER
52 #define MAIN_RETURN(x) return
53 static struct sockaddr_in bind_addr;
55 #define SERVICE_EXIT( e, n ) do { \
56 if ( is_NT_Service ) { \
57 lutil_ServiceStatus.dwWin32ExitCode = (e); \
58 lutil_ServiceStatus.dwServiceSpecificExitCode = (n); \
63 #define SERVICE_EXIT( e, n )
64 #define MAIN_RETURN(x) return(x)
67 typedef int (MainFunc) LDAP_P(( int argc, char *argv[] ));
68 extern MainFunc slapadd, slapcat, slapindex, slappasswd;
76 {"slapindex", slapindex},
77 {"slappasswd", slappasswd},
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
86 #ifndef HAVE_MKVERSION
87 const char Versionstr[] =
88 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
93 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
95 typedef struct _str2intDispatch {
99 } STRDISP, *STRDISP_P;
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 },
115 static int cnvt_str2int( char *, STRDISP_P, int );
117 #endif /* LOG_LOCAL4 */
119 static int check_config = 0;
120 static int version = 0;
126 "usage: %s options\n", name );
128 "\t-4\t\tIPv4 only\n"
129 "\t-6\t\tIPv6 only\n"
130 "\t-T (a|c|i|p)\tRun in Tool mode\n"
131 "\t-c cookie\tSync cookie of consumer\n"
132 "\t-d level\tDebug level" "\n"
133 "\t-f filename\tConfiguration file\n"
134 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
135 "\t-g group\tGroup (id or name) to run as\n"
137 "\t-h URLs\t\tList of URLs to serve\n"
139 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
141 "\t-n serverName\tService name\n"
143 "\t-r directory\tSandbox directory to chroot to\n"
145 "\t-s level\tSyslog level\n"
146 "\t-t\t\tCheck configuration file and exit\n"
147 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
148 "\t-u user\t\tUser (id or name) to run as\n"
149 "\t-V\t\tprint version info (-VV only)\n"
154 #ifdef HAVE_NT_SERVICE_MANAGER
155 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
157 int main( int argc, char **argv )
160 int i, no_detach = 0;
163 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
164 char *username = NULL;
165 char *groupname = NULL;
167 #if defined(HAVE_CHROOT)
168 char *sandbox = NULL;
171 int syslogUser = DEFAULT_SYSLOG_USER;
175 char **g_argv = argv;
177 #ifdef HAVE_NT_SERVICE_MANAGER
178 char *configfile = ".\\slapd.conf";
180 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
183 int serverMode = SLAP_SERVER_MODE;
185 struct berval cookie = { 0, NULL };
186 struct sync_cookie *scp = NULL;
187 struct sync_cookie *scp_entry = NULL;
191 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
198 serverName = lutil_progname( "slapd", argc, argv );
200 if ( strcmp( serverName, "slapd" ) ) {
201 for (i=0; tools[i].name; i++) {
202 if ( !strcmp( serverName, tools[i].name ) ) {
203 rc = tools[i].func(argc, argv);
209 #ifdef HAVE_NT_SERVICE_MANAGER
214 char *regService = NULL;
216 if ( is_NT_Service ) {
217 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
218 if ( strcmp(serverName, SERVICE_NAME) )
219 regService = serverName;
222 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
227 lutil_log_initialize( argc, argv );
228 LDAP_LOG( SLAPD, INFO,
229 "main: new debug level from registry is: %d\n",
232 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
236 newUrls = (char *) lutil_getRegParam(regService, "Urls");
242 urls = ch_strdup(newUrls);
244 LDAP_LOG( SLAPD, INFO,
245 "main: new urls from registry: %s\n", urls, 0, 0 );
247 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
253 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
254 if ( newConfigFile != NULL )
256 configfile = newConfigFile;
258 LDAP_LOG( SLAPD, INFO,
259 "main: new config file from registry is: %s\n", configfile, 0, 0 );
261 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
268 while ( (i = getopt( argc, argv,
279 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
286 slap_inet4or6 = AF_INET;
289 slap_inet4or6 = AF_INET6;
293 case 'h': /* listen URLs */
294 if ( urls != NULL ) free( urls );
295 urls = ch_strdup( optarg );
298 case 'c': /* provide sync cookie, override if exist in replica */
299 scp = (struct sync_cookie *) ch_calloc( 1,
300 sizeof( struct sync_cookie ));
301 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
302 ber_bvarray_add( &scp->octet_str, &cookie );
303 slap_parse_sync_cookie( scp );
305 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
306 if ( scp->rid == scp_entry->rid ) {
308 LDAP_LOG( OPERATION, CRIT,
309 "main: duplicated replica id in cookies\n",
312 Debug( LDAP_DEBUG_ANY,
313 "main: duplicated replica id in cookies\n",
316 slap_sync_cookie_free( scp, 1 );
320 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
323 case 'd': /* set debug level and 'do not detach' flag */
326 slap_debug |= atoi( optarg );
328 if ( atoi( optarg ) != 0 )
329 fputs( "must compile with LDAP_DEBUG for debugging\n",
334 case 'f': /* read config file */
335 configfile = ch_strdup( optarg );
338 case 's': /* set syslog level */
339 ldap_syslog = atoi( optarg );
343 case 'l': /* set syslog local user */
344 syslogUser = cnvt_str2int( optarg,
345 syslog_types, DEFAULT_SYSLOG_USER );
351 if( sandbox ) free(sandbox);
352 sandbox = ch_strdup( optarg );
356 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
357 case 'u': /* user name */
358 if( username ) free(username);
359 username = ch_strdup( optarg );
362 case 'g': /* group name */
363 if( groupname ) free(groupname);
364 groupname = ch_strdup( optarg );
366 #endif /* SETUID && GETUID */
368 case 'n': /* NT service name */
369 serverName = ch_strdup( optarg );
380 for (i=0; tools[i].name; i++) {
381 if ( optarg[0] == tools[i].name[4] ) {
382 rc = tools[i].func(argc, argv);
390 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
396 lutil_log_initialize( argc, argv );
398 lutil_set_debug_level( "slapd", slap_debug );
399 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
400 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
401 ldif_debug = slap_debug;
405 fprintf( stderr, "%s\n", Versionstr );
406 if ( version > 1 ) goto stop;
412 logName = ch_strdup( serverName );
415 logName = serverName;
419 openlog( logName, OPENLOG_OPTIONS, syslogUser );
421 openlog( logName, OPENLOG_OPTIONS );
429 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
431 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
434 if( !check_config && slapd_daemon_init( urls ) != 0 ) {
436 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
440 #if defined(HAVE_CHROOT)
442 if ( chdir( sandbox ) ) {
447 if ( chroot( sandbox ) ) {
455 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
456 if ( username != NULL || groupname != NULL ) {
457 slap_init_user( username, groupname );
466 if ( module_init() != 0 ) {
468 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
473 if ( slap_schema_init( ) != 0 ) {
475 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
477 Debug( LDAP_DEBUG_ANY,
478 "schema initialization error\n",
485 if ( slap_init( serverMode, serverName ) != 0 ) {
487 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
491 if ( slap_controls_init( ) != 0 ) {
493 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
495 Debug( LDAP_DEBUG_ANY,
496 "controls initialization error\n",
504 /* Library defaults to full certificate checking. This is correct when
505 * a client is verifying a server because all servers should have a
506 * valid cert. But few clients have valid certs, so we want our default
507 * to be no checking. The config file can override this as usual.
510 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
514 if ( slapi_int_initialize() != 0 ) {
516 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
518 Debug( LDAP_DEBUG_ANY,
519 "slapi initialization error\n",
525 #endif /* LDAP_SLAPI */
527 if ( overlay_init() ) {
531 if ( read_config( configfile, 0 ) != 0 ) {
533 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
535 if ( check_config ) {
536 fprintf( stderr, "config check failed\n" );
542 if ( check_config ) {
544 fprintf( stderr, "config check succeeded\n" );
548 if ( glue_sub_init( ) != 0 ) {
550 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
552 Debug( LDAP_DEBUG_ANY,
553 "subordinate config error\n",
559 if ( slap_schema_check( ) != 0 ) {
561 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
563 Debug( LDAP_DEBUG_ANY,
564 "schema prep error\n",
572 rc = ldap_pvt_tls_init();
575 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
577 Debug( LDAP_DEBUG_ANY,
578 "main: TLS init failed: %d\n",
582 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
586 rc = ldap_pvt_tls_init_def_ctx();
589 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
591 Debug( LDAP_DEBUG_ANY,
592 "main: TLS init def ctx failed: %d\n",
596 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
601 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
602 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
605 (void) SIGNAL( SIGPIPE, SIG_IGN );
608 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
610 (void) SIGNAL( SIGINT, slap_sig_shutdown );
611 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
613 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
616 /* SIGBREAK is generated when Ctrl-Break is pressed. */
617 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
621 lutil_detach( no_detach, 0 );
622 #endif /* HAVE_WINSOCK */
629 * FIXME: moved here from slapd_daemon_task()
630 * because back-monitor db_open() needs it
634 if ( slap_startup( NULL ) != 0 ) {
636 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
641 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
643 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
647 if ( slapd_pid_file != NULL ) {
648 FILE *fp = fopen( slapd_pid_file, "w" );
651 fprintf( fp, "%d\n", (int) getpid() );
655 free(slapd_pid_file);
656 slapd_pid_file = NULL;
660 if ( slapd_args_file != NULL ) {
661 FILE *fp = fopen( slapd_args_file, "w" );
664 for ( i = 0; i < g_argc; i++ ) {
665 fprintf( fp, "%s ", g_argv[i] );
670 free(slapd_args_file);
671 slapd_args_file = NULL;
675 #ifdef HAVE_NT_EVENT_LOG
677 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
682 #ifdef HAVE_NT_SERVICE_MANAGER
683 /* Throw away the event that we used during the startup process. */
685 ldap_pvt_thread_cond_destroy( &started_event );
689 /* remember an error during shutdown */
690 rc |= slap_shutdown( NULL );
693 /* remember an error during destroy */
694 rc |= slap_destroy();
696 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
697 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
698 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
711 #ifdef HAVE_NT_EVENT_LOG
713 lutil_LogStoppedEvent( serverName );
717 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
719 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
723 #ifdef HAVE_NT_SERVICE_MANAGER
724 lutil_ReportShutdownComplete();
730 slapd_daemon_destroy();
736 lutil_passwd_destroy();
739 ldap_pvt_tls_destroy();
742 if ( slapd_pid_file != NULL ) {
743 unlink( slapd_pid_file );
745 if ( slapd_args_file != NULL ) {
746 unlink( slapd_args_file );
752 mal_dumpleaktrace( leakfile );
762 * Catch and discard terminated child processes, to avoid zombies.
766 wait4child( int sig )
768 int save_errno = errno;
773 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
776 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
782 (void) SIGNAL_REINSTALL( sig, wait4child );
786 #endif /* LDAP_SIGCHLD */
792 * Convert a string to an integer by means of a dispatcher table
793 * if the string is not in the table return the default
797 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
799 int retVal = defaultVal;
802 for (disp = dispatcher; disp->stringVal; disp++) {
804 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
806 retVal = disp->intVal;
815 #endif /* LOG_LOCAL4 */