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;
141 "usage: %s options\n", name );
143 "\t-4\t\tIPv4 only\n"
144 "\t-6\t\tIPv6 only\n"
145 "\t-T {add|auth|cat|dn|index|passwd|test}\n"
146 "\t\t\tRun in Tool mode\n"
147 "\t-c cookie\tSync cookie of consumer\n"
148 "\t-d level\tDebug level" "\n"
149 "\t-f filename\tConfiguration file\n"
150 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
151 "\t-g group\tGroup (id or name) to run as\n"
153 "\t-h URLs\t\tList of URLs to serve\n"
155 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
157 "\t-n serverName\tService name\n"
159 "\t-r directory\tSandbox directory to chroot to\n"
161 "\t-s level\tSyslog level\n"
162 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
163 "\t-u user\t\tUser (id or name) to run as\n"
164 "\t-V\t\tprint version info (-VV only)\n"
169 #ifdef HAVE_NT_SERVICE_MANAGER
170 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
172 int main( int argc, char **argv )
175 int i, no_detach = 0;
178 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
179 char *username = NULL;
180 char *groupname = NULL;
182 #if defined(HAVE_CHROOT)
183 char *sandbox = NULL;
186 int syslogUser = DEFAULT_SYSLOG_USER;
190 char **g_argv = argv;
192 #ifdef HAVE_NT_SERVICE_MANAGER
193 char *configfile = ".\\slapd.conf";
195 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
198 int serverMode = SLAP_SERVER_MODE;
200 struct berval cookie = BER_BVNULL;
201 struct sync_cookie *scp = NULL;
202 struct sync_cookie *scp_entry = NULL;
206 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
210 char *serverNamePrefix = "";
215 serverName = lutil_progname( "slapd", argc, argv );
217 if ( strcmp( serverName, "slapd" ) ) {
218 for (i=0; tools[i].name; i++) {
219 if ( !strcmp( serverName, tools[i].name ) ) {
220 rc = tools[i].func(argc, argv);
226 #ifdef HAVE_NT_SERVICE_MANAGER
231 char *regService = NULL;
233 if ( is_NT_Service ) {
234 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
235 if ( strcmp(serverName, SERVICE_NAME) )
236 regService = serverName;
239 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
244 lutil_log_initialize( argc, argv );
245 LDAP_LOG( SLAPD, INFO,
246 "main: new debug level from registry is: %d\n",
249 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
253 newUrls = (char *) lutil_getRegParam(regService, "Urls");
259 urls = ch_strdup(newUrls);
261 LDAP_LOG( SLAPD, INFO,
262 "main: new urls from registry: %s\n", urls, 0, 0 );
264 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
270 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
271 if ( newConfigFile != NULL )
273 configfile = newConfigFile;
275 LDAP_LOG( SLAPD, INFO,
276 "main: new config file from registry is: %s\n", configfile, 0, 0 );
278 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
285 while ( (i = getopt( argc, argv,
296 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
303 slap_inet4or6 = AF_INET;
306 slap_inet4or6 = AF_INET6;
310 case 'h': /* listen URLs */
311 if ( urls != NULL ) free( urls );
312 urls = ch_strdup( optarg );
315 case 'c': /* provide sync cookie, override if exist in replica */
316 scp = (struct sync_cookie *) ch_calloc( 1,
317 sizeof( struct sync_cookie ));
318 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
319 ber_bvarray_add( &scp->octet_str, &cookie );
320 slap_parse_sync_cookie( scp );
322 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
323 if ( scp->rid == scp_entry->rid ) {
325 LDAP_LOG( OPERATION, CRIT,
326 "main: duplicated replica id in cookies\n",
329 Debug( LDAP_DEBUG_ANY,
330 "main: duplicated replica id in cookies\n",
333 slap_sync_cookie_free( scp, 1 );
337 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
340 case 'd': /* set debug level and 'do not detach' flag */
343 slap_debug |= atoi( optarg );
345 if ( atoi( optarg ) != 0 )
346 fputs( "must compile with LDAP_DEBUG for debugging\n",
351 case 'f': /* read config file */
352 configfile = ch_strdup( optarg );
355 case 's': /* set syslog level */
356 ldap_syslog = atoi( optarg );
360 case 'l': /* set syslog local user */
361 syslogUser = cnvt_str2int( optarg,
362 syslog_types, DEFAULT_SYSLOG_USER );
368 if( sandbox ) free(sandbox);
369 sandbox = ch_strdup( optarg );
373 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
374 case 'u': /* user name */
375 if( username ) free(username);
376 username = ch_strdup( optarg );
379 case 'g': /* group name */
380 if( groupname ) free(groupname);
381 groupname = ch_strdup( optarg );
383 #endif /* SETUID && GETUID */
385 case 'n': /* NT service name */
386 serverName = ch_strdup( optarg );
390 /* deprecated; use slaptest instead */
391 fprintf( stderr, "option -t deprecated; "
392 "use slaptest command instead\n" );
393 check |= CHECK_CONFIG;
401 /* try full option string first */
402 for ( i = 0; tools[i].name; i++ ) {
403 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
404 rc = tools[i].func( argc, argv );
409 /* try bits of option string (backward compatibility for single char) */
410 l = strlen( optarg );
411 for ( i = 0; tools[i].name; i++ ) {
412 if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
413 rc = tools[i].func( argc, argv );
420 serverNamePrefix = "slap";
421 fprintf( stderr, "program name \"%s%s\" unrecognized; "
422 "aborting...\n", serverNamePrefix, serverName );
427 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
433 lutil_log_initialize( argc, argv );
435 lutil_set_debug_level( "slapd", slap_debug );
436 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
437 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
438 ldif_debug = slap_debug;
442 fprintf( stderr, "%s\n", Versionstr );
443 if ( version > 1 ) goto stop;
449 logName = ch_strdup( serverName );
452 logName = serverName;
456 openlog( logName, OPENLOG_OPTIONS, syslogUser );
458 openlog( logName, OPENLOG_OPTIONS );
466 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
468 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
471 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
473 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
477 #if defined(HAVE_CHROOT)
479 if ( chdir( sandbox ) ) {
484 if ( chroot( sandbox ) ) {
492 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
493 if ( username != NULL || groupname != NULL ) {
494 slap_init_user( username, groupname );
503 if ( module_init() != 0 ) {
505 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
510 if ( slap_schema_init( ) != 0 ) {
512 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
514 Debug( LDAP_DEBUG_ANY,
515 "schema initialization error\n",
522 if ( slap_init( serverMode, serverName ) != 0 ) {
524 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
528 if ( slap_controls_init( ) != 0 ) {
530 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
532 Debug( LDAP_DEBUG_ANY,
533 "controls initialization error\n",
541 /* Library defaults to full certificate checking. This is correct when
542 * a client is verifying a server because all servers should have a
543 * valid cert. But few clients have valid certs, so we want our default
544 * to be no checking. The config file can override this as usual.
547 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
551 if ( slapi_int_initialize() != 0 ) {
553 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
555 Debug( LDAP_DEBUG_ANY,
556 "slapi initialization error\n",
562 #endif /* LDAP_SLAPI */
564 if ( overlay_init() ) {
568 if ( read_config( configfile, 0 ) != 0 ) {
570 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
572 if ( check & CHECK_CONFIG ) {
573 fprintf( stderr, "config check failed\n" );
579 if ( check & CHECK_CONFIG ) {
580 fprintf( stderr, "config check succeeded\n" );
582 check &= ~CHECK_CONFIG;
583 if ( check == CHECK_NONE ) {
589 if ( glue_sub_init( ) != 0 ) {
591 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
593 Debug( LDAP_DEBUG_ANY,
594 "subordinate config error\n",
600 if ( slap_schema_check( ) != 0 ) {
602 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
604 Debug( LDAP_DEBUG_ANY,
605 "schema prep error\n",
613 rc = ldap_pvt_tls_init();
616 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
618 Debug( LDAP_DEBUG_ANY,
619 "main: TLS init failed: %d\n",
623 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
628 void *def_ctx = NULL;
630 /* Save existing default ctx, if any */
631 ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &def_ctx );
633 /* Force new ctx to be created */
634 ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_CTX, NULL );
636 rc = ldap_pvt_tls_init_def_ctx();
639 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
641 Debug( LDAP_DEBUG_ANY,
642 "main: TLS init def ctx failed: %d\n",
646 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
649 /* Retrieve slapd's own ctx */
650 ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
651 /* Restore previous ctx */
652 ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_CTX, def_ctx );
656 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
657 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
660 (void) SIGNAL( SIGPIPE, SIG_IGN );
663 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
665 (void) SIGNAL( SIGINT, slap_sig_shutdown );
666 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
668 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
671 /* SIGBREAK is generated when Ctrl-Break is pressed. */
672 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
676 lutil_detach( no_detach, 0 );
677 #endif /* HAVE_WINSOCK */
684 * FIXME: moved here from slapd_daemon_task()
685 * because back-monitor db_open() needs it
689 if ( slap_startup( NULL ) != 0 ) {
691 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
696 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
698 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
702 if ( slapd_pid_file != NULL ) {
703 FILE *fp = fopen( slapd_pid_file, "w" );
706 fprintf( fp, "%d\n", (int) getpid() );
710 free(slapd_pid_file);
711 slapd_pid_file = NULL;
715 if ( slapd_args_file != NULL ) {
716 FILE *fp = fopen( slapd_args_file, "w" );
719 for ( i = 0; i < g_argc; i++ ) {
720 fprintf( fp, "%s ", g_argv[i] );
725 free(slapd_args_file);
726 slapd_args_file = NULL;
730 #ifdef HAVE_NT_EVENT_LOG
732 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
737 #ifdef HAVE_NT_SERVICE_MANAGER
738 /* Throw away the event that we used during the startup process. */
740 ldap_pvt_thread_cond_destroy( &started_event );
744 /* remember an error during shutdown */
745 rc |= slap_shutdown( NULL );
748 /* remember an error during destroy */
749 rc |= slap_destroy();
751 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
752 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
753 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
766 #ifdef HAVE_NT_EVENT_LOG
768 lutil_LogStoppedEvent( serverName );
772 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
774 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
778 #ifdef HAVE_NT_SERVICE_MANAGER
779 lutil_ReportShutdownComplete();
785 slapd_daemon_destroy();
791 lutil_passwd_destroy();
794 ldap_pvt_tls_destroy();
797 if ( slapd_pid_file != NULL ) {
798 unlink( slapd_pid_file );
800 if ( slapd_args_file != NULL ) {
801 unlink( slapd_args_file );
807 mal_dumpleaktrace( leakfile );
817 * Catch and discard terminated child processes, to avoid zombies.
821 wait4child( int sig )
823 int save_errno = errno;
828 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
831 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
837 (void) SIGNAL_REINSTALL( sig, wait4child );
841 #endif /* LDAP_SIGCHLD */
847 * Convert a string to an integer by means of a dispatcher table
848 * if the string is not in the table return the default
852 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
854 int retVal = defaultVal;
857 for (disp = dispatcher; disp->stringVal; disp++) {
859 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
861 retVal = disp->intVal;
870 #endif /* LOG_LOCAL4 */