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;
151 int use_tls_port = 0;
153 (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
154 bind_addr.sin_family = AF_INET;
155 bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
156 bind_addr.sin_port = htons(port);
158 tls_bind_addr.sin_family = AF_INET;
159 tls_bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
160 tls_bind_addr.sin_port = htons(tls_port);
172 if ( is_NT_Service ) CommenceStartupProcessing( NTservice, slap_set_shutdown );
173 newPort = (int*)getRegParam( NULL, "Port" );
174 if ( newPort != NULL )
177 bind_addr.sin_port = htons(port);
178 Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
180 newDebugLevel = (int*)getRegParam( NULL, "DebugLevel" );
181 if ( newDebugLevel != NULL )
183 slap_debug = *newDebugLevel;
184 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
186 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
187 if ( newConfigFile != NULL )
189 configfile = newConfigFile;
190 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
195 while ( (i = getopt( argc, argv,
203 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
206 #ifdef LDAP_CONNECTIONLESS
217 case 'a': /* bind address */
219 if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
221 if(!inet_aton(optarg, &bind_addr.sin_addr))
224 fprintf(stderr, "invalid address (%s) for -a option", optarg);
228 tls_bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg);
230 inet_aton(optarg, &tls_bind_addr.sin_addr);
236 case 'd': /* turn on debugging */
237 if ( optarg[0] == '?' ) {
238 printf( "Debug levels:\n" );
239 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
241 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
242 LDAP_DEBUG_PACKETS );
243 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
245 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
247 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
249 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
251 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
253 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
255 printf( "\tLDAP_DEBUG_STATS\t%d\n",
257 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
259 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
261 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
263 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
267 slap_debug |= atoi( optarg );
271 case 'd': /* turn on debugging */
273 "must compile with LDAP_DEBUG for debugging\n" );
277 case 'f': /* read config file */
278 configfile = ch_strdup( optarg );
281 case 'i': /* run from inetd */
285 case 'p': { /* port on which to listen */
286 port = (short)atoi( optarg );
288 fprintf(stderr, "-p %s must be numeric\n", optarg);
290 bind_addr.sin_port = htons(port);
295 case 'P': { /* port on which to listen for TLS */
296 tls_port = (short)atoi( optarg );
298 fprintf(stderr, "-P %s must be numeric\n", optarg);
300 tls_bind_addr.sin_port = htons(tls_port);
305 case 's': /* set syslog level */
306 ldap_syslog = atoi( optarg );
310 case 'l': /* set syslog local user */
311 syslogUser = cnvt_str2int( optarg, syslog_types,
312 DEFAULT_SYSLOG_USER );
316 #ifdef LDAP_CONNECTIONLESS
317 case 'c': /* do connectionless (udp) */
323 case 't': /* timed server */
324 serverMode = SLAP_TIMEDSERVER_MODE;
328 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
329 case 'u': /* user name */
330 if( username ) free(username);
331 username = ch_strdup( optarg );
334 case 'g': /* group name */
335 if( groupname ) free(groupname);
336 groupname = ch_strdup( optarg );
338 #endif /* SETUID && GETUID */
340 case 'n': /* NT service name */
341 NTservice = ch_strdup( optarg );
345 case 'T': /* Bind on TLS port */
352 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
357 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
358 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
359 ldif_debug = slap_debug;
361 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
363 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
364 serverName = ch_strdup( argv[0] );
366 serverName = ch_strdup( serverName + 1 );
370 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
372 openlog( serverName, OPENLOG_OPTIONS );
375 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
376 if ( username != NULL || groupname != NULL )
377 slap_init_user( username, groupname );
380 if ( slap_init( serverMode, serverName ) != 0 ) {
382 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
386 if ( read_config( configfile ) != 0 ) {
388 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
394 ldap_pvt_tls_init_def_ctx();
397 tcps = set_socket( inetd ? NULL : &bind_addr );
401 if ( use_tls_port ) {
402 tls_tcps = set_socket( inetd ? NULL : &tls_bind_addr );
403 if ( tls_tcps == -1 )
410 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
411 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
413 (void) SIGNAL( SIGPIPE, SIG_IGN );
416 (void) SIGNAL( SIGHUP, slap_set_shutdown );
418 (void) SIGNAL( SIGINT, slap_set_shutdown );
419 (void) SIGNAL( SIGTERM, slap_set_shutdown );
421 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
424 /* SIGBREAK is generated when Ctrl-Break is pressed. */
425 (void) SIGNAL( SIGBREAK, slap_set_shutdown );
431 lutil_detach( ldap_debug, 0 );
433 lutil_detach( 0, 0 );
436 #endif /* HAVE_WINSOC */
438 if ( slap_startup(-1) != 0 ) {
440 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
447 args.addr = &bind_addr;
449 args.tls_addr = &tls_bind_addr;
452 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
454 if (( slapd_pid_file != NULL ) &&
455 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
457 fprintf( fp, "%d\n", (int) getpid() );
461 if (( slapd_args_file != NULL ) &&
462 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
464 for ( i = 0; i < g_argc; i++ ) {
465 fprintf( fp, "%s ", g_argv[i] );
474 args.tls_addr = NULL;
479 args.tls_tcps = tls_tcps;
484 LogSlapdStartedEvent( NTservice, slap_debug, configfile, port, udp );
487 rc = slapd_daemon( &args );
490 /* Throw away the event that we used during the startup process. */
492 ldap_pvt_thread_cond_destroy( &started_event );
496 /* remember an error during shutdown */
497 rc |= slap_shutdown(-1);
499 /* remember an error during destroy */
500 rc |= slap_destroy();
504 LogSlapdStoppedEvent( NTservice );
506 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
508 ReportSlapdShutdownComplete();
520 * Catch and discard terminated child processes, to avoid zombies.
524 wait4child( int sig )
526 int save_errno = errno;
531 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
534 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
540 (void) SIGNAL( sig, wait4child );
544 #endif /* SIGCHLD || SIGCLD */
550 * Convert a string to an integer by means of a dispatcher table
551 * if the string is not in the table return the default
555 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
557 int retVal = defaultVal;
560 for (disp = dispatcher; disp->stringVal; disp++) {
562 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
564 retVal = disp->intVal;
574 #endif /* LOG_LOCAL4 */