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 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
181 "main: new debug level from registry is: %d\n", slap_debug ));
183 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
187 newUrls = (char *) getRegParam(regService, "Urls");
193 urls = ch_strdup(newUrls);
195 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
196 "main: new urls from registry: %s\n", urls ));
198 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
204 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
205 if ( newConfigFile != NULL )
207 configfile = newConfigFile;
209 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
210 "main: new config file from registry is: %s\n", configfile ));
212 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
219 while ( (i = getopt( argc, argv,
230 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
237 slap_inet4or6 = AF_INET;
240 slap_inet4or6 = AF_INET6;
244 case 'h': /* listen URLs */
245 if ( urls != NULL ) free( urls );
246 urls = ch_strdup( optarg );
249 case 'd': /* set debug level and 'do not detach' flag */
252 slap_debug |= atoi( optarg );
254 if ( atoi( optarg ) != 0 )
255 fputs( "must compile with LDAP_DEBUG for debugging\n",
260 case 'f': /* read config file */
261 configfile = ch_strdup( optarg );
264 case 's': /* set syslog level */
265 ldap_syslog = atoi( optarg );
269 case 'l': /* set syslog local user */
270 syslogUser = cnvt_str2int( optarg,
271 syslog_types, DEFAULT_SYSLOG_USER );
277 if( sandbox ) free(sandbox);
278 sandbox = ch_strdup( optarg );
282 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
283 case 'u': /* user name */
284 if( username ) free(username);
285 username = ch_strdup( optarg );
288 case 'g': /* group name */
289 if( groupname ) free(groupname);
290 groupname = ch_strdup( optarg );
292 #endif /* SETUID && GETUID */
294 case 'n': /* NT service name */
295 if( serverName != NULL ) free( serverName );
296 serverName = ch_strdup( optarg );
306 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
312 lutil_log_initialize( argc, argv );
315 lutil_set_debug_level( "slapd", slap_debug );
316 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
317 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
318 ldif_debug = slap_debug;
321 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
324 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
328 if( serverName == NULL ) {
329 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
330 serverName = argv[0];
332 serverName = serverName + 1;
337 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
339 openlog( serverName, OPENLOG_OPTIONS );
342 if( !check_config && slapd_daemon_init( urls ) != 0 ) {
344 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
348 #if defined(HAVE_CHROOT)
350 if ( chdir( sandbox ) ) {
355 if ( chroot( sandbox ) ) {
363 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
364 if ( username != NULL || groupname != NULL ) {
365 slap_init_user( username, groupname );
372 if ( module_init() != 0 ) {
374 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
379 if ( slap_init( serverMode, serverName ) != 0 ) {
381 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
385 if ( slap_schema_init( ) != 0 ) {
387 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
388 "main: schema initialization error\n" ));
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 ) {
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(( "operation", LDAP_LEVEL_CRIT,
428 "main: subordinate config error\n"));
430 Debug( LDAP_DEBUG_ANY,
431 "subordinate config error\n",
437 if ( slap_schema_check( ) != 0 ) {
439 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
440 "main: schema prep error\n"));
442 Debug( LDAP_DEBUG_ANY,
443 "schema prep error\n",
451 rc = ldap_pvt_tls_init();
454 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
455 "main: tls init failed: %d\n", rc ));
457 Debug( LDAP_DEBUG_ANY,
458 "main: TLS init failed: %d\n",
462 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
466 rc = ldap_pvt_tls_init_def_ctx();
469 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
470 "main: tls init def ctx failed: %d\n", rc ));
472 Debug( LDAP_DEBUG_ANY,
473 "main: TLS init def ctx failed: %d\n",
477 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
482 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
483 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
486 (void) SIGNAL( SIGPIPE, SIG_IGN );
489 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
491 (void) SIGNAL( SIGINT, slap_sig_shutdown );
492 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
494 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
497 /* SIGBREAK is generated when Ctrl-Break is pressed. */
498 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
502 lutil_detach( no_detach, 0 );
503 #endif /* HAVE_WINSOCK */
509 if ( slap_startup( NULL ) != 0 ) {
511 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
519 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
520 "main: slapd starting.\n" ));
522 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
526 if (( slapd_pid_file != NULL ) &&
527 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
529 fprintf( fp, "%d\n", (int) getpid() );
533 if (( slapd_args_file != NULL ) &&
534 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
536 for ( i = 0; i < g_argc; i++ ) {
537 fprintf( fp, "%s ", g_argv[i] );
544 #ifdef HAVE_NT_EVENT_LOG
546 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
551 #ifdef HAVE_NT_SERVICE_MANAGER
552 /* Throw away the event that we used during the startup process. */
554 ldap_pvt_thread_cond_destroy( &started_event );
558 /* remember an error during shutdown */
559 rc |= slap_shutdown( NULL );
562 /* remember an error during destroy */
563 rc |= slap_destroy();
572 #ifdef HAVE_NT_EVENT_LOG
574 LogSlapdStoppedEvent( serverName );
578 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
579 "main: slapd stopped.\n" ));
581 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
585 #ifdef HAVE_NT_SERVICE_MANAGER
586 ReportSlapdShutdownComplete();
592 slapd_daemon_destroy();
597 ldap_pvt_tls_destroy();
603 mal_dumpleaktrace( leakfile );
613 * Catch and discard terminated child processes, to avoid zombies.
617 wait4child( int sig )
619 int save_errno = errno;
624 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
627 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
633 (void) SIGNAL_REINSTALL( sig, wait4child );
637 #endif /* SIGCHLD || SIGCLD */
643 * Convert a string to an integer by means of a dispatcher table
644 * if the string is not in the table return the default
648 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
650 int retVal = defaultVal;
653 for (disp = dispatcher; disp->stringVal; disp++) {
655 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
657 retVal = disp->intVal;
666 #endif /* LOG_LOCAL4 */