10 #include "ldapconfig.h"
12 #include "lutil.h" /* Get lutil_detach() */
15 * when more than one slapd is running on one machine, each one might have
16 * it's own LOCAL for syslogging and must have its own pid/args files
21 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
23 typedef struct _str2intDispatch {
29 } STRDISP, *STRDISP_P;
32 /* table to compute syslog-options to integer */
33 static STRDISP syslog_types[] = {
35 { "LOCAL0", 6, LOG_LOCAL0 },
36 { "LOCAL1", 6, LOG_LOCAL1 },
37 { "LOCAL2", 6, LOG_LOCAL2 },
38 { "LOCAL3", 6, LOG_LOCAL3 },
39 { "LOCAL4", 6, LOG_LOCAL4 },
40 { "LOCAL5", 6, LOG_LOCAL5 },
41 { "LOCAL6", 6, LOG_LOCAL6 },
42 { "LOCAL7", 6, LOG_LOCAL7 },
47 static int cnvt_str2int();
49 #endif /* LOG_LOCAL4 */
55 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
57 fprintf( stderr, " [-l sysloguser]" );
59 fprintf( stderr, "\n" );
63 main( int argc, char **argv )
74 int syslogUser = DEFAULT_SYSLOG_USER;
77 configfile = SLAPD_DEFAULT_CONFIGFILE;
82 while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
85 case 'd': /* turn on debugging */
86 if ( optarg[0] == '?' ) {
87 printf( "Debug levels:\n" );
88 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
90 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
92 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
94 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
96 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
98 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
100 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
102 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
104 printf( "\tLDAP_DEBUG_STATS\t%d\n",
106 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
108 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
110 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
112 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
116 ldap_debug |= atoi( optarg );
117 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
121 case 'd': /* turn on debugging */
123 "must compile with LDAP_DEBUG for debugging\n" );
127 case 'f': /* read config file */
128 configfile = ch_strdup( optarg );
131 case 'i': /* run from inetd */
135 case 'p': /* port on which to listen */
136 port = atoi( optarg );
139 case 's': /* set syslog level */
140 ldap_syslog = atoi( optarg );
145 case 'l': /* set syslog local user */
146 syslogUser = cnvt_str2int( optarg, syslog_types,
147 DEFAULT_SYSLOG_USER );
152 case 'u': /* do udp */
162 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
164 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
165 serverName = ch_strdup( argv[0] );
167 serverName = ch_strdup( serverName + 1 );
171 /* pre-open config file before detach in case it is a relative path */
172 fp = fopen( configfile, "r" );
174 lutil_detach( ldap_debug, 0 );
176 lutil_detach( 0, 0 );
181 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
183 openlog( serverName, OPENLOG_OPTIONS );
187 read_config( configfile, &be, fp );
194 if ( pthread_create( &listener_tid, NULL, slapd_daemon,
195 (void *) port ) != 0 ) {
196 Debug( LDAP_DEBUG_ANY,
197 "listener pthread_create failed\n", 0, 0, 0 );
201 #ifdef HAVE_PTHREADS_FINAL
202 pthread_join( listener_tid, (void *) NULL );
204 pthread_join( listener_tid, (void *) &status );
213 unsigned long len, tag;
216 struct sockaddr_in from;
223 c.c_sb.sb_options = 0;
224 c.c_sb.sb_naddr = udp ? 1 : 0;
225 c.c_sb.sb_ber.ber_buf = NULL;
226 c.c_sb.sb_ber.ber_ptr = NULL;
227 c.c_sb.sb_ber.ber_end = NULL;
228 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
229 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
230 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
232 c.c_sb.sb_addrs = (void **) saddrlist;
233 c.c_sb.sb_fromaddr = &faddr;
234 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
237 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
238 #ifdef SLAPD_RLOOKUPS
239 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
240 sizeof(from.sin_addr.s_addr), AF_INET );
245 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
246 hp == NULL ? "unknown" : hp->h_name,
247 inet_ntoa( from.sin_addr ), 0 );
249 c.c_addr = inet_ntoa( from.sin_addr );
250 c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
252 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
257 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
258 == LDAP_TAG_MESSAGE ) {
259 pthread_mutex_lock( ¤ttime_mutex );
260 time( ¤ttime );
261 pthread_mutex_unlock( ¤ttime_mutex );
263 if ( (tag = ber_get_int( &ber, &msgid ))
264 != LDAP_TAG_MSGID ) {
265 /* log and send error */
266 Debug( LDAP_DEBUG_ANY,
267 "ber_get_int returns 0x%lx\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%lx\n", tag, 0, 0 );
278 close( c.c_sb.sb_sd );
283 connection_activity( &c );
295 * Convert a string to an integer by means of a dispatcher table
296 * if the string is not in the table return the default
300 cnvt_str2int (stringVal, dispatcher, defaultVal)
302 STRDISP_P dispatcher;
305 int retVal = defaultVal;
308 for (disp = dispatcher; disp->stringVal; disp++) {
310 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
312 retVal = disp->intVal;
322 #endif /* LOG_LOCAL4 */