10 #include "ldapconfig.h"
12 #include "lutil.h" /* Get lutil_detach() */
16 * read-only global variables or variables only written by the listener
17 * thread (after they are initialized) - no need to protect them with a mutex.
22 int ldap_syslog = LDAP_DEBUG_STATS;
27 int ldap_syslog_level = LOG_DEBUG;
29 char *default_referral;
32 pthread_t listener_tid;
36 * global variables that need mutex protection
39 pthread_mutex_t currenttime_mutex;
40 pthread_mutex_t strtok_mutex;
42 pthread_mutex_t active_threads_mutex;
43 pthread_mutex_t new_conn_mutex;
45 pthread_mutex_t crypt_mutex;
50 pthread_mutex_t ops_mutex;
51 long num_entries_sent;
53 pthread_mutex_t num_sent_mutex;
55 * these mutexes must be used when calling the entry2str()
56 * routine since it returns a pointer to static data.
58 pthread_mutex_t entry2str_mutex;
59 pthread_mutex_t replog_mutex;
62 * when more than one slapd is running on one machine, each one might have
63 * it's own LOCAL for syslogging and must have its own pid/args files
68 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
70 typedef struct _str2intDispatch {
76 } STRDISP, *STRDISP_P;
79 /* table to compute syslog-options to integer */
80 static STRDISP syslog_types[] = {
82 { "LOCAL0", 6, LOG_LOCAL0 },
83 { "LOCAL1", 6, LOG_LOCAL1 },
84 { "LOCAL2", 6, LOG_LOCAL2 },
85 { "LOCAL3", 6, LOG_LOCAL3 },
86 { "LOCAL4", 6, LOG_LOCAL4 },
87 { "LOCAL5", 6, LOG_LOCAL5 },
88 { "LOCAL6", 6, LOG_LOCAL6 },
89 { "LOCAL7", 6, LOG_LOCAL7 },
94 static int cnvt_str2int();
96 #endif /* LOG_LOCAL4 */
99 * the server's name must be accessible from the daemon module,
100 * to construct the pid/args file names
102 char *serverName = NULL;
108 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
110 fprintf( stderr, " [-l sysloguser]" );
112 fprintf( stderr, "\n" );
116 main( int argc, char **argv )
124 int syslogUser = DEFAULT_SYSLOG_USER;
127 configfile = SLAPD_DEFAULT_CONFIGFILE;
132 while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
135 case 'd': /* turn on debugging */
136 if ( optarg[0] == '?' ) {
137 printf( "Debug levels:\n" );
138 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
140 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
141 LDAP_DEBUG_PACKETS );
142 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
144 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
146 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
148 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
150 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
152 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
154 printf( "\tLDAP_DEBUG_STATS\t%d\n",
156 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
158 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
160 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
162 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
166 slap_debug |= atoi( optarg );
170 case 'd': /* turn on debugging */
172 "must compile with LDAP_DEBUG for debugging\n" );
176 case 'f': /* read config file */
177 configfile = ch_strdup( optarg );
180 case 'i': /* run from inetd */
184 case 'p': /* port on which to listen */
185 port = atoi( optarg );
188 case 's': /* set syslog level */
189 ldap_syslog = atoi( optarg );
194 case 'l': /* set syslog local user */
195 syslogUser = cnvt_str2int( optarg, syslog_types,
196 DEFAULT_SYSLOG_USER );
201 case 'u': /* do udp */
211 lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
212 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
213 ldif_debug = slap_debug;
215 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
217 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
218 serverName = ch_strdup( argv[0] );
220 serverName = ch_strdup( serverName + 1 );
224 /* pre-open config file before detach in case it is a relative path */
225 fp = fopen( configfile, "r" );
227 lutil_detach( ldap_debug, 0 );
229 lutil_detach( 0, 0 );
234 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
236 openlog( serverName, OPENLOG_OPTIONS );
240 read_config( configfile, &be, fp );
247 if ( pthread_create( &listener_tid, NULL, slapd_daemon,
248 (void *) port ) != 0 ) {
249 Debug( LDAP_DEBUG_ANY,
250 "listener pthread_create failed\n", 0, 0, 0 );
254 #ifdef HAVE_PHREADS_FINAL
255 pthread_join( listener_tid, (void *) NULL );
257 pthread_join( listener_tid, (void *) &status );
266 unsigned long len, tag;
269 struct sockaddr_in from;
275 c.c_sb.sb_options = 0;
276 c.c_sb.sb_naddr = udp ? 1 : 0;
277 c.c_sb.sb_ber.ber_buf = NULL;
278 c.c_sb.sb_ber.ber_ptr = NULL;
279 c.c_sb.sb_ber.ber_end = NULL;
280 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
281 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
282 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
284 c.c_sb.sb_addrs = (void **) saddrlist;
285 c.c_sb.sb_fromaddr = &faddr;
286 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
289 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
290 #ifdef SLAPD_RLOOKUPS
291 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
292 sizeof(from.sin_addr.s_addr), AF_INET );
297 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
298 hp == NULL ? "unknown" : hp->h_name,
299 inet_ntoa( from.sin_addr ), 0 );
301 c.c_addr = inet_ntoa( from.sin_addr );
302 c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
304 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
308 ber_init_w_nullc( &ber, 0 );
310 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
311 == LDAP_TAG_MESSAGE ) {
312 pthread_mutex_lock( ¤ttime_mutex );
313 time( ¤ttime );
314 pthread_mutex_unlock( ¤ttime_mutex );
316 if ( (tag = ber_get_int( &ber, &msgid ))
317 != LDAP_TAG_MSGID ) {
318 /* log and send error */
319 Debug( LDAP_DEBUG_ANY,
320 "ber_get_int returns 0x%lx\n", tag, 0, 0 );
325 if ( (tag = ber_peek_tag( &ber, &len ))
327 /* log, close and send error */
328 Debug( LDAP_DEBUG_ANY,
329 "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
331 close( c.c_sb.sb_sd );
336 connection_activity( &c );
348 * Convert a string to an integer by means of a dispatcher table
349 * if the string is not in the table return the default
353 cnvt_str2int (stringVal, dispatcher, defaultVal)
355 STRDISP_P dispatcher;
358 int retVal = defaultVal;
361 for (disp = dispatcher; disp->stringVal; disp++) {
363 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
365 retVal = disp->intVal;
375 #endif /* LOG_LOCAL4 */