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 #define SERVICE_EXIT( e, n ) do { \
36 if ( is_NT_Service ) { \
37 lutil_ServiceStatus.dwWin32ExitCode = (e); \
38 lutil_ServiceStatus.dwServiceSpecificExitCode = (n); \
43 #define SERVICE_EXIT( e, n )
44 #define MAIN_RETURN(x) return(x)
48 * when more than one slapd is running on one machine, each one might have
49 * it's own LOCAL for syslogging and must have its own pid/args files
52 #ifndef HAVE_MKVERSION
53 const char Versionstr[] =
54 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
59 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
61 typedef struct _str2intDispatch {
65 } STRDISP, *STRDISP_P;
68 /* table to compute syslog-options to integer */
69 static STRDISP syslog_types[] = {
70 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
71 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
72 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
73 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
74 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
75 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
76 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
77 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
81 static int cnvt_str2int( char *, STRDISP_P, int );
83 #endif /* LOG_LOCAL4 */
85 static int check_config = 0;
91 "usage: %s options\n", name );
95 "\t-d level\tDebug level" "\n"
96 "\t-f filename\tConfiguration file\n"
97 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
98 "\t-g group\tGroup (id or name) to run as\n"
100 "\t-h URLs\t\tList of URLs to serve\n"
102 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
104 "\t-n serverName\tService name\n"
106 "\t-r directory\tSandbox directory to chroot to\n"
108 "\t-s level\tSyslog level\n"
109 "\t-t\t\tCheck configuration file and exit\n"
110 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
111 "\t-u user\t\tUser (id or name) to run as\n"
116 #ifdef HAVE_NT_SERVICE_MANAGER
117 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
119 int main( int argc, char **argv )
122 int i, no_detach = 0;
125 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
126 char *username = NULL;
127 char *groupname = NULL;
129 #if defined(HAVE_CHROOT)
130 char *sandbox = NULL;
133 int syslogUser = DEFAULT_SYSLOG_USER;
137 char **g_argv = argv;
139 #ifdef HAVE_NT_SERVICE_MANAGER
140 char *configfile = ".\\slapd.conf";
142 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
144 char *serverName = NULL;
145 int serverMode = SLAP_SERVER_MODE;
149 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
156 #ifdef HAVE_NT_SERVICE_MANAGER
161 char *regService = NULL;
163 if ( is_NT_Service ) {
164 serverName = argv[0];
165 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
166 if ( strcmp(serverName, SERVICE_NAME) )
167 regService = serverName;
170 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
175 lutil_log_initialize( argc, argv );
176 LDAP_LOG( SLAPD, INFO,
177 "main: new debug level from registry is: %d\n",
180 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
184 newUrls = (char *) lutil_getRegParam(regService, "Urls");
190 urls = ch_strdup(newUrls);
192 LDAP_LOG( SLAPD, INFO,
193 "main: new urls from registry: %s\n", urls, 0, 0 );
195 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
201 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
202 if ( newConfigFile != NULL )
204 configfile = newConfigFile;
206 LDAP_LOG( SLAPD, INFO,
207 "main: new config file from registry is: %s\n", configfile, 0, 0 );
209 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
216 while ( (i = getopt( argc, argv,
227 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
234 slap_inet4or6 = AF_INET;
237 slap_inet4or6 = AF_INET6;
241 case 'h': /* listen URLs */
242 if ( urls != NULL ) free( urls );
243 urls = ch_strdup( optarg );
246 case 'd': /* set debug level and 'do not detach' flag */
249 slap_debug |= atoi( optarg );
251 if ( atoi( optarg ) != 0 )
252 fputs( "must compile with LDAP_DEBUG for debugging\n",
257 case 'f': /* read config file */
258 configfile = ch_strdup( optarg );
261 case 's': /* set syslog level */
262 ldap_syslog = atoi( optarg );
266 case 'l': /* set syslog local user */
267 syslogUser = cnvt_str2int( optarg,
268 syslog_types, DEFAULT_SYSLOG_USER );
274 if( sandbox ) free(sandbox);
275 sandbox = ch_strdup( optarg );
279 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
280 case 'u': /* user name */
281 if( username ) free(username);
282 username = ch_strdup( optarg );
285 case 'g': /* group name */
286 if( groupname ) free(groupname);
287 groupname = ch_strdup( optarg );
289 #endif /* SETUID && GETUID */
291 case 'n': /* NT service name */
292 if( serverName != NULL ) free( serverName );
293 serverName = ch_strdup( optarg );
303 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
309 lutil_log_initialize( argc, argv );
311 lutil_set_debug_level( "slapd", slap_debug );
312 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
313 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
314 ldif_debug = slap_debug;
318 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
320 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
323 if( serverName == NULL ) {
324 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
325 serverName = argv[0];
327 serverName = serverName + 1;
332 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
334 openlog( serverName, OPENLOG_OPTIONS );
337 if( !check_config && slapd_daemon_init( urls ) != 0 ) {
339 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
343 #if defined(HAVE_CHROOT)
345 if ( chdir( sandbox ) ) {
350 if ( chroot( sandbox ) ) {
358 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
359 if ( username != NULL || groupname != NULL ) {
360 slap_init_user( username, groupname );
369 if ( module_init() != 0 ) {
371 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
376 if ( slap_init( serverMode, serverName ) != 0 ) {
378 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
382 if ( slap_schema_init( ) != 0 ) {
384 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
386 Debug( LDAP_DEBUG_ANY,
387 "schema initialization error\n",
394 if ( slap_controls_init( ) != 0 ) {
396 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
398 Debug( LDAP_DEBUG_ANY,
399 "controls initialization error\n",
407 /* Library defaults to full certificate checking. This is correct when
408 * a client is verifying a server because all servers should have a
409 * valid cert. But few clients have valid certs, so we want our default
410 * to be no checking. The config file can override this as usual.
413 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
417 if ( slapi_init() != 0 ) {
419 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
421 Debug( LDAP_DEBUG_ANY,
422 "slapi initialization error\n",
428 #endif /* LDAP_SLAPI */
430 if ( read_config( configfile, 0 ) != 0 ) {
432 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
434 if ( check_config ) {
435 fprintf( stderr, "config check failed\n" );
441 if ( check_config ) {
443 fprintf( stderr, "config check succeeded\n" );
447 if ( glue_sub_init( ) != 0 ) {
449 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
451 Debug( LDAP_DEBUG_ANY,
452 "subordinate config error\n",
458 if ( slap_schema_check( ) != 0 ) {
460 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
462 Debug( LDAP_DEBUG_ANY,
463 "schema prep error\n",
471 rc = ldap_pvt_tls_init();
474 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
476 Debug( LDAP_DEBUG_ANY,
477 "main: TLS init failed: %d\n",
481 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
485 rc = ldap_pvt_tls_init_def_ctx();
488 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
490 Debug( LDAP_DEBUG_ANY,
491 "main: TLS init def ctx failed: %d\n",
495 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
500 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
501 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
504 (void) SIGNAL( SIGPIPE, SIG_IGN );
507 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
509 (void) SIGNAL( SIGINT, slap_sig_shutdown );
510 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
512 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
515 /* SIGBREAK is generated when Ctrl-Break is pressed. */
516 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
520 lutil_detach( no_detach, 0 );
521 #endif /* HAVE_WINSOCK */
528 * FIXME: moved here from slapd_daemon_task()
529 * because back-monitor db_open() needs it
533 if ( slap_startup( NULL ) != 0 ) {
535 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
540 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
542 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
546 if ( slapd_pid_file != NULL ) {
547 FILE *fp = fopen( slapd_pid_file, "w" );
550 fprintf( fp, "%d\n", (int) getpid() );
554 free(slapd_pid_file);
555 slapd_pid_file = NULL;
559 if ( slapd_args_file != NULL ) {
560 FILE *fp = fopen( slapd_args_file, "w" );
563 for ( i = 0; i < g_argc; i++ ) {
564 fprintf( fp, "%s ", g_argv[i] );
569 free(slapd_args_file);
570 slapd_args_file = NULL;
574 #ifdef HAVE_NT_EVENT_LOG
576 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
581 #ifdef HAVE_NT_SERVICE_MANAGER
582 /* Throw away the event that we used during the startup process. */
584 ldap_pvt_thread_cond_destroy( &started_event );
588 /* remember an error during shutdown */
589 rc |= slap_shutdown( NULL );
592 /* remember an error during destroy */
593 rc |= slap_destroy();
604 #ifdef HAVE_NT_EVENT_LOG
606 lutil_LogStoppedEvent( serverName );
610 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
612 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
616 #ifdef HAVE_NT_SERVICE_MANAGER
617 lutil_ReportShutdownComplete();
623 slapd_daemon_destroy();
629 lutil_passwd_destroy();
632 ldap_pvt_tls_destroy();
635 if ( slapd_pid_file != NULL ) {
636 unlink( slapd_pid_file );
638 if ( slapd_args_file != NULL ) {
639 unlink( slapd_args_file );
645 mal_dumpleaktrace( leakfile );
655 * Catch and discard terminated child processes, to avoid zombies.
659 wait4child( int sig )
661 int save_errno = errno;
666 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
669 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
675 (void) SIGNAL_REINSTALL( sig, wait4child );
679 #endif /* LDAP_SIGCHLD */
685 * Convert a string to an integer by means of a dispatcher table
686 * if the string is not in the table return the default
690 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
692 int retVal = defaultVal;
695 for (disp = dispatcher; disp->stringVal; disp++) {
697 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
699 retVal = disp->intVal;
708 #endif /* LOG_LOCAL4 */