10 #include <ac/unistd.h>
12 #include "ldapconfig.h"
14 #include "lutil.h" /* Get lutil_detach() */
17 static void wait4child( int sig );
21 * when more than one slapd is running on one machine, each one might have
22 * it's own LOCAL for syslogging and must have its own pid/args files
27 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
29 typedef struct _str2intDispatch {
35 } STRDISP, *STRDISP_P;
38 /* table to compute syslog-options to integer */
39 static STRDISP syslog_types[] = {
41 { "LOCAL0", 6, LOG_LOCAL0 },
42 { "LOCAL1", 6, LOG_LOCAL1 },
43 { "LOCAL2", 6, LOG_LOCAL2 },
44 { "LOCAL3", 6, LOG_LOCAL3 },
45 { "LOCAL4", 6, LOG_LOCAL4 },
46 { "LOCAL5", 6, LOG_LOCAL5 },
47 { "LOCAL6", 6, LOG_LOCAL6 },
48 { "LOCAL7", 6, LOG_LOCAL7 },
53 static int cnvt_str2int();
55 #endif /* LOG_LOCAL4 */
61 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
63 fprintf( stderr, " [-l sysloguser]" );
65 fprintf( stderr, "\n" );
69 main( int argc, char **argv )
80 char *optstring = "d:f:ip:s:ul:";
81 int syslogUser = DEFAULT_SYSLOG_USER;
83 char *optstring = "d:f:ip:s:u";
86 configfile = SLAPD_DEFAULT_CONFIGFILE;
91 while ( (i = getopt( argc, argv, optstring )) != EOF ) {
94 case 'd': /* turn on debugging */
95 if ( optarg[0] == '?' ) {
96 printf( "Debug levels:\n" );
97 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
99 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
100 LDAP_DEBUG_PACKETS );
101 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
103 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
105 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
107 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
109 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
111 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
113 printf( "\tLDAP_DEBUG_STATS\t%d\n",
115 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
117 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
119 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
121 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
125 ldap_debug |= atoi( optarg );
126 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
130 case 'd': /* turn on debugging */
132 "must compile with LDAP_DEBUG for debugging\n" );
136 case 'f': /* read config file */
137 configfile = ch_strdup( optarg );
140 case 'i': /* run from inetd */
144 case 'p': /* port on which to listen */
145 port = atoi( optarg );
148 case 's': /* set syslog level */
149 ldap_syslog = atoi( optarg );
153 case 'l': /* set syslog local user */
154 syslogUser = cnvt_str2int( optarg, syslog_types,
155 DEFAULT_SYSLOG_USER );
159 case 'u': /* do udp */
169 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
171 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
172 serverName = ch_strdup( argv[0] );
174 serverName = ch_strdup( serverName + 1 );
178 /* pre-open config file before detach in case it is a relative path */
179 fp = fopen( configfile, "r" );
181 lutil_detach( ldap_debug, 0 );
183 lutil_detach( 0, 0 );
188 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
190 openlog( serverName, OPENLOG_OPTIONS );
194 read_config( configfile, &be, fp );
199 (void) SIGNAL( SIGPIPE, SIG_IGN );
200 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
201 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
202 (void) SIGNAL( SIGTERM, slap_set_shutdown );
203 (void) SIGNAL( SIGINT, slap_set_shutdown );
204 (void) SIGNAL( SIGHUP, slap_set_shutdown );
206 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
211 if ( status = ldap_pvt_thread_create( &listener_tid, 0,
212 slapd_daemon, (void *) port ) != 0 )
214 Debug( LDAP_DEBUG_ANY,
215 "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
219 ldap_pvt_thread_join( listener_tid, (void *) NULL );
227 unsigned long len, tag;
230 struct sockaddr_in from;
237 c.c_sb.sb_options = 0;
238 c.c_sb.sb_naddr = udp ? 1 : 0;
239 c.c_sb.sb_ber.ber_buf = NULL;
240 c.c_sb.sb_ber.ber_ptr = NULL;
241 c.c_sb.sb_ber.ber_end = NULL;
242 ldap_pvt_thread_mutex_init( &c.c_dnmutex );
243 ldap_pvt_thread_mutex_init( &c.c_opsmutex );
244 ldap_pvt_thread_mutex_init( &c.c_pdumutex );
246 c.c_sb.sb_addrs = (void **) saddrlist;
247 c.c_sb.sb_fromaddr = &faddr;
248 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
251 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
252 #ifdef SLAPD_RLOOKUPS
253 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
254 sizeof(from.sin_addr.s_addr), AF_INET );
259 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
260 hp == NULL ? "unknown" : hp->h_name,
261 inet_ntoa( from.sin_addr ), 0 );
263 c.c_addr = inet_ntoa( from.sin_addr );
264 c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
266 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
271 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
272 == LDAP_TAG_MESSAGE ) {
273 ldap_pvt_thread_mutex_lock( ¤ttime_mutex );
274 time( ¤ttime );
275 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
277 if ( (tag = ber_get_int( &ber, &msgid ))
278 != LDAP_TAG_MSGID ) {
279 /* log and send error */
280 Debug( LDAP_DEBUG_ANY,
281 "ber_get_int returns 0x%lx\n", tag, 0, 0 );
286 if ( (tag = ber_peek_tag( &ber, &len ))
288 /* log, close and send error */
289 Debug( LDAP_DEBUG_ANY,
290 "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
292 close( c.c_sb.sb_sd );
297 connection_activity( &c );
309 * Catch and discard terminated child processes, to avoid zombies.
313 wait4child( int sig )
315 int save_errno = errno;
320 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
323 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
329 (void) SIGNAL( sig, wait4child );
333 #endif /* SIGCHLD || SIGCLD */
339 * Convert a string to an integer by means of a dispatcher table
340 * if the string is not in the table return the default
344 cnvt_str2int (stringVal, dispatcher, defaultVal)
346 STRDISP_P dispatcher;
349 int retVal = defaultVal;
352 for (disp = dispatcher; disp->stringVal; disp++) {
354 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
356 retVal = disp->intVal;
366 #endif /* LOG_LOCAL4 */