3 * Copyright 1998-2002 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>
24 static RETSIGTYPE wait4child( int sig );
27 #ifdef HAVE_NT_SERVICE_MANAGER
28 #define MAIN_RETURN(x) return
29 static struct sockaddr_in bind_addr;
31 void CommenceStartupProcessing( LPCTSTR serverName,
33 void ReportSlapdShutdownComplete( void );
34 void *getRegParam( char *svc, char *value );
36 #define SERVICE_EXIT( e, n ) do { \
37 if ( is_NT_Service ) { \
38 SLAPDServiceStatus.dwWin32ExitCode = (e); \
39 SLAPDServiceStatus.dwServiceSpecificExitCode = (n); \
44 #define SERVICE_EXIT( e, n )
45 #define MAIN_RETURN(x) return(x)
48 #ifdef HAVE_NT_EVENT_LOG
49 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
50 void LogSlapdStoppedEvent( char *svc );
54 * when more than one slapd is running on one machine, each one might have
55 * it's own LOCAL for syslogging and must have its own pid/args files
58 #ifndef HAVE_MKVERSION
59 const char Versionstr[] =
60 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
65 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
67 typedef struct _str2intDispatch {
71 } STRDISP, *STRDISP_P;
74 /* table to compute syslog-options to integer */
75 static STRDISP syslog_types[] = {
76 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
77 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
78 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
79 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
80 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
81 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
82 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
83 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
87 static int cnvt_str2int( char *, STRDISP_P, int );
89 #endif /* LOG_LOCAL4 */
91 static int check_config = 0;
97 "usage: %s options\n", name );
99 "\t-d level\tDebug level" "\n"
100 "\t-f filename\tConfiguration file\n"
101 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
102 "\t-g group\tGroup (id or name) to run as\n"
104 "\t-h URLs\t\tList of URLs to serve\n"
106 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
108 "\t-n serverName\tService name\n"
110 "\t-r directory\tSandbox directory to chroot to\n"
112 "\t-s level\tSyslog level\n"
113 "\t-t\t\tCheck configuration file and exit\n"
114 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
115 "\t-u user\t\tUser (id or name) to run as\n"
120 #ifdef HAVE_NT_SERVICE_MANAGER
121 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
123 int main( int argc, char **argv )
126 int i, no_detach = 0;
129 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
130 char *username = NULL;
131 char *groupname = NULL;
133 #if defined(HAVE_CHROOT)
134 char *sandbox = NULL;
137 int syslogUser = DEFAULT_SYSLOG_USER;
141 char **g_argv = argv;
143 #ifdef HAVE_NT_SERVICE_MANAGER
144 char *configfile = ".\\slapd.conf";
146 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
148 char *serverName = NULL;
149 int serverMode = SLAP_SERVER_MODE;
153 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
159 #ifdef HAVE_NT_SERVICE_MANAGER
164 char *regService = NULL;
166 if ( is_NT_Service ) {
167 serverName = argv[0];
168 CommenceStartupProcessing( serverName, slap_sig_shutdown );
169 if ( strcmp(serverName, SERVICE_NAME) )
170 regService = serverName;
173 i = (int*)getRegParam( regService, "DebugLevel" );
178 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
179 "main: new debug level from registry is: %d\n", slap_debug ));
181 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
185 newUrls = (char *) getRegParam(regService, "Urls");
191 urls = ch_strdup(newUrls);
193 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
194 "main: new urls from registry: %s\n", urls ));
196 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
202 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
203 if ( newConfigFile != NULL )
205 configfile = newConfigFile;
207 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
208 "main: new config file from registry is: %s\n", configfile ));
210 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
217 while ( (i = getopt( argc, argv,
225 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
230 case 'h': /* listen URLs */
231 if ( urls != NULL ) free( urls );
232 urls = ch_strdup( optarg );
235 case 'd': /* set debug level and 'do not detach' flag */
238 slap_debug |= atoi( optarg );
240 if ( atoi( optarg ) != 0 )
241 fputs( "must compile with LDAP_DEBUG for debugging\n",
246 case 'f': /* read config file */
247 configfile = ch_strdup( optarg );
250 case 's': /* set syslog level */
251 ldap_syslog = atoi( optarg );
255 case 'l': /* set syslog local user */
256 syslogUser = cnvt_str2int( optarg,
257 syslog_types, DEFAULT_SYSLOG_USER );
263 if( sandbox ) free(sandbox);
264 sandbox = ch_strdup( optarg );
268 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
269 case 'u': /* user name */
270 if( username ) free(username);
271 username = ch_strdup( optarg );
274 case 'g': /* group name */
275 if( groupname ) free(groupname);
276 groupname = ch_strdup( optarg );
278 #endif /* SETUID && GETUID */
280 case 'n': /* NT service name */
281 if( serverName != NULL ) free( serverName );
282 serverName = ch_strdup( optarg );
292 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
298 lutil_log_initialize( argc, argv );
301 lutil_set_debug_level( "slapd", slap_debug );
302 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
303 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
304 ldif_debug = slap_debug;
307 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
310 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
314 if( serverName == NULL ) {
315 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
316 serverName = argv[0];
318 serverName = serverName + 1;
323 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
325 openlog( serverName, OPENLOG_OPTIONS );
328 if( !check_config && slapd_daemon_init( urls ) != 0 ) {
330 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
334 #if defined(HAVE_CHROOT)
336 if ( chdir( sandbox ) ) {
341 if ( chroot( sandbox ) ) {
349 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
350 if ( username != NULL || groupname != NULL ) {
351 slap_init_user( username, groupname );
358 if ( module_init() != 0 ) {
360 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
365 if ( slap_init( serverMode, serverName ) != 0 ) {
367 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
371 if ( slap_schema_init( ) != 0 ) {
373 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
374 "main: schema initialization error\n" ));
376 Debug( LDAP_DEBUG_ANY,
377 "schema initialization error\n",
385 /* Library defaults to full certificate checking. This is correct when
386 * a client is verifying a server because all servers should have a
387 * valid cert. But few clients have valid certs, so we want our default
388 * to be no checking. The config file can override this as usual.
391 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
394 if ( read_config( configfile ) != 0 ) {
396 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
398 if ( check_config ) {
399 fprintf( stderr, "config check failed\n" );
405 if ( check_config ) {
407 fprintf( stderr, "config check succeeded\n" );
411 if ( glue_sub_init( ) != 0 ) {
413 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
414 "main: subordinate config error\n"));
416 Debug( LDAP_DEBUG_ANY,
417 "subordinate config error\n",
423 if ( slap_schema_check( ) != 0 ) {
425 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
426 "main: schema prep error\n"));
428 Debug( LDAP_DEBUG_ANY,
429 "schema prep error\n",
437 rc = ldap_pvt_tls_init();
440 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
441 "main: tls init failed: %d\n", rc ));
443 Debug( LDAP_DEBUG_ANY,
444 "main: TLS init failed: %d\n",
448 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
452 rc = ldap_pvt_tls_init_def_ctx();
455 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
456 "main: tls init def ctx failed: %d\n", rc ));
458 Debug( LDAP_DEBUG_ANY,
459 "main: TLS init def ctx failed: %d\n",
463 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
468 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
469 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
472 (void) SIGNAL( SIGPIPE, SIG_IGN );
475 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
477 (void) SIGNAL( SIGINT, slap_sig_shutdown );
478 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
480 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
483 /* SIGBREAK is generated when Ctrl-Break is pressed. */
484 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
488 lutil_detach( no_detach, 0 );
489 #endif /* HAVE_WINSOCK */
495 if ( slap_startup( NULL ) != 0 ) {
497 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
505 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
506 "main: slapd starting.\n" ));
508 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
512 if (( slapd_pid_file != NULL ) &&
513 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
515 fprintf( fp, "%d\n", (int) getpid() );
519 if (( slapd_args_file != NULL ) &&
520 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
522 for ( i = 0; i < g_argc; i++ ) {
523 fprintf( fp, "%s ", g_argv[i] );
530 #ifdef HAVE_NT_EVENT_LOG
532 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
537 #ifdef HAVE_NT_SERVICE_MANAGER
538 /* Throw away the event that we used during the startup process. */
540 ldap_pvt_thread_cond_destroy( &started_event );
544 /* remember an error during shutdown */
545 rc |= slap_shutdown( NULL );
548 /* remember an error during destroy */
549 rc |= slap_destroy();
558 #ifdef HAVE_NT_EVENT_LOG
560 LogSlapdStoppedEvent( serverName );
564 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
565 "main: slapd stopped.\n" ));
567 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
571 #ifdef HAVE_NT_SERVICE_MANAGER
572 ReportSlapdShutdownComplete();
578 slapd_daemon_destroy();
583 ldap_pvt_tls_destroy();
589 mal_dumpleaktrace( leakfile );
599 * Catch and discard terminated child processes, to avoid zombies.
603 wait4child( int sig )
605 int save_errno = errno;
610 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
613 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
619 (void) SIGNAL_REINSTALL( sig, wait4child );
623 #endif /* SIGCHLD || SIGCLD */
629 * Convert a string to an integer by means of a dispatcher table
630 * if the string is not in the table return the default
634 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
636 int retVal = defaultVal;
639 for (disp = dispatcher; disp->stringVal; disp++) {
641 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
643 retVal = disp->intVal;
652 #endif /* LOG_LOCAL4 */