5 #include <sys/socket.h>
6 #include <netinet/in.h>
11 #include "ldapconfig.h"
13 extern void slapd_daemon();
14 extern int lber_debug;
16 extern char Versionstr[];
20 * read-only global variables or variables only written by the listener
21 * thread (after they are initialized) - no need to protect them with a mutex.
25 int ldap_syslog = LDAP_DEBUG_STATS;
29 int ldap_syslog_level = LOG_DEBUG;
32 char *default_referral;
35 pthread_t listener_tid;
39 * global variables that need mutex protection
42 pthread_mutex_t currenttime_mutex;
44 pthread_mutex_t active_threads_mutex;
45 pthread_mutex_t new_conn_mutex;
49 pthread_mutex_t ops_mutex;
50 long num_entries_sent;
52 pthread_mutex_t num_sent_mutex;
54 * these mutexes must be used when calling the entry2str()
55 * routine since it returns a pointer to static data.
57 pthread_mutex_t entry2str_mutex;
58 pthread_mutex_t replog_mutex;
60 pthread_mutex_t regex_mutex;
67 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]\n", name );
82 configfile = SLAPD_DEFAULT_CONFIGFILE;
87 while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
90 case 'd': /* turn on debugging */
91 if ( optarg[0] == '?' ) {
92 printf( "Debug levels:\n" );
93 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
95 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
97 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
99 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
101 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
103 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
105 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
107 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
109 printf( "\tLDAP_DEBUG_STATS\t%d\n",
111 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
113 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
115 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
117 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
121 ldap_debug |= atoi( optarg );
122 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
126 case 'd': /* turn on debugging */
128 "must compile with LDAP_DEBUG for debugging\n" );
132 case 'f': /* read config file */
133 configfile = strdup( optarg );
136 case 'i': /* run from inetd */
140 case 'p': /* port on which to listen */
141 port = atoi( optarg );
144 case 's': /* set syslog level */
145 ldap_syslog = atoi( optarg );
148 case 'u': /* do udp */
158 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
160 if ( (myname = strrchr( argv[0], '/' )) == NULL ) {
161 myname = strdup( argv[0] );
163 myname = strdup( myname + 1 );
167 /* pre-open config file before detach in case it is a relative path */
168 fp = fopen( configfile, "r" );
172 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
174 openlog( myname, OPENLOG_OPTIONS );
178 read_config( configfile, &be, fp );
185 pthread_attr_init( &attr );
186 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
188 #ifndef THREAD_MIT_PTHREADS
189 /* POSIX_THREADS or compatible
190 * This is a draft 10 or standard pthreads implementation
192 if ( pthread_create( &listener_tid, &attr, (void *) slapd_daemon,
193 (void *) port ) != 0 ) {
194 Debug( LDAP_DEBUG_ANY,
195 "listener pthread_create failed\n", 0, 0, 0 );
198 #else /* !THREAD_MIT_PTHREADS */
200 * This is a draft 4 or earlier pthreads implementation
202 if ( pthread_create( &listener_tid, attr, (void *) slapd_daemon,
203 (void *) port ) != 0 ) {
204 Debug( LDAP_DEBUG_ANY,
205 "listener pthread_create failed\n", 0, 0, 0 );
208 #endif /* !THREAD_MIT_PTHREADS */
209 pthread_attr_destroy( &attr );
210 pthread_join( listener_tid, (void *) &status );
216 unsigned long len, tag;
219 struct sockaddr_in from;
225 c.c_sb.sb_options = 0;
226 c.c_sb.sb_naddr = udp ? 1 : 0;
227 c.c_sb.sb_ber.ber_buf = NULL;
228 c.c_sb.sb_ber.ber_ptr = NULL;
229 c.c_sb.sb_ber.ber_end = NULL;
230 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
231 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
232 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
234 c.c_sb.sb_addrs = (void **) saddrlist;
235 c.c_sb.sb_fromaddr = &faddr;
236 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
239 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
240 #ifdef REVERSE_LOOKUP
241 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
242 sizeof(from.sin_addr.s_addr), AF_INET );
247 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
248 hp == NULL ? "unknown" : hp->h_name,
249 inet_ntoa( from.sin_addr ), 0 );
251 c.c_addr = inet_ntoa( from.sin_addr );
252 c.c_domain = strdup( hp == NULL ? "" : hp->h_name );
254 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
259 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
260 == LDAP_TAG_MESSAGE ) {
261 pthread_mutex_lock( ¤ttime_mutex );
262 time( ¤ttime );
263 pthread_mutex_unlock( ¤ttime_mutex );
265 if ( (tag = ber_get_int( &ber, &msgid ))
266 != LDAP_TAG_MSGID ) {
267 /* log and send error */
268 Debug( LDAP_DEBUG_ANY,
269 "ber_get_int returns 0x%x\n", tag, 0, 0 );
273 if ( (tag = ber_peek_tag( &ber, &len ))
275 /* log, close and send error */
276 Debug( LDAP_DEBUG_ANY,
277 "ber_peek_tag returns 0x%x\n", tag, 0, 0 );
279 close( c.c_sb.sb_sd );
284 connection_activity( &c );