2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 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>
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;
111 "usage: %s options\n", name );
113 "\t-4\t\tIPv4 only\n"
114 "\t-6\t\tIPv6 only\n"
115 "\t-c cookie\tSync cookie of consumer\n"
116 "\t-d level\tDebug level" "\n"
117 "\t-f filename\tConfiguration file\n"
118 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
119 "\t-g group\tGroup (id or name) to run as\n"
121 "\t-h URLs\t\tList of URLs to serve\n"
123 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
125 "\t-n serverName\tService name\n"
127 "\t-r directory\tSandbox directory to chroot to\n"
129 "\t-s level\tSyslog level\n"
130 "\t-t\t\tCheck configuration file and exit\n"
131 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
132 "\t-u user\t\tUser (id or name) to run as\n"
137 #ifdef HAVE_NT_SERVICE_MANAGER
138 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
140 int main( int argc, char **argv )
143 int i, no_detach = 0;
146 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
147 char *username = NULL;
148 char *groupname = NULL;
150 #if defined(HAVE_CHROOT)
151 char *sandbox = NULL;
154 int syslogUser = DEFAULT_SYSLOG_USER;
158 char **g_argv = argv;
160 #ifdef HAVE_NT_SERVICE_MANAGER
161 char *configfile = ".\\slapd.conf";
163 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
165 char *serverName = NULL;
166 int serverMode = SLAP_SERVER_MODE;
168 struct berval cookie = { 0, NULL };
169 struct sync_cookie *scp = NULL;
170 struct sync_cookie *scp_entry = NULL;
174 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
181 #ifdef HAVE_NT_SERVICE_MANAGER
186 char *regService = NULL;
188 if ( is_NT_Service ) {
189 serverName = argv[0];
190 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
191 if ( strcmp(serverName, SERVICE_NAME) )
192 regService = serverName;
195 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
200 lutil_log_initialize( argc, argv );
201 LDAP_LOG( SLAPD, INFO,
202 "main: new debug level from registry is: %d\n",
205 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
209 newUrls = (char *) lutil_getRegParam(regService, "Urls");
215 urls = ch_strdup(newUrls);
217 LDAP_LOG( SLAPD, INFO,
218 "main: new urls from registry: %s\n", urls, 0, 0 );
220 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
226 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
227 if ( newConfigFile != NULL )
229 configfile = newConfigFile;
231 LDAP_LOG( SLAPD, INFO,
232 "main: new config file from registry is: %s\n", configfile, 0, 0 );
234 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
241 while ( (i = getopt( argc, argv,
252 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
259 slap_inet4or6 = AF_INET;
262 slap_inet4or6 = AF_INET6;
266 case 'h': /* listen URLs */
267 if ( urls != NULL ) free( urls );
268 urls = ch_strdup( optarg );
271 case 'c': /* provide sync cookie, override if exist in replica */
272 scp = (struct sync_cookie *) ch_calloc( 1,
273 sizeof( struct sync_cookie ));
274 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
275 ber_bvarray_add( &scp->octet_str, &cookie );
276 slap_parse_sync_cookie( scp );
278 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
279 if ( scp->rid == scp_entry->rid ) {
281 LDAP_LOG( OPERATION, CRIT,
282 "main: duplicated replica id in cookies\n",
285 Debug( LDAP_DEBUG_ANY,
286 "main: duplicated replica id in cookies\n",
289 slap_sync_cookie_free( scp, 1 );
293 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
296 case 'd': /* set debug level and 'do not detach' flag */
299 slap_debug |= atoi( optarg );
301 if ( atoi( optarg ) != 0 )
302 fputs( "must compile with LDAP_DEBUG for debugging\n",
307 case 'f': /* read config file */
308 configfile = ch_strdup( optarg );
311 case 's': /* set syslog level */
312 ldap_syslog = atoi( optarg );
316 case 'l': /* set syslog local user */
317 syslogUser = cnvt_str2int( optarg,
318 syslog_types, DEFAULT_SYSLOG_USER );
324 if( sandbox ) free(sandbox);
325 sandbox = ch_strdup( optarg );
329 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
330 case 'u': /* user name */
331 if( username ) free(username);
332 username = ch_strdup( optarg );
335 case 'g': /* group name */
336 if( groupname ) free(groupname);
337 groupname = ch_strdup( optarg );
339 #endif /* SETUID && GETUID */
341 case 'n': /* NT service name */
342 if( serverName != NULL ) free( serverName );
343 serverName = ch_strdup( optarg );
353 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
359 lutil_log_initialize( argc, argv );
361 lutil_set_debug_level( "slapd", slap_debug );
362 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
363 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
364 ldif_debug = slap_debug;
368 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
370 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
373 if( serverName == NULL ) {
374 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
375 serverName = argv[0];
377 serverName = serverName + 1;
382 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
384 openlog( serverName, OPENLOG_OPTIONS );
387 if( !check_config && slapd_daemon_init( urls ) != 0 ) {
389 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
393 #if defined(HAVE_CHROOT)
395 if ( chdir( sandbox ) ) {
400 if ( chroot( sandbox ) ) {
408 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
409 if ( username != NULL || groupname != NULL ) {
410 slap_init_user( username, groupname );
419 if ( module_init() != 0 ) {
421 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
426 if ( slap_schema_init( ) != 0 ) {
428 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
430 Debug( LDAP_DEBUG_ANY,
431 "schema initialization error\n",
438 if ( slap_init( serverMode, serverName ) != 0 ) {
440 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
444 if ( slap_controls_init( ) != 0 ) {
446 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
448 Debug( LDAP_DEBUG_ANY,
449 "controls initialization error\n",
457 /* Library defaults to full certificate checking. This is correct when
458 * a client is verifying a server because all servers should have a
459 * valid cert. But few clients have valid certs, so we want our default
460 * to be no checking. The config file can override this as usual.
463 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
467 if ( slapi_init() != 0 ) {
469 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
471 Debug( LDAP_DEBUG_ANY,
472 "slapi initialization error\n",
478 #endif /* LDAP_SLAPI */
480 if ( read_config( configfile, 0 ) != 0 ) {
482 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
484 if ( check_config ) {
485 fprintf( stderr, "config check failed\n" );
491 if ( check_config ) {
493 fprintf( stderr, "config check succeeded\n" );
497 if ( glue_sub_init( ) != 0 ) {
499 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
501 Debug( LDAP_DEBUG_ANY,
502 "subordinate config error\n",
508 if ( slap_schema_check( ) != 0 ) {
510 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
512 Debug( LDAP_DEBUG_ANY,
513 "schema prep error\n",
521 rc = ldap_pvt_tls_init();
524 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
526 Debug( LDAP_DEBUG_ANY,
527 "main: TLS init failed: %d\n",
531 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
535 rc = ldap_pvt_tls_init_def_ctx();
538 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
540 Debug( LDAP_DEBUG_ANY,
541 "main: TLS init def ctx failed: %d\n",
545 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
550 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
551 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
554 (void) SIGNAL( SIGPIPE, SIG_IGN );
557 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
559 (void) SIGNAL( SIGINT, slap_sig_shutdown );
560 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
562 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
565 /* SIGBREAK is generated when Ctrl-Break is pressed. */
566 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
570 lutil_detach( no_detach, 0 );
571 #endif /* HAVE_WINSOCK */
578 * FIXME: moved here from slapd_daemon_task()
579 * because back-monitor db_open() needs it
583 if ( slap_startup( NULL ) != 0 ) {
585 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
590 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
592 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
596 if ( slapd_pid_file != NULL ) {
597 FILE *fp = fopen( slapd_pid_file, "w" );
600 fprintf( fp, "%d\n", (int) getpid() );
604 free(slapd_pid_file);
605 slapd_pid_file = NULL;
609 if ( slapd_args_file != NULL ) {
610 FILE *fp = fopen( slapd_args_file, "w" );
613 for ( i = 0; i < g_argc; i++ ) {
614 fprintf( fp, "%s ", g_argv[i] );
619 free(slapd_args_file);
620 slapd_args_file = NULL;
624 #ifdef HAVE_NT_EVENT_LOG
626 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
631 #ifdef HAVE_NT_SERVICE_MANAGER
632 /* Throw away the event that we used during the startup process. */
634 ldap_pvt_thread_cond_destroy( &started_event );
638 /* remember an error during shutdown */
639 rc |= slap_shutdown( NULL );
642 /* remember an error during destroy */
643 rc |= slap_destroy();
645 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
646 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
647 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
660 #ifdef HAVE_NT_EVENT_LOG
662 lutil_LogStoppedEvent( serverName );
666 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
668 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
672 #ifdef HAVE_NT_SERVICE_MANAGER
673 lutil_ReportShutdownComplete();
679 slapd_daemon_destroy();
685 lutil_passwd_destroy();
688 ldap_pvt_tls_destroy();
691 if ( slapd_pid_file != NULL ) {
692 unlink( slapd_pid_file );
694 if ( slapd_args_file != NULL ) {
695 unlink( slapd_args_file );
701 mal_dumpleaktrace( leakfile );
711 * Catch and discard terminated child processes, to avoid zombies.
715 wait4child( int sig )
717 int save_errno = errno;
722 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
725 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
731 (void) SIGNAL_REINSTALL( sig, wait4child );
735 #endif /* LDAP_SIGCHLD */
741 * Convert a string to an integer by means of a dispatcher table
742 * if the string is not in the table return the default
746 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
748 int retVal = defaultVal;
751 for (disp = dispatcher; disp->stringVal; disp++) {
753 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
755 retVal = disp->intVal;
764 #endif /* LOG_LOCAL4 */