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>
28 static RETSIGTYPE wait4child( int sig );
31 #ifdef HAVE_NT_SERVICE_MANAGER
32 #define MAIN_RETURN(x) return
33 static struct sockaddr_in bind_addr;
35 /* FIXME: no externs should appear in a .c, should be in a .h instead */
36 extern void CommenceStartupProcessing( LPCTSTR serverName,
38 extern void ReportSlapdShutdownComplete( void );
39 extern void *getRegParam( char *svc, char *value );
41 #define SERVICE_EXIT( e, n ) do { \
42 if ( is_NT_Service ) { \
43 SLAPDServiceStatus.dwWin32ExitCode = (e); \
44 SLAPDServiceStatus.dwServiceSpecificExitCode = (n); \
49 #define SERVICE_EXIT( e, n )
50 #define MAIN_RETURN(x) return(x)
53 #ifdef HAVE_NT_EVENT_LOG
54 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
55 void LogSlapdStoppedEvent( char *svc );
59 * when more than one slapd is running on one machine, each one might have
60 * it's own LOCAL for syslogging and must have its own pid/args files
63 #ifndef HAVE_MKVERSION
64 const char Versionstr[] =
65 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
70 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
72 typedef struct _str2intDispatch {
76 } STRDISP, *STRDISP_P;
79 /* table to compute syslog-options to integer */
80 static STRDISP syslog_types[] = {
81 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
82 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
83 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
84 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
85 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
86 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
87 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
88 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
92 static int cnvt_str2int( char *, STRDISP_P, int );
94 #endif /* LOG_LOCAL4 */
96 static int check_config = 0;
102 "usage: %s options\n", name );
104 "\t-4\t\tIPv4 only\n"
105 "\t-6\t\tIPv6 only\n"
106 "\t-d level\tDebug level" "\n"
107 "\t-f filename\tConfiguration file\n"
108 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
109 "\t-g group\tGroup (id or name) to run as\n"
111 "\t-h URLs\t\tList of URLs to serve\n"
113 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
115 "\t-n serverName\tService name\n"
117 "\t-r directory\tSandbox directory to chroot to\n"
119 "\t-s level\tSyslog level\n"
120 "\t-t\t\tCheck configuration file and exit\n"
121 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
122 "\t-u user\t\tUser (id or name) to run as\n"
127 #ifdef HAVE_NT_SERVICE_MANAGER
128 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
130 int main( int argc, char **argv )
133 int i, no_detach = 0;
136 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
137 char *username = NULL;
138 char *groupname = NULL;
140 #if defined(HAVE_CHROOT)
141 char *sandbox = NULL;
144 int syslogUser = DEFAULT_SYSLOG_USER;
148 char **g_argv = argv;
150 #ifdef HAVE_NT_SERVICE_MANAGER
151 char *configfile = ".\\slapd.conf";
153 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
155 char *serverName = NULL;
156 int serverMode = SLAP_SERVER_MODE;
160 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
165 #ifdef HAVE_NT_SERVICE_MANAGER
170 char *regService = NULL;
172 if ( is_NT_Service ) {
173 serverName = argv[0];
174 CommenceStartupProcessing( serverName, slap_sig_shutdown );
175 if ( strcmp(serverName, SERVICE_NAME) )
176 regService = serverName;
179 i = (int*)getRegParam( regService, "DebugLevel" );
184 lutil_log_initialize( argc, argv );
185 LDAP_LOG( SLAPD, INFO,
186 "main: new debug level from registry is: %d\n",
189 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
193 newUrls = (char *) getRegParam(regService, "Urls");
199 urls = ch_strdup(newUrls);
201 LDAP_LOG( SLAPD, INFO,
202 "main: new urls from registry: %s\n", urls, 0, 0 );
204 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
210 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
211 if ( newConfigFile != NULL )
213 configfile = newConfigFile;
215 LDAP_LOG( SLAPD, INFO,
216 "main: new config file from registry is: %s\n", configfile, 0, 0 );
218 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
225 while ( (i = getopt( argc, argv,
236 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
243 slap_inet4or6 = AF_INET;
246 slap_inet4or6 = AF_INET6;
250 case 'h': /* listen URLs */
251 if ( urls != NULL ) free( urls );
252 urls = ch_strdup( optarg );
255 case 'd': /* set debug level and 'do not detach' flag */
258 slap_debug |= atoi( optarg );
260 if ( atoi( optarg ) != 0 )
261 fputs( "must compile with LDAP_DEBUG for debugging\n",
266 case 'f': /* read config file */
267 configfile = ch_strdup( optarg );
270 case 's': /* set syslog level */
271 ldap_syslog = atoi( optarg );
275 case 'l': /* set syslog local user */
276 syslogUser = cnvt_str2int( optarg,
277 syslog_types, DEFAULT_SYSLOG_USER );
283 if( sandbox ) free(sandbox);
284 sandbox = ch_strdup( optarg );
288 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
289 case 'u': /* user name */
290 if( username ) free(username);
291 username = ch_strdup( optarg );
294 case 'g': /* group name */
295 if( groupname ) free(groupname);
296 groupname = ch_strdup( optarg );
298 #endif /* SETUID && GETUID */
300 case 'n': /* NT service name */
301 if( serverName != NULL ) free( serverName );
302 serverName = ch_strdup( optarg );
312 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
318 lutil_log_initialize( argc, argv );
320 lutil_set_debug_level( "slapd", slap_debug );
321 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
322 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
323 ldif_debug = slap_debug;
327 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
329 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
332 if( serverName == NULL ) {
333 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
334 serverName = argv[0];
336 serverName = serverName + 1;
341 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
343 openlog( serverName, OPENLOG_OPTIONS );
346 if( !check_config && slapd_daemon_init( urls ) != 0 ) {
348 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
352 #if defined(HAVE_CHROOT)
354 if ( chdir( sandbox ) ) {
359 if ( chroot( sandbox ) ) {
367 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
368 if ( username != NULL || groupname != NULL ) {
369 slap_init_user( username, groupname );
376 if ( module_init() != 0 ) {
378 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
383 if ( slap_init( serverMode, serverName ) != 0 ) {
385 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
389 if ( slap_schema_init( ) != 0 ) {
391 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
393 Debug( LDAP_DEBUG_ANY,
394 "schema initialization error\n",
401 if ( slap_controls_init( ) != 0 ) {
403 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
405 Debug( LDAP_DEBUG_ANY,
406 "controls initialization error\n",
414 /* Library defaults to full certificate checking. This is correct when
415 * a client is verifying a server because all servers should have a
416 * valid cert. But few clients have valid certs, so we want our default
417 * to be no checking. The config file can override this as usual.
420 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
424 if ( slapi_init() != 0 ) {
426 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
428 Debug( LDAP_DEBUG_ANY,
429 "slapi initialization error\n",
435 #endif /* LDAP_SLAPI */
437 if ( read_config( configfile, 0 ) != 0 ) {
439 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
441 if ( check_config ) {
442 fprintf( stderr, "config check failed\n" );
448 if ( check_config ) {
450 fprintf( stderr, "config check succeeded\n" );
454 if ( glue_sub_init( ) != 0 ) {
456 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
458 Debug( LDAP_DEBUG_ANY,
459 "subordinate config error\n",
465 if ( slap_schema_check( ) != 0 ) {
467 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
469 Debug( LDAP_DEBUG_ANY,
470 "schema prep error\n",
478 rc = ldap_pvt_tls_init();
481 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
483 Debug( LDAP_DEBUG_ANY,
484 "main: TLS init failed: %d\n",
488 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
492 rc = ldap_pvt_tls_init_def_ctx();
495 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
497 Debug( LDAP_DEBUG_ANY,
498 "main: TLS init def ctx failed: %d\n",
502 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
507 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
508 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
511 (void) SIGNAL( SIGPIPE, SIG_IGN );
514 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
516 (void) SIGNAL( SIGINT, slap_sig_shutdown );
517 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
519 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
522 /* SIGBREAK is generated when Ctrl-Break is pressed. */
523 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
527 lutil_detach( no_detach, 0 );
528 #endif /* HAVE_WINSOCK */
534 if ( slap_startup( NULL ) != 0 ) {
536 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
541 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
543 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
547 if ( slapd_pid_file != NULL ) {
548 FILE *fp = fopen( slapd_pid_file, "w" );
551 fprintf( fp, "%d\n", (int) getpid() );
555 free(slapd_pid_file);
556 slapd_pid_file = NULL;
560 if ( slapd_args_file != NULL ) {
561 FILE *fp = fopen( slapd_args_file, "w" );
564 for ( i = 0; i < g_argc; i++ ) {
565 fprintf( fp, "%s ", g_argv[i] );
570 free(slapd_args_file);
571 slapd_args_file = NULL;
575 #ifdef HAVE_NT_EVENT_LOG
577 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
582 #ifdef HAVE_NT_SERVICE_MANAGER
583 /* Throw away the event that we used during the startup process. */
585 ldap_pvt_thread_cond_destroy( &started_event );
589 /* remember an error during shutdown */
590 rc |= slap_shutdown( NULL );
593 /* remember an error during destroy */
594 rc |= slap_destroy();
603 #ifdef HAVE_NT_EVENT_LOG
605 LogSlapdStoppedEvent( serverName );
609 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
611 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
615 #ifdef HAVE_NT_SERVICE_MANAGER
616 ReportSlapdShutdownComplete();
622 slapd_daemon_destroy();
629 ldap_pvt_tls_destroy();
632 if ( slapd_pid_file != NULL ) {
633 unlink( slapd_pid_file );
635 if ( slapd_args_file != NULL ) {
636 unlink( slapd_args_file );
642 mal_dumpleaktrace( leakfile );
652 * Catch and discard terminated child processes, to avoid zombies.
656 wait4child( int sig )
658 int save_errno = errno;
663 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
666 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
672 (void) SIGNAL_REINSTALL( sig, wait4child );
676 #endif /* LDAP_SIGCHLD */
682 * Convert a string to an integer by means of a dispatcher table
683 * if the string is not in the table return the default
687 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
689 int retVal = defaultVal;
692 for (disp = dispatcher; disp->stringVal; disp++) {
694 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
696 retVal = disp->intVal;
705 #endif /* LOG_LOCAL4 */