10 #include "ldapconfig.h"
12 #include "lutil.h" /* Get lutil_detach() */
17 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]\n", name );
21 main( int argc, char **argv )
32 configfile = SLAPD_DEFAULT_CONFIGFILE;
37 while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
40 case 'd': /* turn on debugging */
41 if ( optarg[0] == '?' ) {
42 printf( "Debug levels:\n" );
43 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
45 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
47 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
49 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
51 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
53 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
55 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
57 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
59 printf( "\tLDAP_DEBUG_STATS\t%d\n",
61 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
63 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
65 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
67 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
71 ldap_debug |= atoi( optarg );
72 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
76 case 'd': /* turn on debugging */
78 "must compile with LDAP_DEBUG for debugging\n" );
82 case 'f': /* read config file */
83 configfile = ch_strdup( optarg );
86 case 'i': /* run from inetd */
90 case 'p': /* port on which to listen */
91 port = atoi( optarg );
94 case 's': /* set syslog level */
95 ldap_syslog = atoi( optarg );
98 case 'u': /* do udp */
108 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
110 if ( (myname = strrchr( argv[0], '/' )) == NULL ) {
111 myname = ch_strdup( argv[0] );
113 myname = ch_strdup( myname + 1 );
117 /* pre-open config file before detach in case it is a relative path */
118 fp = fopen( configfile, "r" );
120 lutil_detach( ldap_debug, 0 );
122 lutil_detach( 0, 0 );
126 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
128 openlog( myname, OPENLOG_OPTIONS );
132 read_config( configfile, &be, fp );
139 if ( pthread_create( &listener_tid, NULL, slapd_daemon,
140 (void *) port ) != 0 ) {
141 Debug( LDAP_DEBUG_ANY,
142 "listener pthread_create failed\n", 0, 0, 0 );
146 #ifdef HAVE_PHREADS_FINAL
147 pthread_join( listener_tid, (void *) NULL );
149 pthread_join( listener_tid, (void *) &status );
158 unsigned long len, tag;
161 struct sockaddr_in from;
167 c.c_sb.sb_options = 0;
168 c.c_sb.sb_naddr = udp ? 1 : 0;
169 c.c_sb.sb_ber.ber_buf = NULL;
170 c.c_sb.sb_ber.ber_ptr = NULL;
171 c.c_sb.sb_ber.ber_end = NULL;
172 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
173 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
174 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
176 c.c_sb.sb_addrs = (void **) saddrlist;
177 c.c_sb.sb_fromaddr = &faddr;
178 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
181 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
182 #ifdef SLAPD_RLOOKUPS
183 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
184 sizeof(from.sin_addr.s_addr), AF_INET );
189 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
190 hp == NULL ? "unknown" : hp->h_name,
191 inet_ntoa( from.sin_addr ), 0 );
193 c.c_addr = inet_ntoa( from.sin_addr );
194 c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
196 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
201 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
202 == LDAP_TAG_MESSAGE ) {
203 pthread_mutex_lock( ¤ttime_mutex );
204 time( ¤ttime );
205 pthread_mutex_unlock( ¤ttime_mutex );
207 if ( (tag = ber_get_int( &ber, &msgid ))
208 != LDAP_TAG_MSGID ) {
209 /* log and send error */
210 Debug( LDAP_DEBUG_ANY,
211 "ber_get_int returns 0x%lx\n", tag, 0, 0 );
216 if ( (tag = ber_peek_tag( &ber, &len ))
218 /* log, close and send error */
219 Debug( LDAP_DEBUG_ANY,
220 "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
222 close( c.c_sb.sb_sd );
227 connection_activity( &c );