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>
25 static RETSIGTYPE wait4child( int sig );
28 #ifdef HAVE_NT_SERVICE_MANAGER
29 #define MAIN_RETURN(x) return
30 static struct sockaddr_in bind_addr;
32 void CommenceStartupProcessing( LPCTSTR serverName,
34 void ReportSlapdShutdownComplete( void );
35 void *getRegParam( char *svc, char *value );
37 #define SERVICE_EXIT( e, n ) do { \
38 if ( is_NT_Service ) { \
39 SLAPDServiceStatus.dwWin32ExitCode = (e); \
40 SLAPDServiceStatus.dwServiceSpecificExitCode = (n); \
45 #define SERVICE_EXIT( e, n )
46 #define MAIN_RETURN(x) return(x)
49 #ifdef HAVE_NT_EVENT_LOG
50 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
51 void LogSlapdStoppedEvent( char *svc );
55 * when more than one slapd is running on one machine, each one might have
56 * it's own LOCAL for syslogging and must have its own pid/args files
59 #ifndef HAVE_MKVERSION
60 const char Versionstr[] =
61 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
66 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
68 typedef struct _str2intDispatch {
72 } STRDISP, *STRDISP_P;
75 /* table to compute syslog-options to integer */
76 static STRDISP syslog_types[] = {
77 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
78 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
79 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
80 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
81 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
82 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
83 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
84 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
88 static int cnvt_str2int( char *, STRDISP_P, int );
90 #endif /* LOG_LOCAL4 */
92 static int check_config = 0;
98 "usage: %s options\n", name );
100 "\t-4\t\tIPv4 only\n"
101 "\t-6\t\tIPv6 only\n"
102 "\t-d level\tDebug level" "\n"
103 "\t-f filename\tConfiguration file\n"
104 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
105 "\t-g group\tGroup (id or name) to run as\n"
107 "\t-h URLs\t\tList of URLs to serve\n"
109 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
111 "\t-n serverName\tService name\n"
113 "\t-r directory\tSandbox directory to chroot to\n"
115 "\t-s level\tSyslog level\n"
116 "\t-t\t\tCheck configuration file and exit\n"
117 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
118 "\t-u user\t\tUser (id or name) to run as\n"
123 #ifdef HAVE_NT_SERVICE_MANAGER
124 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
126 int main( int argc, char **argv )
129 int i, no_detach = 0;
132 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
133 char *username = NULL;
134 char *groupname = NULL;
136 #if defined(HAVE_CHROOT)
137 char *sandbox = NULL;
140 int syslogUser = DEFAULT_SYSLOG_USER;
144 char **g_argv = argv;
146 #ifdef HAVE_NT_SERVICE_MANAGER
147 char *configfile = ".\\slapd.conf";
149 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
151 char *serverName = NULL;
152 int serverMode = SLAP_SERVER_MODE;
156 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 );
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, CRIT, "main: schema initialization error\n", 0, 0, 0 );
389 Debug( LDAP_DEBUG_ANY,
390 "schema initialization error\n",
398 /* Library defaults to full certificate checking. This is correct when
399 * a client is verifying a server because all servers should have a
400 * valid cert. But few clients have valid certs, so we want our default
401 * to be no checking. The config file can override this as usual.
404 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
408 if ( slapi_init() != 0 ) {
410 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
412 Debug( LDAP_DEBUG_ANY,
413 "slapi initialization error\n",
419 #endif /* LDAP_SLAPI */
421 if ( read_config( configfile, 0 ) != 0 ) {
423 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
425 if ( check_config ) {
426 fprintf( stderr, "config check failed\n" );
432 if ( check_config ) {
434 fprintf( stderr, "config check succeeded\n" );
438 if ( glue_sub_init( ) != 0 ) {
440 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
442 Debug( LDAP_DEBUG_ANY,
443 "subordinate config error\n",
449 if ( slap_schema_check( ) != 0 ) {
451 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
453 Debug( LDAP_DEBUG_ANY,
454 "schema prep error\n",
462 rc = ldap_pvt_tls_init();
465 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
467 Debug( LDAP_DEBUG_ANY,
468 "main: TLS init failed: %d\n",
472 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
476 rc = ldap_pvt_tls_init_def_ctx();
479 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
481 Debug( LDAP_DEBUG_ANY,
482 "main: TLS init def ctx failed: %d\n",
486 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
491 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
492 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
495 (void) SIGNAL( SIGPIPE, SIG_IGN );
498 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
500 (void) SIGNAL( SIGINT, slap_sig_shutdown );
501 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
503 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
506 /* SIGBREAK is generated when Ctrl-Break is pressed. */
507 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
511 lutil_detach( no_detach, 0 );
512 #endif /* HAVE_WINSOCK */
518 if ( slap_startup( NULL ) != 0 ) {
520 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
528 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
530 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
534 if (( slapd_pid_file != NULL ) &&
535 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
537 fprintf( fp, "%d\n", (int) getpid() );
541 if (( slapd_args_file != NULL ) &&
542 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
544 for ( i = 0; i < g_argc; i++ ) {
545 fprintf( fp, "%s ", g_argv[i] );
552 #ifdef HAVE_NT_EVENT_LOG
554 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
559 #ifdef HAVE_NT_SERVICE_MANAGER
560 /* Throw away the event that we used during the startup process. */
562 ldap_pvt_thread_cond_destroy( &started_event );
566 /* remember an error during shutdown */
567 rc |= slap_shutdown( NULL );
570 /* remember an error during destroy */
571 rc |= slap_destroy();
580 #ifdef HAVE_NT_EVENT_LOG
582 LogSlapdStoppedEvent( serverName );
586 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
588 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
592 #ifdef HAVE_NT_SERVICE_MANAGER
593 ReportSlapdShutdownComplete();
599 slapd_daemon_destroy();
604 ldap_pvt_tls_destroy();
610 mal_dumpleaktrace( leakfile );
620 * Catch and discard terminated child processes, to avoid zombies.
624 wait4child( int sig )
626 int save_errno = errno;
631 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
634 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
640 (void) SIGNAL_REINSTALL( sig, wait4child );
644 #endif /* LDAP_SIGCHLD */
650 * Convert a string to an integer by means of a dispatcher table
651 * if the string is not in the table return the default
655 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
657 int retVal = defaultVal;
660 for (disp = dispatcher; disp->stringVal; disp++) {
662 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
664 retVal = disp->intVal;
673 #endif /* LOG_LOCAL4 */