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>
26 static RETSIGTYPE wait4child( int sig );
29 #ifdef HAVE_NT_SERVICE_MANAGER
30 #define MAIN_RETURN(x) return
31 static struct sockaddr_in bind_addr;
33 void CommenceStartupProcessing( LPCTSTR serverName,
35 void ReportSlapdShutdownComplete( void );
36 void *getRegParam( char *svc, char *value );
38 #define SERVICE_EXIT( e, n ) do { \
39 if ( is_NT_Service ) { \
40 SLAPDServiceStatus.dwWin32ExitCode = (e); \
41 SLAPDServiceStatus.dwServiceSpecificExitCode = (n); \
46 #define SERVICE_EXIT( e, n )
47 #define MAIN_RETURN(x) return(x)
50 #ifdef HAVE_NT_EVENT_LOG
51 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
52 void LogSlapdStoppedEvent( char *svc );
56 * when more than one slapd is running on one machine, each one might have
57 * it's own LOCAL for syslogging and must have its own pid/args files
60 #ifndef HAVE_MKVERSION
61 const char Versionstr[] =
62 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
67 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
69 typedef struct _str2intDispatch {
73 } STRDISP, *STRDISP_P;
76 /* table to compute syslog-options to integer */
77 static STRDISP syslog_types[] = {
78 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
79 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
80 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
81 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
82 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
83 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
84 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
85 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
89 static int cnvt_str2int( char *, STRDISP_P, int );
91 #endif /* LOG_LOCAL4 */
93 static int check_config = 0;
99 "usage: %s options\n", name );
101 "\t-4\t\tIPv4 only\n"
102 "\t-6\t\tIPv6 only\n"
103 "\t-d level\tDebug level" "\n"
104 "\t-f filename\tConfiguration file\n"
105 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
106 "\t-g group\tGroup (id or name) to run as\n"
108 "\t-h URLs\t\tList of URLs to serve\n"
110 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
112 "\t-n serverName\tService name\n"
114 "\t-r directory\tSandbox directory to chroot to\n"
116 "\t-s level\tSyslog level\n"
117 "\t-t\t\tCheck configuration file and exit\n"
118 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
119 "\t-u user\t\tUser (id or name) to run as\n"
124 #ifdef HAVE_NT_SERVICE_MANAGER
125 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
127 int main( int argc, char **argv )
130 int i, no_detach = 0;
133 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
134 char *username = NULL;
135 char *groupname = NULL;
137 #if defined(HAVE_CHROOT)
138 char *sandbox = NULL;
141 int syslogUser = DEFAULT_SYSLOG_USER;
145 char **g_argv = argv;
147 #ifdef HAVE_NT_SERVICE_MANAGER
148 char *configfile = ".\\slapd.conf";
150 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
152 char *serverName = NULL;
153 int serverMode = SLAP_SERVER_MODE;
157 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
162 #ifdef HAVE_NT_SERVICE_MANAGER
167 char *regService = NULL;
169 if ( is_NT_Service ) {
170 serverName = argv[0];
171 CommenceStartupProcessing( serverName, slap_sig_shutdown );
172 if ( strcmp(serverName, SERVICE_NAME) )
173 regService = serverName;
176 i = (int*)getRegParam( regService, "DebugLevel" );
181 lutil_log_initialize( argc, argv );
182 LDAP_LOG( SLAPD, INFO,
183 "main: new debug level from registry is: %d\n",
186 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
190 newUrls = (char *) getRegParam(regService, "Urls");
196 urls = ch_strdup(newUrls);
198 LDAP_LOG( SLAPD, INFO,
199 "main: new urls from registry: %s\n", urls, 0, 0 );
201 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
207 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
208 if ( newConfigFile != NULL )
210 configfile = newConfigFile;
212 LDAP_LOG( SLAPD, INFO,
213 "main: new config file from registry is: %s\n", configfile, 0, 0 );
215 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
222 while ( (i = getopt( argc, argv,
233 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
240 slap_inet4or6 = AF_INET;
243 slap_inet4or6 = AF_INET6;
247 case 'h': /* listen URLs */
248 if ( urls != NULL ) free( urls );
249 urls = ch_strdup( optarg );
252 case 'd': /* set debug level and 'do not detach' flag */
255 slap_debug |= atoi( optarg );
257 if ( atoi( optarg ) != 0 )
258 fputs( "must compile with LDAP_DEBUG for debugging\n",
263 case 'f': /* read config file */
264 configfile = ch_strdup( optarg );
267 case 's': /* set syslog level */
268 ldap_syslog = atoi( optarg );
272 case 'l': /* set syslog local user */
273 syslogUser = cnvt_str2int( optarg,
274 syslog_types, DEFAULT_SYSLOG_USER );
280 if( sandbox ) free(sandbox);
281 sandbox = ch_strdup( optarg );
285 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
286 case 'u': /* user name */
287 if( username ) free(username);
288 username = ch_strdup( optarg );
291 case 'g': /* group name */
292 if( groupname ) free(groupname);
293 groupname = ch_strdup( optarg );
295 #endif /* SETUID && GETUID */
297 case 'n': /* NT service name */
298 if( serverName != NULL ) free( serverName );
299 serverName = ch_strdup( optarg );
309 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
315 lutil_log_initialize( argc, argv );
317 lutil_set_debug_level( "slapd", slap_debug );
318 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
319 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
320 ldif_debug = slap_debug;
324 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
326 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 );
409 if ( slapi_init() != 0 ) {
411 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
413 Debug( LDAP_DEBUG_ANY,
414 "slapi initialization error\n",
420 #endif /* LDAP_SLAPI */
422 if ( read_config( configfile, 0 ) != 0 ) {
424 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
426 if ( check_config ) {
427 fprintf( stderr, "config check failed\n" );
433 if ( check_config ) {
435 fprintf( stderr, "config check succeeded\n" );
439 if ( glue_sub_init( ) != 0 ) {
441 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
443 Debug( LDAP_DEBUG_ANY,
444 "subordinate config error\n",
450 if ( slap_schema_check( ) != 0 ) {
452 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
454 Debug( LDAP_DEBUG_ANY,
455 "schema prep error\n",
463 rc = ldap_pvt_tls_init();
466 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
468 Debug( LDAP_DEBUG_ANY,
469 "main: TLS init failed: %d\n",
473 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
477 rc = ldap_pvt_tls_init_def_ctx();
480 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
482 Debug( LDAP_DEBUG_ANY,
483 "main: TLS init def ctx failed: %d\n",
487 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
492 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
493 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
496 (void) SIGNAL( SIGPIPE, SIG_IGN );
499 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
501 (void) SIGNAL( SIGINT, slap_sig_shutdown );
502 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
504 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
507 /* SIGBREAK is generated when Ctrl-Break is pressed. */
508 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
512 lutil_detach( no_detach, 0 );
513 #endif /* HAVE_WINSOCK */
519 if ( slap_startup( NULL ) != 0 ) {
521 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
529 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
531 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
535 if (( slapd_pid_file != NULL ) &&
536 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
538 fprintf( fp, "%d\n", (int) getpid() );
542 if (( slapd_args_file != NULL ) &&
543 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
545 for ( i = 0; i < g_argc; i++ ) {
546 fprintf( fp, "%s ", g_argv[i] );
553 #ifdef HAVE_NT_EVENT_LOG
555 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
560 #ifdef HAVE_NT_SERVICE_MANAGER
561 /* Throw away the event that we used during the startup process. */
563 ldap_pvt_thread_cond_destroy( &started_event );
567 /* remember an error during shutdown */
568 rc |= slap_shutdown( NULL );
571 /* remember an error during destroy */
572 rc |= slap_destroy();
581 #ifdef HAVE_NT_EVENT_LOG
583 LogSlapdStoppedEvent( serverName );
587 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
589 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
593 #ifdef HAVE_NT_SERVICE_MANAGER
594 ReportSlapdShutdownComplete();
600 slapd_daemon_destroy();
605 ldap_pvt_tls_destroy();
611 mal_dumpleaktrace( leakfile );
621 * Catch and discard terminated child processes, to avoid zombies.
625 wait4child( int sig )
627 int save_errno = errno;
632 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
635 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
641 (void) SIGNAL_REINSTALL( sig, wait4child );
645 #endif /* LDAP_SIGCHLD */
651 * Convert a string to an integer by means of a dispatcher table
652 * if the string is not in the table return the default
656 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
658 int retVal = defaultVal;
661 for (disp = dispatcher; disp->stringVal; disp++) {
663 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
665 retVal = disp->intVal;
674 #endif /* LOG_LOCAL4 */