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"
163 "\t-S enable SLP registration\n"
165 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
166 "\t-u user\t\tUser (id or name) to run as\n"
167 "\t-V\t\tprint version info (-VV only)\n"
172 #ifdef HAVE_NT_SERVICE_MANAGER
173 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
175 int main( int argc, char **argv )
178 int i, no_detach = 0;
181 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
182 char *username = NULL;
183 char *groupname = NULL;
185 #if defined(HAVE_CHROOT)
186 char *sandbox = NULL;
189 int syslogUser = DEFAULT_SYSLOG_USER;
193 char **g_argv = argv;
195 #ifdef HAVE_NT_SERVICE_MANAGER
196 char *configfile = ".\\slapd.conf";
198 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
201 int serverMode = SLAP_SERVER_MODE;
203 struct berval cookie = BER_BVNULL;
204 struct sync_cookie *scp = NULL;
205 struct sync_cookie *scp_entry = NULL;
209 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
213 char *serverNamePrefix = "";
218 serverName = lutil_progname( "slapd", argc, argv );
220 if ( strcmp( serverName, "slapd" ) ) {
221 for (i=0; tools[i].name; i++) {
222 if ( !strcmp( serverName, tools[i].name ) ) {
223 rc = tools[i].func(argc, argv);
229 #ifdef HAVE_NT_SERVICE_MANAGER
234 char *regService = NULL;
236 if ( is_NT_Service ) {
237 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
238 if ( strcmp(serverName, SERVICE_NAME) )
239 regService = serverName;
242 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
246 lutil_log_initialize( argc, argv );
247 LDAP_LOG( SLAPD, INFO,
248 "main: new debug level from registry is: %d\n",
251 Debug( LDAP_DEBUG_ANY,
252 "new debug level from registry is: %d\n", slap_debug, 0, 0 );
256 newUrls = (char *) lutil_getRegParam(regService, "Urls");
261 urls = ch_strdup(newUrls);
263 LDAP_LOG( SLAPD, INFO,
264 "main: new urls from registry: %s\n", urls, 0, 0 );
266 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
271 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
272 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 );
284 while ( (i = getopt( argc, argv,
295 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
302 slap_inet4or6 = AF_INET;
305 slap_inet4or6 = AF_INET6;
309 case 'h': /* listen URLs */
310 if ( urls != NULL ) free( urls );
311 urls = ch_strdup( optarg );
314 case 'c': /* provide sync cookie, override if exist in replica */
315 scp = (struct sync_cookie *) ch_calloc( 1,
316 sizeof( struct sync_cookie ));
317 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
318 ber_bvarray_add( &scp->octet_str, &cookie );
319 slap_parse_sync_cookie( scp );
321 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
322 if ( scp->rid == scp_entry->rid ) {
324 LDAP_LOG( OPERATION, CRIT,
325 "main: duplicated replica id in cookies\n",
328 Debug( LDAP_DEBUG_ANY,
329 "main: duplicated replica id in cookies\n",
332 slap_sync_cookie_free( scp, 1 );
336 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
339 case 'd': /* set debug level and 'do not detach' flag */
342 slap_debug |= atoi( optarg );
344 if ( atoi( optarg ) != 0 )
345 fputs( "must compile with LDAP_DEBUG for debugging\n",
350 case 'f': /* read config file */
351 configfile = ch_strdup( optarg );
354 case 's': /* set syslog level */
355 ldap_syslog = atoi( optarg );
358 case 'S': /* enable SLP */
360 slapd_register_slp++;
362 fputs( "slapd: SLP support is not available\n", stderr );
367 case 'l': /* set syslog local user */
368 syslogUser = cnvt_str2int( optarg,
369 syslog_types, DEFAULT_SYSLOG_USER );
375 if( sandbox ) free(sandbox);
376 sandbox = ch_strdup( optarg );
380 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
381 case 'u': /* user name */
382 if( username ) free(username);
383 username = ch_strdup( optarg );
386 case 'g': /* group name */
387 if( groupname ) free(groupname);
388 groupname = ch_strdup( optarg );
390 #endif /* SETUID && GETUID */
392 case 'n': /* NT service name */
393 serverName = ch_strdup( optarg );
397 /* deprecated; use slaptest instead */
398 fprintf( stderr, "option -t deprecated; "
399 "use slaptest command instead\n" );
400 check |= CHECK_CONFIG;
408 /* try full option string first */
409 for ( i = 0; tools[i].name; i++ ) {
410 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
411 rc = tools[i].func( argc, argv );
416 /* try bits of option string (backward compatibility for single char) */
417 l = strlen( optarg );
418 for ( i = 0; tools[i].name; i++ ) {
419 if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
420 rc = tools[i].func( argc, argv );
427 serverNamePrefix = "slap";
428 fprintf( stderr, "program name \"%s%s\" unrecognized; "
429 "aborting...\n", serverNamePrefix, serverName );
434 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
440 lutil_log_initialize( argc, argv );
442 lutil_set_debug_level( "slapd", slap_debug );
443 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
444 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
445 ldif_debug = slap_debug;
449 fprintf( stderr, "%s\n", Versionstr );
450 if ( version > 1 ) goto stop;
456 logName = ch_strdup( serverName );
459 logName = serverName;
463 openlog( logName, OPENLOG_OPTIONS, syslogUser );
465 openlog( logName, OPENLOG_OPTIONS );
473 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
475 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
478 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
480 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
484 #if defined(HAVE_CHROOT)
486 if ( chdir( sandbox ) ) {
491 if ( chroot( sandbox ) ) {
499 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
500 if ( username != NULL || groupname != NULL ) {
501 slap_init_user( username, groupname );
510 if ( module_init() != 0 ) {
512 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
517 if ( slap_schema_init( ) != 0 ) {
519 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
521 Debug( LDAP_DEBUG_ANY,
522 "schema initialization error\n",
529 if ( slap_init( serverMode, serverName ) != 0 ) {
531 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
535 if ( slap_controls_init( ) != 0 ) {
537 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
539 Debug( LDAP_DEBUG_ANY,
540 "controls initialization error\n",
548 /* Library defaults to full certificate checking. This is correct when
549 * a client is verifying a server because all servers should have a
550 * valid cert. But few clients have valid certs, so we want our default
551 * to be no checking. The config file can override this as usual.
554 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
558 if ( slapi_int_initialize() != 0 ) {
560 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
562 Debug( LDAP_DEBUG_ANY,
563 "slapi initialization error\n",
569 #endif /* LDAP_SLAPI */
571 if ( overlay_init() ) {
575 if ( read_config( configfile, 0 ) != 0 ) {
577 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
579 if ( check & CHECK_CONFIG ) {
580 fprintf( stderr, "config check failed\n" );
586 if ( check & CHECK_CONFIG ) {
587 fprintf( stderr, "config check succeeded\n" );
589 check &= ~CHECK_CONFIG;
590 if ( check == CHECK_NONE ) {
596 if ( glue_sub_init( ) != 0 ) {
598 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
600 Debug( LDAP_DEBUG_ANY,
601 "subordinate config error\n",
607 if ( slap_schema_check( ) != 0 ) {
609 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
611 Debug( LDAP_DEBUG_ANY,
612 "schema prep error\n",
620 rc = ldap_pvt_tls_init();
623 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
625 Debug( LDAP_DEBUG_ANY,
626 "main: TLS init failed: %d\n",
630 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
635 void *def_ctx = NULL;
637 /* Save existing default ctx, if any */
638 ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &def_ctx );
640 /* Force new ctx to be created */
641 ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_CTX, NULL );
643 rc = ldap_pvt_tls_init_def_ctx();
646 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
648 Debug( LDAP_DEBUG_ANY,
649 "main: TLS init def ctx failed: %d\n",
653 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
656 /* Retrieve slapd's own ctx */
657 ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
658 /* Restore previous ctx */
659 ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_CTX, def_ctx );
663 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
664 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
667 (void) SIGNAL( SIGPIPE, SIG_IGN );
670 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
672 (void) SIGNAL( SIGINT, slap_sig_shutdown );
673 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
675 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
678 /* SIGBREAK is generated when Ctrl-Break is pressed. */
679 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
683 lutil_detach( no_detach, 0 );
684 #endif /* HAVE_WINSOCK */
691 * FIXME: moved here from slapd_daemon_task()
692 * because back-monitor db_open() needs it
696 if ( slap_startup( NULL ) != 0 ) {
698 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
703 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
705 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
709 if ( slapd_pid_file != NULL ) {
710 FILE *fp = fopen( slapd_pid_file, "w" );
713 fprintf( fp, "%d\n", (int) getpid() );
717 free(slapd_pid_file);
718 slapd_pid_file = NULL;
722 if ( slapd_args_file != NULL ) {
723 FILE *fp = fopen( slapd_args_file, "w" );
726 for ( i = 0; i < g_argc; i++ ) {
727 fprintf( fp, "%s ", g_argv[i] );
732 free(slapd_args_file);
733 slapd_args_file = NULL;
737 #ifdef HAVE_NT_EVENT_LOG
739 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
744 #ifdef HAVE_NT_SERVICE_MANAGER
745 /* Throw away the event that we used during the startup process. */
747 ldap_pvt_thread_cond_destroy( &started_event );
751 /* remember an error during shutdown */
752 rc |= slap_shutdown( NULL );
755 /* remember an error during destroy */
756 rc |= slap_destroy();
758 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
759 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
760 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
773 #ifdef HAVE_NT_EVENT_LOG
775 lutil_LogStoppedEvent( serverName );
779 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
781 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
785 #ifdef HAVE_NT_SERVICE_MANAGER
786 lutil_ReportShutdownComplete();
792 slapd_daemon_destroy();
798 lutil_passwd_destroy();
801 ldap_pvt_tls_destroy();
804 if ( slapd_pid_file != NULL ) {
805 unlink( slapd_pid_file );
807 if ( slapd_args_file != NULL ) {
808 unlink( slapd_args_file );
814 mal_dumpleaktrace( leakfile );
824 * Catch and discard terminated child processes, to avoid zombies.
828 wait4child( int sig )
830 int save_errno = errno;
835 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
838 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
844 (void) SIGNAL_REINSTALL( sig, wait4child );
848 #endif /* LDAP_SIGCHLD */
854 * Convert a string to an integer by means of a dispatcher table
855 * if the string is not in the table return the default
859 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
861 int retVal = defaultVal;
864 for (disp = dispatcher; disp->stringVal; disp++) {
866 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
868 retVal = disp->intVal;
877 #endif /* LOG_LOCAL4 */