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)
67 typedef int (MainFunc) LDAP_P(( int argc, char *argv[] ));
68 extern MainFunc slapadd, slapcat, slapdn, slapindex, slappasswd, slaptest;
77 {"slapindex", slapindex},
78 {"slappasswd", slappasswd},
79 {"slaptest", slaptest},
84 * when more than one slapd is running on one machine, each one might have
85 * it's own LOCAL for syslogging and must have its own pid/args files
88 #ifndef HAVE_MKVERSION
89 const char Versionstr[] =
90 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
95 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
97 typedef struct _str2intDispatch {
101 } STRDISP, *STRDISP_P;
104 /* table to compute syslog-options to integer */
105 static STRDISP syslog_types[] = {
106 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
107 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
108 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
109 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
110 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
111 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
112 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
113 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
117 static int cnvt_str2int( char *, STRDISP_P, int );
119 #endif /* LOG_LOCAL4 */
121 #define CHECK_NONE 0x00
122 #define CHECK_CONFIG 0x01
123 static int check = CHECK_NONE;
124 static int version = 0;
130 "usage: %s options\n", name );
132 "\t-4\t\tIPv4 only\n"
133 "\t-6\t\tIPv6 only\n"
134 "\t-T {acdipt}\tRun in Tool mode\n"
135 "\t-c cookie\tSync cookie of consumer\n"
136 "\t-d level\tDebug level" "\n"
137 "\t-f filename\tConfiguration file\n"
138 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
139 "\t-g group\tGroup (id or name) to run as\n"
141 "\t-h URLs\t\tList of URLs to serve\n"
143 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
145 "\t-n serverName\tService name\n"
147 "\t-r directory\tSandbox directory to chroot to\n"
149 "\t-s level\tSyslog level\n"
150 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
151 "\t-u user\t\tUser (id or name) to run as\n"
152 "\t-V\t\tprint version info (-VV only)\n"
157 #ifdef HAVE_NT_SERVICE_MANAGER
158 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
160 int main( int argc, char **argv )
163 int i, no_detach = 0;
166 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
167 char *username = NULL;
168 char *groupname = NULL;
170 #if defined(HAVE_CHROOT)
171 char *sandbox = NULL;
174 int syslogUser = DEFAULT_SYSLOG_USER;
178 char **g_argv = argv;
180 #ifdef HAVE_NT_SERVICE_MANAGER
181 char *configfile = ".\\slapd.conf";
183 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
186 int serverMode = SLAP_SERVER_MODE;
188 struct berval cookie = { 0, NULL };
189 struct sync_cookie *scp = NULL;
190 struct sync_cookie *scp_entry = NULL;
194 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
201 serverName = lutil_progname( "slapd", argc, argv );
203 if ( strcmp( serverName, "slapd" ) ) {
204 for (i=0; tools[i].name; i++) {
205 if ( !strcmp( serverName, tools[i].name ) ) {
206 rc = tools[i].func(argc, argv);
212 #ifdef HAVE_NT_SERVICE_MANAGER
217 char *regService = NULL;
219 if ( is_NT_Service ) {
220 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
221 if ( strcmp(serverName, SERVICE_NAME) )
222 regService = serverName;
225 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
230 lutil_log_initialize( argc, argv );
231 LDAP_LOG( SLAPD, INFO,
232 "main: new debug level from registry is: %d\n",
235 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
239 newUrls = (char *) lutil_getRegParam(regService, "Urls");
245 urls = ch_strdup(newUrls);
247 LDAP_LOG( SLAPD, INFO,
248 "main: new urls from registry: %s\n", urls, 0, 0 );
250 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
256 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
257 if ( newConfigFile != NULL )
259 configfile = newConfigFile;
261 LDAP_LOG( SLAPD, INFO,
262 "main: new config file from registry is: %s\n", configfile, 0, 0 );
264 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
271 while ( (i = getopt( argc, argv,
282 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
289 slap_inet4or6 = AF_INET;
292 slap_inet4or6 = AF_INET6;
296 case 'h': /* listen URLs */
297 if ( urls != NULL ) free( urls );
298 urls = ch_strdup( optarg );
301 case 'c': /* provide sync cookie, override if exist in replica */
302 scp = (struct sync_cookie *) ch_calloc( 1,
303 sizeof( struct sync_cookie ));
304 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
305 ber_bvarray_add( &scp->octet_str, &cookie );
306 slap_parse_sync_cookie( scp );
308 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
309 if ( scp->rid == scp_entry->rid ) {
311 LDAP_LOG( OPERATION, CRIT,
312 "main: duplicated replica id in cookies\n",
315 Debug( LDAP_DEBUG_ANY,
316 "main: duplicated replica id in cookies\n",
319 slap_sync_cookie_free( scp, 1 );
323 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
326 case 'd': /* set debug level and 'do not detach' flag */
329 slap_debug |= atoi( optarg );
331 if ( atoi( optarg ) != 0 )
332 fputs( "must compile with LDAP_DEBUG for debugging\n",
337 case 'f': /* read config file */
338 configfile = ch_strdup( optarg );
341 case 's': /* set syslog level */
342 ldap_syslog = atoi( optarg );
346 case 'l': /* set syslog local user */
347 syslogUser = cnvt_str2int( optarg,
348 syslog_types, DEFAULT_SYSLOG_USER );
354 if( sandbox ) free(sandbox);
355 sandbox = ch_strdup( optarg );
359 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
360 case 'u': /* user name */
361 if( username ) free(username);
362 username = ch_strdup( optarg );
365 case 'g': /* group name */
366 if( groupname ) free(groupname);
367 groupname = ch_strdup( optarg );
369 #endif /* SETUID && GETUID */
371 case 'n': /* NT service name */
372 serverName = ch_strdup( optarg );
376 /* deprecated; use slaptest instead */
377 fprintf( stderr, "option -t deprecated; "
378 "use slaptest command instead\n" );
379 check |= CHECK_CONFIG;
387 for (i=0; tools[i].name; i++) {
388 if ( optarg[0] == tools[i].name[4] ) {
389 rc = tools[i].func(argc, argv);
397 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
403 lutil_log_initialize( argc, argv );
405 lutil_set_debug_level( "slapd", slap_debug );
406 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
407 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
408 ldif_debug = slap_debug;
412 fprintf( stderr, "%s\n", Versionstr );
413 if ( version > 1 ) goto stop;
419 logName = ch_strdup( serverName );
422 logName = serverName;
426 openlog( logName, OPENLOG_OPTIONS, syslogUser );
428 openlog( logName, OPENLOG_OPTIONS );
436 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
438 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
441 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
443 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
447 #if defined(HAVE_CHROOT)
449 if ( chdir( sandbox ) ) {
454 if ( chroot( sandbox ) ) {
462 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
463 if ( username != NULL || groupname != NULL ) {
464 slap_init_user( username, groupname );
473 if ( module_init() != 0 ) {
475 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
480 if ( slap_schema_init( ) != 0 ) {
482 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
484 Debug( LDAP_DEBUG_ANY,
485 "schema initialization error\n",
492 if ( slap_init( serverMode, serverName ) != 0 ) {
494 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
498 if ( slap_controls_init( ) != 0 ) {
500 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
502 Debug( LDAP_DEBUG_ANY,
503 "controls initialization error\n",
511 /* Library defaults to full certificate checking. This is correct when
512 * a client is verifying a server because all servers should have a
513 * valid cert. But few clients have valid certs, so we want our default
514 * to be no checking. The config file can override this as usual.
517 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
521 if ( slapi_int_initialize() != 0 ) {
523 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
525 Debug( LDAP_DEBUG_ANY,
526 "slapi initialization error\n",
532 #endif /* LDAP_SLAPI */
534 if ( overlay_init() ) {
538 if ( read_config( configfile, 0 ) != 0 ) {
540 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
542 if ( check & CHECK_CONFIG ) {
543 fprintf( stderr, "config check failed\n" );
549 if ( check & CHECK_CONFIG ) {
550 fprintf( stderr, "config check succeeded\n" );
552 check &= ~CHECK_CONFIG;
553 if ( check == CHECK_NONE ) {
559 if ( glue_sub_init( ) != 0 ) {
561 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
563 Debug( LDAP_DEBUG_ANY,
564 "subordinate config error\n",
570 if ( slap_schema_check( ) != 0 ) {
572 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
574 Debug( LDAP_DEBUG_ANY,
575 "schema prep error\n",
583 rc = ldap_pvt_tls_init();
586 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
588 Debug( LDAP_DEBUG_ANY,
589 "main: TLS init failed: %d\n",
593 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
597 rc = ldap_pvt_tls_init_def_ctx();
600 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
602 Debug( LDAP_DEBUG_ANY,
603 "main: TLS init def ctx failed: %d\n",
607 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
612 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
613 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
616 (void) SIGNAL( SIGPIPE, SIG_IGN );
619 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
621 (void) SIGNAL( SIGINT, slap_sig_shutdown );
622 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
624 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
627 /* SIGBREAK is generated when Ctrl-Break is pressed. */
628 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
632 lutil_detach( no_detach, 0 );
633 #endif /* HAVE_WINSOCK */
640 * FIXME: moved here from slapd_daemon_task()
641 * because back-monitor db_open() needs it
645 if ( slap_startup( NULL ) != 0 ) {
647 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
652 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
654 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
658 if ( slapd_pid_file != NULL ) {
659 FILE *fp = fopen( slapd_pid_file, "w" );
662 fprintf( fp, "%d\n", (int) getpid() );
666 free(slapd_pid_file);
667 slapd_pid_file = NULL;
671 if ( slapd_args_file != NULL ) {
672 FILE *fp = fopen( slapd_args_file, "w" );
675 for ( i = 0; i < g_argc; i++ ) {
676 fprintf( fp, "%s ", g_argv[i] );
681 free(slapd_args_file);
682 slapd_args_file = NULL;
686 #ifdef HAVE_NT_EVENT_LOG
688 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
693 #ifdef HAVE_NT_SERVICE_MANAGER
694 /* Throw away the event that we used during the startup process. */
696 ldap_pvt_thread_cond_destroy( &started_event );
700 /* remember an error during shutdown */
701 rc |= slap_shutdown( NULL );
704 /* remember an error during destroy */
705 rc |= slap_destroy();
707 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
708 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
709 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
722 #ifdef HAVE_NT_EVENT_LOG
724 lutil_LogStoppedEvent( serverName );
728 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
730 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
734 #ifdef HAVE_NT_SERVICE_MANAGER
735 lutil_ReportShutdownComplete();
741 slapd_daemon_destroy();
747 lutil_passwd_destroy();
750 ldap_pvt_tls_destroy();
753 if ( slapd_pid_file != NULL ) {
754 unlink( slapd_pid_file );
756 if ( slapd_args_file != NULL ) {
757 unlink( slapd_args_file );
763 mal_dumpleaktrace( leakfile );
773 * Catch and discard terminated child processes, to avoid zombies.
777 wait4child( int sig )
779 int save_errno = errno;
784 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
787 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
793 (void) SIGNAL_REINSTALL( sig, wait4child );
797 #endif /* LDAP_SIGCHLD */
803 * Convert a string to an integer by means of a dispatcher table
804 * if the string is not in the table return the default
808 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
810 int retVal = defaultVal;
813 for (disp = dispatcher; disp->stringVal; disp++) {
815 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
817 retVal = disp->intVal;
826 #endif /* LOG_LOCAL4 */