7 #include <sys/socket.h>
8 #include <netinet/in.h>
13 #include "ldapconfig.h"
15 extern void slapd_daemon();
16 extern int lber_debug;
18 extern char Versionstr[];
22 * read-only global variables or variables only written by the listener
23 * thread (after they are initialized) - no need to protect them with a mutex.
27 int ldap_syslog = LDAP_DEBUG_STATS;
31 int ldap_syslog_level = LOG_DEBUG;
34 char *default_referral;
37 pthread_t listener_tid;
41 * global variables that need mutex protection
44 pthread_mutex_t currenttime_mutex;
46 pthread_mutex_t active_threads_mutex;
47 pthread_mutex_t new_conn_mutex;
51 pthread_mutex_t ops_mutex;
52 long num_entries_sent;
54 pthread_mutex_t num_sent_mutex;
56 * these mutexes must be used when calling the entry2str()
57 * routine since it returns a pointer to static data.
59 pthread_mutex_t entry2str_mutex;
60 pthread_mutex_t replog_mutex;
66 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]\n", name );
81 configfile = SLAPD_DEFAULT_CONFIGFILE;
86 while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
89 case 'd': /* turn on debugging */
90 if ( optarg[0] == '?' ) {
91 printf( "Debug levels:\n" );
92 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
94 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
96 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
98 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
100 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
102 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
104 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
106 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
108 printf( "\tLDAP_DEBUG_STATS\t%d\n",
110 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
112 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
114 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
116 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
120 ldap_debug |= atoi( optarg );
121 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
125 case 'd': /* turn on debugging */
127 "must compile with LDAP_DEBUG for debugging\n" );
131 case 'f': /* read config file */
132 configfile = strdup( optarg );
135 case 'i': /* run from inetd */
139 case 'p': /* port on which to listen */
140 port = atoi( optarg );
143 case 's': /* set syslog level */
144 ldap_syslog = atoi( optarg );
147 case 'u': /* do udp */
157 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
159 if ( (myname = strrchr( argv[0], '/' )) == NULL ) {
160 myname = strdup( argv[0] );
162 myname = strdup( myname + 1 );
166 /* pre-open config file before detach in case it is a relative path */
167 fp = fopen( configfile, "r" );
171 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
173 openlog( myname, OPENLOG_OPTIONS );
177 read_config( configfile, &be, fp );
184 pthread_attr_init( &attr );
185 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
187 #ifndef THREAD_MIT_PTHREADS
188 /* POSIX_THREADS or compatible
189 * This is a draft 10 or standard pthreads implementation
191 if ( pthread_create( &listener_tid, &attr, (void *) slapd_daemon,
192 (void *) port ) != 0 ) {
193 Debug( LDAP_DEBUG_ANY,
194 "listener pthread_create failed\n", 0, 0, 0 );
197 #else /* !THREAD_MIT_PTHREADS */
199 * This is a draft 4 or earlier pthreads implementation
201 if ( pthread_create( &listener_tid, attr, (void *) slapd_daemon,
202 (void *) port ) != 0 ) {
203 Debug( LDAP_DEBUG_ANY,
204 "listener pthread_create failed\n", 0, 0, 0 );
207 #endif /* !THREAD_MIT_PTHREADS */
208 pthread_attr_destroy( &attr );
209 pthread_join( listener_tid, (void *) &status );
215 unsigned long len, tag;
218 struct sockaddr_in from;
224 c.c_sb.sb_options = 0;
225 c.c_sb.sb_naddr = udp ? 1 : 0;
226 c.c_sb.sb_ber.ber_buf = NULL;
227 c.c_sb.sb_ber.ber_ptr = NULL;
228 c.c_sb.sb_ber.ber_end = NULL;
229 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
230 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
231 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
233 c.c_sb.sb_addrs = (void **) saddrlist;
234 c.c_sb.sb_fromaddr = &faddr;
235 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
238 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
239 #ifdef REVERSE_LOOKUP
240 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
241 sizeof(from.sin_addr.s_addr), AF_INET );
246 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
247 hp == NULL ? "unknown" : hp->h_name,
248 inet_ntoa( from.sin_addr ), 0 );
250 c.c_addr = inet_ntoa( from.sin_addr );
251 c.c_domain = strdup( hp == NULL ? "" : hp->h_name );
253 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
258 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
259 == LDAP_TAG_MESSAGE ) {
260 pthread_mutex_lock( ¤ttime_mutex );
261 time( ¤ttime );
262 pthread_mutex_unlock( ¤ttime_mutex );
264 if ( (tag = ber_get_int( &ber, &msgid ))
265 != LDAP_TAG_MSGID ) {
266 /* log and send error */
267 Debug( LDAP_DEBUG_ANY,
268 "ber_get_int returns 0x%x\n", tag, 0, 0 );
272 if ( (tag = ber_peek_tag( &ber, &len ))
274 /* log, close and send error */
275 Debug( LDAP_DEBUG_ANY,
276 "ber_peek_tag returns 0x%x\n", tag, 0, 0 );
278 close( c.c_sb.sb_sd );
283 connection_activity( &c );