11 #include <ac/signal.h>
14 #include "ldapconfig.h"
16 #include "lutil.h" /* Get lutil_detach() */
19 static RETSIGTYPE wait4child( int sig );
23 const char Versionstr[] = "OpenLDAP slapd 1.2 for Windows NT";
27 * when more than one slapd is running on one machine, each one might have
28 * it's own LOCAL for syslogging and must have its own pid/args files
33 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
35 typedef struct _str2intDispatch {
39 } STRDISP, *STRDISP_P;
42 /* table to compute syslog-options to integer */
43 static STRDISP syslog_types[] = {
44 { "LOCAL0", 6, LOG_LOCAL0 },
45 { "LOCAL1", 6, LOG_LOCAL1 },
46 { "LOCAL2", 6, LOG_LOCAL2 },
47 { "LOCAL3", 6, LOG_LOCAL3 },
48 { "LOCAL4", 6, LOG_LOCAL4 },
49 { "LOCAL5", 6, LOG_LOCAL5 },
50 { "LOCAL6", 6, LOG_LOCAL6 },
51 { "LOCAL7", 6, LOG_LOCAL7 },
55 static int cnvt_str2int( char *, STRDISP_P, int );
57 #endif /* LOG_LOCAL4 */
63 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
64 fprintf( stderr, "\n [-a bind-address] [-i]" );
65 #if LDAP_CONNECTIONLESS
66 fprintf( stderr, " [-c]" );
69 fprintf( stderr, " [-t]" );
72 fprintf( stderr, " [-l sysloguser]" );
74 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
75 fprintf( stderr, " [-u user] [-g group]" );
77 fprintf( stderr, "\n" );
81 struct sockaddr_in bind_addr;
84 main( int argc, char **argv )
92 int syslogUser = DEFAULT_SYSLOG_USER;
94 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
95 char *username = NULL, *groupname = NULL;
99 int serverMode = SLAP_SERVER_MODE;
101 configfile = SLAPD_DEFAULT_CONFIGFILE;
103 (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
104 bind_addr.sin_family = AF_INET;
105 bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
106 bind_addr.sin_port = htons(LDAP_PORT);
111 while ( (i = getopt( argc, argv,
119 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
124 case 'a': /* bind address */
126 if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
128 if(!inet_aton(optarg, &bind_addr.sin_addr))
131 fprintf(stderr, "invalid address (%s) for -a option", optarg);
136 case 'd': /* turn on debugging */
137 if ( optarg[0] == '?' ) {
138 printf( "Debug levels:\n" );
139 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
141 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
142 LDAP_DEBUG_PACKETS );
143 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
145 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
147 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
149 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
151 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
153 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
155 printf( "\tLDAP_DEBUG_STATS\t%d\n",
157 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
159 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
161 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
163 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
167 slap_debug |= atoi( optarg );
171 case 'd': /* turn on debugging */
173 "must compile with LDAP_DEBUG for debugging\n" );
177 case 'f': /* read config file */
178 configfile = ch_strdup( optarg );
181 case 'i': /* run from inetd */
185 case 'p': { /* port on which to listen */
186 short port = (short)atoi( optarg );
188 fprintf(stderr, "-p %s must be numeric\n", optarg);
190 bind_addr.sin_port = htons(port);
194 case 's': /* set syslog level */
195 ldap_syslog = atoi( optarg );
199 case 'l': /* set syslog local user */
200 syslogUser = cnvt_str2int( optarg, syslog_types,
201 DEFAULT_SYSLOG_USER );
205 #ifdef LDAP_CONNECTIONLESS
206 case 'c': /* do connectionless (udp) */
212 case 't': /* timed server */
213 serverMode = SLAP_TIMEDSERVER_MODE;
217 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
218 case 'u': /* user name */
219 if( username ) free(username);
220 username = ch_strdup( optarg );
223 case 'g': /* group name */
224 if( groupname ) free(groupname);
225 groupname = ch_strdup( optarg );
227 #endif /* SETUID && GETUID */
235 lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
236 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
237 ldif_debug = slap_debug;
239 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
241 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
242 serverName = ch_strdup( argv[0] );
244 serverName = ch_strdup( serverName + 1 );
248 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
250 openlog( serverName, OPENLOG_OPTIONS );
253 tcps = set_socket( inetd ? NULL : &bind_addr );
255 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
256 if ( username != NULL || groupname != NULL )
257 slap_init_user( username, groupname );
260 if ( slap_init( serverMode, serverName ) != 0 ) {
265 if ( read_config( configfile ) != 0 ) {
270 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
271 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
273 (void) SIGNAL( SIGPIPE, SIG_IGN );
276 (void) SIGNAL( SIGHUP, slap_set_shutdown );
278 (void) SIGNAL( SIGINT, slap_set_shutdown );
279 (void) SIGNAL( SIGTERM, slap_set_shutdown );
281 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
287 lutil_detach( ldap_debug, 0 );
289 lutil_detach( 0, 0 );
294 if ( slap_startup(-1) != 0 ) {
302 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
304 if (( slapd_pid_file != NULL ) &&
305 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
307 fprintf( fp, "%d\n", (int) getpid() );
311 if (( slapd_args_file != NULL ) &&
312 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
314 for ( i = 0; i < g_argc; i++ ) {
315 fprintf( fp, "%s ", g_argv[i] );
324 rc = slapd_daemon( inetd, tcps );
327 /* remember an error during shutdown */
328 rc |= slap_shutdown(-1);
330 /* remember an error during destroy */
331 rc |= slap_destroy();
333 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
344 * Catch and discard terminated child processes, to avoid zombies.
348 wait4child( int sig )
350 int save_errno = errno;
355 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
358 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
364 (void) SIGNAL( sig, wait4child );
368 #endif /* SIGCHLD || SIGCLD */
374 * Convert a string to an integer by means of a dispatcher table
375 * if the string is not in the table return the default
379 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
381 int retVal = defaultVal;
384 for (disp = dispatcher; disp->stringVal; disp++) {
386 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
388 retVal = disp->intVal;
398 #endif /* LOG_LOCAL4 */