11 #include <ac/signal.h>
14 #include "ldap_defaults.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 int main( int argc, char **argv )
128 struct slapd_args args;
130 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
131 char *username = NULL;
132 char *groupname = NULL;
135 int syslogUser = DEFAULT_SYSLOG_USER;
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);
154 //if ( is_NT_Service )
160 if ( is_NT_Service ) CommenceStartupProcessing( SERVICE_NAME, slap_set_shutdown );
161 newPort = (int*)getRegParam( NULL, "Port" );
162 if ( newPort != NULL )
165 bind_addr.sin_port = htons(port);
166 Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
168 newDebugLevel = (int*)getRegParam( NULL, "DebugLevel" );
169 if ( newDebugLevel != NULL )
171 slap_debug = *newDebugLevel;
172 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
174 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
175 if ( newConfigFile != NULL )
177 configfile = newConfigFile;
178 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
184 while ( (i = getopt( argc, argv,
192 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
195 #ifdef LDAP_CONNECTIONLESS
200 case 'a': /* bind address */
202 if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
204 if(!inet_aton(optarg, &bind_addr.sin_addr))
207 fprintf(stderr, "invalid address (%s) for -a option", optarg);
212 case 'd': /* turn on debugging */
213 if ( optarg[0] == '?' ) {
214 printf( "Debug levels:\n" );
215 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
217 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
218 LDAP_DEBUG_PACKETS );
219 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
221 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
223 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
225 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
227 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
229 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
231 printf( "\tLDAP_DEBUG_STATS\t%d\n",
233 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
235 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
237 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
239 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
243 slap_debug |= atoi( optarg );
247 case 'd': /* turn on debugging */
249 "must compile with LDAP_DEBUG for debugging\n" );
253 case 'f': /* read config file */
254 configfile = ch_strdup( optarg );
257 case 'i': /* run from inetd */
261 case 'p': { /* port on which to listen */
262 port = (short)atoi( optarg );
264 fprintf(stderr, "-p %s must be numeric\n", optarg);
266 bind_addr.sin_port = htons(port);
270 case 's': /* set syslog level */
271 ldap_syslog = atoi( optarg );
275 case 'l': /* set syslog local user */
276 syslogUser = cnvt_str2int( optarg, syslog_types,
277 DEFAULT_SYSLOG_USER );
281 #ifdef LDAP_CONNECTIONLESS
282 case 'c': /* do connectionless (udp) */
288 case 't': /* timed server */
289 serverMode = SLAP_TIMEDSERVER_MODE;
293 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
294 case 'u': /* user name */
295 if( username ) free(username);
296 username = ch_strdup( optarg );
299 case 'g': /* group name */
300 if( groupname ) free(groupname);
301 groupname = ch_strdup( optarg );
303 #endif /* SETUID && GETUID */
308 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
313 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
314 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
315 ldif_debug = slap_debug;
317 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
319 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
320 serverName = ch_strdup( argv[0] );
322 serverName = ch_strdup( serverName + 1 );
326 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
328 openlog( serverName, OPENLOG_OPTIONS );
331 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
332 if ( username != NULL || groupname != NULL )
333 slap_init_user( username, groupname );
336 if ( slap_init( serverMode, serverName ) != 0 ) {
338 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
342 if ( read_config( configfile ) != 0 ) {
344 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
349 tcps = set_socket( inetd ? NULL : &bind_addr );
351 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
352 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
354 (void) SIGNAL( SIGPIPE, SIG_IGN );
357 (void) SIGNAL( SIGHUP, slap_set_shutdown );
359 (void) SIGNAL( SIGINT, slap_set_shutdown );
360 (void) SIGNAL( SIGTERM, slap_set_shutdown );
362 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
365 // SIGBREAK is generated when Ctrl-Break is pressed.
366 (void) SIGNAL( SIGBREAK, slap_set_shutdown );
372 lutil_detach( ldap_debug, 0 );
374 lutil_detach( 0, 0 );
377 #endif /* HAVE_WINSOC */
379 if ( slap_startup(-1) != 0 ) {
381 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
388 args.addr = &bind_addr;
390 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
392 if (( slapd_pid_file != NULL ) &&
393 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
395 fprintf( fp, "%d\n", (int) getpid() );
399 if (( slapd_args_file != NULL ) &&
400 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
402 for ( i = 0; i < g_argc; i++ ) {
403 fprintf( fp, "%s ", g_argv[i] );
416 LogSlapdStartedEvent( SERVICE_NAME, slap_debug, configfile, port, udp );
419 rc = slapd_daemon( &args );
422 // Throw away the event that we used during the startup process.
424 ldap_pvt_thread_cond_destroy( &started_event );
429 /* remember an error during shutdown */
430 rc |= slap_shutdown(-1);
432 /* remember an error during destroy */
433 rc |= slap_destroy();
437 LogSlapdStoppedEvent( SERVICE_NAME );
439 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
441 ReportSlapdShutdownComplete();
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;
507 #endif /* LOG_LOCAL4 */