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 );
100 "\t-6\t\tIPv6 only\n"
101 "\t-d level\tDebug level" "\n"
102 "\t-f filename\tConfiguration file\n"
103 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
104 "\t-g group\tGroup (id or name) to run as\n"
106 "\t-h URLs\t\tList of URLs to serve\n"
108 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
110 "\t-n serverName\tService name\n"
112 "\t-r directory\tSandbox directory to chroot to\n"
114 "\t-s level\tSyslog level\n"
115 "\t-t\t\tCheck configuration file and exit\n"
116 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
117 "\t-u user\t\tUser (id or name) to run as\n"
122 #ifdef HAVE_NT_SERVICE_MANAGER
123 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
125 int main( int argc, char **argv )
128 int i, no_detach = 0;
131 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
132 char *username = NULL;
133 char *groupname = NULL;
135 #if defined(HAVE_CHROOT)
136 char *sandbox = NULL;
139 int syslogUser = DEFAULT_SYSLOG_USER;
143 char **g_argv = argv;
145 #ifdef HAVE_NT_SERVICE_MANAGER
146 char *configfile = ".\\slapd.conf";
148 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
150 char *serverName = NULL;
151 int serverMode = SLAP_SERVER_MODE;
155 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
161 #ifdef HAVE_NT_SERVICE_MANAGER
166 char *regService = NULL;
168 if ( is_NT_Service ) {
169 serverName = argv[0];
170 CommenceStartupProcessing( serverName, slap_sig_shutdown );
171 if ( strcmp(serverName, SERVICE_NAME) )
172 regService = serverName;
175 i = (int*)getRegParam( regService, "DebugLevel" );
180 lutil_log_initialize( argc, argv );
181 LDAP_LOG( SLAPD, INFO,
182 "main: new debug level from registry is: %d\n",
185 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
189 newUrls = (char *) getRegParam(regService, "Urls");
195 urls = ch_strdup(newUrls);
197 LDAP_LOG( SLAPD, INFO,
198 "main: new urls from registry: %s\n", urls, 0, 0 );
200 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
206 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
207 if ( newConfigFile != NULL )
209 configfile = newConfigFile;
211 LDAP_LOG( SLAPD, INFO,
212 "main: new config file from registry is: %s\n", configfile, 0, 0 );
214 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
221 while ( (i = getopt( argc, argv,
232 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
239 slap_inet4or6 = AF_INET;
242 slap_inet4or6 = AF_INET6;
246 case 'h': /* listen URLs */
247 if ( urls != NULL ) free( urls );
248 urls = ch_strdup( optarg );
251 case 'd': /* set debug level and 'do not detach' flag */
254 slap_debug |= atoi( optarg );
256 if ( atoi( optarg ) != 0 )
257 fputs( "must compile with LDAP_DEBUG for debugging\n",
262 case 'f': /* read config file */
263 configfile = ch_strdup( optarg );
266 case 's': /* set syslog level */
267 ldap_syslog = atoi( optarg );
271 case 'l': /* set syslog local user */
272 syslogUser = cnvt_str2int( optarg,
273 syslog_types, DEFAULT_SYSLOG_USER );
279 if( sandbox ) free(sandbox);
280 sandbox = ch_strdup( optarg );
284 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
285 case 'u': /* user name */
286 if( username ) free(username);
287 username = ch_strdup( optarg );
290 case 'g': /* group name */
291 if( groupname ) free(groupname);
292 groupname = ch_strdup( optarg );
294 #endif /* SETUID && GETUID */
296 case 'n': /* NT service name */
297 if( serverName != NULL ) free( serverName );
298 serverName = ch_strdup( optarg );
308 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
314 lutil_log_initialize( argc, argv );
316 lutil_set_debug_level( "slapd", slap_debug );
317 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
318 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
319 ldif_debug = slap_debug;
323 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
325 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
329 if( serverName == NULL ) {
330 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
331 serverName = argv[0];
333 serverName = serverName + 1;
338 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
340 openlog( serverName, OPENLOG_OPTIONS );
343 if( !check_config && slapd_daemon_init( urls ) != 0 ) {
345 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
349 #if defined(HAVE_CHROOT)
351 if ( chdir( sandbox ) ) {
356 if ( chroot( sandbox ) ) {
364 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
365 if ( username != NULL || groupname != NULL ) {
366 slap_init_user( username, groupname );
373 if ( module_init() != 0 ) {
375 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
380 if ( slap_init( serverMode, serverName ) != 0 ) {
382 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
386 if ( slap_schema_init( ) != 0 ) {
388 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
390 Debug( LDAP_DEBUG_ANY,
391 "schema initialization error\n",
399 /* Library defaults to full certificate checking. This is correct when
400 * a client is verifying a server because all servers should have a
401 * valid cert. But few clients have valid certs, so we want our default
402 * to be no checking. The config file can override this as usual.
405 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
408 if ( read_config( configfile, 0 ) != 0 ) {
410 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
412 if ( check_config ) {
413 fprintf( stderr, "config check failed\n" );
419 if ( check_config ) {
421 fprintf( stderr, "config check succeeded\n" );
425 if ( glue_sub_init( ) != 0 ) {
427 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
429 Debug( LDAP_DEBUG_ANY,
430 "subordinate config error\n",
436 if ( slap_schema_check( ) != 0 ) {
438 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
440 Debug( LDAP_DEBUG_ANY,
441 "schema prep error\n",
449 rc = ldap_pvt_tls_init();
452 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
454 Debug( LDAP_DEBUG_ANY,
455 "main: TLS init failed: %d\n",
459 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
463 rc = ldap_pvt_tls_init_def_ctx();
466 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
468 Debug( LDAP_DEBUG_ANY,
469 "main: TLS init def ctx failed: %d\n",
473 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
478 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
479 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
482 (void) SIGNAL( SIGPIPE, SIG_IGN );
485 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
487 (void) SIGNAL( SIGINT, slap_sig_shutdown );
488 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
490 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
493 /* SIGBREAK is generated when Ctrl-Break is pressed. */
494 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
498 lutil_detach( no_detach, 0 );
499 #endif /* HAVE_WINSOCK */
505 if ( slap_startup( NULL ) != 0 ) {
507 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
515 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
517 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
521 if (( slapd_pid_file != NULL ) &&
522 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
524 fprintf( fp, "%d\n", (int) getpid() );
528 if (( slapd_args_file != NULL ) &&
529 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
531 for ( i = 0; i < g_argc; i++ ) {
532 fprintf( fp, "%s ", g_argv[i] );
539 #ifdef HAVE_NT_EVENT_LOG
541 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
546 #ifdef HAVE_NT_SERVICE_MANAGER
547 /* Throw away the event that we used during the startup process. */
549 ldap_pvt_thread_cond_destroy( &started_event );
553 /* remember an error during shutdown */
554 rc |= slap_shutdown( NULL );
557 /* remember an error during destroy */
558 rc |= slap_destroy();
567 #ifdef HAVE_NT_EVENT_LOG
569 LogSlapdStoppedEvent( serverName );
573 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
575 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
579 #ifdef HAVE_NT_SERVICE_MANAGER
580 ReportSlapdShutdownComplete();
586 slapd_daemon_destroy();
591 ldap_pvt_tls_destroy();
597 mal_dumpleaktrace( leakfile );
607 * Catch and discard terminated child processes, to avoid zombies.
611 wait4child( int sig )
613 int save_errno = errno;
618 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
621 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
627 (void) SIGNAL_REINSTALL( sig, wait4child );
631 #endif /* LDAP_SIGCHLD */
637 * Convert a string to an integer by means of a dispatcher table
638 * if the string is not in the table return the default
642 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
644 int retVal = defaultVal;
647 for (disp = dispatcher; disp->stringVal; disp++) {
649 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
651 retVal = disp->intVal;
660 #endif /* LOG_LOCAL4 */