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>
22 static RETSIGTYPE wait4child( int sig );
26 #define MAIN_RETURN(x) return
27 struct sockaddr_in bind_addr;
30 extern SERVICE_STATUS SLAPDServiceStatus;
31 extern SERVICE_STATUS_HANDLE hSLAPDServiceStatus;
32 extern ldap_pvt_thread_cond_t started_event, stopped_event;
33 extern int is_NT_Service;
35 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
36 void LogSlapdStoppedEvent( char *svc );
38 void CommenceStartupProcessing( LPCTSTR serviceName,
40 void ReportSlapdShutdownComplete( void );
41 void *getRegParam( char *svc, char *value );
43 #define SERVICE_EXIT( e, n ) \
44 if ( is_NT_Service ) \
46 SLAPDServiceStatus.dwWin32ExitCode = e; \
47 SLAPDServiceStatus.dwServiceSpecificExitCode = n; \
50 #define SERVICE_EXIT( e, n )
51 #define MAIN_RETURN(x) return(x)
55 * when more than one slapd is running on one machine, each one might have
56 * it's own LOCAL for syslogging and must have its own pid/args files
59 #ifndef HAVE_MKVERSION
60 const char Versionstr[] =
61 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
66 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
68 typedef struct _str2intDispatch {
72 } STRDISP, *STRDISP_P;
75 /* table to compute syslog-options to integer */
76 static STRDISP syslog_types[] = {
77 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
78 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
79 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
80 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
81 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
82 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
83 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
84 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
88 static int cnvt_str2int( char *, STRDISP_P, int );
90 #endif /* LOG_LOCAL4 */
97 "usage: %s options\n", name );
99 #if LDAP_CONNECTIONLESS
100 "\t-c\t\tEnable (experimental) Connectionless LDAP\n"
102 "\t-d level\tDebug Level" "\n"
103 "\t-f filename\tConfiguration File\n"
104 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
105 "\t-g group\tGroup (id or name) to ran as\n"
107 "\t-h URLs\tList of URLs to serve\n"
109 "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
112 "\t-n NTserviceName\tNT service name\n"
115 "\t-p port\tLDAP Port\n"
117 "\t-P port\tLDAP over TLS Port\n"
119 "\t-s level\tSyslog Level\n"
121 "\t-t\t\tEnable BDB2 timing\n"
123 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
124 "\t-u user\tUser (id or name) to ran as\n"
130 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
132 int main( int argc, char **argv )
138 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
139 char *username = NULL;
140 char *groupname = NULL;
143 int syslogUser = DEFAULT_SYSLOG_USER;
146 char *NTservice = SERVICE_NAME;
147 char *configfile = ".\\slapd.conf";
149 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
152 int serverMode = SLAP_SERVER_MODE;
154 int port = LDAP_PORT;
156 int tls_port = LDAPS_PORT;
163 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
176 if ( is_NT_Service ) {
177 CommenceStartupProcessing( NTservice, slap_sig_shutdown );
180 i = (int*)getRegParam( NULL, "Port" );
184 Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
187 i = (int*)getRegParam( NULL, "TLSPort" );
191 Debug ( LDAP_DEBUG_ANY, "new TLS port from registry is: %d\n", tls_port, 0, 0 );
194 i = (int*)getRegParam( NULL, "DebugLevel" );
198 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
200 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
201 if ( newConfigFile != NULL )
203 configfile = newConfigFile;
204 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
209 while ( (i = getopt( argc, argv,
217 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
220 #ifdef LDAP_CONNECTIONLESS
231 case 'h': /* listen URLs */
232 if ( urls != NULL ) free( urls );
233 urls = ch_strdup( optarg );
237 case 'd': /* turn on debugging */
238 slap_debug |= atoi( optarg );
241 case 'd': /* turn on debugging */
243 "must compile with LDAP_DEBUG for debugging\n" );
247 case 'f': /* read config file */
248 configfile = ch_strdup( optarg );
251 case 'p': { /* port on which to listen */
252 int p = atoi( optarg );
254 fprintf(stderr, "-p %s must be numeric\n", optarg);
255 } else if( p < 0 || p >= 1<<16) {
256 fprintf(stderr, "-p %s invalid\n", optarg);
263 case 'P': { /* port on which to listen for TLS */
264 int p = atoi( optarg );
266 fprintf(stderr, "-P %s must be numeric\n", optarg);
267 } else if( p < 0 || p >= 1<<16) {
268 fprintf(stderr, "-P %s invalid\n", optarg);
275 case 's': /* set syslog level */
276 ldap_syslog = atoi( optarg );
280 case 'l': /* set syslog local user */
281 syslogUser = cnvt_str2int( optarg,
282 syslog_types, DEFAULT_SYSLOG_USER );
286 #ifdef LDAP_CONNECTIONLESS
287 case 'c': /* do connectionless (udp) */
289 fprintf( stderr, "connectionless support not supported");
290 exit( EXIT_FAILURE );
295 case 't': /* timed server */
296 serverMode |= SLAP_TIMED_MODE;
300 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
301 case 'u': /* user name */
302 if( username ) free(username);
303 username = ch_strdup( optarg );
306 case 'g': /* group name */
307 if( groupname ) free(groupname);
308 groupname = ch_strdup( optarg );
310 #endif /* SETUID && GETUID */
313 case 'n': /* NT service name */
314 NTservice = ch_strdup( optarg );
320 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
325 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
326 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
327 ldif_debug = slap_debug;
329 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
331 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
332 serverName = ch_strdup( argv[0] );
334 serverName = ch_strdup( serverName + 1 );
338 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
340 openlog( serverName, OPENLOG_OPTIONS );
343 if( slapd_daemon_init( urls, port, tls_port ) != 0 ) {
345 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
349 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
350 if ( username != NULL || groupname != NULL ) {
351 slap_init_user( username, groupname );
355 if ( slap_init( serverMode, serverName ) != 0 ) {
357 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
361 if ( read_config( configfile ) != 0 ) {
363 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
369 ldap_pvt_tls_init_def_ctx();
372 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
373 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
376 (void) SIGNAL( SIGPIPE, SIG_IGN );
379 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
381 (void) SIGNAL( SIGINT, slap_sig_shutdown );
382 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
384 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
387 /* SIGBREAK is generated when Ctrl-Break is pressed. */
388 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
393 lutil_detach( ldap_debug, 0 );
395 lutil_detach( 0, 0 );
397 #endif /* HAVE_WINSOCK */
403 if ( slap_startup( NULL ) != 0 ) {
405 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
412 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
414 if (( slapd_pid_file != NULL ) &&
415 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
417 fprintf( fp, "%d\n", (int) getpid() );
421 if (( slapd_args_file != NULL ) &&
422 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
424 for ( i = 0; i < g_argc; i++ ) {
425 fprintf( fp, "%s ", g_argv[i] );
433 LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
439 /* Throw away the event that we used during the startup process. */
441 ldap_pvt_thread_cond_destroy( &started_event );
445 /* remember an error during shutdown */
446 rc |= slap_shutdown( NULL );
449 /* remember an error during destroy */
450 rc |= slap_destroy();
454 LogSlapdStoppedEvent( NTservice );
457 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
460 ReportSlapdShutdownComplete();
464 slapd_daemon_destroy();
467 mal_dumpleaktrace( leakfile );
477 * Catch and discard terminated child processes, to avoid zombies.
481 wait4child( int sig )
483 int save_errno = errno;
488 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
491 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
497 (void) SIGNAL( sig, wait4child );
501 #endif /* SIGCHLD || SIGCLD */
507 * Convert a string to an integer by means of a dispatcher table
508 * if the string is not in the table return the default
512 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
514 int retVal = defaultVal;
517 for (disp = dispatcher; disp->stringVal; disp++) {
519 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
521 retVal = disp->intVal;
530 #endif /* LOG_LOCAL4 */