11 #include <ac/signal.h>
14 #include "ldapconfig.h"
17 #include "lutil.h" /* Get lutil_detach() */
21 static RETSIGTYPE wait4child( int sig );
25 #define SERVICE_NAME "OpenLDAP"
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, short port, int udp );
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 )
53 short port = LDAP_PORT;
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", 6, LOG_LOCAL0 },
78 { "LOCAL1", 6, LOG_LOCAL1 },
79 { "LOCAL2", 6, LOG_LOCAL2 },
80 { "LOCAL3", 6, LOG_LOCAL3 },
81 { "LOCAL4", 6, LOG_LOCAL4 },
82 { "LOCAL5", 6, LOG_LOCAL5 },
83 { "LOCAL6", 6, LOG_LOCAL6 },
84 { "LOCAL7", 6, LOG_LOCAL7 },
88 static int cnvt_str2int( char *, STRDISP_P, int );
90 #endif /* LOG_LOCAL4 */
96 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
97 fprintf( stderr, "\n [-a bind-address] [-i] [-u]" );
98 #if LDAP_CONNECTIONLESS
99 fprintf( stderr, " [-c]" );
102 fprintf( stderr, " [-t]" );
105 fprintf( stderr, " [-l sysloguser]" );
107 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
108 fprintf( stderr, " [-u user] [-g group]" );
110 fprintf( stderr, "\n" );
114 struct sockaddr_in bind_addr;
118 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
121 void main( int argc, char **argv )
128 struct sockaddr_in *slapd_addr;
131 int syslogUser = DEFAULT_SYSLOG_USER;
134 char *configfile = ".\\slapd.conf";
136 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
139 int serverMode = SLAP_SERVER_MODE;
141 (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
142 bind_addr.sin_family = AF_INET;
143 bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
144 bind_addr.sin_port = htons(port);
150 //if ( is_NT_Service )
156 if ( is_NT_Service ) CommenceStartupProcessing( SERVICE_NAME, slap_set_shutdown );
157 newPort = (int*)getRegParam( NULL, "Port" );
158 if ( newPort != NULL )
161 bind_addr.sin_port = htons(port);
162 Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
164 newDebugLevel = (int*)getRegParam( NULL, "DebugLevel" );
165 if ( newDebugLevel != NULL )
167 slap_debug = *newDebugLevel;
168 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
170 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
171 if ( newConfigFile != NULL )
173 configfile = newConfigFile;
174 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
180 while ( (i = getopt( argc, argv,
188 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
191 #ifdef LDAP_CONNECTIONLESS
196 case 'a': /* bind address */
198 if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
200 if(!inet_aton(optarg, &bind_addr.sin_addr))
203 fprintf(stderr, "invalid address (%s) for -a option", optarg);
208 case 'd': /* turn on debugging */
209 if ( optarg[0] == '?' ) {
210 printf( "Debug levels:\n" );
211 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
213 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
214 LDAP_DEBUG_PACKETS );
215 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
217 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
219 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
221 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
223 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
225 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
227 printf( "\tLDAP_DEBUG_STATS\t%d\n",
229 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
231 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
233 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
235 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
239 slap_debug |= atoi( optarg );
243 case 'd': /* turn on debugging */
245 "must compile with LDAP_DEBUG for debugging\n" );
249 case 'f': /* read config file */
250 configfile = ch_strdup( optarg );
253 case 'i': /* run from inetd */
257 case 'p': { /* port on which to listen */
258 port = (short)atoi( optarg );
260 fprintf(stderr, "-p %s must be numeric\n", optarg);
262 bind_addr.sin_port = htons(port);
266 case 's': /* set syslog level */
267 ldap_syslog = atoi( optarg );
271 case 'l': /* set syslog local user */
272 syslogUser = cnvt_str2int( optarg, syslog_types,
273 DEFAULT_SYSLOG_USER );
277 #ifdef LDAP_CONNECTIONLESS
278 case 'c': /* do connectionless (udp) */
284 case 't': /* timed server */
285 serverMode = SLAP_TIMEDSERVER_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 */
304 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
309 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
310 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
311 ldif_debug = slap_debug;
313 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
315 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
316 serverName = ch_strdup( argv[0] );
318 serverName = ch_strdup( serverName + 1 );
322 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
324 openlog( serverName, OPENLOG_OPTIONS );
327 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
328 if ( username != NULL || groupname != NULL )
329 slap_init_user( username, groupname );
332 if ( slap_init( serverMode, serverName ) != 0 ) {
334 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
338 if ( read_config( configfile ) != 0 ) {
340 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
345 tcps = set_socket( inetd ? NULL : &bind_addr );
347 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
348 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
350 (void) SIGNAL( SIGPIPE, SIG_IGN );
353 (void) SIGNAL( SIGHUP, slap_set_shutdown );
355 (void) SIGNAL( SIGINT, slap_set_shutdown );
356 (void) SIGNAL( SIGTERM, slap_set_shutdown );
358 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
361 // SIGBREAK is generated when Ctrl-Break is pressed.
362 (void) SIGNAL( SIGBREAK, slap_set_shutdown );
368 lutil_detach( ldap_debug, 0 );
370 lutil_detach( 0, 0 );
373 #endif /* HAVE_WINSOC */
375 if ( slap_startup(-1) != 0 ) {
377 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
384 slapd_addr = &bind_addr;
386 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
388 if (( slapd_pid_file != NULL ) &&
389 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
391 fprintf( fp, "%d\n", (int) getpid() );
395 if (( slapd_args_file != NULL ) &&
396 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
398 for ( i = 0; i < g_argc; i++ ) {
399 fprintf( fp, "%s ", g_argv[i] );
411 LogSlapdStartedEvent( SERVICE_NAME, slap_debug, configfile, port, udp );
413 rc = slapd_daemon( slapd_addr, tcps );
416 // Throw away the event that we used during the startup process.
418 ldap_pvt_thread_cond_destroy( &started_event );
423 /* remember an error during shutdown */
424 rc |= slap_shutdown(-1);
426 /* remember an error during destroy */
427 rc |= slap_destroy();
431 LogSlapdStoppedEvent( SERVICE_NAME );
433 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
435 ReportSlapdShutdownComplete();
447 * Catch and discard terminated child processes, to avoid zombies.
451 wait4child( int sig )
453 int save_errno = errno;
458 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
461 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
467 (void) SIGNAL( sig, wait4child );
471 #endif /* SIGCHLD || SIGCLD */
477 * Convert a string to an integer by means of a dispatcher table
478 * if the string is not in the table return the default
482 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
484 int retVal = defaultVal;
487 for (disp = dispatcher; disp->stringVal; disp++) {
489 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
491 retVal = disp->intVal;
501 #endif /* LOG_LOCAL4 */