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 ) {
154 #ifdef HAVE_NT_SERVICE_MANAGER
159 char *regService = NULL;
161 if ( is_NT_Service ) {
162 serverName = argv[0];
163 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
164 if ( strcmp(serverName, SERVICE_NAME) )
165 regService = serverName;
168 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
173 lutil_log_initialize( argc, argv );
174 LDAP_LOG( SLAPD, INFO,
175 "main: new debug level from registry is: %d\n",
178 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
182 newUrls = (char *) lutil_getRegParam(regService, "Urls");
188 urls = ch_strdup(newUrls);
190 LDAP_LOG( SLAPD, INFO,
191 "main: new urls from registry: %s\n", urls, 0, 0 );
193 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
199 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
200 if ( newConfigFile != NULL )
202 configfile = newConfigFile;
204 LDAP_LOG( SLAPD, INFO,
205 "main: new config file from registry is: %s\n", configfile, 0, 0 );
207 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
214 while ( (i = getopt( argc, argv,
225 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
232 slap_inet4or6 = AF_INET;
235 slap_inet4or6 = AF_INET6;
239 case 'h': /* listen URLs */
240 if ( urls != NULL ) free( urls );
241 urls = ch_strdup( optarg );
244 case 'd': /* set debug level and 'do not detach' flag */
247 slap_debug |= atoi( optarg );
249 if ( atoi( optarg ) != 0 )
250 fputs( "must compile with LDAP_DEBUG for debugging\n",
255 case 'f': /* read config file */
256 configfile = ch_strdup( optarg );
259 case 's': /* set syslog level */
260 ldap_syslog = atoi( optarg );
264 case 'l': /* set syslog local user */
265 syslogUser = cnvt_str2int( optarg,
266 syslog_types, DEFAULT_SYSLOG_USER );
272 if( sandbox ) free(sandbox);
273 sandbox = ch_strdup( optarg );
277 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
278 case 'u': /* user name */
279 if( username ) free(username);
280 username = ch_strdup( optarg );
283 case 'g': /* group name */
284 if( groupname ) free(groupname);
285 groupname = ch_strdup( optarg );
287 #endif /* SETUID && GETUID */
289 case 'n': /* NT service name */
290 if( serverName != NULL ) free( serverName );
291 serverName = ch_strdup( optarg );
301 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
307 lutil_log_initialize( argc, argv );
309 lutil_set_debug_level( "slapd", slap_debug );
310 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
311 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
312 ldif_debug = slap_debug;
316 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
318 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
321 if( serverName == NULL ) {
322 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
323 serverName = argv[0];
325 serverName = serverName + 1;
330 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
332 openlog( serverName, OPENLOG_OPTIONS );
335 if( !check_config && slapd_daemon_init( urls ) != 0 ) {
337 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
341 #if defined(HAVE_CHROOT)
343 if ( chdir( sandbox ) ) {
348 if ( chroot( sandbox ) ) {
356 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
357 if ( username != NULL || groupname != NULL ) {
358 slap_init_user( username, groupname );
365 if ( module_init() != 0 ) {
367 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
372 if ( slap_init( serverMode, serverName ) != 0 ) {
374 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
378 if ( slap_schema_init( ) != 0 ) {
380 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
382 Debug( LDAP_DEBUG_ANY,
383 "schema initialization error\n",
390 if ( slap_controls_init( ) != 0 ) {
392 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
394 Debug( LDAP_DEBUG_ANY,
395 "controls initialization error\n",
403 /* Library defaults to full certificate checking. This is correct when
404 * a client is verifying a server because all servers should have a
405 * valid cert. But few clients have valid certs, so we want our default
406 * to be no checking. The config file can override this as usual.
409 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
413 if ( slapi_init() != 0 ) {
415 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
417 Debug( LDAP_DEBUG_ANY,
418 "slapi initialization error\n",
424 #endif /* LDAP_SLAPI */
426 if ( read_config( configfile, 0 ) != 0 ) {
428 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
430 if ( check_config ) {
431 fprintf( stderr, "config check failed\n" );
437 if ( check_config ) {
439 fprintf( stderr, "config check succeeded\n" );
443 if ( glue_sub_init( ) != 0 ) {
445 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
447 Debug( LDAP_DEBUG_ANY,
448 "subordinate config error\n",
454 if ( slap_schema_check( ) != 0 ) {
456 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
458 Debug( LDAP_DEBUG_ANY,
459 "schema prep error\n",
467 rc = ldap_pvt_tls_init();
470 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
472 Debug( LDAP_DEBUG_ANY,
473 "main: TLS init failed: %d\n",
477 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
481 rc = ldap_pvt_tls_init_def_ctx();
484 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
486 Debug( LDAP_DEBUG_ANY,
487 "main: TLS init def ctx failed: %d\n",
491 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
496 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
497 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
500 (void) SIGNAL( SIGPIPE, SIG_IGN );
503 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
505 (void) SIGNAL( SIGINT, slap_sig_shutdown );
506 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
508 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
511 /* SIGBREAK is generated when Ctrl-Break is pressed. */
512 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
516 lutil_detach( no_detach, 0 );
517 #endif /* HAVE_WINSOCK */
523 if ( slap_startup( NULL ) != 0 ) {
525 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
530 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
532 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
536 if ( slapd_pid_file != NULL ) {
537 FILE *fp = fopen( slapd_pid_file, "w" );
540 fprintf( fp, "%d\n", (int) getpid() );
544 free(slapd_pid_file);
545 slapd_pid_file = NULL;
549 if ( slapd_args_file != NULL ) {
550 FILE *fp = fopen( slapd_args_file, "w" );
553 for ( i = 0; i < g_argc; i++ ) {
554 fprintf( fp, "%s ", g_argv[i] );
559 free(slapd_args_file);
560 slapd_args_file = NULL;
564 #ifdef HAVE_NT_EVENT_LOG
566 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
571 #ifdef HAVE_NT_SERVICE_MANAGER
572 /* Throw away the event that we used during the startup process. */
574 ldap_pvt_thread_cond_destroy( &started_event );
578 /* remember an error during shutdown */
579 rc |= slap_shutdown( NULL );
582 /* remember an error during destroy */
583 rc |= slap_destroy();
592 #ifdef HAVE_NT_EVENT_LOG
594 lutil_LogStoppedEvent( serverName );
598 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
600 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
604 #ifdef HAVE_NT_SERVICE_MANAGER
605 lutil_ReportShutdownComplete();
611 slapd_daemon_destroy();
618 ldap_pvt_tls_destroy();
621 if ( slapd_pid_file != NULL ) {
622 unlink( slapd_pid_file );
624 if ( slapd_args_file != NULL ) {
625 unlink( slapd_args_file );
631 mal_dumpleaktrace( leakfile );
641 * Catch and discard terminated child processes, to avoid zombies.
645 wait4child( int sig )
647 int save_errno = errno;
652 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
655 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
661 (void) SIGNAL_REINSTALL( sig, wait4child );
665 #endif /* LDAP_SIGCHLD */
671 * Convert a string to an integer by means of a dispatcher table
672 * if the string is not in the table return the default
676 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
678 int retVal = defaultVal;
681 for (disp = dispatcher; disp->stringVal; disp++) {
683 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
685 retVal = disp->intVal;
694 #endif /* LOG_LOCAL4 */