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,
78 {"slapindex", slapindex},
79 {"slappasswd", slappasswd},
80 {"slaptest", slaptest},
81 {"slapauth", slapauth},
86 * when more than one slapd is running on one machine, each one might have
87 * it's own LOCAL for syslogging and must have its own pid/args files
90 #ifndef HAVE_MKVERSION
91 const char Versionstr[] =
92 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
97 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
99 typedef struct _str2intDispatch {
103 } STRDISP, *STRDISP_P;
106 /* table to compute syslog-options to integer */
107 static STRDISP syslog_types[] = {
108 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
109 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
110 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
111 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
112 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
113 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
114 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
115 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
119 static int cnvt_str2int( char *, STRDISP_P, int );
121 #endif /* LOG_LOCAL4 */
123 #define CHECK_NONE 0x00
124 #define CHECK_CONFIG 0x01
125 static int check = CHECK_NONE;
126 static int version = 0;
132 "usage: %s options\n", name );
134 "\t-4\t\tIPv4 only\n"
135 "\t-6\t\tIPv6 only\n"
136 "\t-T {add|auth|cat|dn|index|passwd|test}\n"
137 "\t\t\tRun in Tool mode\n"
138 "\t-c cookie\tSync cookie of consumer\n"
139 "\t-d level\tDebug level" "\n"
140 "\t-f filename\tConfiguration file\n"
141 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
142 "\t-g group\tGroup (id or name) to run as\n"
144 "\t-h URLs\t\tList of URLs to serve\n"
146 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
148 "\t-n serverName\tService name\n"
150 "\t-r directory\tSandbox directory to chroot to\n"
152 "\t-s level\tSyslog level\n"
153 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
154 "\t-u user\t\tUser (id or name) to run as\n"
155 "\t-V\t\tprint version info (-VV only)\n"
160 #ifdef HAVE_NT_SERVICE_MANAGER
161 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
163 int main( int argc, char **argv )
166 int i, no_detach = 0;
169 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
170 char *username = NULL;
171 char *groupname = NULL;
173 #if defined(HAVE_CHROOT)
174 char *sandbox = NULL;
177 int syslogUser = DEFAULT_SYSLOG_USER;
181 char **g_argv = argv;
183 #ifdef HAVE_NT_SERVICE_MANAGER
184 char *configfile = ".\\slapd.conf";
186 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
189 int serverMode = SLAP_SERVER_MODE;
191 struct berval cookie = BER_BVNULL;
192 struct sync_cookie *scp = NULL;
193 struct sync_cookie *scp_entry = NULL;
197 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
204 serverName = lutil_progname( "slapd", argc, argv );
206 if ( strcmp( serverName, "slapd" ) ) {
207 for (i=0; tools[i].name; i++) {
208 if ( !strcmp( serverName, tools[i].name ) ) {
209 rc = tools[i].func(argc, argv);
215 #ifdef HAVE_NT_SERVICE_MANAGER
220 char *regService = NULL;
222 if ( is_NT_Service ) {
223 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
224 if ( strcmp(serverName, SERVICE_NAME) )
225 regService = serverName;
228 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
233 lutil_log_initialize( argc, argv );
234 LDAP_LOG( SLAPD, INFO,
235 "main: new debug level from registry is: %d\n",
238 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
242 newUrls = (char *) lutil_getRegParam(regService, "Urls");
248 urls = ch_strdup(newUrls);
250 LDAP_LOG( SLAPD, INFO,
251 "main: new urls from registry: %s\n", urls, 0, 0 );
253 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
259 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
260 if ( newConfigFile != NULL )
262 configfile = newConfigFile;
264 LDAP_LOG( SLAPD, INFO,
265 "main: new config file from registry is: %s\n", configfile, 0, 0 );
267 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
274 while ( (i = getopt( argc, argv,
285 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
292 slap_inet4or6 = AF_INET;
295 slap_inet4or6 = AF_INET6;
299 case 'h': /* listen URLs */
300 if ( urls != NULL ) free( urls );
301 urls = ch_strdup( optarg );
304 case 'c': /* provide sync cookie, override if exist in replica */
305 scp = (struct sync_cookie *) ch_calloc( 1,
306 sizeof( struct sync_cookie ));
307 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
308 ber_bvarray_add( &scp->octet_str, &cookie );
309 slap_parse_sync_cookie( scp );
311 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
312 if ( scp->rid == scp_entry->rid ) {
314 LDAP_LOG( OPERATION, CRIT,
315 "main: duplicated replica id in cookies\n",
318 Debug( LDAP_DEBUG_ANY,
319 "main: duplicated replica id in cookies\n",
322 slap_sync_cookie_free( scp, 1 );
326 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
329 case 'd': /* set debug level and 'do not detach' flag */
332 slap_debug |= atoi( optarg );
334 if ( atoi( optarg ) != 0 )
335 fputs( "must compile with LDAP_DEBUG for debugging\n",
340 case 'f': /* read config file */
341 configfile = ch_strdup( optarg );
344 case 's': /* set syslog level */
345 ldap_syslog = atoi( optarg );
349 case 'l': /* set syslog local user */
350 syslogUser = cnvt_str2int( optarg,
351 syslog_types, DEFAULT_SYSLOG_USER );
357 if( sandbox ) free(sandbox);
358 sandbox = ch_strdup( optarg );
362 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
363 case 'u': /* user name */
364 if( username ) free(username);
365 username = ch_strdup( optarg );
368 case 'g': /* group name */
369 if( groupname ) free(groupname);
370 groupname = ch_strdup( optarg );
372 #endif /* SETUID && GETUID */
374 case 'n': /* NT service name */
375 serverName = ch_strdup( optarg );
379 /* deprecated; use slaptest instead */
380 fprintf( stderr, "option -t deprecated; "
381 "use slaptest command instead\n" );
382 check |= CHECK_CONFIG;
390 for (i=0; tools[i].name; i++) {
391 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
392 rc = tools[i].func(argc, argv);
400 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
406 lutil_log_initialize( argc, argv );
408 lutil_set_debug_level( "slapd", slap_debug );
409 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
410 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
411 ldif_debug = slap_debug;
415 fprintf( stderr, "%s\n", Versionstr );
416 if ( version > 1 ) goto stop;
422 logName = ch_strdup( serverName );
425 logName = serverName;
429 openlog( logName, OPENLOG_OPTIONS, syslogUser );
431 openlog( logName, OPENLOG_OPTIONS );
439 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
441 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
444 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
446 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
450 #if defined(HAVE_CHROOT)
452 if ( chdir( sandbox ) ) {
457 if ( chroot( sandbox ) ) {
465 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
466 if ( username != NULL || groupname != NULL ) {
467 slap_init_user( username, groupname );
476 if ( module_init() != 0 ) {
478 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
483 if ( slap_schema_init( ) != 0 ) {
485 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
487 Debug( LDAP_DEBUG_ANY,
488 "schema initialization error\n",
495 if ( slap_init( serverMode, serverName ) != 0 ) {
497 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
501 if ( slap_controls_init( ) != 0 ) {
503 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
505 Debug( LDAP_DEBUG_ANY,
506 "controls initialization error\n",
514 /* Library defaults to full certificate checking. This is correct when
515 * a client is verifying a server because all servers should have a
516 * valid cert. But few clients have valid certs, so we want our default
517 * to be no checking. The config file can override this as usual.
520 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
524 if ( slapi_int_initialize() != 0 ) {
526 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
528 Debug( LDAP_DEBUG_ANY,
529 "slapi initialization error\n",
535 #endif /* LDAP_SLAPI */
537 if ( overlay_init() ) {
541 if ( read_config( configfile, 0 ) != 0 ) {
543 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
545 if ( check & CHECK_CONFIG ) {
546 fprintf( stderr, "config check failed\n" );
552 if ( check & CHECK_CONFIG ) {
553 fprintf( stderr, "config check succeeded\n" );
555 check &= ~CHECK_CONFIG;
556 if ( check == CHECK_NONE ) {
562 if ( glue_sub_init( ) != 0 ) {
564 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
566 Debug( LDAP_DEBUG_ANY,
567 "subordinate config error\n",
573 if ( slap_schema_check( ) != 0 ) {
575 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
577 Debug( LDAP_DEBUG_ANY,
578 "schema prep error\n",
586 rc = ldap_pvt_tls_init();
589 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
591 Debug( LDAP_DEBUG_ANY,
592 "main: TLS init failed: %d\n",
596 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
600 rc = ldap_pvt_tls_init_def_ctx();
603 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
605 Debug( LDAP_DEBUG_ANY,
606 "main: TLS init def ctx failed: %d\n",
610 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
615 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
616 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
619 (void) SIGNAL( SIGPIPE, SIG_IGN );
622 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
624 (void) SIGNAL( SIGINT, slap_sig_shutdown );
625 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
627 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
630 /* SIGBREAK is generated when Ctrl-Break is pressed. */
631 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
635 lutil_detach( no_detach, 0 );
636 #endif /* HAVE_WINSOCK */
643 * FIXME: moved here from slapd_daemon_task()
644 * because back-monitor db_open() needs it
648 if ( slap_startup( NULL ) != 0 ) {
650 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
655 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
657 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
661 if ( slapd_pid_file != NULL ) {
662 FILE *fp = fopen( slapd_pid_file, "w" );
665 fprintf( fp, "%d\n", (int) getpid() );
669 free(slapd_pid_file);
670 slapd_pid_file = NULL;
674 if ( slapd_args_file != NULL ) {
675 FILE *fp = fopen( slapd_args_file, "w" );
678 for ( i = 0; i < g_argc; i++ ) {
679 fprintf( fp, "%s ", g_argv[i] );
684 free(slapd_args_file);
685 slapd_args_file = NULL;
689 #ifdef HAVE_NT_EVENT_LOG
691 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
696 #ifdef HAVE_NT_SERVICE_MANAGER
697 /* Throw away the event that we used during the startup process. */
699 ldap_pvt_thread_cond_destroy( &started_event );
703 /* remember an error during shutdown */
704 rc |= slap_shutdown( NULL );
707 /* remember an error during destroy */
708 rc |= slap_destroy();
710 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
711 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
712 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
725 #ifdef HAVE_NT_EVENT_LOG
727 lutil_LogStoppedEvent( serverName );
731 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
733 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
737 #ifdef HAVE_NT_SERVICE_MANAGER
738 lutil_ReportShutdownComplete();
744 slapd_daemon_destroy();
750 lutil_passwd_destroy();
753 ldap_pvt_tls_destroy();
756 if ( slapd_pid_file != NULL ) {
757 unlink( slapd_pid_file );
759 if ( slapd_args_file != NULL ) {
760 unlink( slapd_args_file );
766 mal_dumpleaktrace( leakfile );
776 * Catch and discard terminated child processes, to avoid zombies.
780 wait4child( int sig )
782 int save_errno = errno;
787 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
790 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
796 (void) SIGNAL_REINSTALL( sig, wait4child );
800 #endif /* LDAP_SIGCHLD */
806 * Convert a string to an integer by means of a dispatcher table
807 * if the string is not in the table return the default
811 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
813 int retVal = defaultVal;
816 for (disp = dispatcher; disp->stringVal; disp++) {
818 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
820 retVal = disp->intVal;
829 #endif /* LOG_LOCAL4 */