11 #include <ac/signal.h>
14 #include "ldap_defaults.h"
19 static RETSIGTYPE wait4child( int sig );
23 #define MAIN_RETURN(x) return
24 struct sockaddr_in bind_addr;
27 extern SERVICE_STATUS SLAPDServiceStatus;
28 extern SERVICE_STATUS_HANDLE hSLAPDServiceStatus;
29 extern ldap_pvt_thread_cond_t started_event, stopped_event;
30 extern int is_NT_Service;
32 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
33 void LogSlapdStoppedEvent( char *svc );
35 void CommenceStartupProcessing( LPCTSTR serviceName,
37 void ReportSlapdShutdownComplete( void );
38 void *getRegParam( char *svc, char *value );
40 #define SERVICE_EXIT( e, n ) \
41 if ( is_NT_Service ) \
43 SLAPDServiceStatus.dwWin32ExitCode = e; \
44 SLAPDServiceStatus.dwServiceSpecificExitCode = n; \
47 #define SERVICE_EXIT( e, n )
48 #define MAIN_RETURN(x) return(x)
52 * when more than one slapd is running on one machine, each one might have
53 * it's own LOCAL for syslogging and must have its own pid/args files
56 #ifndef HAVE_MKVERSION
57 const char Versionstr[] =
58 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
63 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
65 typedef struct _str2intDispatch {
69 } STRDISP, *STRDISP_P;
72 /* table to compute syslog-options to integer */
73 static STRDISP syslog_types[] = {
74 { "LOCAL0", 6, LOG_LOCAL0 },
75 { "LOCAL1", 6, LOG_LOCAL1 },
76 { "LOCAL2", 6, LOG_LOCAL2 },
77 { "LOCAL3", 6, LOG_LOCAL3 },
78 { "LOCAL4", 6, LOG_LOCAL4 },
79 { "LOCAL5", 6, LOG_LOCAL5 },
80 { "LOCAL6", 6, LOG_LOCAL6 },
81 { "LOCAL7", 6, LOG_LOCAL7 },
85 static int cnvt_str2int( char *, STRDISP_P, int );
87 #endif /* LOG_LOCAL4 */
94 "usage: %s options\n", name );
96 #if LDAP_CONNECTIONLESS
97 "\t-c\t\tEnable (experimental) Connectionless LDAP\n"
99 "\t-d level\tDebug Level" "\n"
100 "\t-f filename\tConfiguration File\n"
101 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
102 "\t-g group\tGroup (id or name) to ran as\n"
104 "\t-h URLs\tList of URLs to serve"
106 "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
109 "\t-n NTserviceName\tNT service name\n"
112 "\t-p port\tLDAP Port\n"
114 "\t-P port\tLDAP over TLS Port\n"
116 "\t-s level\tSyslog Level\n"
118 "\t-t\t\tEnable BDB2 timing\n"
120 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
121 "\t-u user\tUser (id or name) to ran as\n"
127 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
129 int main( int argc, char **argv )
135 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
136 char *username = NULL;
137 char *groupname = NULL;
140 int syslogUser = DEFAULT_SYSLOG_USER;
143 char *NTservice = SERVICE_NAME;
144 char *configfile = ".\\slapd.conf";
146 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
149 int serverMode = SLAP_SERVER_MODE;
151 int port = LDAP_PORT;
153 int tls_port = LDAPS_PORT;
165 if ( is_NT_Service ) CommenceStartupProcessing( NTservice, slap_set_shutdown );
166 i = (int*)getRegParam( NULL, "Port" );
170 Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
173 i = (int*)getRegParam( NULL, "TLSPort" );
177 Debug ( LDAP_DEBUG_ANY, "new TLS port from registry is: %d\n", tls_port, 0, 0 );
180 i = (int*)getRegParam( NULL, "DebugLevel" );
184 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
186 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
187 if ( newConfigFile != NULL )
189 configfile = newConfigFile;
190 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
195 while ( (i = getopt( argc, argv,
203 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
206 #ifdef LDAP_CONNECTIONLESS
217 case 'h': /* listen URLs */
218 urls = ch_strdup( optarg );
222 case 'd': /* turn on debugging */
223 slap_debug |= atoi( optarg );
226 case 'd': /* turn on debugging */
228 "must compile with LDAP_DEBUG for debugging\n" );
232 case 'f': /* read config file */
233 configfile = ch_strdup( optarg );
236 case 'p': { /* port on which to listen */
237 int p = atoi( optarg );
239 fprintf(stderr, "-p %s must be numeric\n", optarg);
240 } else if( p < 0 || p >= 1<<16) {
241 fprintf(stderr, "-p %s invalid\n", optarg);
248 case 'P': { /* port on which to listen for TLS */
249 int p = atoi( optarg );
251 fprintf(stderr, "-P %s must be numeric\n", optarg);
252 } else if( p < 0 || p >= 1<<16) {
253 fprintf(stderr, "-P %s invalid\n", optarg);
260 case 's': /* set syslog level */
261 ldap_syslog = atoi( optarg );
265 case 'l': /* set syslog local user */
266 syslogUser = cnvt_str2int( optarg,
267 syslog_types, DEFAULT_SYSLOG_USER );
271 #ifdef LDAP_CONNECTIONLESS
272 case 'c': /* do connectionless (udp) */
274 fprintf( stderr, "connectionless support not supported");
275 exit( EXIT_FAILURE );
280 case 't': /* timed server */
281 serverMode = SLAP_TIMEDSERVER_MODE;
285 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
286 case 'u': /* user name */
287 if( username ) free(username);
288 username = ch_strdup( optarg );
291 case 'g': /* group name */
292 if( groupname ) free(groupname);
293 groupname = ch_strdup( optarg );
295 #endif /* SETUID && GETUID */
298 case 'n': /* NT service name */
299 NTservice = ch_strdup( optarg );
305 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
310 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
311 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
312 ldif_debug = slap_debug;
314 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
316 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
317 serverName = ch_strdup( argv[0] );
319 serverName = ch_strdup( serverName + 1 );
323 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
325 openlog( serverName, OPENLOG_OPTIONS );
328 if( slapd_daemon_init( urls, port, tls_port ) != 0 ) {
330 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
334 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
335 if ( username != NULL || groupname != NULL ) {
336 slap_init_user( username, groupname );
340 if ( slap_init( serverMode, serverName ) != 0 ) {
342 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
346 if ( read_config( configfile ) != 0 ) {
348 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
354 ldap_pvt_tls_init_def_ctx();
357 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
358 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
360 (void) SIGNAL( SIGPIPE, SIG_IGN );
363 (void) SIGNAL( SIGHUP, slap_set_shutdown );
365 (void) SIGNAL( SIGINT, slap_set_shutdown );
366 (void) SIGNAL( SIGTERM, slap_set_shutdown );
368 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
371 /* SIGBREAK is generated when Ctrl-Break is pressed. */
372 (void) SIGNAL( SIGBREAK, slap_set_shutdown );
377 lutil_detach( ldap_debug, 0 );
379 lutil_detach( 0, 0 );
381 #endif /* HAVE_WINSOCK */
383 if ( slap_startup(-1) != 0 ) {
385 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
392 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
394 if (( slapd_pid_file != NULL ) &&
395 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
397 fprintf( fp, "%d\n", (int) getpid() );
401 if (( slapd_args_file != NULL ) &&
402 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
404 for ( i = 0; i < g_argc; i++ ) {
405 fprintf( fp, "%s ", g_argv[i] );
413 LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
419 /* Throw away the event that we used during the startup process. */
421 ldap_pvt_thread_cond_destroy( &started_event );
425 /* remember an error during shutdown */
426 rc |= slap_shutdown(-1);
428 /* remember an error during destroy */
429 rc |= slap_destroy();
433 LogSlapdStoppedEvent( NTservice );
436 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
439 ReportSlapdShutdownComplete();
443 slapd_daemon_destroy();
452 * Catch and discard terminated child processes, to avoid zombies.
456 wait4child( int sig )
458 int save_errno = errno;
463 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
466 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
472 (void) SIGNAL( sig, wait4child );
476 #endif /* SIGCHLD || SIGCLD */
482 * Convert a string to an integer by means of a dispatcher table
483 * if the string is not in the table return the default
487 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
489 int retVal = defaultVal;
492 for (disp = dispatcher; disp->stringVal; disp++) {
494 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
496 retVal = disp->intVal;
505 #endif /* LOG_LOCAL4 */