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[] =
29 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
34 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
36 typedef struct _str2intDispatch {
40 } STRDISP, *STRDISP_P;
43 /* table to compute syslog-options to integer */
44 static STRDISP syslog_types[] = {
45 { "LOCAL0", 6, LOG_LOCAL0 },
46 { "LOCAL1", 6, LOG_LOCAL1 },
47 { "LOCAL2", 6, LOG_LOCAL2 },
48 { "LOCAL3", 6, LOG_LOCAL3 },
49 { "LOCAL4", 6, LOG_LOCAL4 },
50 { "LOCAL5", 6, LOG_LOCAL5 },
51 { "LOCAL6", 6, LOG_LOCAL6 },
52 { "LOCAL7", 6, LOG_LOCAL7 },
56 static int cnvt_str2int( char *, STRDISP_P, int );
58 #endif /* LOG_LOCAL4 */
64 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
65 fprintf( stderr, "\n [-a bind-address] [-i]" );
66 #if LDAP_CONNECTIONLESS
67 fprintf( stderr, " [-c]" );
70 fprintf( stderr, " [-t]" );
73 fprintf( stderr, " [-l sysloguser]" );
75 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
76 fprintf( stderr, " [-u user] [-g group]" );
78 fprintf( stderr, "\n" );
82 struct sockaddr_in bind_addr;
85 main( int argc, char **argv )
91 #ifdef LDAP_CONNECTIONLESS
95 int syslogUser = DEFAULT_SYSLOG_USER;
97 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
98 char *username = NULL, *groupname = NULL;
102 int serverMode = SLAP_SERVER_MODE;
104 configfile = SLAPD_DEFAULT_CONFIGFILE;
106 (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
107 bind_addr.sin_family = AF_INET;
108 bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
109 bind_addr.sin_port = htons(LDAP_PORT);
114 while ( (i = getopt( argc, argv,
122 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
125 #ifdef LDAP_CONNECTIONLESS
130 case 'a': /* bind address */
132 if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
134 if(!inet_aton(optarg, &bind_addr.sin_addr))
137 fprintf(stderr, "invalid address (%s) for -a option", optarg);
142 case 'd': /* turn on debugging */
143 if ( optarg[0] == '?' ) {
144 printf( "Debug levels:\n" );
145 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
147 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
148 LDAP_DEBUG_PACKETS );
149 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
151 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
153 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
155 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
157 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
159 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
161 printf( "\tLDAP_DEBUG_STATS\t%d\n",
163 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
165 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
167 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
169 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
173 slap_debug |= atoi( optarg );
177 case 'd': /* turn on debugging */
179 "must compile with LDAP_DEBUG for debugging\n" );
183 case 'f': /* read config file */
184 configfile = ch_strdup( optarg );
187 case 'i': /* run from inetd */
191 case 'p': { /* port on which to listen */
192 short port = (short)atoi( optarg );
194 fprintf(stderr, "-p %s must be numeric\n", optarg);
196 bind_addr.sin_port = htons(port);
200 case 's': /* set syslog level */
201 ldap_syslog = atoi( optarg );
205 case 'l': /* set syslog local user */
206 syslogUser = cnvt_str2int( optarg, syslog_types,
207 DEFAULT_SYSLOG_USER );
211 #ifdef LDAP_CONNECTIONLESS
212 case 'c': /* do connectionless (udp) */
218 case 't': /* timed server */
219 serverMode = SLAP_TIMEDSERVER_MODE;
223 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
224 case 'u': /* user name */
225 if( username ) free(username);
226 username = ch_strdup( optarg );
229 case 'g': /* group name */
230 if( groupname ) free(groupname);
231 groupname = ch_strdup( optarg );
233 #endif /* SETUID && GETUID */
241 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
242 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
243 ldif_debug = slap_debug;
245 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
247 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
248 serverName = ch_strdup( argv[0] );
250 serverName = ch_strdup( serverName + 1 );
254 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
256 openlog( serverName, OPENLOG_OPTIONS );
259 tcps = set_socket( inetd ? NULL : &bind_addr );
261 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
262 if ( username != NULL || groupname != NULL )
263 slap_init_user( username, groupname );
266 if ( slap_init( serverMode, serverName ) != 0 ) {
271 if ( read_config( configfile ) != 0 ) {
276 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
277 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
279 (void) SIGNAL( SIGPIPE, SIG_IGN );
282 (void) SIGNAL( SIGHUP, slap_set_shutdown );
284 (void) SIGNAL( SIGINT, slap_set_shutdown );
285 (void) SIGNAL( SIGTERM, slap_set_shutdown );
287 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
293 lutil_detach( ldap_debug, 0 );
295 lutil_detach( 0, 0 );
300 if ( slap_startup(-1) != 0 ) {
308 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
310 if (( slapd_pid_file != NULL ) &&
311 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
313 fprintf( fp, "%d\n", (int) getpid() );
317 if (( slapd_args_file != NULL ) &&
318 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
320 for ( i = 0; i < g_argc; i++ ) {
321 fprintf( fp, "%s ", g_argv[i] );
330 rc = slapd_daemon( inetd, tcps );
333 /* remember an error during shutdown */
334 rc |= slap_shutdown(-1);
336 /* remember an error during destroy */
337 rc |= slap_destroy();
339 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
350 * Catch and discard terminated child processes, to avoid zombies.
354 wait4child( int sig )
356 int save_errno = errno;
361 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
364 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
370 (void) SIGNAL( sig, wait4child );
374 #endif /* SIGCHLD || SIGCLD */
380 * Convert a string to an integer by means of a dispatcher table
381 * if the string is not in the table return the default
385 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
387 int retVal = defaultVal;
390 for (disp = dispatcher; disp->stringVal; disp++) {
392 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
394 retVal = disp->intVal;
404 #endif /* LOG_LOCAL4 */