2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
30 #include <ac/socket.h>
31 #include <ac/string.h>
33 #include <ac/unistd.h>
44 #include "slapi/slapi.h"
48 static RETSIGTYPE wait4child( int sig );
51 #ifdef HAVE_NT_SERVICE_MANAGER
52 #define MAIN_RETURN(x) return
53 static struct sockaddr_in bind_addr;
55 #define SERVICE_EXIT( e, n ) do { \
56 if ( is_NT_Service ) { \
57 lutil_ServiceStatus.dwWin32ExitCode = (e); \
58 lutil_ServiceStatus.dwServiceSpecificExitCode = (n); \
63 #define SERVICE_EXIT( e, n )
64 #define MAIN_RETURN(x) return(x)
68 * when more than one slapd is running on one machine, each one might have
69 * it's own LOCAL for syslogging and must have its own pid/args files
72 #ifndef HAVE_MKVERSION
73 const char Versionstr[] =
74 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
79 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
81 typedef struct _str2intDispatch {
85 } STRDISP, *STRDISP_P;
88 /* table to compute syslog-options to integer */
89 static STRDISP syslog_types[] = {
90 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
91 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
92 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
93 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
94 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
95 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
96 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
97 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
101 static int cnvt_str2int( char *, STRDISP_P, int );
103 #endif /* LOG_LOCAL4 */
105 static int check_config = 0;
106 static int version = 0;
112 "usage: %s options\n", name );
114 "\t-4\t\tIPv4 only\n"
115 "\t-6\t\tIPv6 only\n"
116 "\t-c cookie\tSync cookie of consumer\n"
117 "\t-d level\tDebug level" "\n"
118 "\t-f filename\tConfiguration file\n"
119 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
120 "\t-g group\tGroup (id or name) to run as\n"
122 "\t-h URLs\t\tList of URLs to serve\n"
124 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
126 "\t-n serverName\tService name\n"
128 "\t-r directory\tSandbox directory to chroot to\n"
130 "\t-s level\tSyslog level\n"
131 "\t-t\t\tCheck configuration file and exit\n"
132 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
133 "\t-u user\t\tUser (id or name) to run as\n"
134 "\t-V\t\tprint version info (-VV only)\n"
139 #ifdef HAVE_NT_SERVICE_MANAGER
140 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
142 int main( int argc, char **argv )
145 int i, no_detach = 0;
148 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
149 char *username = NULL;
150 char *groupname = NULL;
152 #if defined(HAVE_CHROOT)
153 char *sandbox = NULL;
156 int syslogUser = DEFAULT_SYSLOG_USER;
160 char **g_argv = argv;
162 #ifdef HAVE_NT_SERVICE_MANAGER
163 char *configfile = ".\\slapd.conf";
165 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
167 char *serverName = NULL;
168 int serverMode = SLAP_SERVER_MODE;
170 struct berval cookie = { 0, NULL };
171 struct sync_cookie *scp = NULL;
172 struct sync_cookie *scp_entry = NULL;
176 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
183 #ifdef HAVE_NT_SERVICE_MANAGER
188 char *regService = NULL;
190 if ( is_NT_Service ) {
191 serverName = argv[0];
192 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
193 if ( strcmp(serverName, SERVICE_NAME) )
194 regService = serverName;
197 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
202 lutil_log_initialize( argc, argv );
203 LDAP_LOG( SLAPD, INFO,
204 "main: new debug level from registry is: %d\n",
207 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
211 newUrls = (char *) lutil_getRegParam(regService, "Urls");
217 urls = ch_strdup(newUrls);
219 LDAP_LOG( SLAPD, INFO,
220 "main: new urls from registry: %s\n", urls, 0, 0 );
222 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
228 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
229 if ( newConfigFile != NULL )
231 configfile = newConfigFile;
233 LDAP_LOG( SLAPD, INFO,
234 "main: new config file from registry is: %s\n", configfile, 0, 0 );
236 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
243 while ( (i = getopt( argc, argv,
254 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
261 slap_inet4or6 = AF_INET;
264 slap_inet4or6 = AF_INET6;
268 case 'h': /* listen URLs */
269 if ( urls != NULL ) free( urls );
270 urls = ch_strdup( optarg );
273 case 'c': /* provide sync cookie, override if exist in replica */
274 scp = (struct sync_cookie *) ch_calloc( 1,
275 sizeof( struct sync_cookie ));
276 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
277 ber_bvarray_add( &scp->octet_str, &cookie );
278 slap_parse_sync_cookie( scp );
280 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
281 if ( scp->rid == scp_entry->rid ) {
283 LDAP_LOG( OPERATION, CRIT,
284 "main: duplicated replica id in cookies\n",
287 Debug( LDAP_DEBUG_ANY,
288 "main: duplicated replica id in cookies\n",
291 slap_sync_cookie_free( scp, 1 );
295 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
298 case 'd': /* set debug level and 'do not detach' flag */
301 slap_debug |= atoi( optarg );
303 if ( atoi( optarg ) != 0 )
304 fputs( "must compile with LDAP_DEBUG for debugging\n",
309 case 'f': /* read config file */
310 configfile = ch_strdup( optarg );
313 case 's': /* set syslog level */
314 ldap_syslog = atoi( optarg );
318 case 'l': /* set syslog local user */
319 syslogUser = cnvt_str2int( optarg,
320 syslog_types, DEFAULT_SYSLOG_USER );
326 if( sandbox ) free(sandbox);
327 sandbox = ch_strdup( optarg );
331 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
332 case 'u': /* user name */
333 if( username ) free(username);
334 username = ch_strdup( optarg );
337 case 'g': /* group name */
338 if( groupname ) free(groupname);
339 groupname = ch_strdup( optarg );
341 #endif /* SETUID && GETUID */
343 case 'n': /* NT service name */
344 if( serverName != NULL ) free( serverName );
345 serverName = ch_strdup( optarg );
358 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
364 lutil_log_initialize( argc, argv );
366 lutil_set_debug_level( "slapd", slap_debug );
367 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
368 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
369 ldif_debug = slap_debug;
373 fprintf( stderr, "%s\n", Versionstr );
374 if ( version > 1 ) goto stop;
377 if( serverName == NULL ) {
378 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
379 serverName = argv[0];
381 serverName = serverName + 1;
386 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
388 openlog( serverName, OPENLOG_OPTIONS );
392 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
394 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
397 if( !check_config && slapd_daemon_init( urls ) != 0 ) {
399 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
403 #if defined(HAVE_CHROOT)
405 if ( chdir( sandbox ) ) {
410 if ( chroot( sandbox ) ) {
418 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
419 if ( username != NULL || groupname != NULL ) {
420 slap_init_user( username, groupname );
429 if ( module_init() != 0 ) {
431 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
436 if ( slap_schema_init( ) != 0 ) {
438 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
440 Debug( LDAP_DEBUG_ANY,
441 "schema initialization error\n",
448 if ( slap_init( serverMode, serverName ) != 0 ) {
450 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
454 if ( slap_controls_init( ) != 0 ) {
456 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
458 Debug( LDAP_DEBUG_ANY,
459 "controls initialization error\n",
467 /* Library defaults to full certificate checking. This is correct when
468 * a client is verifying a server because all servers should have a
469 * valid cert. But few clients have valid certs, so we want our default
470 * to be no checking. The config file can override this as usual.
473 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
477 if ( slapi_int_initialize() != 0 ) {
479 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
481 Debug( LDAP_DEBUG_ANY,
482 "slapi initialization error\n",
488 #endif /* LDAP_SLAPI */
490 if ( overlay_init() ) {
494 if ( read_config( configfile, 0 ) != 0 ) {
496 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
498 if ( check_config ) {
499 fprintf( stderr, "config check failed\n" );
505 if ( check_config ) {
507 fprintf( stderr, "config check succeeded\n" );
511 if ( glue_sub_init( ) != 0 ) {
513 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
515 Debug( LDAP_DEBUG_ANY,
516 "subordinate config error\n",
522 if ( slap_schema_check( ) != 0 ) {
524 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
526 Debug( LDAP_DEBUG_ANY,
527 "schema prep error\n",
535 rc = ldap_pvt_tls_init();
538 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
540 Debug( LDAP_DEBUG_ANY,
541 "main: TLS init failed: %d\n",
545 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
549 rc = ldap_pvt_tls_init_def_ctx();
552 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
554 Debug( LDAP_DEBUG_ANY,
555 "main: TLS init def ctx failed: %d\n",
559 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
564 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
565 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
568 (void) SIGNAL( SIGPIPE, SIG_IGN );
571 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
573 (void) SIGNAL( SIGINT, slap_sig_shutdown );
574 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
576 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
579 /* SIGBREAK is generated when Ctrl-Break is pressed. */
580 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
584 lutil_detach( no_detach, 0 );
585 #endif /* HAVE_WINSOCK */
592 * FIXME: moved here from slapd_daemon_task()
593 * because back-monitor db_open() needs it
597 if ( slap_startup( NULL ) != 0 ) {
599 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
604 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
606 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
610 if ( slapd_pid_file != NULL ) {
611 FILE *fp = fopen( slapd_pid_file, "w" );
614 fprintf( fp, "%d\n", (int) getpid() );
618 free(slapd_pid_file);
619 slapd_pid_file = NULL;
623 if ( slapd_args_file != NULL ) {
624 FILE *fp = fopen( slapd_args_file, "w" );
627 for ( i = 0; i < g_argc; i++ ) {
628 fprintf( fp, "%s ", g_argv[i] );
633 free(slapd_args_file);
634 slapd_args_file = NULL;
638 #ifdef HAVE_NT_EVENT_LOG
640 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
645 #ifdef HAVE_NT_SERVICE_MANAGER
646 /* Throw away the event that we used during the startup process. */
648 ldap_pvt_thread_cond_destroy( &started_event );
652 /* remember an error during shutdown */
653 rc |= slap_shutdown( NULL );
656 /* remember an error during destroy */
657 rc |= slap_destroy();
659 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
660 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
661 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
674 #ifdef HAVE_NT_EVENT_LOG
676 lutil_LogStoppedEvent( serverName );
680 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
682 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
686 #ifdef HAVE_NT_SERVICE_MANAGER
687 lutil_ReportShutdownComplete();
693 slapd_daemon_destroy();
699 lutil_passwd_destroy();
702 ldap_pvt_tls_destroy();
705 if ( slapd_pid_file != NULL ) {
706 unlink( slapd_pid_file );
708 if ( slapd_args_file != NULL ) {
709 unlink( slapd_args_file );
715 mal_dumpleaktrace( leakfile );
725 * Catch and discard terminated child processes, to avoid zombies.
729 wait4child( int sig )
731 int save_errno = errno;
736 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
739 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
745 (void) SIGNAL_REINSTALL( sig, wait4child );
749 #endif /* LDAP_SIGCHLD */
755 * Convert a string to an integer by means of a dispatcher table
756 * if the string is not in the table return the default
760 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
762 int retVal = defaultVal;
765 for (disp = dispatcher; disp->stringVal; disp++) {
767 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
769 retVal = disp->intVal;
778 #endif /* LOG_LOCAL4 */