11 #include <ac/signal.h>
14 #include "ldap_defaults.h"
16 #include "lutil.h" /* Get lutil_detach() */
18 int set_socket( struct sockaddr_in *addr );
21 static RETSIGTYPE wait4child( int sig );
25 #define MAIN_RETURN(x) return
26 struct sockaddr_in bind_addr;
29 extern SERVICE_STATUS SLAPDServiceStatus;
30 extern SERVICE_STATUS_HANDLE hSLAPDServiceStatus;
31 extern ldap_pvt_thread_cond_t started_event, stopped_event;
32 extern int is_NT_Service;
34 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, short port, int udp );
35 void LogSlapdStoppedEvent( char *svc );
37 void CommenceStartupProcessing( LPCTSTR serviceName,
39 void ReportSlapdShutdownComplete( void );
40 void *getRegParam( char *svc, char *value );
42 #define SERVICE_EXIT( e, n ) \
43 if ( is_NT_Service ) \
45 SLAPDServiceStatus.dwWin32ExitCode = e; \
46 SLAPDServiceStatus.dwServiceSpecificExitCode = n; \
49 #define SERVICE_EXIT( e, n )
50 #define MAIN_RETURN(x) return(x)
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]" );
99 fprintf( stderr, " [-v NTserviceName]" );
101 #if LDAP_CONNECTIONLESS
102 fprintf( stderr, " [-c]" );
105 fprintf( stderr, " [-t]" );
108 fprintf( stderr, " [-l sysloguser]" );
110 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
111 fprintf( stderr, " [-u user] [-g group]" );
113 fprintf( stderr, "\n" );
117 struct sockaddr_in bind_addr;
121 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
124 int main( int argc, char **argv )
131 struct slapd_args args;
133 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
134 char *username = NULL;
135 char *groupname = NULL;
138 int syslogUser = DEFAULT_SYSLOG_USER;
141 char *NTservice = SERVICE_NAME;
142 char *configfile = ".\\slapd.conf";
144 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
147 int serverMode = SLAP_SERVER_MODE;
149 (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
150 bind_addr.sin_family = AF_INET;
151 bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
152 bind_addr.sin_port = htons(port);
163 if ( is_NT_Service ) CommenceStartupProcessing( NTservice, slap_set_shutdown );
164 newPort = (int*)getRegParam( NULL, "Port" );
165 if ( newPort != NULL )
168 bind_addr.sin_port = htons(port);
169 Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
171 newDebugLevel = (int*)getRegParam( NULL, "DebugLevel" );
172 if ( newDebugLevel != NULL )
174 slap_debug = *newDebugLevel;
175 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
177 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
178 if ( newConfigFile != NULL )
180 configfile = newConfigFile;
181 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
187 while ( (i = getopt( argc, argv,
195 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
198 #ifdef LDAP_CONNECTIONLESS
206 case 'a': /* bind address */
208 if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
210 if(!inet_aton(optarg, &bind_addr.sin_addr))
213 fprintf(stderr, "invalid address (%s) for -a option", optarg);
218 case 'd': /* turn on debugging */
219 if ( optarg[0] == '?' ) {
220 printf( "Debug levels:\n" );
221 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
223 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
224 LDAP_DEBUG_PACKETS );
225 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
227 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
229 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
231 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
233 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
235 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
237 printf( "\tLDAP_DEBUG_STATS\t%d\n",
239 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
241 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
243 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
245 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
249 slap_debug |= atoi( optarg );
253 case 'd': /* turn on debugging */
255 "must compile with LDAP_DEBUG for debugging\n" );
259 case 'f': /* read config file */
260 configfile = ch_strdup( optarg );
263 case 'i': /* run from inetd */
267 case 'p': { /* port on which to listen */
268 port = (short)atoi( optarg );
270 fprintf(stderr, "-p %s must be numeric\n", optarg);
272 bind_addr.sin_port = htons(port);
276 case 's': /* set syslog level */
277 ldap_syslog = atoi( optarg );
281 case 'l': /* set syslog local user */
282 syslogUser = cnvt_str2int( optarg, syslog_types,
283 DEFAULT_SYSLOG_USER );
287 #ifdef LDAP_CONNECTIONLESS
288 case 'c': /* do connectionless (udp) */
294 case 't': /* timed server */
295 serverMode = SLAP_TIMEDSERVER_MODE;
299 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
300 case 'u': /* user name */
301 if( username ) free(username);
302 username = ch_strdup( optarg );
305 case 'g': /* group name */
306 if( groupname ) free(groupname);
307 groupname = ch_strdup( optarg );
309 #endif /* SETUID && GETUID */
311 case 'v': /* NT service name */
312 NTservice = ch_strdup( optarg );
318 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
323 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
324 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
325 ldif_debug = slap_debug;
327 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
329 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
330 serverName = ch_strdup( argv[0] );
332 serverName = ch_strdup( serverName + 1 );
336 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
338 openlog( serverName, OPENLOG_OPTIONS );
341 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
342 if ( username != NULL || groupname != NULL )
343 slap_init_user( username, groupname );
346 if ( slap_init( serverMode, serverName ) != 0 ) {
348 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
352 if ( read_config( configfile ) != 0 ) {
354 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
359 tcps = set_socket( inetd ? NULL : &bind_addr );
363 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
364 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
366 (void) SIGNAL( SIGPIPE, SIG_IGN );
369 (void) SIGNAL( SIGHUP, slap_set_shutdown );
371 (void) SIGNAL( SIGINT, slap_set_shutdown );
372 (void) SIGNAL( SIGTERM, slap_set_shutdown );
374 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
377 /* SIGBREAK is generated when Ctrl-Break is pressed. */
378 (void) SIGNAL( SIGBREAK, slap_set_shutdown );
384 lutil_detach( ldap_debug, 0 );
386 lutil_detach( 0, 0 );
389 #endif /* HAVE_WINSOC */
391 if ( slap_startup(-1) != 0 ) {
393 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
400 args.addr = &bind_addr;
402 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
404 if (( slapd_pid_file != NULL ) &&
405 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
407 fprintf( fp, "%d\n", (int) getpid() );
411 if (( slapd_args_file != NULL ) &&
412 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
414 for ( i = 0; i < g_argc; i++ ) {
415 fprintf( fp, "%s ", g_argv[i] );
428 LogSlapdStartedEvent( NTservice, slap_debug, configfile, port, udp );
431 rc = slapd_daemon( &args );
434 /* Throw away the event that we used during the startup process. */
436 ldap_pvt_thread_cond_destroy( &started_event );
441 /* remember an error during shutdown */
442 rc |= slap_shutdown(-1);
444 /* remember an error during destroy */
445 rc |= slap_destroy();
449 LogSlapdStoppedEvent( NTservice );
451 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
453 ReportSlapdShutdownComplete();
465 * Catch and discard terminated child processes, to avoid zombies.
469 wait4child( int sig )
471 int save_errno = errno;
476 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
479 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
485 (void) SIGNAL( sig, wait4child );
489 #endif /* SIGCHLD || SIGCLD */
495 * Convert a string to an integer by means of a dispatcher table
496 * if the string is not in the table return the default
500 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
502 int retVal = defaultVal;
505 for (disp = dispatcher; disp->stringVal; disp++) {
507 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
509 retVal = disp->intVal;
519 #endif /* LOG_LOCAL4 */