2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/socket.h>
11 #include <ac/string.h>
13 #include <ac/unistd.h>
15 #include <ac/signal.h>
18 #include "ldap_defaults.h"
23 static RETSIGTYPE wait4child( int sig );
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 LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
37 void LogSlapdStoppedEvent( char *svc );
39 void CommenceStartupProcessing( LPCTSTR serviceName,
41 void ReportSlapdShutdownComplete( void );
42 void *getRegParam( char *svc, char *value );
44 #define SERVICE_EXIT( e, n ) \
45 if ( is_NT_Service ) \
47 SLAPDServiceStatus.dwWin32ExitCode = e; \
48 SLAPDServiceStatus.dwServiceSpecificExitCode = n; \
51 #define SERVICE_EXIT( e, n )
52 #define MAIN_RETURN(x) return(x)
56 * when more than one slapd is running on one machine, each one might have
57 * it's own LOCAL for syslogging and must have its own pid/args files
60 #ifndef HAVE_MKVERSION
61 const char Versionstr[] =
62 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
67 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
69 typedef struct _str2intDispatch {
73 } STRDISP, *STRDISP_P;
76 /* table to compute syslog-options to integer */
77 static STRDISP syslog_types[] = {
78 { "LOCAL0", 6, LOG_LOCAL0 },
79 { "LOCAL1", 6, LOG_LOCAL1 },
80 { "LOCAL2", 6, LOG_LOCAL2 },
81 { "LOCAL3", 6, LOG_LOCAL3 },
82 { "LOCAL4", 6, LOG_LOCAL4 },
83 { "LOCAL5", 6, LOG_LOCAL5 },
84 { "LOCAL6", 6, LOG_LOCAL6 },
85 { "LOCAL7", 6, LOG_LOCAL7 },
89 static int cnvt_str2int( char *, STRDISP_P, int );
91 #endif /* LOG_LOCAL4 */
98 "usage: %s options\n", name );
100 #if LDAP_CONNECTIONLESS
101 "\t-c\t\tEnable (experimental) Connectionless LDAP\n"
103 "\t-d level\tDebug Level" "\n"
104 "\t-f filename\tConfiguration File\n"
105 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
106 "\t-g group\tGroup (id or name) to ran as\n"
108 "\t-h URLs\tList of URLs to serve\n"
110 "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
113 "\t-n NTserviceName\tNT service name\n"
116 "\t-p port\tLDAP Port\n"
118 "\t-P port\tLDAP over TLS Port\n"
120 "\t-s level\tSyslog Level\n"
122 "\t-t\t\tEnable BDB2 timing\n"
124 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
125 "\t-u user\tUser (id or name) to ran as\n"
131 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
133 int main( int argc, char **argv )
139 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
140 char *username = NULL;
141 char *groupname = NULL;
144 int syslogUser = DEFAULT_SYSLOG_USER;
147 char *NTservice = SERVICE_NAME;
148 char *configfile = ".\\slapd.conf";
150 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
153 int serverMode = SLAP_SERVER_MODE;
155 int port = LDAP_PORT;
157 int tls_port = LDAPS_PORT;
169 if ( is_NT_Service ) CommenceStartupProcessing( NTservice, slap_set_shutdown );
170 i = (int*)getRegParam( NULL, "Port" );
174 Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
177 i = (int*)getRegParam( NULL, "TLSPort" );
181 Debug ( LDAP_DEBUG_ANY, "new TLS port from registry is: %d\n", tls_port, 0, 0 );
184 i = (int*)getRegParam( NULL, "DebugLevel" );
188 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
190 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
191 if ( newConfigFile != NULL )
193 configfile = newConfigFile;
194 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
199 while ( (i = getopt( argc, argv,
207 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
210 #ifdef LDAP_CONNECTIONLESS
221 case 'h': /* listen URLs */
222 urls = ch_strdup( optarg );
226 case 'd': /* turn on debugging */
227 slap_debug |= atoi( optarg );
230 case 'd': /* turn on debugging */
232 "must compile with LDAP_DEBUG for debugging\n" );
236 case 'f': /* read config file */
237 configfile = ch_strdup( optarg );
240 case 'p': { /* port on which to listen */
241 int p = atoi( optarg );
243 fprintf(stderr, "-p %s must be numeric\n", optarg);
244 } else if( p < 0 || p >= 1<<16) {
245 fprintf(stderr, "-p %s invalid\n", optarg);
252 case 'P': { /* port on which to listen for TLS */
253 int p = atoi( optarg );
255 fprintf(stderr, "-P %s must be numeric\n", optarg);
256 } else if( p < 0 || p >= 1<<16) {
257 fprintf(stderr, "-P %s invalid\n", optarg);
264 case 's': /* set syslog level */
265 ldap_syslog = atoi( optarg );
269 case 'l': /* set syslog local user */
270 syslogUser = cnvt_str2int( optarg,
271 syslog_types, DEFAULT_SYSLOG_USER );
275 #ifdef LDAP_CONNECTIONLESS
276 case 'c': /* do connectionless (udp) */
278 fprintf( stderr, "connectionless support not supported");
279 exit( EXIT_FAILURE );
284 case 't': /* timed server */
285 serverMode |= SLAP_TIMED_MODE;
289 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
290 case 'u': /* user name */
291 if( username ) free(username);
292 username = ch_strdup( optarg );
295 case 'g': /* group name */
296 if( groupname ) free(groupname);
297 groupname = ch_strdup( optarg );
299 #endif /* SETUID && GETUID */
302 case 'n': /* NT service name */
303 NTservice = ch_strdup( optarg );
309 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
314 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
315 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
316 ldif_debug = slap_debug;
318 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
320 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
321 serverName = ch_strdup( argv[0] );
323 serverName = ch_strdup( serverName + 1 );
327 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
329 openlog( serverName, OPENLOG_OPTIONS );
332 if( slapd_daemon_init( urls, port, tls_port ) != 0 ) {
334 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
338 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
339 if ( username != NULL || groupname != NULL ) {
340 slap_init_user( username, groupname );
344 if ( slap_init( serverMode, serverName ) != 0 ) {
346 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
350 if ( read_config( configfile ) != 0 ) {
352 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
358 ldap_pvt_tls_init_def_ctx();
361 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
362 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
364 (void) SIGNAL( SIGPIPE, SIG_IGN );
367 (void) SIGNAL( SIGHUP, slap_set_shutdown );
369 (void) SIGNAL( SIGINT, slap_set_shutdown );
370 (void) SIGNAL( SIGTERM, slap_set_shutdown );
372 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
375 /* SIGBREAK is generated when Ctrl-Break is pressed. */
376 (void) SIGNAL( SIGBREAK, slap_set_shutdown );
381 lutil_detach( ldap_debug, 0 );
383 lutil_detach( 0, 0 );
385 #endif /* HAVE_WINSOCK */
387 if ( slap_startup( NULL ) != 0 ) {
389 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
396 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
398 if (( slapd_pid_file != NULL ) &&
399 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
401 fprintf( fp, "%d\n", (int) getpid() );
405 if (( slapd_args_file != NULL ) &&
406 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
408 for ( i = 0; i < g_argc; i++ ) {
409 fprintf( fp, "%s ", g_argv[i] );
417 LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
423 /* Throw away the event that we used during the startup process. */
425 ldap_pvt_thread_cond_destroy( &started_event );
429 /* remember an error during shutdown */
430 rc |= slap_shutdown( NULL );
432 /* remember an error during destroy */
433 rc |= slap_destroy();
437 LogSlapdStoppedEvent( NTservice );
440 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
443 ReportSlapdShutdownComplete();
447 slapd_daemon_destroy();
456 * Catch and discard terminated child processes, to avoid zombies.
460 wait4child( int sig )
462 int save_errno = errno;
467 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
470 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
476 (void) SIGNAL( sig, wait4child );
480 #endif /* SIGCHLD || SIGCLD */
486 * Convert a string to an integer by means of a dispatcher table
487 * if the string is not in the table return the default
491 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
493 int retVal = defaultVal;
496 for (disp = dispatcher; disp->stringVal; disp++) {
498 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
500 retVal = disp->intVal;
509 #endif /* LOG_LOCAL4 */