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);
223 #ifdef HAVE_NT_SERVICE_MANAGER
228 char *regService = NULL;
230 if ( is_NT_Service ) {
231 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
232 if ( strcmp(serverName, SERVICE_NAME) )
233 regService = serverName;
236 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
241 lutil_log_initialize( argc, argv );
242 LDAP_LOG( SLAPD, INFO,
243 "main: new debug level from registry is: %d\n",
246 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
250 newUrls = (char *) lutil_getRegParam(regService, "Urls");
256 urls = ch_strdup(newUrls);
258 LDAP_LOG( SLAPD, INFO,
259 "main: new urls from registry: %s\n", urls, 0, 0 );
261 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
267 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
268 if ( newConfigFile != NULL )
270 configfile = newConfigFile;
272 LDAP_LOG( SLAPD, INFO,
273 "main: new config file from registry is: %s\n", configfile, 0, 0 );
275 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
282 while ( (i = getopt( argc, argv,
293 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
300 slap_inet4or6 = AF_INET;
303 slap_inet4or6 = AF_INET6;
307 case 'h': /* listen URLs */
308 if ( urls != NULL ) free( urls );
309 urls = ch_strdup( optarg );
312 case 'c': /* provide sync cookie, override if exist in replica */
313 scp = (struct sync_cookie *) ch_calloc( 1,
314 sizeof( struct sync_cookie ));
315 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
316 ber_bvarray_add( &scp->octet_str, &cookie );
317 slap_parse_sync_cookie( scp );
319 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
320 if ( scp->rid == scp_entry->rid ) {
322 LDAP_LOG( OPERATION, CRIT,
323 "main: duplicated replica id in cookies\n",
326 Debug( LDAP_DEBUG_ANY,
327 "main: duplicated replica id in cookies\n",
330 slap_sync_cookie_free( scp, 1 );
334 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
337 case 'd': /* set debug level and 'do not detach' flag */
340 slap_debug |= atoi( optarg );
342 if ( atoi( optarg ) != 0 )
343 fputs( "must compile with LDAP_DEBUG for debugging\n",
348 case 'f': /* read config file */
349 configfile = ch_strdup( optarg );
352 case 's': /* set syslog level */
353 ldap_syslog = atoi( optarg );
357 case 'l': /* set syslog local user */
358 syslogUser = cnvt_str2int( optarg,
359 syslog_types, DEFAULT_SYSLOG_USER );
365 if( sandbox ) free(sandbox);
366 sandbox = ch_strdup( optarg );
370 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
371 case 'u': /* user name */
372 if( username ) free(username);
373 username = ch_strdup( optarg );
376 case 'g': /* group name */
377 if( groupname ) free(groupname);
378 groupname = ch_strdup( optarg );
380 #endif /* SETUID && GETUID */
382 case 'n': /* NT service name */
383 serverName = ch_strdup( optarg );
387 /* deprecated; use slaptest instead */
388 fprintf( stderr, "option -t deprecated; "
389 "use slaptest command instead\n" );
390 check |= CHECK_CONFIG;
398 /* try full option string first */
399 for ( i = 0; tools[i].name; i++ ) {
400 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
401 rc = tools[i].func( argc, argv );
406 /* try bits of option string (backward compatibility for single char) */
407 l = strlen( optarg );
408 for ( i = 0; tools[i].name; i++ ) {
409 if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
410 rc = tools[i].func( argc, argv );
417 serverNamePrefix = "slap";
418 fprintf( stderr, "program name \"%s%s\" unrecognized; "
419 "aborting...\n", serverNamePrefix, serverName );
424 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
430 lutil_log_initialize( argc, argv );
432 lutil_set_debug_level( "slapd", slap_debug );
433 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
434 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
435 ldif_debug = slap_debug;
439 fprintf( stderr, "%s\n", Versionstr );
440 if ( version > 1 ) goto stop;
446 logName = ch_strdup( serverName );
449 logName = serverName;
453 openlog( logName, OPENLOG_OPTIONS, syslogUser );
455 openlog( logName, OPENLOG_OPTIONS );
463 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
465 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
468 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
470 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
474 #if defined(HAVE_CHROOT)
476 if ( chdir( sandbox ) ) {
481 if ( chroot( sandbox ) ) {
489 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
490 if ( username != NULL || groupname != NULL ) {
491 slap_init_user( username, groupname );
500 if ( module_init() != 0 ) {
502 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
507 if ( slap_schema_init( ) != 0 ) {
509 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
511 Debug( LDAP_DEBUG_ANY,
512 "schema initialization error\n",
519 if ( slap_init( serverMode, serverName ) != 0 ) {
521 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
525 if ( slap_controls_init( ) != 0 ) {
527 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
529 Debug( LDAP_DEBUG_ANY,
530 "controls initialization error\n",
538 /* Library defaults to full certificate checking. This is correct when
539 * a client is verifying a server because all servers should have a
540 * valid cert. But few clients have valid certs, so we want our default
541 * to be no checking. The config file can override this as usual.
544 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
548 if ( slapi_int_initialize() != 0 ) {
550 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
552 Debug( LDAP_DEBUG_ANY,
553 "slapi initialization error\n",
559 #endif /* LDAP_SLAPI */
561 if ( overlay_init() ) {
565 if ( read_config( configfile, 0 ) != 0 ) {
567 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
569 if ( check & CHECK_CONFIG ) {
570 fprintf( stderr, "config check failed\n" );
576 if ( check & CHECK_CONFIG ) {
577 fprintf( stderr, "config check succeeded\n" );
579 check &= ~CHECK_CONFIG;
580 if ( check == CHECK_NONE ) {
586 if ( glue_sub_init( ) != 0 ) {
588 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
590 Debug( LDAP_DEBUG_ANY,
591 "subordinate config error\n",
597 if ( slap_schema_check( ) != 0 ) {
599 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
601 Debug( LDAP_DEBUG_ANY,
602 "schema prep error\n",
610 rc = ldap_pvt_tls_init();
613 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
615 Debug( LDAP_DEBUG_ANY,
616 "main: TLS init failed: %d\n",
620 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
624 rc = ldap_pvt_tls_init_def_ctx();
627 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
629 Debug( LDAP_DEBUG_ANY,
630 "main: TLS init def ctx failed: %d\n",
634 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
639 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
640 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
643 (void) SIGNAL( SIGPIPE, SIG_IGN );
646 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
648 (void) SIGNAL( SIGINT, slap_sig_shutdown );
649 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
651 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
654 /* SIGBREAK is generated when Ctrl-Break is pressed. */
655 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
659 lutil_detach( no_detach, 0 );
660 #endif /* HAVE_WINSOCK */
667 * FIXME: moved here from slapd_daemon_task()
668 * because back-monitor db_open() needs it
672 if ( slap_startup( NULL ) != 0 ) {
674 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
679 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
681 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
685 if ( slapd_pid_file != NULL ) {
686 FILE *fp = fopen( slapd_pid_file, "w" );
689 fprintf( fp, "%d\n", (int) getpid() );
693 free(slapd_pid_file);
694 slapd_pid_file = NULL;
698 if ( slapd_args_file != NULL ) {
699 FILE *fp = fopen( slapd_args_file, "w" );
702 for ( i = 0; i < g_argc; i++ ) {
703 fprintf( fp, "%s ", g_argv[i] );
708 free(slapd_args_file);
709 slapd_args_file = NULL;
713 #ifdef HAVE_NT_EVENT_LOG
715 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
720 #ifdef HAVE_NT_SERVICE_MANAGER
721 /* Throw away the event that we used during the startup process. */
723 ldap_pvt_thread_cond_destroy( &started_event );
727 /* remember an error during shutdown */
728 rc |= slap_shutdown( NULL );
731 /* remember an error during destroy */
732 rc |= slap_destroy();
734 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
735 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
736 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
749 #ifdef HAVE_NT_EVENT_LOG
751 lutil_LogStoppedEvent( serverName );
755 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
757 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
761 #ifdef HAVE_NT_SERVICE_MANAGER
762 lutil_ReportShutdownComplete();
768 slapd_daemon_destroy();
774 lutil_passwd_destroy();
777 ldap_pvt_tls_destroy();
780 if ( slapd_pid_file != NULL ) {
781 unlink( slapd_pid_file );
783 if ( slapd_args_file != NULL ) {
784 unlink( slapd_args_file );
790 mal_dumpleaktrace( leakfile );
800 * Catch and discard terminated child processes, to avoid zombies.
804 wait4child( int sig )
806 int save_errno = errno;
811 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
814 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
820 (void) SIGNAL_REINSTALL( sig, wait4child );
824 #endif /* LDAP_SIGCHLD */
830 * Convert a string to an integer by means of a dispatcher table
831 * if the string is not in the table return the default
835 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
837 int retVal = defaultVal;
840 for (disp = dispatcher; disp->stringVal; disp++) {
842 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
844 retVal = disp->intVal;
853 #endif /* LOG_LOCAL4 */