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, slapindex, slappasswd;
76 {"slapindex", slapindex},
77 {"slappasswd", slappasswd},
82 * when more than one slapd is running on one machine, each one might have
83 * it's own LOCAL for syslogging and must have its own pid/args files
86 #ifndef HAVE_MKVERSION
87 const char Versionstr[] =
88 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
93 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
95 typedef struct _str2intDispatch {
99 } STRDISP, *STRDISP_P;
102 /* table to compute syslog-options to integer */
103 static STRDISP syslog_types[] = {
104 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
105 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
106 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
107 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
108 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
109 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
110 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
111 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
115 static int cnvt_str2int( char *, STRDISP_P, int );
117 #endif /* LOG_LOCAL4 */
119 #define CHECK_NONE 0x00
120 #define CHECK_CONFIG 0x01
121 #define CHECK_DN 0x02
122 static int check = CHECK_NONE;
123 static struct berval check_dn = BER_BVC("");
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 (a|c|i|p)\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 "\t-t\t\tCheck configuration file and exit\n"
151 "\t-D dn\tCheck dn and exit\n"
152 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
153 "\t-u user\t\tUser (id or name) to run as\n"
154 "\t-V\t\tprint version info (-VV only)\n"
159 #ifdef HAVE_NT_SERVICE_MANAGER
160 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
162 int main( int argc, char **argv )
165 int i, no_detach = 0;
168 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
169 char *username = NULL;
170 char *groupname = NULL;
172 #if defined(HAVE_CHROOT)
173 char *sandbox = NULL;
176 int syslogUser = DEFAULT_SYSLOG_USER;
180 char **g_argv = argv;
182 #ifdef HAVE_NT_SERVICE_MANAGER
183 char *configfile = ".\\slapd.conf";
185 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
188 int serverMode = SLAP_SERVER_MODE;
190 struct berval cookie = { 0, NULL };
191 struct sync_cookie *scp = NULL;
192 struct sync_cookie *scp_entry = NULL;
196 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
203 serverName = lutil_progname( "slapd", argc, argv );
205 if ( strcmp( serverName, "slapd" ) ) {
206 for (i=0; tools[i].name; i++) {
207 if ( !strcmp( serverName, tools[i].name ) ) {
208 rc = tools[i].func(argc, argv);
214 #ifdef HAVE_NT_SERVICE_MANAGER
219 char *regService = NULL;
221 if ( is_NT_Service ) {
222 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
223 if ( strcmp(serverName, SERVICE_NAME) )
224 regService = serverName;
227 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
232 lutil_log_initialize( argc, argv );
233 LDAP_LOG( SLAPD, INFO,
234 "main: new debug level from registry is: %d\n",
237 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
241 newUrls = (char *) lutil_getRegParam(regService, "Urls");
247 urls = ch_strdup(newUrls);
249 LDAP_LOG( SLAPD, INFO,
250 "main: new urls from registry: %s\n", urls, 0, 0 );
252 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
258 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
259 if ( newConfigFile != NULL )
261 configfile = newConfigFile;
263 LDAP_LOG( SLAPD, INFO,
264 "main: new config file from registry is: %s\n", configfile, 0, 0 );
266 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
273 while ( (i = getopt( argc, argv,
284 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
291 slap_inet4or6 = AF_INET;
294 slap_inet4or6 = AF_INET6;
298 case 'h': /* listen URLs */
299 if ( urls != NULL ) free( urls );
300 urls = ch_strdup( optarg );
303 case 'c': /* provide sync cookie, override if exist in replica */
304 scp = (struct sync_cookie *) ch_calloc( 1,
305 sizeof( struct sync_cookie ));
306 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
307 ber_bvarray_add( &scp->octet_str, &cookie );
308 slap_parse_sync_cookie( scp );
310 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
311 if ( scp->rid == scp_entry->rid ) {
313 LDAP_LOG( OPERATION, CRIT,
314 "main: duplicated replica id in cookies\n",
317 Debug( LDAP_DEBUG_ANY,
318 "main: duplicated replica id in cookies\n",
321 slap_sync_cookie_free( scp, 1 );
325 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
328 case 'd': /* set debug level and 'do not detach' flag */
331 slap_debug |= atoi( optarg );
333 if ( atoi( optarg ) != 0 )
334 fputs( "must compile with LDAP_DEBUG for debugging\n",
339 case 'f': /* read config file */
340 configfile = ch_strdup( optarg );
343 case 's': /* set syslog level */
344 ldap_syslog = atoi( optarg );
348 case 'l': /* set syslog local user */
349 syslogUser = cnvt_str2int( optarg,
350 syslog_types, DEFAULT_SYSLOG_USER );
356 if( sandbox ) free(sandbox);
357 sandbox = ch_strdup( optarg );
361 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
362 case 'u': /* user name */
363 if( username ) free(username);
364 username = ch_strdup( optarg );
367 case 'g': /* group name */
368 if( groupname ) free(groupname);
369 groupname = ch_strdup( optarg );
371 #endif /* SETUID && GETUID */
373 case 'n': /* NT service name */
374 serverName = ch_strdup( optarg );
378 check |= CHECK_CONFIG;
383 check_dn.bv_val = optarg;
384 check_dn.bv_len = strlen( optarg );
392 for (i=0; tools[i].name; i++) {
393 if ( optarg[0] == tools[i].name[4] ) {
394 rc = tools[i].func(argc, argv);
402 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
408 lutil_log_initialize( argc, argv );
410 lutil_set_debug_level( "slapd", slap_debug );
411 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
412 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
413 ldif_debug = slap_debug;
417 fprintf( stderr, "%s\n", Versionstr );
418 if ( version > 1 ) goto stop;
424 logName = ch_strdup( serverName );
427 logName = serverName;
431 openlog( logName, OPENLOG_OPTIONS, syslogUser );
433 openlog( logName, OPENLOG_OPTIONS );
441 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
443 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
446 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
448 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
452 #if defined(HAVE_CHROOT)
454 if ( chdir( sandbox ) ) {
459 if ( chroot( sandbox ) ) {
467 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
468 if ( username != NULL || groupname != NULL ) {
469 slap_init_user( username, groupname );
478 if ( module_init() != 0 ) {
480 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
485 if ( slap_schema_init( ) != 0 ) {
487 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
489 Debug( LDAP_DEBUG_ANY,
490 "schema initialization error\n",
497 if ( slap_init( serverMode, serverName ) != 0 ) {
499 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
503 if ( slap_controls_init( ) != 0 ) {
505 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
507 Debug( LDAP_DEBUG_ANY,
508 "controls initialization error\n",
516 /* Library defaults to full certificate checking. This is correct when
517 * a client is verifying a server because all servers should have a
518 * valid cert. But few clients have valid certs, so we want our default
519 * to be no checking. The config file can override this as usual.
522 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
526 if ( slapi_int_initialize() != 0 ) {
528 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
530 Debug( LDAP_DEBUG_ANY,
531 "slapi initialization error\n",
537 #endif /* LDAP_SLAPI */
539 if ( overlay_init() ) {
543 if ( read_config( configfile, 0 ) != 0 ) {
545 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
547 if ( check & CHECK_CONFIG ) {
548 fprintf( stderr, "config check failed\n" );
554 if ( check & CHECK_CONFIG ) {
555 fprintf( stderr, "config check succeeded\n" );
557 check &= ~CHECK_CONFIG;
558 if ( check == CHECK_NONE ) {
564 if ( glue_sub_init( ) != 0 ) {
566 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
568 Debug( LDAP_DEBUG_ANY,
569 "subordinate config error\n",
575 if ( slap_schema_check( ) != 0 ) {
577 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
579 Debug( LDAP_DEBUG_ANY,
580 "schema prep error\n",
587 if ( check & CHECK_DN ) {
588 struct berval pdn, ndn;
590 rc = dnPrettyNormal( NULL, &check_dn,
592 if ( rc != LDAP_SUCCESS ) {
593 fprintf( stderr, "DN: <%s> check failed %d (%s)\n",
595 ldap_err2string( rc ) );
599 fprintf( stderr, "DN: <%s> check succeeded\n"
603 ndn.bv_val, pdn.bv_val );
608 if ( check == CHECK_NONE ) {
614 rc = ldap_pvt_tls_init();
617 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
619 Debug( LDAP_DEBUG_ANY,
620 "main: TLS init failed: %d\n",
624 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
628 rc = ldap_pvt_tls_init_def_ctx();
631 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
633 Debug( LDAP_DEBUG_ANY,
634 "main: TLS init def ctx failed: %d\n",
638 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
643 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
644 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
647 (void) SIGNAL( SIGPIPE, SIG_IGN );
650 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
652 (void) SIGNAL( SIGINT, slap_sig_shutdown );
653 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
655 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
658 /* SIGBREAK is generated when Ctrl-Break is pressed. */
659 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
663 lutil_detach( no_detach, 0 );
664 #endif /* HAVE_WINSOCK */
671 * FIXME: moved here from slapd_daemon_task()
672 * because back-monitor db_open() needs it
676 if ( slap_startup( NULL ) != 0 ) {
678 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
683 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
685 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
689 if ( slapd_pid_file != NULL ) {
690 FILE *fp = fopen( slapd_pid_file, "w" );
693 fprintf( fp, "%d\n", (int) getpid() );
697 free(slapd_pid_file);
698 slapd_pid_file = NULL;
702 if ( slapd_args_file != NULL ) {
703 FILE *fp = fopen( slapd_args_file, "w" );
706 for ( i = 0; i < g_argc; i++ ) {
707 fprintf( fp, "%s ", g_argv[i] );
712 free(slapd_args_file);
713 slapd_args_file = NULL;
717 #ifdef HAVE_NT_EVENT_LOG
719 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
724 #ifdef HAVE_NT_SERVICE_MANAGER
725 /* Throw away the event that we used during the startup process. */
727 ldap_pvt_thread_cond_destroy( &started_event );
731 /* remember an error during shutdown */
732 rc |= slap_shutdown( NULL );
735 /* remember an error during destroy */
736 rc |= slap_destroy();
738 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
739 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
740 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
753 #ifdef HAVE_NT_EVENT_LOG
755 lutil_LogStoppedEvent( serverName );
759 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
761 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
765 #ifdef HAVE_NT_SERVICE_MANAGER
766 lutil_ReportShutdownComplete();
772 slapd_daemon_destroy();
778 lutil_passwd_destroy();
781 ldap_pvt_tls_destroy();
784 if ( slapd_pid_file != NULL ) {
785 unlink( slapd_pid_file );
787 if ( slapd_args_file != NULL ) {
788 unlink( slapd_args_file );
794 mal_dumpleaktrace( leakfile );
804 * Catch and discard terminated child processes, to avoid zombies.
808 wait4child( int sig )
810 int save_errno = errno;
815 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
818 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
824 (void) SIGNAL_REINSTALL( sig, wait4child );
828 #endif /* LDAP_SIGCHLD */
834 * Convert a string to an integer by means of a dispatcher table
835 * if the string is not in the table return the default
839 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
841 int retVal = defaultVal;
844 for (disp = dispatcher; disp->stringVal; disp++) {
846 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
848 retVal = disp->intVal;
857 #endif /* LOG_LOCAL4 */