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 ( status = ldap_pvt_thread_create( &listener_tid, 0,
195 slapd_daemon, (void *) port ) != 0 )
197 Debug( LDAP_DEBUG_ANY,
198 "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
202 ldap_pvt_thread_join( listener_tid, (void *) NULL );
210 unsigned long len, tag;
213 struct sockaddr_in from;
220 c.c_sb.sb_options = 0;
221 c.c_sb.sb_naddr = udp ? 1 : 0;
222 c.c_sb.sb_ber.ber_buf = NULL;
223 c.c_sb.sb_ber.ber_ptr = NULL;
224 c.c_sb.sb_ber.ber_end = NULL;
225 ldap_pvt_thread_mutex_init( &c.c_dnmutex );
226 ldap_pvt_thread_mutex_init( &c.c_opsmutex );
227 ldap_pvt_thread_mutex_init( &c.c_pdumutex );
229 c.c_sb.sb_addrs = (void **) saddrlist;
230 c.c_sb.sb_fromaddr = &faddr;
231 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
234 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
235 #ifdef SLAPD_RLOOKUPS
236 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
237 sizeof(from.sin_addr.s_addr), AF_INET );
242 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
243 hp == NULL ? "unknown" : hp->h_name,
244 inet_ntoa( from.sin_addr ), 0 );
246 c.c_addr = inet_ntoa( from.sin_addr );
247 c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
249 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
254 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
255 == LDAP_TAG_MESSAGE ) {
256 ldap_pvt_thread_mutex_lock( ¤ttime_mutex );
257 time( ¤ttime );
258 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
260 if ( (tag = ber_get_int( &ber, &msgid ))
261 != LDAP_TAG_MSGID ) {
262 /* log and send error */
263 Debug( LDAP_DEBUG_ANY,
264 "ber_get_int returns 0x%lx\n", tag, 0, 0 );
269 if ( (tag = ber_peek_tag( &ber, &len ))
271 /* log, close and send error */
272 Debug( LDAP_DEBUG_ANY,
273 "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
275 close( c.c_sb.sb_sd );
280 connection_activity( &c );
292 * Convert a string to an integer by means of a dispatcher table
293 * if the string is not in the table return the default
297 cnvt_str2int (stringVal, dispatcher, defaultVal)
299 STRDISP_P dispatcher;
302 int retVal = defaultVal;
305 for (disp = dispatcher; disp->stringVal; disp++) {
307 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
309 retVal = disp->intVal;
319 #endif /* LOG_LOCAL4 */