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,
69 slaptest, slapauth, slapacl;
78 {"slapindex", slapindex},
79 {"slappasswd", slappasswd},
80 {"slaptest", slaptest},
81 {"slapauth", slapauth},
83 /* NOTE: new tools must be added in chronological order,
84 * not in alphabetical order, because for backwards
85 * compatibility name[4] is used to identify the
86 * tools; so name[4]=='a' must refer to "slapadd" and
87 * not to "slapauth". Alphabetical order can be used
88 * for tools whose name[4] is not used yet */
93 * when more than one slapd is running on one machine, each one might have
94 * it's own LOCAL for syslogging and must have its own pid/args files
97 #ifndef HAVE_MKVERSION
98 const char Versionstr[] =
99 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
104 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
106 typedef struct _str2intDispatch {
110 } STRDISP, *STRDISP_P;
113 /* table to compute syslog-options to integer */
114 static STRDISP syslog_types[] = {
115 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
116 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
117 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
118 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
119 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
120 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
121 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
122 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
126 static int cnvt_str2int( char *, STRDISP_P, int );
128 #endif /* LOG_LOCAL4 */
130 #define CHECK_NONE 0x00
131 #define CHECK_CONFIG 0x01
132 static int check = CHECK_NONE;
133 static int version = 0;
139 "usage: %s options\n", name );
141 "\t-4\t\tIPv4 only\n"
142 "\t-6\t\tIPv6 only\n"
143 "\t-T {add|auth|cat|dn|index|passwd|test}\n"
144 "\t\t\tRun in Tool mode\n"
145 "\t-c cookie\tSync cookie of consumer\n"
146 "\t-d level\tDebug level" "\n"
147 "\t-f filename\tConfiguration file\n"
148 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
149 "\t-g group\tGroup (id or name) to run as\n"
151 "\t-h URLs\t\tList of URLs to serve\n"
153 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
155 "\t-n serverName\tService name\n"
157 "\t-r directory\tSandbox directory to chroot to\n"
159 "\t-s level\tSyslog level\n"
160 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
161 "\t-u user\t\tUser (id or name) to run as\n"
162 "\t-V\t\tprint version info (-VV only)\n"
167 #ifdef HAVE_NT_SERVICE_MANAGER
168 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
170 int main( int argc, char **argv )
173 int i, no_detach = 0;
176 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
177 char *username = NULL;
178 char *groupname = NULL;
180 #if defined(HAVE_CHROOT)
181 char *sandbox = NULL;
184 int syslogUser = DEFAULT_SYSLOG_USER;
188 char **g_argv = argv;
190 #ifdef HAVE_NT_SERVICE_MANAGER
191 char *configfile = ".\\slapd.conf";
193 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
196 int serverMode = SLAP_SERVER_MODE;
198 struct berval cookie = BER_BVNULL;
199 struct sync_cookie *scp = NULL;
200 struct sync_cookie *scp_entry = NULL;
204 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
208 char *serverNamePrefix = "";
213 serverName = lutil_progname( "slapd", argc, argv );
215 if ( strcmp( serverName, "slapd" ) ) {
216 for (i=0; tools[i].name; i++) {
217 if ( !strcmp( serverName, tools[i].name ) ) {
218 rc = tools[i].func(argc, argv);
224 #ifdef HAVE_NT_SERVICE_MANAGER
229 char *regService = NULL;
231 if ( is_NT_Service ) {
232 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
233 if ( strcmp(serverName, SERVICE_NAME) )
234 regService = serverName;
237 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
242 lutil_log_initialize( argc, argv );
243 LDAP_LOG( SLAPD, INFO,
244 "main: new debug level from registry is: %d\n",
247 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
251 newUrls = (char *) lutil_getRegParam(regService, "Urls");
257 urls = ch_strdup(newUrls);
259 LDAP_LOG( SLAPD, INFO,
260 "main: new urls from registry: %s\n", urls, 0, 0 );
262 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
268 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
269 if ( newConfigFile != NULL )
271 configfile = newConfigFile;
273 LDAP_LOG( SLAPD, INFO,
274 "main: new config file from registry is: %s\n", configfile, 0, 0 );
276 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
283 while ( (i = getopt( argc, argv,
294 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
301 slap_inet4or6 = AF_INET;
304 slap_inet4or6 = AF_INET6;
308 case 'h': /* listen URLs */
309 if ( urls != NULL ) free( urls );
310 urls = ch_strdup( optarg );
313 case 'c': /* provide sync cookie, override if exist in replica */
314 scp = (struct sync_cookie *) ch_calloc( 1,
315 sizeof( struct sync_cookie ));
316 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
317 ber_bvarray_add( &scp->octet_str, &cookie );
318 slap_parse_sync_cookie( scp );
320 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
321 if ( scp->rid == scp_entry->rid ) {
323 LDAP_LOG( OPERATION, CRIT,
324 "main: duplicated replica id in cookies\n",
327 Debug( LDAP_DEBUG_ANY,
328 "main: duplicated replica id in cookies\n",
331 slap_sync_cookie_free( scp, 1 );
335 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
338 case 'd': /* set debug level and 'do not detach' flag */
341 slap_debug |= atoi( optarg );
343 if ( atoi( optarg ) != 0 )
344 fputs( "must compile with LDAP_DEBUG for debugging\n",
349 case 'f': /* read config file */
350 configfile = ch_strdup( optarg );
353 case 's': /* set syslog level */
354 ldap_syslog = atoi( optarg );
358 case 'l': /* set syslog local user */
359 syslogUser = cnvt_str2int( optarg,
360 syslog_types, DEFAULT_SYSLOG_USER );
366 if( sandbox ) free(sandbox);
367 sandbox = ch_strdup( optarg );
371 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
372 case 'u': /* user name */
373 if( username ) free(username);
374 username = ch_strdup( optarg );
377 case 'g': /* group name */
378 if( groupname ) free(groupname);
379 groupname = ch_strdup( optarg );
381 #endif /* SETUID && GETUID */
383 case 'n': /* NT service name */
384 serverName = ch_strdup( optarg );
388 /* deprecated; use slaptest instead */
389 fprintf( stderr, "option -t deprecated; "
390 "use slaptest command instead\n" );
391 check |= CHECK_CONFIG;
399 /* try full option string first */
400 for ( i = 0; tools[i].name; i++ ) {
401 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
402 rc = tools[i].func( argc, argv );
407 /* try bits of option string (backward compatibility for single char) */
408 l = strlen( optarg );
409 for ( i = 0; tools[i].name; i++ ) {
410 if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
411 rc = tools[i].func( argc, argv );
418 serverNamePrefix = "slap";
419 fprintf( stderr, "program name \"%s%s\" unrecognized; "
420 "aborting...\n", serverNamePrefix, serverName );
425 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
431 lutil_log_initialize( argc, argv );
433 lutil_set_debug_level( "slapd", slap_debug );
434 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
435 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
436 ldif_debug = slap_debug;
440 fprintf( stderr, "%s\n", Versionstr );
441 if ( version > 1 ) goto stop;
447 logName = ch_strdup( serverName );
450 logName = serverName;
454 openlog( logName, OPENLOG_OPTIONS, syslogUser );
456 openlog( logName, OPENLOG_OPTIONS );
464 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
466 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
469 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
471 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
475 #if defined(HAVE_CHROOT)
477 if ( chdir( sandbox ) ) {
482 if ( chroot( sandbox ) ) {
490 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
491 if ( username != NULL || groupname != NULL ) {
492 slap_init_user( username, groupname );
501 if ( module_init() != 0 ) {
503 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
508 if ( slap_schema_init( ) != 0 ) {
510 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
512 Debug( LDAP_DEBUG_ANY,
513 "schema initialization error\n",
520 if ( slap_init( serverMode, serverName ) != 0 ) {
522 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
526 if ( slap_controls_init( ) != 0 ) {
528 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
530 Debug( LDAP_DEBUG_ANY,
531 "controls initialization error\n",
539 /* Library defaults to full certificate checking. This is correct when
540 * a client is verifying a server because all servers should have a
541 * valid cert. But few clients have valid certs, so we want our default
542 * to be no checking. The config file can override this as usual.
545 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
549 if ( slapi_int_initialize() != 0 ) {
551 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
553 Debug( LDAP_DEBUG_ANY,
554 "slapi initialization error\n",
560 #endif /* LDAP_SLAPI */
562 if ( overlay_init() ) {
566 if ( read_config( configfile, 0 ) != 0 ) {
568 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
570 if ( check & CHECK_CONFIG ) {
571 fprintf( stderr, "config check failed\n" );
577 if ( check & CHECK_CONFIG ) {
578 fprintf( stderr, "config check succeeded\n" );
580 check &= ~CHECK_CONFIG;
581 if ( check == CHECK_NONE ) {
587 if ( glue_sub_init( ) != 0 ) {
589 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
591 Debug( LDAP_DEBUG_ANY,
592 "subordinate config error\n",
598 if ( slap_schema_check( ) != 0 ) {
600 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
602 Debug( LDAP_DEBUG_ANY,
603 "schema prep error\n",
611 rc = ldap_pvt_tls_init();
614 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
616 Debug( LDAP_DEBUG_ANY,
617 "main: TLS init failed: %d\n",
621 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
625 rc = ldap_pvt_tls_init_def_ctx();
628 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
630 Debug( LDAP_DEBUG_ANY,
631 "main: TLS init def ctx failed: %d\n",
635 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
640 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
641 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
644 (void) SIGNAL( SIGPIPE, SIG_IGN );
647 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
649 (void) SIGNAL( SIGINT, slap_sig_shutdown );
650 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
652 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
655 /* SIGBREAK is generated when Ctrl-Break is pressed. */
656 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
660 lutil_detach( no_detach, 0 );
661 #endif /* HAVE_WINSOCK */
668 * FIXME: moved here from slapd_daemon_task()
669 * because back-monitor db_open() needs it
673 if ( slap_startup( NULL ) != 0 ) {
675 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
680 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
682 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
686 if ( slapd_pid_file != NULL ) {
687 FILE *fp = fopen( slapd_pid_file, "w" );
690 fprintf( fp, "%d\n", (int) getpid() );
694 free(slapd_pid_file);
695 slapd_pid_file = NULL;
699 if ( slapd_args_file != NULL ) {
700 FILE *fp = fopen( slapd_args_file, "w" );
703 for ( i = 0; i < g_argc; i++ ) {
704 fprintf( fp, "%s ", g_argv[i] );
709 free(slapd_args_file);
710 slapd_args_file = NULL;
714 #ifdef HAVE_NT_EVENT_LOG
716 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
721 #ifdef HAVE_NT_SERVICE_MANAGER
722 /* Throw away the event that we used during the startup process. */
724 ldap_pvt_thread_cond_destroy( &started_event );
728 /* remember an error during shutdown */
729 rc |= slap_shutdown( NULL );
732 /* remember an error during destroy */
733 rc |= slap_destroy();
735 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
736 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
737 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
750 #ifdef HAVE_NT_EVENT_LOG
752 lutil_LogStoppedEvent( serverName );
756 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
758 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
762 #ifdef HAVE_NT_SERVICE_MANAGER
763 lutil_ReportShutdownComplete();
769 slapd_daemon_destroy();
775 lutil_passwd_destroy();
778 ldap_pvt_tls_destroy();
781 if ( slapd_pid_file != NULL ) {
782 unlink( slapd_pid_file );
784 if ( slapd_args_file != NULL ) {
785 unlink( slapd_args_file );
791 mal_dumpleaktrace( leakfile );
801 * Catch and discard terminated child processes, to avoid zombies.
805 wait4child( int sig )
807 int save_errno = errno;
812 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
815 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
821 (void) SIGNAL_REINSTALL( sig, wait4child );
825 #endif /* LDAP_SIGCHLD */
831 * Convert a string to an integer by means of a dispatcher table
832 * if the string is not in the table return the default
836 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
838 int retVal = defaultVal;
841 for (disp = dispatcher; disp->stringVal; disp++) {
843 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
845 retVal = disp->intVal;
854 #endif /* LOG_LOCAL4 */