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;
166 if ( is_NT_Service ) CommenceStartupProcessing( NTservice, slap_set_shutdown );
167 i = (int*)getRegParam( NULL, "Port" );
171 Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
174 i = (int*)getRegParam( NULL, "TLSPort" );
178 Debug ( LDAP_DEBUG_ANY, "new TLS port from registry is: %d\n", tls_port, 0, 0 );
181 i = (int*)getRegParam( NULL, "DebugLevel" );
185 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
187 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
188 if ( newConfigFile != NULL )
190 configfile = newConfigFile;
191 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
196 while ( (i = getopt( argc, argv,
204 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
207 #ifdef LDAP_CONNECTIONLESS
218 case 'h': /* listen URLs */
219 urls = ch_strdup( optarg );
223 case 'd': /* turn on debugging */
224 slap_debug |= atoi( optarg );
227 case 'd': /* turn on debugging */
229 "must compile with LDAP_DEBUG for debugging\n" );
233 case 'f': /* read config file */
234 configfile = ch_strdup( optarg );
237 case 'p': { /* port on which to listen */
238 int p = atoi( optarg );
240 fprintf(stderr, "-p %s must be numeric\n", optarg);
241 } else if( p < 0 || p >= 1<<16) {
242 fprintf(stderr, "-p %s invalid\n", optarg);
249 case 'P': { /* port on which to listen for TLS */
250 int p = atoi( optarg );
252 fprintf(stderr, "-P %s must be numeric\n", optarg);
253 } else if( p < 0 || p >= 1<<16) {
254 fprintf(stderr, "-P %s invalid\n", optarg);
261 case 's': /* set syslog level */
262 ldap_syslog = atoi( optarg );
266 case 'l': /* set syslog local user */
267 syslogUser = cnvt_str2int( optarg,
268 syslog_types, DEFAULT_SYSLOG_USER );
272 #ifdef LDAP_CONNECTIONLESS
273 case 'c': /* do connectionless (udp) */
275 fprintf( stderr, "connectionless support not supported");
281 case 't': /* timed server */
282 serverMode = SLAP_TIMEDSERVER_MODE;
286 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
287 case 'u': /* user name */
288 if( username ) free(username);
289 username = ch_strdup( optarg );
292 case 'g': /* group name */
293 if( groupname ) free(groupname);
294 groupname = ch_strdup( optarg );
296 #endif /* SETUID && GETUID */
299 case 'n': /* NT service name */
300 NTservice = ch_strdup( optarg );
306 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
311 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
312 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
313 ldif_debug = slap_debug;
315 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
317 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
318 serverName = ch_strdup( argv[0] );
320 serverName = ch_strdup( serverName + 1 );
324 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
326 openlog( serverName, OPENLOG_OPTIONS );
329 if( slapd_daemon_init( urls, port, tls_port ) != 0 ) {
331 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
335 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
336 if ( username != NULL || groupname != NULL ) {
337 slap_init_user( username, groupname );
341 if ( slap_init( serverMode, serverName ) != 0 ) {
343 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
347 if ( read_config( configfile ) != 0 ) {
349 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
355 ldap_pvt_tls_init_def_ctx();
358 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
359 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
361 (void) SIGNAL( SIGPIPE, SIG_IGN );
364 (void) SIGNAL( SIGHUP, slap_set_shutdown );
366 (void) SIGNAL( SIGINT, slap_set_shutdown );
367 (void) SIGNAL( SIGTERM, slap_set_shutdown );
369 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
372 /* SIGBREAK is generated when Ctrl-Break is pressed. */
373 (void) SIGNAL( SIGBREAK, slap_set_shutdown );
378 lutil_detach( ldap_debug, 0 );
380 lutil_detach( 0, 0 );
382 #endif /* HAVE_WINSOCK */
384 if ( slap_startup(-1) != 0 ) {
386 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
393 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
395 if (( slapd_pid_file != NULL ) &&
396 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
398 fprintf( fp, "%d\n", (int) getpid() );
402 if (( slapd_args_file != NULL ) &&
403 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
405 for ( i = 0; i < g_argc; i++ ) {
406 fprintf( fp, "%s ", g_argv[i] );
414 LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
420 /* Throw away the event that we used during the startup process. */
422 ldap_pvt_thread_cond_destroy( &started_event );
426 /* remember an error during shutdown */
427 rc |= slap_shutdown(-1);
429 /* remember an error during destroy */
430 rc |= slap_destroy();
434 LogSlapdStoppedEvent( NTservice );
437 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
440 ReportSlapdShutdownComplete();
444 slapd_daemon_destroy();
453 * Catch and discard terminated child processes, to avoid zombies.
457 wait4child( int sig )
459 int save_errno = errno;
464 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
467 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
473 (void) SIGNAL( sig, wait4child );
477 #endif /* SIGCHLD || SIGCLD */
483 * Convert a string to an integer by means of a dispatcher table
484 * if the string is not in the table return the default
488 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
490 int retVal = defaultVal;
493 for (disp = dispatcher; disp->stringVal; disp++) {
495 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
497 retVal = disp->intVal;
506 #endif /* LOG_LOCAL4 */