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
29 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
31 typedef struct _str2intDispatch {
35 } STRDISP, *STRDISP_P;
38 /* table to compute syslog-options to integer */
39 static STRDISP syslog_types[] = {
40 { "LOCAL0", 6, LOG_LOCAL0 },
41 { "LOCAL1", 6, LOG_LOCAL1 },
42 { "LOCAL2", 6, LOG_LOCAL2 },
43 { "LOCAL3", 6, LOG_LOCAL3 },
44 { "LOCAL4", 6, LOG_LOCAL4 },
45 { "LOCAL5", 6, LOG_LOCAL5 },
46 { "LOCAL6", 6, LOG_LOCAL6 },
47 { "LOCAL7", 6, LOG_LOCAL7 },
51 static int cnvt_str2int( char *, STRDISP_P, int );
53 #endif /* LOG_LOCAL4 */
59 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
60 fprintf( stderr, "\n [-a bind-address] [-i]" );
61 #if LDAP_CONNECTIONLESS
62 fprintf( stderr, " [-c]" );
65 fprintf( stderr, " [-t]" );
68 fprintf( stderr, " [-l sysloguser]" );
70 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
71 fprintf( stderr, " [-u user] [-g group]" );
73 fprintf( stderr, "\n" );
79 main( int argc, char **argv )
84 struct sockaddr_in bind_addr;
88 int syslogUser = DEFAULT_SYSLOG_USER;
90 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
91 char *username = NULL, *groupname = NULL;
95 int serverMode = SLAP_SERVER_MODE;
97 configfile = SLAPD_DEFAULT_CONFIGFILE;
99 (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
100 bind_addr.sin_family = AF_INET;
101 bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
102 bind_addr.sin_port = htons(LDAP_PORT);
107 while ( (i = getopt( argc, argv,
115 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
120 case 'a': /* bind address */
122 if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
124 if(!inet_aton(optarg, &bind_addr.sin_addr))
127 fprintf(stderr, "invalid address (%s) for -a option", optarg);
132 case 'd': /* turn on debugging */
133 if ( optarg[0] == '?' ) {
134 printf( "Debug levels:\n" );
135 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
137 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
138 LDAP_DEBUG_PACKETS );
139 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
141 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
143 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
145 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
147 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
149 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
151 printf( "\tLDAP_DEBUG_STATS\t%d\n",
153 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
155 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
157 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
159 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
163 slap_debug |= atoi( optarg );
167 case 'd': /* turn on debugging */
169 "must compile with LDAP_DEBUG for debugging\n" );
173 case 'f': /* read config file */
174 configfile = ch_strdup( optarg );
177 case 'i': /* run from inetd */
181 case 'p': { /* port on which to listen */
182 short port = (short)atoi( optarg );
184 fprintf(stderr, "-p %s must be numeric\n", optarg);
186 bind_addr.sin_port = htons(port);
190 case 's': /* set syslog level */
191 ldap_syslog = atoi( optarg );
195 case 'l': /* set syslog local user */
196 syslogUser = cnvt_str2int( optarg, syslog_types,
197 DEFAULT_SYSLOG_USER );
201 #ifdef LDAP_CONNECTIONLESS
202 case 'c': /* do connectionless (udp) */
208 case 't': /* timed server */
209 serverMode = SLAP_TIMEDSERVER_MODE;
213 #if defined(HAVE_SETUID) && defined(HAVE_GETUID)
214 case 'u': /* user name */
215 if( username ) free(username);
216 username = ch_strdup( optarg );
219 case 'g': /* group name */
220 if( groupname ) free(groupname);
221 groupname = ch_strdup( optarg );
223 #endif /* SETUID && GETUID */
231 lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
232 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
233 ldif_debug = slap_debug;
235 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
237 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
238 serverName = ch_strdup( argv[0] );
240 serverName = ch_strdup( serverName + 1 );
244 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
246 openlog( serverName, OPENLOG_OPTIONS );
249 tcps = set_socket( inetd ? NULL : &bind_addr );
251 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
252 if ( username != NULL || groupname != NULL )
253 slap_init_user( username, groupname );
256 if ( slap_init( serverMode, serverName ) != 0 ) {
261 if ( read_config( configfile ) != 0 ) {
266 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
267 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
269 (void) SIGNAL( SIGPIPE, SIG_IGN );
272 (void) SIGNAL( SIGHUP, slap_set_shutdown );
274 (void) SIGNAL( SIGINT, slap_set_shutdown );
275 (void) SIGNAL( SIGTERM, slap_set_shutdown );
277 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
283 lutil_detach( ldap_debug, 0 );
285 lutil_detach( 0, 0 );
290 if ( slap_startup(-1) != 0 ) {
298 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
300 if (( slapd_pid_file != NULL ) &&
301 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
303 fprintf( fp, "%d\n", (int) getpid() );
307 if (( slapd_args_file != NULL ) &&
308 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
310 for ( i = 0; i < g_argc; i++ ) {
311 fprintf( fp, "%s ", g_argv[i] );
320 rc = slapd_daemon( inetd, tcps );
323 /* remember an error during shutdown */
324 rc |= slap_shutdown(-1);
326 /* remember an error during destroy */
327 rc |= slap_destroy();
329 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
340 * Catch and discard terminated child processes, to avoid zombies.
344 wait4child( int sig )
346 int save_errno = errno;
351 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
354 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
360 (void) SIGNAL( sig, wait4child );
364 #endif /* SIGCHLD || SIGCLD */
370 * Convert a string to an integer by means of a dispatcher table
371 * if the string is not in the table return the default
375 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
377 int retVal = defaultVal;
380 for (disp = dispatcher; disp->stringVal; disp++) {
382 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
384 retVal = disp->intVal;
394 #endif /* LOG_LOCAL4 */