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, slapsaslauth;
77 {"slapindex", slapindex},
78 {"slappasswd", slappasswd},
79 {"slaptest", slaptest},
80 {"slapsaslauth", slapsaslauth},
85 * when more than one slapd is running on one machine, each one might have
86 * it's own LOCAL for syslogging and must have its own pid/args files
89 #ifndef HAVE_MKVERSION
90 const char Versionstr[] =
91 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
96 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
98 typedef struct _str2intDispatch {
102 } STRDISP, *STRDISP_P;
105 /* table to compute syslog-options to integer */
106 static STRDISP syslog_types[] = {
107 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
108 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
109 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
110 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
111 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
112 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
113 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
114 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
118 static int cnvt_str2int( char *, STRDISP_P, int );
120 #endif /* LOG_LOCAL4 */
122 #define CHECK_NONE 0x00
123 #define CHECK_CONFIG 0x01
124 static int check = CHECK_NONE;
125 static int version = 0;
131 "usage: %s options\n", name );
133 "\t-4\t\tIPv4 only\n"
134 "\t-6\t\tIPv6 only\n"
135 "\t-T {acdipt}\tRun in Tool mode\n"
136 "\t-c cookie\tSync cookie of consumer\n"
137 "\t-d level\tDebug level" "\n"
138 "\t-f filename\tConfiguration file\n"
139 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
140 "\t-g group\tGroup (id or name) to run as\n"
142 "\t-h URLs\t\tList of URLs to serve\n"
144 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
146 "\t-n serverName\tService name\n"
148 "\t-r directory\tSandbox directory to chroot to\n"
150 "\t-s level\tSyslog level\n"
151 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
152 "\t-u user\t\tUser (id or name) to run as\n"
153 "\t-V\t\tprint version info (-VV only)\n"
158 #ifdef HAVE_NT_SERVICE_MANAGER
159 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
161 int main( int argc, char **argv )
164 int i, no_detach = 0;
167 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
168 char *username = NULL;
169 char *groupname = NULL;
171 #if defined(HAVE_CHROOT)
172 char *sandbox = NULL;
175 int syslogUser = DEFAULT_SYSLOG_USER;
179 char **g_argv = argv;
181 #ifdef HAVE_NT_SERVICE_MANAGER
182 char *configfile = ".\\slapd.conf";
184 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
187 int serverMode = SLAP_SERVER_MODE;
189 struct berval cookie = BER_BVNULL;
190 struct sync_cookie *scp = NULL;
191 struct sync_cookie *scp_entry = NULL;
195 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
202 serverName = lutil_progname( "slapd", argc, argv );
204 if ( strcmp( serverName, "slapd" ) ) {
205 for (i=0; tools[i].name; i++) {
206 if ( !strcmp( serverName, tools[i].name ) ) {
207 rc = tools[i].func(argc, argv);
213 #ifdef HAVE_NT_SERVICE_MANAGER
218 char *regService = NULL;
220 if ( is_NT_Service ) {
221 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
222 if ( strcmp(serverName, SERVICE_NAME) )
223 regService = serverName;
226 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
231 lutil_log_initialize( argc, argv );
232 LDAP_LOG( SLAPD, INFO,
233 "main: new debug level from registry is: %d\n",
236 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
240 newUrls = (char *) lutil_getRegParam(regService, "Urls");
246 urls = ch_strdup(newUrls);
248 LDAP_LOG( SLAPD, INFO,
249 "main: new urls from registry: %s\n", urls, 0, 0 );
251 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
257 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
258 if ( newConfigFile != NULL )
260 configfile = newConfigFile;
262 LDAP_LOG( SLAPD, INFO,
263 "main: new config file from registry is: %s\n", configfile, 0, 0 );
265 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
272 while ( (i = getopt( argc, argv,
283 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
290 slap_inet4or6 = AF_INET;
293 slap_inet4or6 = AF_INET6;
297 case 'h': /* listen URLs */
298 if ( urls != NULL ) free( urls );
299 urls = ch_strdup( optarg );
302 case 'c': /* provide sync cookie, override if exist in replica */
303 scp = (struct sync_cookie *) ch_calloc( 1,
304 sizeof( struct sync_cookie ));
305 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
306 ber_bvarray_add( &scp->octet_str, &cookie );
307 slap_parse_sync_cookie( scp );
309 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
310 if ( scp->rid == scp_entry->rid ) {
312 LDAP_LOG( OPERATION, CRIT,
313 "main: duplicated replica id in cookies\n",
316 Debug( LDAP_DEBUG_ANY,
317 "main: duplicated replica id in cookies\n",
320 slap_sync_cookie_free( scp, 1 );
324 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
327 case 'd': /* set debug level and 'do not detach' flag */
330 slap_debug |= atoi( optarg );
332 if ( atoi( optarg ) != 0 )
333 fputs( "must compile with LDAP_DEBUG for debugging\n",
338 case 'f': /* read config file */
339 configfile = ch_strdup( optarg );
342 case 's': /* set syslog level */
343 ldap_syslog = atoi( optarg );
347 case 'l': /* set syslog local user */
348 syslogUser = cnvt_str2int( optarg,
349 syslog_types, DEFAULT_SYSLOG_USER );
355 if( sandbox ) free(sandbox);
356 sandbox = ch_strdup( optarg );
360 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
361 case 'u': /* user name */
362 if( username ) free(username);
363 username = ch_strdup( optarg );
366 case 'g': /* group name */
367 if( groupname ) free(groupname);
368 groupname = ch_strdup( optarg );
370 #endif /* SETUID && GETUID */
372 case 'n': /* NT service name */
373 serverName = ch_strdup( optarg );
377 /* deprecated; use slaptest instead */
378 fprintf( stderr, "option -t deprecated; "
379 "use slaptest command instead\n" );
380 check |= CHECK_CONFIG;
388 for (i=0; tools[i].name; i++) {
389 if ( optarg[0] == tools[i].name[4] ) {
390 rc = tools[i].func(argc, argv);
398 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
404 lutil_log_initialize( argc, argv );
406 lutil_set_debug_level( "slapd", slap_debug );
407 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
408 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
409 ldif_debug = slap_debug;
413 fprintf( stderr, "%s\n", Versionstr );
414 if ( version > 1 ) goto stop;
420 logName = ch_strdup( serverName );
423 logName = serverName;
427 openlog( logName, OPENLOG_OPTIONS, syslogUser );
429 openlog( logName, OPENLOG_OPTIONS );
437 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
439 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
442 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
444 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
448 #if defined(HAVE_CHROOT)
450 if ( chdir( sandbox ) ) {
455 if ( chroot( sandbox ) ) {
463 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
464 if ( username != NULL || groupname != NULL ) {
465 slap_init_user( username, groupname );
474 if ( module_init() != 0 ) {
476 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
481 if ( slap_schema_init( ) != 0 ) {
483 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
485 Debug( LDAP_DEBUG_ANY,
486 "schema initialization error\n",
493 if ( slap_init( serverMode, serverName ) != 0 ) {
495 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
499 if ( slap_controls_init( ) != 0 ) {
501 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
503 Debug( LDAP_DEBUG_ANY,
504 "controls initialization error\n",
512 /* Library defaults to full certificate checking. This is correct when
513 * a client is verifying a server because all servers should have a
514 * valid cert. But few clients have valid certs, so we want our default
515 * to be no checking. The config file can override this as usual.
518 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
522 if ( slapi_int_initialize() != 0 ) {
524 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
526 Debug( LDAP_DEBUG_ANY,
527 "slapi initialization error\n",
533 #endif /* LDAP_SLAPI */
535 if ( overlay_init() ) {
539 if ( read_config( configfile, 0 ) != 0 ) {
541 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
543 if ( check & CHECK_CONFIG ) {
544 fprintf( stderr, "config check failed\n" );
550 if ( check & CHECK_CONFIG ) {
551 fprintf( stderr, "config check succeeded\n" );
553 check &= ~CHECK_CONFIG;
554 if ( check == CHECK_NONE ) {
560 if ( glue_sub_init( ) != 0 ) {
562 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
564 Debug( LDAP_DEBUG_ANY,
565 "subordinate config error\n",
571 if ( slap_schema_check( ) != 0 ) {
573 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
575 Debug( LDAP_DEBUG_ANY,
576 "schema prep error\n",
584 rc = ldap_pvt_tls_init();
587 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
589 Debug( LDAP_DEBUG_ANY,
590 "main: TLS init failed: %d\n",
594 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
598 rc = ldap_pvt_tls_init_def_ctx();
601 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
603 Debug( LDAP_DEBUG_ANY,
604 "main: TLS init def ctx failed: %d\n",
608 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
613 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
614 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
617 (void) SIGNAL( SIGPIPE, SIG_IGN );
620 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
622 (void) SIGNAL( SIGINT, slap_sig_shutdown );
623 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
625 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
628 /* SIGBREAK is generated when Ctrl-Break is pressed. */
629 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
633 lutil_detach( no_detach, 0 );
634 #endif /* HAVE_WINSOCK */
641 * FIXME: moved here from slapd_daemon_task()
642 * because back-monitor db_open() needs it
646 if ( slap_startup( NULL ) != 0 ) {
648 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
653 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
655 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
659 if ( slapd_pid_file != NULL ) {
660 FILE *fp = fopen( slapd_pid_file, "w" );
663 fprintf( fp, "%d\n", (int) getpid() );
667 free(slapd_pid_file);
668 slapd_pid_file = NULL;
672 if ( slapd_args_file != NULL ) {
673 FILE *fp = fopen( slapd_args_file, "w" );
676 for ( i = 0; i < g_argc; i++ ) {
677 fprintf( fp, "%s ", g_argv[i] );
682 free(slapd_args_file);
683 slapd_args_file = NULL;
687 #ifdef HAVE_NT_EVENT_LOG
689 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
694 #ifdef HAVE_NT_SERVICE_MANAGER
695 /* Throw away the event that we used during the startup process. */
697 ldap_pvt_thread_cond_destroy( &started_event );
701 /* remember an error during shutdown */
702 rc |= slap_shutdown( NULL );
705 /* remember an error during destroy */
706 rc |= slap_destroy();
708 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
709 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
710 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
723 #ifdef HAVE_NT_EVENT_LOG
725 lutil_LogStoppedEvent( serverName );
729 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
731 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
735 #ifdef HAVE_NT_SERVICE_MANAGER
736 lutil_ReportShutdownComplete();
742 slapd_daemon_destroy();
748 lutil_passwd_destroy();
751 ldap_pvt_tls_destroy();
754 if ( slapd_pid_file != NULL ) {
755 unlink( slapd_pid_file );
757 if ( slapd_args_file != NULL ) {
758 unlink( slapd_args_file );
764 mal_dumpleaktrace( leakfile );
774 * Catch and discard terminated child processes, to avoid zombies.
778 wait4child( int sig )
780 int save_errno = errno;
785 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
788 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
794 (void) SIGNAL_REINSTALL( sig, wait4child );
798 #endif /* LDAP_SIGCHLD */
804 * Convert a string to an integer by means of a dispatcher table
805 * if the string is not in the table return the default
809 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
811 int retVal = defaultVal;
814 for (disp = dispatcher; disp->stringVal; disp++) {
816 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
818 retVal = disp->intVal;
827 #endif /* LOG_LOCAL4 */