3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/signal.h>
11 #include <ac/socket.h>
12 #include <ac/string.h>
14 #include <ac/unistd.h>
16 #include <ac/signal.h>
23 static RETSIGTYPE wait4child( int sig );
26 #ifdef HAVE_NT_SERVICE_MANAGER
27 #define MAIN_RETURN(x) return
28 struct sockaddr_in bind_addr;
31 extern SERVICE_STATUS SLAPDServiceStatus;
32 extern SERVICE_STATUS_HANDLE hSLAPDServiceStatus;
33 extern ldap_pvt_thread_cond_t started_event, stopped_event;
34 extern int is_NT_Service;
36 void CommenceStartupProcessing( LPCTSTR serviceName,
38 void ReportSlapdShutdownComplete( void );
39 void *getRegParam( char *svc, char *value );
41 #define SERVICE_EXIT( e, n ) \
42 if ( is_NT_Service ) \
44 SLAPDServiceStatus.dwWin32ExitCode = e; \
45 SLAPDServiceStatus.dwServiceSpecificExitCode = n; \
48 #define SERVICE_EXIT( e, n )
49 #define MAIN_RETURN(x) return(x)
52 #ifdef HAVE_NT_EVENT_MANAGER
53 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
54 void LogSlapdStoppedEvent( char *svc );
58 * when more than one slapd is running on one machine, each one might have
59 * it's own LOCAL for syslogging and must have its own pid/args files
62 #ifndef HAVE_MKVERSION
63 const char Versionstr[] =
64 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
69 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
71 typedef struct _str2intDispatch {
75 } STRDISP, *STRDISP_P;
78 /* table to compute syslog-options to integer */
79 static STRDISP syslog_types[] = {
80 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
81 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
82 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
83 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
84 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
85 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
86 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
87 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
91 static int cnvt_str2int( char *, STRDISP_P, int );
93 #endif /* LOG_LOCAL4 */
100 "usage: %s options\n", name );
102 #if LDAP_CONNECTIONLESS
103 "\t-c\t\tEnable (experimental) Connectionless LDAP\n"
105 "\t-d level\tDebug Level" "\n"
106 "\t-f filename\tConfiguration File\n"
107 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
108 "\t-g group\tGroup (id or name) to ran as\n"
110 "\t-h URLs\tList of URLs to serve\n"
112 "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
115 "\t-n NTserviceName\tNT service name\n"
118 "\t-p port\tLDAP Port\n"
120 "\t-P port\tLDAP over TLS Port\n"
122 "\t-s level\tSyslog Level\n"
124 "\t-t\t\tEnable BDB2 timing\n"
126 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
127 "\t-u user\tUser (id or name) to ran as\n"
132 #ifdef HAVE_NT_SERVICE_MANAGER
133 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
135 int main( int argc, char **argv )
138 int i, no_detach = 0;
141 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
142 char *username = NULL;
143 char *groupname = NULL;
146 int syslogUser = DEFAULT_SYSLOG_USER;
149 char *NTservice = SERVICE_NAME;
150 char *configfile = ".\\slapd.conf";
152 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
155 int serverMode = SLAP_SERVER_MODE;
157 int port = LDAP_PORT;
159 int tls_port = LDAPS_PORT;
166 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
174 #ifdef HAVE_NT_SERVICE_MANAGER
179 if ( is_NT_Service ) {
180 CommenceStartupProcessing( NTservice, slap_sig_shutdown );
183 i = (int*)getRegParam( NULL, "Port" );
187 Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
190 i = (int*)getRegParam( NULL, "TLSPort" );
194 Debug ( LDAP_DEBUG_ANY, "new TLS port from registry is: %d\n", tls_port, 0, 0 );
197 i = (int*)getRegParam( NULL, "DebugLevel" );
201 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
203 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
204 if ( newConfigFile != NULL )
206 configfile = newConfigFile;
207 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
212 while ( (i = getopt( argc, argv,
220 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
223 #ifdef LDAP_CONNECTIONLESS
234 case 'h': /* listen URLs */
235 if ( urls != NULL ) free( urls );
236 urls = ch_strdup( optarg );
239 case 'd': /* set debug level and 'do not detach' flag */
242 slap_debug |= atoi( optarg );
244 if ( atoi( optarg ) != 0 )
245 fputs( "must compile with LDAP_DEBUG for debugging\n",
250 case 'f': /* read config file */
251 configfile = ch_strdup( optarg );
254 case 'p': { /* port on which to listen */
255 int p = atoi( optarg );
257 fprintf(stderr, "-p %s must be numeric\n", optarg);
258 } else if( p < 0 || p >= 1<<16) {
259 fprintf(stderr, "-p %s invalid\n", optarg);
266 case 'P': { /* port on which to listen for TLS */
267 int p = atoi( optarg );
269 fprintf(stderr, "-P %s must be numeric\n", optarg);
270 } else if( p < 0 || p >= 1<<16) {
271 fprintf(stderr, "-P %s invalid\n", optarg);
278 case 's': /* set syslog level */
279 ldap_syslog = atoi( optarg );
283 case 'l': /* set syslog local user */
284 syslogUser = cnvt_str2int( optarg,
285 syslog_types, DEFAULT_SYSLOG_USER );
289 #ifdef LDAP_CONNECTIONLESS
290 case 'c': /* do connectionless (udp) */
292 fprintf( stderr, "connectionless support not supported");
293 exit( EXIT_FAILURE );
298 case 't': /* timed server */
299 serverMode |= SLAP_TIMED_MODE;
303 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
304 case 'u': /* user name */
305 if( username ) free(username);
306 username = ch_strdup( optarg );
309 case 'g': /* group name */
310 if( groupname ) free(groupname);
311 groupname = ch_strdup( optarg );
313 #endif /* SETUID && GETUID */
316 case 'n': /* NT service name */
317 NTservice = ch_strdup( optarg );
323 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
328 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
329 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
330 ldif_debug = slap_debug;
332 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
334 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
335 serverName = ch_strdup( argv[0] );
337 serverName = ch_strdup( serverName + 1 );
341 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
343 openlog( serverName, OPENLOG_OPTIONS );
346 if( slapd_daemon_init( urls, port, tls_port ) != 0 ) {
348 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
352 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
353 if ( username != NULL || groupname != NULL ) {
354 slap_init_user( username, groupname );
358 if ( slap_init( serverMode, serverName ) != 0 ) {
360 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
364 if ( read_config( configfile ) != 0 ) {
366 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
372 ldap_pvt_tls_init_def_ctx();
375 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
376 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
379 (void) SIGNAL( SIGPIPE, SIG_IGN );
382 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
384 (void) SIGNAL( SIGINT, slap_sig_shutdown );
385 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
387 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
390 /* SIGBREAK is generated when Ctrl-Break is pressed. */
391 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
395 lutil_detach( no_detach, 0 );
396 #endif /* HAVE_WINSOCK */
402 if ( slap_startup( NULL ) != 0 ) {
404 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
411 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
413 if (( slapd_pid_file != NULL ) &&
414 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
416 fprintf( fp, "%d\n", (int) getpid() );
420 if (( slapd_args_file != NULL ) &&
421 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
423 for ( i = 0; i < g_argc; i++ ) {
424 fprintf( fp, "%s ", g_argv[i] );
431 #ifdef HAVE_NT_EVENT_MANAGER
432 LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
437 #ifdef HAVE_NT_SERVICE_MANAGER
438 /* Throw away the event that we used during the startup process. */
440 ldap_pvt_thread_cond_destroy( &started_event );
444 /* remember an error during shutdown */
445 rc |= slap_shutdown( NULL );
448 /* remember an error during destroy */
449 rc |= slap_destroy();
452 #ifdef HAVE_NT_EVENT_MANAGER
453 LogSlapdStoppedEvent( NTservice );
456 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
458 #ifdef HAVE_NT_SERVICE_MANAGER
459 ReportSlapdShutdownComplete();
465 slapd_daemon_destroy();
468 mal_dumpleaktrace( leakfile );
478 * Catch and discard terminated child processes, to avoid zombies.
482 wait4child( int sig )
484 int save_errno = errno;
489 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
492 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
498 (void) SIGNAL( sig, wait4child );
502 #endif /* SIGCHLD || SIGCLD */
508 * Convert a string to an integer by means of a dispatcher table
509 * if the string is not in the table return the default
513 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
515 int retVal = defaultVal;
518 for (disp = dispatcher; disp->stringVal; disp++) {
520 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
522 retVal = disp->intVal;
531 #endif /* LOG_LOCAL4 */