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},
86 * when more than one slapd is running on one machine, each one might have
87 * it's own LOCAL for syslogging and must have its own pid/args files
90 #ifndef HAVE_MKVERSION
91 const char Versionstr[] =
92 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
97 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
99 typedef struct _str2intDispatch {
103 } STRDISP, *STRDISP_P;
106 /* table to compute syslog-options to integer */
107 static STRDISP syslog_types[] = {
108 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
109 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
110 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
111 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
112 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
113 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
114 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
115 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
119 static int cnvt_str2int( char *, STRDISP_P, int );
121 #endif /* LOG_LOCAL4 */
123 #define CHECK_NONE 0x00
124 #define CHECK_CONFIG 0x01
125 static int check = CHECK_NONE;
126 static int version = 0;
132 "usage: %s options\n", name );
134 "\t-4\t\tIPv4 only\n"
135 "\t-6\t\tIPv6 only\n"
136 "\t-T {add|auth|cat|dn|index|passwd|test}\n"
137 "\t\t\tRun in Tool mode\n"
138 "\t-c cookie\tSync cookie of consumer\n"
139 "\t-d level\tDebug level" "\n"
140 "\t-f filename\tConfiguration file\n"
141 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
142 "\t-g group\tGroup (id or name) to run as\n"
144 "\t-h URLs\t\tList of URLs to serve\n"
146 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
148 "\t-n serverName\tService name\n"
150 "\t-r directory\tSandbox directory to chroot to\n"
152 "\t-s level\tSyslog level\n"
153 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
154 "\t-u user\t\tUser (id or name) to run as\n"
155 "\t-V\t\tprint version info (-VV only)\n"
160 #ifdef HAVE_NT_SERVICE_MANAGER
161 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
163 int main( int argc, char **argv )
166 int i, no_detach = 0;
169 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
170 char *username = NULL;
171 char *groupname = NULL;
173 #if defined(HAVE_CHROOT)
174 char *sandbox = NULL;
177 int syslogUser = DEFAULT_SYSLOG_USER;
181 char **g_argv = argv;
183 #ifdef HAVE_NT_SERVICE_MANAGER
184 char *configfile = ".\\slapd.conf";
186 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
189 int serverMode = SLAP_SERVER_MODE;
191 struct berval cookie = BER_BVNULL;
192 struct sync_cookie *scp = NULL;
193 struct sync_cookie *scp_entry = NULL;
197 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
201 char *serverNamePrefix = "";
205 serverName = lutil_progname( "slapd", argc, argv );
207 if ( strcmp( serverName, "slapd" ) ) {
208 for (i=0; tools[i].name; i++) {
209 if ( !strcmp( serverName, tools[i].name ) ) {
210 rc = tools[i].func(argc, argv);
215 goto unrecognized_server_name;
218 #ifdef HAVE_NT_SERVICE_MANAGER
223 char *regService = NULL;
225 if ( is_NT_Service ) {
226 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
227 if ( strcmp(serverName, SERVICE_NAME) )
228 regService = serverName;
231 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
236 lutil_log_initialize( argc, argv );
237 LDAP_LOG( SLAPD, INFO,
238 "main: new debug level from registry is: %d\n",
241 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
245 newUrls = (char *) lutil_getRegParam(regService, "Urls");
251 urls = ch_strdup(newUrls);
253 LDAP_LOG( SLAPD, INFO,
254 "main: new urls from registry: %s\n", urls, 0, 0 );
256 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
262 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
263 if ( newConfigFile != NULL )
265 configfile = newConfigFile;
267 LDAP_LOG( SLAPD, INFO,
268 "main: new config file from registry is: %s\n", configfile, 0, 0 );
270 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
277 while ( (i = getopt( argc, argv,
288 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
295 slap_inet4or6 = AF_INET;
298 slap_inet4or6 = AF_INET6;
302 case 'h': /* listen URLs */
303 if ( urls != NULL ) free( urls );
304 urls = ch_strdup( optarg );
307 case 'c': /* provide sync cookie, override if exist in replica */
308 scp = (struct sync_cookie *) ch_calloc( 1,
309 sizeof( struct sync_cookie ));
310 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
311 ber_bvarray_add( &scp->octet_str, &cookie );
312 slap_parse_sync_cookie( scp );
314 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
315 if ( scp->rid == scp_entry->rid ) {
317 LDAP_LOG( OPERATION, CRIT,
318 "main: duplicated replica id in cookies\n",
321 Debug( LDAP_DEBUG_ANY,
322 "main: duplicated replica id in cookies\n",
325 slap_sync_cookie_free( scp, 1 );
329 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
332 case 'd': /* set debug level and 'do not detach' flag */
335 slap_debug |= atoi( optarg );
337 if ( atoi( optarg ) != 0 )
338 fputs( "must compile with LDAP_DEBUG for debugging\n",
343 case 'f': /* read config file */
344 configfile = ch_strdup( optarg );
347 case 's': /* set syslog level */
348 ldap_syslog = atoi( optarg );
352 case 'l': /* set syslog local user */
353 syslogUser = cnvt_str2int( optarg,
354 syslog_types, DEFAULT_SYSLOG_USER );
360 if( sandbox ) free(sandbox);
361 sandbox = ch_strdup( optarg );
365 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
366 case 'u': /* user name */
367 if( username ) free(username);
368 username = ch_strdup( optarg );
371 case 'g': /* group name */
372 if( groupname ) free(groupname);
373 groupname = ch_strdup( optarg );
375 #endif /* SETUID && GETUID */
377 case 'n': /* NT service name */
378 serverName = ch_strdup( optarg );
382 /* deprecated; use slaptest instead */
383 fprintf( stderr, "option -t deprecated; "
384 "use slaptest command instead\n" );
385 check |= CHECK_CONFIG;
393 for (i=0; tools[i].name; i++) {
394 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
395 rc = tools[i].func(argc, argv);
400 serverNamePrefix = "slap";
401 unrecognized_server_name:;
402 fprintf( stderr, "program name \"%s%s\" unrecognized; "
403 "aborting...\n", serverNamePrefix, serverName );
408 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
414 lutil_log_initialize( argc, argv );
416 lutil_set_debug_level( "slapd", slap_debug );
417 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
418 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
419 ldif_debug = slap_debug;
423 fprintf( stderr, "%s\n", Versionstr );
424 if ( version > 1 ) goto stop;
430 logName = ch_strdup( serverName );
433 logName = serverName;
437 openlog( logName, OPENLOG_OPTIONS, syslogUser );
439 openlog( logName, OPENLOG_OPTIONS );
447 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
449 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
452 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
454 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
458 #if defined(HAVE_CHROOT)
460 if ( chdir( sandbox ) ) {
465 if ( chroot( sandbox ) ) {
473 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
474 if ( username != NULL || groupname != NULL ) {
475 slap_init_user( username, groupname );
484 if ( module_init() != 0 ) {
486 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
491 if ( slap_schema_init( ) != 0 ) {
493 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
495 Debug( LDAP_DEBUG_ANY,
496 "schema initialization error\n",
503 if ( slap_init( serverMode, serverName ) != 0 ) {
505 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
509 if ( slap_controls_init( ) != 0 ) {
511 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
513 Debug( LDAP_DEBUG_ANY,
514 "controls initialization error\n",
522 /* Library defaults to full certificate checking. This is correct when
523 * a client is verifying a server because all servers should have a
524 * valid cert. But few clients have valid certs, so we want our default
525 * to be no checking. The config file can override this as usual.
528 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
532 if ( slapi_int_initialize() != 0 ) {
534 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
536 Debug( LDAP_DEBUG_ANY,
537 "slapi initialization error\n",
543 #endif /* LDAP_SLAPI */
545 if ( overlay_init() ) {
549 if ( read_config( configfile, 0 ) != 0 ) {
551 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
553 if ( check & CHECK_CONFIG ) {
554 fprintf( stderr, "config check failed\n" );
560 if ( check & CHECK_CONFIG ) {
561 fprintf( stderr, "config check succeeded\n" );
563 check &= ~CHECK_CONFIG;
564 if ( check == CHECK_NONE ) {
570 if ( glue_sub_init( ) != 0 ) {
572 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
574 Debug( LDAP_DEBUG_ANY,
575 "subordinate config error\n",
581 if ( slap_schema_check( ) != 0 ) {
583 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
585 Debug( LDAP_DEBUG_ANY,
586 "schema prep error\n",
594 rc = ldap_pvt_tls_init();
597 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
599 Debug( LDAP_DEBUG_ANY,
600 "main: TLS init failed: %d\n",
604 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
608 rc = ldap_pvt_tls_init_def_ctx();
611 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
613 Debug( LDAP_DEBUG_ANY,
614 "main: TLS init def ctx failed: %d\n",
618 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
623 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
624 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
627 (void) SIGNAL( SIGPIPE, SIG_IGN );
630 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
632 (void) SIGNAL( SIGINT, slap_sig_shutdown );
633 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
635 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
638 /* SIGBREAK is generated when Ctrl-Break is pressed. */
639 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
643 lutil_detach( no_detach, 0 );
644 #endif /* HAVE_WINSOCK */
651 * FIXME: moved here from slapd_daemon_task()
652 * because back-monitor db_open() needs it
656 if ( slap_startup( NULL ) != 0 ) {
658 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
663 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
665 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
669 if ( slapd_pid_file != NULL ) {
670 FILE *fp = fopen( slapd_pid_file, "w" );
673 fprintf( fp, "%d\n", (int) getpid() );
677 free(slapd_pid_file);
678 slapd_pid_file = NULL;
682 if ( slapd_args_file != NULL ) {
683 FILE *fp = fopen( slapd_args_file, "w" );
686 for ( i = 0; i < g_argc; i++ ) {
687 fprintf( fp, "%s ", g_argv[i] );
692 free(slapd_args_file);
693 slapd_args_file = NULL;
697 #ifdef HAVE_NT_EVENT_LOG
699 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
704 #ifdef HAVE_NT_SERVICE_MANAGER
705 /* Throw away the event that we used during the startup process. */
707 ldap_pvt_thread_cond_destroy( &started_event );
711 /* remember an error during shutdown */
712 rc |= slap_shutdown( NULL );
715 /* remember an error during destroy */
716 rc |= slap_destroy();
718 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
719 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
720 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
733 #ifdef HAVE_NT_EVENT_LOG
735 lutil_LogStoppedEvent( serverName );
739 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
741 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
745 #ifdef HAVE_NT_SERVICE_MANAGER
746 lutil_ReportShutdownComplete();
752 slapd_daemon_destroy();
758 lutil_passwd_destroy();
761 ldap_pvt_tls_destroy();
764 if ( slapd_pid_file != NULL ) {
765 unlink( slapd_pid_file );
767 if ( slapd_args_file != NULL ) {
768 unlink( slapd_args_file );
774 mal_dumpleaktrace( leakfile );
784 * Catch and discard terminated child processes, to avoid zombies.
788 wait4child( int sig )
790 int save_errno = errno;
795 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
798 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
804 (void) SIGNAL_REINSTALL( sig, wait4child );
808 #endif /* LDAP_SIGCHLD */
814 * Convert a string to an integer by means of a dispatcher table
815 * if the string is not in the table return the default
819 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
821 int retVal = defaultVal;
824 for (disp = dispatcher; disp->stringVal; disp++) {
826 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
828 retVal = disp->intVal;
837 #endif /* LOG_LOCAL4 */