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},
82 /* NOTE: new tools must be added in chronological order,
83 * not in alphabetical order, because for backwards
84 * compatibility name[4] is used to identify the
85 * tools; so name[4]=='a' must refer to "slapadd" and
86 * not to "slapauth". Alphabetical order can be used
87 * for tools whose name[4] is not used yet */
92 * when more than one slapd is running on one machine, each one might have
93 * it's own LOCAL for syslogging and must have its own pid/args files
96 #ifndef HAVE_MKVERSION
97 const char Versionstr[] =
98 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
103 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
105 typedef struct _str2intDispatch {
109 } STRDISP, *STRDISP_P;
112 /* table to compute syslog-options to integer */
113 static STRDISP syslog_types[] = {
114 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
115 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
116 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
117 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
118 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
119 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
120 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
121 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
125 static int cnvt_str2int( char *, STRDISP_P, int );
127 #endif /* LOG_LOCAL4 */
129 #define CHECK_NONE 0x00
130 #define CHECK_CONFIG 0x01
131 static int check = CHECK_NONE;
132 static int version = 0;
138 "usage: %s options\n", name );
140 "\t-4\t\tIPv4 only\n"
141 "\t-6\t\tIPv6 only\n"
142 "\t-T {add|auth|cat|dn|index|passwd|test}\n"
143 "\t\t\tRun in Tool mode\n"
144 "\t-c cookie\tSync cookie of consumer\n"
145 "\t-d level\tDebug level" "\n"
146 "\t-f filename\tConfiguration file\n"
147 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
148 "\t-g group\tGroup (id or name) to run as\n"
150 "\t-h URLs\t\tList of URLs to serve\n"
152 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
154 "\t-n serverName\tService name\n"
156 "\t-r directory\tSandbox directory to chroot to\n"
158 "\t-s level\tSyslog level\n"
159 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
160 "\t-u user\t\tUser (id or name) to run as\n"
161 "\t-V\t\tprint version info (-VV only)\n"
166 #ifdef HAVE_NT_SERVICE_MANAGER
167 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
169 int main( int argc, char **argv )
172 int i, no_detach = 0;
175 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
176 char *username = NULL;
177 char *groupname = NULL;
179 #if defined(HAVE_CHROOT)
180 char *sandbox = NULL;
183 int syslogUser = DEFAULT_SYSLOG_USER;
187 char **g_argv = argv;
189 #ifdef HAVE_NT_SERVICE_MANAGER
190 char *configfile = ".\\slapd.conf";
192 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
195 int serverMode = SLAP_SERVER_MODE;
197 struct berval cookie = BER_BVNULL;
198 struct sync_cookie *scp = NULL;
199 struct sync_cookie *scp_entry = NULL;
203 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
207 char *serverNamePrefix = "";
212 serverName = lutil_progname( "slapd", argc, argv );
214 if ( strcmp( serverName, "slapd" ) ) {
215 for (i=0; tools[i].name; i++) {
216 if ( !strcmp( serverName, tools[i].name ) ) {
217 rc = tools[i].func(argc, argv);
222 goto unrecognized_server_name;
225 #ifdef HAVE_NT_SERVICE_MANAGER
230 char *regService = NULL;
232 if ( is_NT_Service ) {
233 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
234 if ( strcmp(serverName, SERVICE_NAME) )
235 regService = serverName;
238 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
243 lutil_log_initialize( argc, argv );
244 LDAP_LOG( SLAPD, INFO,
245 "main: new debug level from registry is: %d\n",
248 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
252 newUrls = (char *) lutil_getRegParam(regService, "Urls");
258 urls = ch_strdup(newUrls);
260 LDAP_LOG( SLAPD, INFO,
261 "main: new urls from registry: %s\n", urls, 0, 0 );
263 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
269 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
270 if ( newConfigFile != NULL )
272 configfile = newConfigFile;
274 LDAP_LOG( SLAPD, INFO,
275 "main: new config file from registry is: %s\n", configfile, 0, 0 );
277 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 );
359 case 'l': /* set syslog local user */
360 syslogUser = cnvt_str2int( optarg,
361 syslog_types, DEFAULT_SYSLOG_USER );
367 if( sandbox ) free(sandbox);
368 sandbox = ch_strdup( optarg );
372 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
373 case 'u': /* user name */
374 if( username ) free(username);
375 username = ch_strdup( optarg );
378 case 'g': /* group name */
379 if( groupname ) free(groupname);
380 groupname = ch_strdup( optarg );
382 #endif /* SETUID && GETUID */
384 case 'n': /* NT service name */
385 serverName = ch_strdup( optarg );
389 /* deprecated; use slaptest instead */
390 fprintf( stderr, "option -t deprecated; "
391 "use slaptest command instead\n" );
392 check |= CHECK_CONFIG;
400 /* try full option string first */
401 for ( i = 0; tools[i].name; i++ ) {
402 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
403 rc = tools[i].func( argc, argv );
408 /* try bits of option string (backward compatibility for single char) */
409 l = strlen( optarg );
410 for ( i = 0; tools[i].name; i++ ) {
411 if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
412 rc = tools[i].func( argc, argv );
419 serverNamePrefix = "slap";
420 unrecognized_server_name:;
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 );
627 rc = ldap_pvt_tls_init_def_ctx();
630 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
632 Debug( LDAP_DEBUG_ANY,
633 "main: TLS init def ctx failed: %d\n",
637 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
642 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
643 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
646 (void) SIGNAL( SIGPIPE, SIG_IGN );
649 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
651 (void) SIGNAL( SIGINT, slap_sig_shutdown );
652 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
654 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
657 /* SIGBREAK is generated when Ctrl-Break is pressed. */
658 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
662 lutil_detach( no_detach, 0 );
663 #endif /* HAVE_WINSOCK */
670 * FIXME: moved here from slapd_daemon_task()
671 * because back-monitor db_open() needs it
675 if ( slap_startup( NULL ) != 0 ) {
677 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
682 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
684 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
688 if ( slapd_pid_file != NULL ) {
689 FILE *fp = fopen( slapd_pid_file, "w" );
692 fprintf( fp, "%d\n", (int) getpid() );
696 free(slapd_pid_file);
697 slapd_pid_file = NULL;
701 if ( slapd_args_file != NULL ) {
702 FILE *fp = fopen( slapd_args_file, "w" );
705 for ( i = 0; i < g_argc; i++ ) {
706 fprintf( fp, "%s ", g_argv[i] );
711 free(slapd_args_file);
712 slapd_args_file = NULL;
716 #ifdef HAVE_NT_EVENT_LOG
718 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
723 #ifdef HAVE_NT_SERVICE_MANAGER
724 /* Throw away the event that we used during the startup process. */
726 ldap_pvt_thread_cond_destroy( &started_event );
730 /* remember an error during shutdown */
731 rc |= slap_shutdown( NULL );
734 /* remember an error during destroy */
735 rc |= slap_destroy();
737 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
738 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
739 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
752 #ifdef HAVE_NT_EVENT_LOG
754 lutil_LogStoppedEvent( serverName );
758 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
760 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
764 #ifdef HAVE_NT_SERVICE_MANAGER
765 lutil_ReportShutdownComplete();
771 slapd_daemon_destroy();
777 lutil_passwd_destroy();
780 ldap_pvt_tls_destroy();
783 if ( slapd_pid_file != NULL ) {
784 unlink( slapd_pid_file );
786 if ( slapd_args_file != NULL ) {
787 unlink( slapd_args_file );
793 mal_dumpleaktrace( leakfile );
803 * Catch and discard terminated child processes, to avoid zombies.
807 wait4child( int sig )
809 int save_errno = errno;
814 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
817 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
823 (void) SIGNAL_REINSTALL( sig, wait4child );
827 #endif /* LDAP_SIGCHLD */
833 * Convert a string to an integer by means of a dispatcher table
834 * if the string is not in the table return the default
838 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
840 int retVal = defaultVal;
843 for (disp = dispatcher; disp->stringVal; disp++) {
845 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
847 retVal = disp->intVal;
856 #endif /* LOG_LOCAL4 */