3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 #include "slapi_common.h"
11 #include <ac/socket.h>
12 #include <ac/string.h>
14 #include <ac/unistd.h>
27 static RETSIGTYPE wait4child( int sig );
30 #ifdef HAVE_NT_SERVICE_MANAGER
31 #define MAIN_RETURN(x) return
32 static struct sockaddr_in bind_addr;
34 void CommenceStartupProcessing( LPCTSTR serverName,
36 void ReportSlapdShutdownComplete( void );
37 void *getRegParam( char *svc, char *value );
39 #define SERVICE_EXIT( e, n ) do { \
40 if ( is_NT_Service ) { \
41 SLAPDServiceStatus.dwWin32ExitCode = (e); \
42 SLAPDServiceStatus.dwServiceSpecificExitCode = (n); \
47 #define SERVICE_EXIT( e, n )
48 #define MAIN_RETURN(x) return(x)
51 #ifdef HAVE_NT_EVENT_LOG
52 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
53 void LogSlapdStoppedEvent( char *svc );
57 * when more than one slapd is running on one machine, each one might have
58 * it's own LOCAL for syslogging and must have its own pid/args files
61 #ifndef HAVE_MKVERSION
62 const char Versionstr[] =
63 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
68 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
70 typedef struct _str2intDispatch {
74 } STRDISP, *STRDISP_P;
77 /* table to compute syslog-options to integer */
78 static STRDISP syslog_types[] = {
79 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
80 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
81 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
82 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
83 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
84 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
85 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
86 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
90 static int cnvt_str2int( char *, STRDISP_P, int );
92 #endif /* LOG_LOCAL4 */
94 static int check_config = 0;
100 "usage: %s options\n", name );
102 "\t-4\t\tIPv4 only\n"
103 "\t-6\t\tIPv6 only\n"
104 "\t-d level\tDebug level" "\n"
105 "\t-f filename\tConfiguration file\n"
106 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
107 "\t-g group\tGroup (id or name) to run as\n"
109 "\t-h URLs\t\tList of URLs to serve\n"
111 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
113 "\t-n serverName\tService name\n"
115 "\t-r directory\tSandbox directory to chroot to\n"
117 "\t-s level\tSyslog level\n"
118 "\t-t\t\tCheck configuration file and exit\n"
119 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
120 "\t-u user\t\tUser (id or name) to run as\n"
125 #ifdef HAVE_NT_SERVICE_MANAGER
126 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
128 int main( int argc, char **argv )
131 int i, no_detach = 0;
134 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
135 char *username = NULL;
136 char *groupname = NULL;
138 #if defined(HAVE_CHROOT)
139 char *sandbox = NULL;
142 int syslogUser = DEFAULT_SYSLOG_USER;
146 char **g_argv = argv;
148 #ifdef HAVE_NT_SERVICE_MANAGER
149 char *configfile = ".\\slapd.conf";
151 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
153 char *serverName = NULL;
154 int serverMode = SLAP_SERVER_MODE;
158 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 );
332 if( serverName == NULL ) {
333 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
334 serverName = argv[0];
336 serverName = serverName + 1;
341 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
343 openlog( serverName, OPENLOG_OPTIONS );
346 if( !check_config && slapd_daemon_init( urls ) != 0 ) {
348 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
352 #if defined(HAVE_CHROOT)
354 if ( chdir( sandbox ) ) {
359 if ( chroot( sandbox ) ) {
367 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
368 if ( username != NULL || groupname != NULL ) {
369 slap_init_user( username, groupname );
376 if ( module_init() != 0 ) {
378 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
383 if ( slap_init( serverMode, serverName ) != 0 ) {
385 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
389 if ( slap_schema_init( ) != 0 ) {
391 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
393 Debug( LDAP_DEBUG_ANY,
394 "schema initialization error\n",
402 /* Library defaults to full certificate checking. This is correct when
403 * a client is verifying a server because all servers should have a
404 * valid cert. But few clients have valid certs, so we want our default
405 * to be no checking. The config file can override this as usual.
408 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
412 if ( slapi_init() != 0 ) {
414 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
416 Debug( LDAP_DEBUG_ANY,
417 "slapi initialization error\n",
423 #endif /* LDAP_SLAPI */
425 if ( read_config( configfile, 0 ) != 0 ) {
427 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
429 if ( check_config ) {
430 fprintf( stderr, "config check failed\n" );
436 if ( check_config ) {
438 fprintf( stderr, "config check succeeded\n" );
442 if ( glue_sub_init( ) != 0 ) {
444 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
446 Debug( LDAP_DEBUG_ANY,
447 "subordinate config error\n",
453 if ( slap_schema_check( ) != 0 ) {
455 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
457 Debug( LDAP_DEBUG_ANY,
458 "schema prep error\n",
466 rc = ldap_pvt_tls_init();
469 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
471 Debug( LDAP_DEBUG_ANY,
472 "main: TLS init failed: %d\n",
476 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
480 rc = ldap_pvt_tls_init_def_ctx();
483 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
485 Debug( LDAP_DEBUG_ANY,
486 "main: TLS init def ctx failed: %d\n",
490 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
495 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
496 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
499 (void) SIGNAL( SIGPIPE, SIG_IGN );
502 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
504 (void) SIGNAL( SIGINT, slap_sig_shutdown );
505 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
507 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
510 /* SIGBREAK is generated when Ctrl-Break is pressed. */
511 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
515 lutil_detach( no_detach, 0 );
516 #endif /* HAVE_WINSOCK */
522 if ( slap_startup( NULL ) != 0 ) {
524 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
532 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
534 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
538 if (( slapd_pid_file != NULL ) &&
539 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
541 fprintf( fp, "%d\n", (int) getpid() );
545 if (( slapd_args_file != NULL ) &&
546 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
548 for ( i = 0; i < g_argc; i++ ) {
549 fprintf( fp, "%s ", g_argv[i] );
556 #ifdef HAVE_NT_EVENT_LOG
558 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
563 #ifdef HAVE_NT_SERVICE_MANAGER
564 /* Throw away the event that we used during the startup process. */
566 ldap_pvt_thread_cond_destroy( &started_event );
570 /* remember an error during shutdown */
571 rc |= slap_shutdown( NULL );
574 /* remember an error during destroy */
575 rc |= slap_destroy();
584 #ifdef HAVE_NT_EVENT_LOG
586 LogSlapdStoppedEvent( serverName );
590 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
592 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
596 #ifdef HAVE_NT_SERVICE_MANAGER
597 ReportSlapdShutdownComplete();
603 slapd_daemon_destroy();
608 ldap_pvt_tls_destroy();
614 mal_dumpleaktrace( leakfile );
624 * Catch and discard terminated child processes, to avoid zombies.
628 wait4child( int sig )
630 int save_errno = errno;
635 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
638 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
644 (void) SIGNAL_REINSTALL( sig, wait4child );
648 #endif /* LDAP_SIGCHLD */
654 * Convert a string to an integer by means of a dispatcher table
655 * if the string is not in the table return the default
659 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
661 int retVal = defaultVal;
664 for (disp = dispatcher; disp->stringVal; disp++) {
666 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
668 retVal = disp->intVal;
677 #endif /* LOG_LOCAL4 */