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;
138 slapd_opt_slp( const char *val, void *arg )
141 /* NULL is default */
142 if ( val == NULL || strcasecmp( val, "on" ) == 0 ) {
143 slapd_register_slp = 1;
145 } else if ( strcasecmp( val, "off" ) == 0 ) {
146 slapd_register_slp = 0;
148 /* NOTE: add support for URL specification? */
151 fprintf(stderr, "unrecognized value \"%s\" for SLP option\n", val );
158 fputs( "slapd: SLP support is not available\n", stderr );
163 struct option_helper {
164 struct berval oh_name;
165 int (*oh_fnc)(const char *val, void *arg);
167 } option_helpers[] = {
168 { BER_BVC("slp"), slapd_opt_slp, NULL },
176 "usage: %s options\n", name );
178 "\t-4\t\tIPv4 only\n"
179 "\t-6\t\tIPv6 only\n"
180 "\t-T {add|auth|cat|dn|index|passwd|test}\n"
181 "\t\t\tRun in Tool mode\n"
182 "\t-c cookie\tSync cookie of consumer\n"
183 "\t-d level\tDebug level" "\n"
184 "\t-f filename\tConfiguration file\n"
185 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
186 "\t-g group\tGroup (id or name) to run as\n"
188 "\t-h URLs\t\tList of URLs to serve\n"
190 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
192 "\t-n serverName\tService name\n"
193 "\t-o <opt>[=val]\tGeneric means to specify options; details in slapd(8)\n"
195 "\t-r directory\tSandbox directory to chroot to\n"
197 "\t-s level\tSyslog level\n"
199 "\t-S enable SLP registration\n"
201 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
202 "\t-u user\t\tUser (id or name) to run as\n"
204 "\t-V\t\tprint version info (-VV only)\n"
208 #ifdef HAVE_NT_SERVICE_MANAGER
209 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
211 int main( int argc, char **argv )
214 int i, no_detach = 0;
217 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
218 char *username = NULL;
219 char *groupname = NULL;
221 #if defined(HAVE_CHROOT)
222 char *sandbox = NULL;
225 int syslogUser = DEFAULT_SYSLOG_USER;
229 char **g_argv = argv;
231 #ifdef HAVE_NT_SERVICE_MANAGER
232 char *configfile = ".\\slapd.conf";
234 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
237 int serverMode = SLAP_SERVER_MODE;
239 struct berval cookie = BER_BVNULL;
240 struct sync_cookie *scp = NULL;
241 struct sync_cookie *scp_entry = NULL;
245 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
249 char *serverNamePrefix = "";
254 serverName = lutil_progname( "slapd", argc, argv );
256 if ( strcmp( serverName, "slapd" ) ) {
257 for (i=0; tools[i].name; i++) {
258 if ( !strcmp( serverName, tools[i].name ) ) {
259 rc = tools[i].func(argc, argv);
265 #ifdef HAVE_NT_SERVICE_MANAGER
270 char *regService = NULL;
272 if ( is_NT_Service ) {
273 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
274 if ( strcmp(serverName, SERVICE_NAME) )
275 regService = serverName;
278 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
282 lutil_log_initialize( argc, argv );
283 LDAP_LOG( SLAPD, INFO,
284 "main: new debug level from registry is: %d\n",
287 Debug( LDAP_DEBUG_ANY,
288 "new debug level from registry is: %d\n", slap_debug, 0, 0 );
292 newUrls = (char *) lutil_getRegParam(regService, "Urls");
297 urls = ch_strdup(newUrls);
299 LDAP_LOG( SLAPD, INFO,
300 "main: new urls from registry: %s\n", urls, 0, 0 );
302 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
307 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
308 if ( newConfigFile != NULL ) {
309 configfile = newConfigFile;
311 LDAP_LOG( SLAPD, INFO,
312 "main: new config file from registry is: %s\n", configfile, 0, 0 );
314 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
320 while ( (i = getopt( argc, argv,
321 "c:d:f:h:n:o:s:StT:V"
331 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
338 slap_inet4or6 = AF_INET;
341 slap_inet4or6 = AF_INET6;
345 case 'h': /* listen URLs */
346 if ( urls != NULL ) free( urls );
347 urls = ch_strdup( optarg );
350 case 'c': /* provide sync cookie, override if exist in replica */
351 scp = (struct sync_cookie *) ch_calloc( 1,
352 sizeof( struct sync_cookie ));
353 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
354 ber_bvarray_add( &scp->octet_str, &cookie );
355 slap_parse_sync_cookie( scp );
357 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
358 if ( scp->rid == scp_entry->rid ) {
360 LDAP_LOG( OPERATION, CRIT,
361 "main: duplicated replica id in cookies\n",
364 Debug( LDAP_DEBUG_ANY,
365 "main: duplicated replica id in cookies\n",
368 slap_sync_cookie_free( scp, 1 );
372 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
375 case 'd': /* set debug level and 'do not detach' flag */
378 slap_debug |= atoi( optarg );
380 if ( atoi( optarg ) != 0 )
381 fputs( "must compile with LDAP_DEBUG for debugging\n",
386 case 'f': /* read config file */
387 configfile = ch_strdup( optarg );
391 char *val = strchr( optarg, '=' );
398 opt.bv_len = ( val - optarg );
402 opt.bv_len = strlen( optarg );
405 for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++ ) {
406 if ( ber_bvstrcasecmp( &option_helpers[i].oh_name, &opt ) == 0 ) {
407 assert( option_helpers[i].oh_fnc != NULL );
408 if ( (*option_helpers[i].oh_fnc)( val, option_helpers[i].oh_arg ) == -1 ) {
409 /* we assume the option parsing helper
410 * issues appropriate and self-explanatory
411 * error messages... */
418 if ( BER_BVISNULL( &option_helpers[i].oh_name ) ) {
419 goto unhandled_option;
424 case 's': /* set syslog level */
425 ldap_syslog = atoi( optarg );
428 case 'S': /* enable SLP */
430 slapd_register_slp++;
432 fputs( "slapd: SLP support is not available\n", stderr );
437 case 'l': /* set syslog local user */
438 syslogUser = cnvt_str2int( optarg,
439 syslog_types, DEFAULT_SYSLOG_USER );
445 if( sandbox ) free(sandbox);
446 sandbox = ch_strdup( optarg );
450 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
451 case 'u': /* user name */
452 if( username ) free(username);
453 username = ch_strdup( optarg );
456 case 'g': /* group name */
457 if( groupname ) free(groupname);
458 groupname = ch_strdup( optarg );
460 #endif /* SETUID && GETUID */
462 case 'n': /* NT service name */
463 serverName = ch_strdup( optarg );
467 /* deprecated; use slaptest instead */
468 fprintf( stderr, "option -t deprecated; "
469 "use slaptest command instead\n" );
470 check |= CHECK_CONFIG;
478 /* try full option string first */
479 for ( i = 0; tools[i].name; i++ ) {
480 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
481 rc = tools[i].func( argc, argv );
486 /* try bits of option string (backward compatibility for single char) */
487 l = strlen( optarg );
488 for ( i = 0; tools[i].name; i++ ) {
489 if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
490 rc = tools[i].func( argc, argv );
497 serverNamePrefix = "slap";
498 fprintf( stderr, "program name \"%s%s\" unrecognized; "
499 "aborting...\n", serverNamePrefix, serverName );
505 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
511 lutil_log_initialize( argc, argv );
513 lutil_set_debug_level( "slapd", slap_debug );
514 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
515 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
516 ldif_debug = slap_debug;
520 fprintf( stderr, "%s\n", Versionstr );
521 if ( version > 1 ) goto stop;
527 logName = ch_strdup( serverName );
530 logName = serverName;
534 openlog( logName, OPENLOG_OPTIONS, syslogUser );
536 openlog( logName, OPENLOG_OPTIONS );
544 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
546 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
549 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
551 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
555 #if defined(HAVE_CHROOT)
557 if ( chdir( sandbox ) ) {
562 if ( chroot( sandbox ) ) {
570 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
571 if ( username != NULL || groupname != NULL ) {
572 slap_init_user( username, groupname );
581 if ( module_init() != 0 ) {
583 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
588 if ( slap_schema_init( ) != 0 ) {
590 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
592 Debug( LDAP_DEBUG_ANY,
593 "schema initialization error\n",
600 if ( slap_init( serverMode, serverName ) != 0 ) {
602 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
606 if ( slap_controls_init( ) != 0 ) {
608 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
610 Debug( LDAP_DEBUG_ANY,
611 "controls initialization error\n",
619 /* Library defaults to full certificate checking. This is correct when
620 * a client is verifying a server because all servers should have a
621 * valid cert. But few clients have valid certs, so we want our default
622 * to be no checking. The config file can override this as usual.
625 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
629 if ( slapi_int_initialize() != 0 ) {
631 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
633 Debug( LDAP_DEBUG_ANY,
634 "slapi initialization error\n",
640 #endif /* LDAP_SLAPI */
642 if ( overlay_init() ) {
646 if ( read_config( configfile, 0 ) != 0 ) {
648 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
650 if ( check & CHECK_CONFIG ) {
651 fprintf( stderr, "config check failed\n" );
657 if ( check & CHECK_CONFIG ) {
658 fprintf( stderr, "config check succeeded\n" );
660 check &= ~CHECK_CONFIG;
661 if ( check == CHECK_NONE ) {
667 if ( glue_sub_init( ) != 0 ) {
669 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
671 Debug( LDAP_DEBUG_ANY,
672 "subordinate config error\n",
678 if ( slap_schema_check( ) != 0 ) {
680 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
682 Debug( LDAP_DEBUG_ANY,
683 "schema prep error\n",
691 rc = ldap_pvt_tls_init();
694 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
696 Debug( LDAP_DEBUG_ANY,
697 "main: TLS init failed: %d\n",
701 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
706 void *def_ctx = NULL;
708 /* Save existing default ctx, if any */
709 ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &def_ctx );
711 /* Force new ctx to be created */
712 ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_CTX, NULL );
714 rc = ldap_pvt_tls_init_def_ctx();
717 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
719 Debug( LDAP_DEBUG_ANY,
720 "main: TLS init def ctx failed: %d\n",
724 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
727 /* Retrieve slapd's own ctx */
728 ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
729 /* Restore previous ctx */
730 ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_CTX, def_ctx );
734 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
735 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
738 (void) SIGNAL( SIGPIPE, SIG_IGN );
741 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
743 (void) SIGNAL( SIGINT, slap_sig_shutdown );
744 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
746 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
749 /* SIGBREAK is generated when Ctrl-Break is pressed. */
750 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
754 lutil_detach( no_detach, 0 );
755 #endif /* HAVE_WINSOCK */
762 * FIXME: moved here from slapd_daemon_task()
763 * because back-monitor db_open() needs it
767 if ( slap_startup( NULL ) != 0 ) {
769 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
774 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
776 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
780 if ( slapd_pid_file != NULL ) {
781 FILE *fp = fopen( slapd_pid_file, "w" );
784 fprintf( fp, "%d\n", (int) getpid() );
788 free(slapd_pid_file);
789 slapd_pid_file = NULL;
793 if ( slapd_args_file != NULL ) {
794 FILE *fp = fopen( slapd_args_file, "w" );
797 for ( i = 0; i < g_argc; i++ ) {
798 fprintf( fp, "%s ", g_argv[i] );
803 free(slapd_args_file);
804 slapd_args_file = NULL;
808 #ifdef HAVE_NT_EVENT_LOG
810 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
815 #ifdef HAVE_NT_SERVICE_MANAGER
816 /* Throw away the event that we used during the startup process. */
818 ldap_pvt_thread_cond_destroy( &started_event );
822 /* remember an error during shutdown */
823 rc |= slap_shutdown( NULL );
826 /* remember an error during destroy */
827 rc |= slap_destroy();
829 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
830 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
831 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
844 #ifdef HAVE_NT_EVENT_LOG
846 lutil_LogStoppedEvent( serverName );
850 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
852 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
856 #ifdef HAVE_NT_SERVICE_MANAGER
857 lutil_ReportShutdownComplete();
863 slapd_daemon_destroy();
869 lutil_passwd_destroy();
872 ldap_pvt_tls_destroy();
875 if ( slapd_pid_file != NULL ) {
876 unlink( slapd_pid_file );
878 if ( slapd_args_file != NULL ) {
879 unlink( slapd_args_file );
885 mal_dumpleaktrace( leakfile );
895 * Catch and discard terminated child processes, to avoid zombies.
899 wait4child( int sig )
901 int save_errno = errno;
906 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
909 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
915 (void) SIGNAL_REINSTALL( sig, wait4child );
919 #endif /* LDAP_SIGCHLD */
925 * Convert a string to an integer by means of a dispatcher table
926 * if the string is not in the table return the default
930 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
932 int retVal = defaultVal;
935 for (disp = dispatcher; disp->stringVal; disp++) {
937 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
939 retVal = disp->intVal;
948 #endif /* LOG_LOCAL4 */