3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/socket.h>
11 #include <ac/string.h>
13 #include <ac/unistd.h>
28 static RETSIGTYPE wait4child( int sig );
31 #ifdef HAVE_NT_SERVICE_MANAGER
32 #define MAIN_RETURN(x) return
33 static struct sockaddr_in bind_addr;
35 void CommenceStartupProcessing( LPCTSTR serverName,
37 void ReportSlapdShutdownComplete( void );
38 void *getRegParam( char *svc, char *value );
40 #define SERVICE_EXIT( e, n ) do { \
41 if ( is_NT_Service ) { \
42 SLAPDServiceStatus.dwWin32ExitCode = (e); \
43 SLAPDServiceStatus.dwServiceSpecificExitCode = (n); \
48 #define SERVICE_EXIT( e, n )
49 #define MAIN_RETURN(x) return(x)
52 #ifdef HAVE_NT_EVENT_LOG
53 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
54 void LogSlapdStoppedEvent( char *svc );
58 * when more than one slapd is running on one machine, each one might have
59 * it's own LOCAL for syslogging and must have its own pid/args files
62 #ifndef HAVE_MKVERSION
63 const char Versionstr[] =
64 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
69 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
71 typedef struct _str2intDispatch {
75 } STRDISP, *STRDISP_P;
78 /* table to compute syslog-options to integer */
79 static STRDISP syslog_types[] = {
80 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
81 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
82 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
83 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
84 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
85 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
86 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
87 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
91 static int cnvt_str2int( char *, STRDISP_P, int );
93 #endif /* LOG_LOCAL4 */
95 static int check_config = 0;
101 "usage: %s options\n", name );
103 "\t-4\t\tIPv4 only\n"
104 "\t-6\t\tIPv6 only\n"
105 "\t-d level\tDebug level" "\n"
106 "\t-f filename\tConfiguration file\n"
107 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
108 "\t-g group\tGroup (id or name) to run as\n"
110 "\t-h URLs\t\tList of URLs to serve\n"
112 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
114 "\t-n serverName\tService name\n"
116 "\t-r directory\tSandbox directory to chroot to\n"
118 "\t-s level\tSyslog level\n"
119 "\t-t\t\tCheck configuration file and exit\n"
120 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
121 "\t-u user\t\tUser (id or name) to run as\n"
126 #ifdef HAVE_NT_SERVICE_MANAGER
127 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
129 int main( int argc, char **argv )
132 int i, no_detach = 0;
135 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
136 char *username = NULL;
137 char *groupname = NULL;
139 #if defined(HAVE_CHROOT)
140 char *sandbox = NULL;
143 int syslogUser = DEFAULT_SYSLOG_USER;
147 char **g_argv = argv;
149 #ifdef HAVE_NT_SERVICE_MANAGER
150 char *configfile = ".\\slapd.conf";
152 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
154 char *serverName = NULL;
155 int serverMode = SLAP_SERVER_MODE;
159 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
164 #ifdef HAVE_NT_SERVICE_MANAGER
169 char *regService = NULL;
171 if ( is_NT_Service ) {
172 serverName = argv[0];
173 CommenceStartupProcessing( serverName, slap_sig_shutdown );
174 if ( strcmp(serverName, SERVICE_NAME) )
175 regService = serverName;
178 i = (int*)getRegParam( regService, "DebugLevel" );
183 lutil_log_initialize( argc, argv );
184 LDAP_LOG( SLAPD, INFO,
185 "main: new debug level from registry is: %d\n",
188 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
192 newUrls = (char *) getRegParam(regService, "Urls");
198 urls = ch_strdup(newUrls);
200 LDAP_LOG( SLAPD, INFO,
201 "main: new urls from registry: %s\n", urls, 0, 0 );
203 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
209 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
210 if ( newConfigFile != NULL )
212 configfile = newConfigFile;
214 LDAP_LOG( SLAPD, INFO,
215 "main: new config file from registry is: %s\n", configfile, 0, 0 );
217 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
224 while ( (i = getopt( argc, argv,
235 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
242 slap_inet4or6 = AF_INET;
245 slap_inet4or6 = AF_INET6;
249 case 'h': /* listen URLs */
250 if ( urls != NULL ) free( urls );
251 urls = ch_strdup( optarg );
254 case 'd': /* set debug level and 'do not detach' flag */
257 slap_debug |= atoi( optarg );
259 if ( atoi( optarg ) != 0 )
260 fputs( "must compile with LDAP_DEBUG for debugging\n",
265 case 'f': /* read config file */
266 configfile = ch_strdup( optarg );
269 case 's': /* set syslog level */
270 ldap_syslog = atoi( optarg );
274 case 'l': /* set syslog local user */
275 syslogUser = cnvt_str2int( optarg,
276 syslog_types, DEFAULT_SYSLOG_USER );
282 if( sandbox ) free(sandbox);
283 sandbox = ch_strdup( optarg );
287 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
288 case 'u': /* user name */
289 if( username ) free(username);
290 username = ch_strdup( optarg );
293 case 'g': /* group name */
294 if( groupname ) free(groupname);
295 groupname = ch_strdup( optarg );
297 #endif /* SETUID && GETUID */
299 case 'n': /* NT service name */
300 if( serverName != NULL ) free( serverName );
301 serverName = ch_strdup( optarg );
311 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
317 lutil_log_initialize( argc, argv );
319 lutil_set_debug_level( "slapd", slap_debug );
320 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
321 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
322 ldif_debug = slap_debug;
326 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
328 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
331 if( serverName == NULL ) {
332 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
333 serverName = argv[0];
335 serverName = serverName + 1;
340 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
342 openlog( serverName, OPENLOG_OPTIONS );
345 if( !check_config && slapd_daemon_init( urls ) != 0 ) {
347 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
351 #if defined(HAVE_CHROOT)
353 if ( chdir( sandbox ) ) {
358 if ( chroot( sandbox ) ) {
366 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
367 if ( username != NULL || groupname != NULL ) {
368 slap_init_user( username, groupname );
375 if ( module_init() != 0 ) {
377 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
382 if ( slap_init( serverMode, serverName ) != 0 ) {
384 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
388 if ( slap_schema_init( ) != 0 ) {
390 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
392 Debug( LDAP_DEBUG_ANY,
393 "schema initialization error\n",
401 /* Library defaults to full certificate checking. This is correct when
402 * a client is verifying a server because all servers should have a
403 * valid cert. But few clients have valid certs, so we want our default
404 * to be no checking. The config file can override this as usual.
407 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
411 if ( slapi_init() != 0 ) {
413 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
415 Debug( LDAP_DEBUG_ANY,
416 "slapi initialization error\n",
422 #endif /* LDAP_SLAPI */
424 if ( read_config( configfile, 0 ) != 0 ) {
426 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
428 if ( check_config ) {
429 fprintf( stderr, "config check failed\n" );
435 if ( check_config ) {
437 fprintf( stderr, "config check succeeded\n" );
441 if ( glue_sub_init( ) != 0 ) {
443 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
445 Debug( LDAP_DEBUG_ANY,
446 "subordinate config error\n",
452 if ( slap_schema_check( ) != 0 ) {
454 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
456 Debug( LDAP_DEBUG_ANY,
457 "schema prep error\n",
465 rc = ldap_pvt_tls_init();
468 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
470 Debug( LDAP_DEBUG_ANY,
471 "main: TLS init failed: %d\n",
475 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
479 rc = ldap_pvt_tls_init_def_ctx();
482 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
484 Debug( LDAP_DEBUG_ANY,
485 "main: TLS init def ctx failed: %d\n",
489 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
494 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
495 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
498 (void) SIGNAL( SIGPIPE, SIG_IGN );
501 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
503 (void) SIGNAL( SIGINT, slap_sig_shutdown );
504 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
506 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
509 /* SIGBREAK is generated when Ctrl-Break is pressed. */
510 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
514 lutil_detach( no_detach, 0 );
515 #endif /* HAVE_WINSOCK */
521 if ( slap_startup( NULL ) != 0 ) {
523 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
531 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
533 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
537 if (( slapd_pid_file != NULL ) &&
538 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
540 fprintf( fp, "%d\n", (int) getpid() );
544 if (( slapd_args_file != NULL ) &&
545 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
547 for ( i = 0; i < g_argc; i++ ) {
548 fprintf( fp, "%s ", g_argv[i] );
555 #ifdef HAVE_NT_EVENT_LOG
557 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
562 #ifdef HAVE_NT_SERVICE_MANAGER
563 /* Throw away the event that we used during the startup process. */
565 ldap_pvt_thread_cond_destroy( &started_event );
569 /* remember an error during shutdown */
570 rc |= slap_shutdown( NULL );
573 /* remember an error during destroy */
574 rc |= slap_destroy();
583 #ifdef HAVE_NT_EVENT_LOG
585 LogSlapdStoppedEvent( serverName );
589 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
591 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
595 #ifdef HAVE_NT_SERVICE_MANAGER
596 ReportSlapdShutdownComplete();
602 slapd_daemon_destroy();
607 ldap_pvt_tls_destroy();
613 mal_dumpleaktrace( leakfile );
623 * Catch and discard terminated child processes, to avoid zombies.
627 wait4child( int sig )
629 int save_errno = errno;
634 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
637 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
643 (void) SIGNAL_REINSTALL( sig, wait4child );
647 #endif /* LDAP_SIGCHLD */
653 * Convert a string to an integer by means of a dispatcher table
654 * if the string is not in the table return the default
658 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
660 int retVal = defaultVal;
663 for (disp = dispatcher; disp->stringVal; disp++) {
665 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
667 retVal = disp->intVal;
676 #endif /* LOG_LOCAL4 */