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, short port, int udp );
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)
51 short port = LDAP_PORT;
53 * when more than one slapd is running on one machine, each one might have
54 * it's own LOCAL for syslogging and must have its own pid/args files
57 #ifndef HAVE_MKVERSION
58 const char Versionstr[] =
59 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
64 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
66 typedef struct _str2intDispatch {
70 } STRDISP, *STRDISP_P;
73 /* table to compute syslog-options to integer */
74 static STRDISP syslog_types[] = {
75 { "LOCAL0", 6, LOG_LOCAL0 },
76 { "LOCAL1", 6, LOG_LOCAL1 },
77 { "LOCAL2", 6, LOG_LOCAL2 },
78 { "LOCAL3", 6, LOG_LOCAL3 },
79 { "LOCAL4", 6, LOG_LOCAL4 },
80 { "LOCAL5", 6, LOG_LOCAL5 },
81 { "LOCAL6", 6, LOG_LOCAL6 },
82 { "LOCAL7", 6, LOG_LOCAL7 },
86 static int cnvt_str2int( char *, STRDISP_P, int );
88 #endif /* LOG_LOCAL4 */
94 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
95 fprintf( stderr, "\n [-a bind-address] [-i] [-u]" );
97 fprintf( stderr, " [-n NTserviceName]" );
99 #if LDAP_CONNECTIONLESS
100 fprintf( stderr, " [-c]" );
103 fprintf( stderr, " [-t]" );
106 fprintf( stderr, " [-l sysloguser]" );
108 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
109 fprintf( stderr, " [-u user] [-g group]" );
111 fprintf( stderr, "\n" );
115 struct sockaddr_in bind_addr;
119 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
121 int main( int argc, char **argv )
127 struct slapd_args args;
129 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
130 char *username = NULL;
131 char *groupname = NULL;
134 int syslogUser = DEFAULT_SYSLOG_USER;
137 char *NTservice = SERVICE_NAME;
138 char *configfile = ".\\slapd.conf";
140 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
143 int serverMode = SLAP_SERVER_MODE;
145 (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
146 bind_addr.sin_family = AF_INET;
147 bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
148 bind_addr.sin_port = htons(port);
159 if ( is_NT_Service ) CommenceStartupProcessing( NTservice, slap_set_shutdown );
160 newPort = (int*)getRegParam( NULL, "Port" );
161 if ( newPort != NULL )
164 bind_addr.sin_port = htons(port);
165 Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
167 newDebugLevel = (int*)getRegParam( NULL, "DebugLevel" );
168 if ( newDebugLevel != NULL )
170 slap_debug = *newDebugLevel;
171 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
173 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
174 if ( newConfigFile != NULL )
176 configfile = newConfigFile;
177 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
182 while ( (i = getopt( argc, argv,
190 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
193 #ifdef LDAP_CONNECTIONLESS
201 case 'a': /* bind address */
203 if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
205 if(!inet_aton(optarg, &bind_addr.sin_addr))
208 fprintf(stderr, "invalid address (%s) for -a option", optarg);
213 case 'd': /* turn on debugging */
214 if ( optarg[0] == '?' ) {
215 printf( "Debug levels:\n" );
216 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
218 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
219 LDAP_DEBUG_PACKETS );
220 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
222 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
224 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
226 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
228 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
230 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
232 printf( "\tLDAP_DEBUG_STATS\t%d\n",
234 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
236 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
238 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
240 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
244 slap_debug |= atoi( optarg );
248 case 'd': /* turn on debugging */
250 "must compile with LDAP_DEBUG for debugging\n" );
254 case 'f': /* read config file */
255 configfile = ch_strdup( optarg );
258 case 'i': /* run from inetd */
262 case 'p': { /* port on which to listen */
263 port = (short)atoi( optarg );
265 fprintf(stderr, "-p %s must be numeric\n", optarg);
267 bind_addr.sin_port = htons(port);
271 case 's': /* set syslog level */
272 ldap_syslog = atoi( optarg );
276 case 'l': /* set syslog local user */
277 syslogUser = cnvt_str2int( optarg, syslog_types,
278 DEFAULT_SYSLOG_USER );
282 #ifdef LDAP_CONNECTIONLESS
283 case 'c': /* do connectionless (udp) */
289 case 't': /* timed server */
290 serverMode = SLAP_TIMEDSERVER_MODE;
294 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
295 case 'u': /* user name */
296 if( username ) free(username);
297 username = ch_strdup( optarg );
300 case 'g': /* group name */
301 if( groupname ) free(groupname);
302 groupname = ch_strdup( optarg );
304 #endif /* SETUID && GETUID */
306 case 'n': /* NT service name */
307 NTservice = ch_strdup( optarg );
313 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
318 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
319 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
320 ldif_debug = slap_debug;
322 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
324 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
325 serverName = ch_strdup( argv[0] );
327 serverName = ch_strdup( serverName + 1 );
331 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
333 openlog( serverName, OPENLOG_OPTIONS );
336 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
337 if ( username != NULL || groupname != NULL )
338 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 );
353 tcps = set_socket( inetd ? NULL : &bind_addr );
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 );
378 lutil_detach( ldap_debug, 0 );
380 lutil_detach( 0, 0 );
383 #endif /* HAVE_WINSOC */
385 if ( slap_startup(-1) != 0 ) {
387 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
394 args.addr = &bind_addr;
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] );
422 LogSlapdStartedEvent( NTservice, slap_debug, configfile, port, udp );
425 rc = slapd_daemon( &args );
428 /* Throw away the event that we used during the startup process. */
430 ldap_pvt_thread_cond_destroy( &started_event );
434 /* remember an error during shutdown */
435 rc |= slap_shutdown(-1);
437 /* remember an error during destroy */
438 rc |= slap_destroy();
442 LogSlapdStoppedEvent( NTservice );
444 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
446 ReportSlapdShutdownComplete();
458 * Catch and discard terminated child processes, to avoid zombies.
462 wait4child( int sig )
464 int save_errno = errno;
469 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
472 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
478 (void) SIGNAL( sig, wait4child );
482 #endif /* SIGCHLD || SIGCLD */
488 * Convert a string to an integer by means of a dispatcher table
489 * if the string is not in the table return the default
493 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
495 int retVal = defaultVal;
498 for (disp = dispatcher; disp->stringVal; disp++) {
500 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
502 retVal = disp->intVal;
512 #endif /* LOG_LOCAL4 */