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 short tls_port = LDAP_TLS_PORT;
56 * when more than one slapd is running on one machine, each one might have
57 * it's own LOCAL for syslogging and must have its own pid/args files
60 #ifndef HAVE_MKVERSION
61 const char Versionstr[] =
62 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
67 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
69 typedef struct _str2intDispatch {
73 } STRDISP, *STRDISP_P;
76 /* table to compute syslog-options to integer */
77 static STRDISP syslog_types[] = {
78 { "LOCAL0", 6, LOG_LOCAL0 },
79 { "LOCAL1", 6, LOG_LOCAL1 },
80 { "LOCAL2", 6, LOG_LOCAL2 },
81 { "LOCAL3", 6, LOG_LOCAL3 },
82 { "LOCAL4", 6, LOG_LOCAL4 },
83 { "LOCAL5", 6, LOG_LOCAL5 },
84 { "LOCAL6", 6, LOG_LOCAL6 },
85 { "LOCAL7", 6, LOG_LOCAL7 },
89 static int cnvt_str2int( char *, STRDISP_P, int );
91 #endif /* LOG_LOCAL4 */
97 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
98 fprintf( stderr, "\n [-a bind-address] [-i] [-u]" );
100 fprintf( stderr, " [-n NTserviceName]" );
102 #if LDAP_CONNECTIONLESS
103 fprintf( stderr, " [-c]" );
106 fprintf( stderr, " [-t]" );
109 fprintf( stderr, " [-l sysloguser]" );
111 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
112 fprintf( stderr, " [-u user] [-g group]" );
114 fprintf( stderr, "\n" );
118 struct sockaddr_in bind_addr;
121 struct sockaddr_in tls_bind_addr;
126 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
128 int main( int argc, char **argv )
134 struct slapd_args args;
136 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
137 char *username = NULL;
138 char *groupname = NULL;
141 int syslogUser = DEFAULT_SYSLOG_USER;
144 char *NTservice = SERVICE_NAME;
145 char *configfile = ".\\slapd.conf";
147 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
150 int serverMode = SLAP_SERVER_MODE;
152 (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
153 bind_addr.sin_family = AF_INET;
154 bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
155 bind_addr.sin_port = htons(port);
157 tls_bind_addr.sin_family = AF_INET;
158 tls_bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
159 tls_bind_addr.sin_port = htons(tls_port);
171 if ( is_NT_Service ) CommenceStartupProcessing( NTservice, slap_set_shutdown );
172 newPort = (int*)getRegParam( NULL, "Port" );
173 if ( newPort != NULL )
176 bind_addr.sin_port = htons(port);
177 Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
179 newDebugLevel = (int*)getRegParam( NULL, "DebugLevel" );
180 if ( newDebugLevel != NULL )
182 slap_debug = *newDebugLevel;
183 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
185 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
186 if ( newConfigFile != NULL )
188 configfile = newConfigFile;
189 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
194 while ( (i = getopt( argc, argv,
202 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
205 #ifdef LDAP_CONNECTIONLESS
216 case 'a': /* bind address */
218 if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
220 if(!inet_aton(optarg, &bind_addr.sin_addr))
223 fprintf(stderr, "invalid address (%s) for -a option", optarg);
227 tls_bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg);
229 inet_aton(optarg, &tls_bind_addr.sin_addr);
235 case 'd': /* turn on debugging */
236 if ( optarg[0] == '?' ) {
237 printf( "Debug levels:\n" );
238 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
240 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
241 LDAP_DEBUG_PACKETS );
242 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
244 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
246 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
248 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
250 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
252 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
254 printf( "\tLDAP_DEBUG_STATS\t%d\n",
256 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
258 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
260 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
262 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
266 slap_debug |= atoi( optarg );
270 case 'd': /* turn on debugging */
272 "must compile with LDAP_DEBUG for debugging\n" );
276 case 'f': /* read config file */
277 configfile = ch_strdup( optarg );
280 case 'i': /* run from inetd */
284 case 'p': { /* port on which to listen */
285 port = (short)atoi( optarg );
287 fprintf(stderr, "-p %s must be numeric\n", optarg);
289 bind_addr.sin_port = htons(port);
294 case 'P': { /* port on which to listen for TLS */
295 tls_port = (short)atoi( optarg );
297 fprintf(stderr, "-P %s must be numeric\n", optarg);
299 tls_bind_addr.sin_port = htons(tls_port);
304 case 's': /* set syslog level */
305 ldap_syslog = atoi( optarg );
309 case 'l': /* set syslog local user */
310 syslogUser = cnvt_str2int( optarg, syslog_types,
311 DEFAULT_SYSLOG_USER );
315 #ifdef LDAP_CONNECTIONLESS
316 case 'c': /* do connectionless (udp) */
322 case 't': /* timed server */
323 serverMode = SLAP_TIMEDSERVER_MODE;
327 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
328 case 'u': /* user name */
329 if( username ) free(username);
330 username = ch_strdup( optarg );
333 case 'g': /* group name */
334 if( groupname ) free(groupname);
335 groupname = ch_strdup( optarg );
337 #endif /* SETUID && GETUID */
339 case 'n': /* NT service name */
340 NTservice = ch_strdup( optarg );
346 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
351 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
352 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
353 ldif_debug = slap_debug;
355 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
357 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
358 serverName = ch_strdup( argv[0] );
360 serverName = ch_strdup( serverName + 1 );
364 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
366 openlog( serverName, OPENLOG_OPTIONS );
369 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
370 if ( username != NULL || groupname != NULL )
371 slap_init_user( username, groupname );
374 if ( slap_init( serverMode, serverName ) != 0 ) {
376 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
380 if ( read_config( configfile ) != 0 ) {
382 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
386 tcps = set_socket( inetd ? NULL : &bind_addr );
390 tls_tcps = set_socket( inetd ? NULL : &tls_bind_addr );
391 if ( tls_tcps == -1 )
395 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
396 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
398 (void) SIGNAL( SIGPIPE, SIG_IGN );
401 (void) SIGNAL( SIGHUP, slap_set_shutdown );
403 (void) SIGNAL( SIGINT, slap_set_shutdown );
404 (void) SIGNAL( SIGTERM, slap_set_shutdown );
406 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
409 /* SIGBREAK is generated when Ctrl-Break is pressed. */
410 (void) SIGNAL( SIGBREAK, slap_set_shutdown );
416 lutil_detach( ldap_debug, 0 );
418 lutil_detach( 0, 0 );
421 #endif /* HAVE_WINSOC */
423 if ( slap_startup(-1) != 0 ) {
425 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
432 args.addr = &bind_addr;
434 args.tls_addr = &tls_bind_addr;
437 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
439 if (( slapd_pid_file != NULL ) &&
440 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
442 fprintf( fp, "%d\n", (int) getpid() );
446 if (( slapd_args_file != NULL ) &&
447 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
449 for ( i = 0; i < g_argc; i++ ) {
450 fprintf( fp, "%s ", g_argv[i] );
459 args.tls_addr = NULL;
464 args.tls_tcps = tls_tcps;
469 LogSlapdStartedEvent( NTservice, slap_debug, configfile, port, udp );
472 rc = slapd_daemon( &args );
475 /* Throw away the event that we used during the startup process. */
477 ldap_pvt_thread_cond_destroy( &started_event );
481 /* remember an error during shutdown */
482 rc |= slap_shutdown(-1);
484 /* remember an error during destroy */
485 rc |= slap_destroy();
489 LogSlapdStoppedEvent( NTservice );
491 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
493 ReportSlapdShutdownComplete();
505 * Catch and discard terminated child processes, to avoid zombies.
509 wait4child( int sig )
511 int save_errno = errno;
516 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
519 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
525 (void) SIGNAL( sig, wait4child );
529 #endif /* SIGCHLD || SIGCLD */
535 * Convert a string to an integer by means of a dispatcher table
536 * if the string is not in the table return the default
540 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
542 int retVal = defaultVal;
545 for (disp = dispatcher; disp->stringVal; disp++) {
547 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
549 retVal = disp->intVal;
559 #endif /* LOG_LOCAL4 */