11 #include <ac/signal.h>
14 #include "ldapconfig.h"
16 #include "lutil.h" /* Get lutil_detach() */
19 static RETSIGTYPE wait4child( int sig );
23 * when more than one slapd is running on one machine, each one might have
24 * it's own LOCAL for syslogging and must have its own pid/args files
27 #ifndef HAVE_MKVERSION
28 const char Versionstr[] = "OpenLDAP Standalone LDAP Server (slapd)";
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 )
90 #ifdef LDAP_CONNECTIONLESS
94 int syslogUser = DEFAULT_SYSLOG_USER;
96 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
97 char *username = NULL, *groupname = NULL;
101 int serverMode = SLAP_SERVER_MODE;
103 configfile = SLAPD_DEFAULT_CONFIGFILE;
105 (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
106 bind_addr.sin_family = AF_INET;
107 bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
108 bind_addr.sin_port = htons(LDAP_PORT);
113 while ( (i = getopt( argc, argv,
121 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
124 #ifdef LDAP_CONNECTIONLESS
129 case 'a': /* bind address */
131 if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
133 if(!inet_aton(optarg, &bind_addr.sin_addr))
136 fprintf(stderr, "invalid address (%s) for -a option", optarg);
141 case 'd': /* turn on debugging */
142 if ( optarg[0] == '?' ) {
143 printf( "Debug levels:\n" );
144 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
146 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
147 LDAP_DEBUG_PACKETS );
148 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
150 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
152 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
154 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
156 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
158 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
160 printf( "\tLDAP_DEBUG_STATS\t%d\n",
162 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
164 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
166 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
168 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
172 slap_debug |= atoi( optarg );
176 case 'd': /* turn on debugging */
178 "must compile with LDAP_DEBUG for debugging\n" );
182 case 'f': /* read config file */
183 configfile = ch_strdup( optarg );
186 case 'i': /* run from inetd */
190 case 'p': { /* port on which to listen */
191 short port = (short)atoi( optarg );
193 fprintf(stderr, "-p %s must be numeric\n", optarg);
195 bind_addr.sin_port = htons(port);
199 case 's': /* set syslog level */
200 ldap_syslog = atoi( optarg );
204 case 'l': /* set syslog local user */
205 syslogUser = cnvt_str2int( optarg, syslog_types,
206 DEFAULT_SYSLOG_USER );
210 #ifdef LDAP_CONNECTIONLESS
211 case 'c': /* do connectionless (udp) */
217 case 't': /* timed server */
218 serverMode = SLAP_TIMEDSERVER_MODE;
222 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
223 case 'u': /* user name */
224 if( username ) free(username);
225 username = ch_strdup( optarg );
228 case 'g': /* group name */
229 if( groupname ) free(groupname);
230 groupname = ch_strdup( optarg );
232 #endif /* SETUID && GETUID */
240 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
241 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
242 ldif_debug = slap_debug;
244 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
246 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
247 serverName = ch_strdup( argv[0] );
249 serverName = ch_strdup( serverName + 1 );
253 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
255 openlog( serverName, OPENLOG_OPTIONS );
258 tcps = set_socket( inetd ? NULL : &bind_addr );
260 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
261 if ( username != NULL || groupname != NULL )
262 slap_init_user( username, groupname );
265 if ( slap_init( serverMode, serverName ) != 0 ) {
270 if ( read_config( configfile ) != 0 ) {
275 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
276 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
278 (void) SIGNAL( SIGPIPE, SIG_IGN );
281 (void) SIGNAL( SIGHUP, slap_set_shutdown );
283 (void) SIGNAL( SIGINT, slap_set_shutdown );
284 (void) SIGNAL( SIGTERM, slap_set_shutdown );
286 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
292 lutil_detach( ldap_debug, 0 );
294 lutil_detach( 0, 0 );
299 if ( slap_startup(-1) != 0 ) {
307 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
309 if (( slapd_pid_file != NULL ) &&
310 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
312 fprintf( fp, "%d\n", (int) getpid() );
316 if (( slapd_args_file != NULL ) &&
317 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
319 for ( i = 0; i < g_argc; i++ ) {
320 fprintf( fp, "%s ", g_argv[i] );
329 rc = slapd_daemon( inetd, tcps );
332 /* remember an error during shutdown */
333 rc |= slap_shutdown(-1);
335 /* remember an error during destroy */
336 rc |= slap_destroy();
338 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
349 * Catch and discard terminated child processes, to avoid zombies.
353 wait4child( int sig )
355 int save_errno = errno;
360 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
363 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
369 (void) SIGNAL( sig, wait4child );
373 #endif /* SIGCHLD || SIGCLD */
379 * Convert a string to an integer by means of a dispatcher table
380 * if the string is not in the table return the default
384 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
386 int retVal = defaultVal;
389 for (disp = dispatcher; disp->stringVal; disp++) {
391 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
393 retVal = disp->intVal;
403 #endif /* LOG_LOCAL4 */