10 #include "ldapconfig.h"
12 #include "lutil.h" /* Get lutil_detach() */
16 * when more than one slapd is running on one machine, each one might have
17 * it's own LOCAL for syslogging and must have its own pid/args files
22 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
24 typedef struct _str2intDispatch {
30 } STRDISP, *STRDISP_P;
33 /* table to compute syslog-options to integer */
34 static STRDISP syslog_types[] = {
36 { "LOCAL0", 6, LOG_LOCAL0 },
37 { "LOCAL1", 6, LOG_LOCAL1 },
38 { "LOCAL2", 6, LOG_LOCAL2 },
39 { "LOCAL3", 6, LOG_LOCAL3 },
40 { "LOCAL4", 6, LOG_LOCAL4 },
41 { "LOCAL5", 6, LOG_LOCAL5 },
42 { "LOCAL6", 6, LOG_LOCAL6 },
43 { "LOCAL7", 6, LOG_LOCAL7 },
48 static int cnvt_str2int();
50 #endif /* LOG_LOCAL4 */
56 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
58 fprintf( stderr, " [-l sysloguser]" );
60 fprintf( stderr, "\n" );
64 main( int argc, char **argv )
73 int syslogUser = DEFAULT_SYSLOG_USER;
78 configfile = SLAPD_DEFAULT_CONFIGFILE;
83 while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
86 case 'd': /* turn on debugging */
87 if ( optarg[0] == '?' ) {
88 printf( "Debug levels:\n" );
89 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
91 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
93 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
95 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
97 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
99 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
101 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
103 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
105 printf( "\tLDAP_DEBUG_STATS\t%d\n",
107 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
109 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
111 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
113 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
117 slap_debug |= atoi( optarg );
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 lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
163 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
164 ldif_debug = slap_debug;
166 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
168 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
169 serverName = ch_strdup( argv[0] );
171 serverName = ch_strdup( serverName + 1 );
175 /* pre-open config file before detach in case it is a relative path */
176 fp = fopen( configfile, "r" );
178 lutil_detach( ldap_debug, 0 );
180 lutil_detach( 0, 0 );
185 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
187 openlog( serverName, OPENLOG_OPTIONS );
191 read_config_env( configfile, &be, fp, 1 );
198 if ( status = ldap_pvt_thread_create( &listener_tid, 0,
199 slapd_daemon, (void *) port ) != 0 )
201 Debug( LDAP_DEBUG_ANY,
202 "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
206 /* wait for the listener thread to complete */
207 ldap_pvt_thread_join( listener_tid, (void *) NULL );
215 unsigned long len, tag;
218 struct sockaddr_in from;
225 c.c_sb.sb_options = 0;
226 c.c_sb.sb_naddr = udp ? 1 : 0;
227 c.c_sb.sb_ber.ber_buf = NULL;
228 c.c_sb.sb_ber.ber_ptr = NULL;
229 c.c_sb.sb_ber.ber_end = NULL;
230 ldap_pvt_thread_mutex_init( &c.c_dnmutex );
231 ldap_pvt_thread_mutex_init( &c.c_opsmutex );
232 ldap_pvt_thread_mutex_init( &c.c_pdumutex );
234 c.c_sb.sb_addrs = (void **) saddrlist;
235 c.c_sb.sb_fromaddr = &faddr;
236 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
239 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
240 #ifdef SLAPD_RLOOKUPS
241 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
242 sizeof(from.sin_addr.s_addr), AF_INET );
247 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
248 hp == NULL ? "unknown" : hp->h_name,
249 inet_ntoa( from.sin_addr ), 0 );
251 c.c_addr = inet_ntoa( from.sin_addr );
252 c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
254 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
258 ber_init_w_nullc( &ber, 0 );
260 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
261 == LDAP_TAG_MESSAGE ) {
262 ldap_pvt_thread_mutex_lock( ¤ttime_mutex );
263 time( ¤ttime );
264 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
266 if ( (tag = ber_get_int( &ber, &msgid ))
267 != LDAP_TAG_MSGID ) {
268 /* log and send error */
269 Debug( LDAP_DEBUG_ANY,
270 "ber_get_int returns 0x%lx\n", tag, 0, 0 );
275 if ( (tag = ber_peek_tag( &ber, &len ))
277 /* log, close and send error */
278 Debug( LDAP_DEBUG_ANY,
279 "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
281 close( c.c_sb.sb_sd );
286 connection_activity( &c );
298 * Convert a string to an integer by means of a dispatcher table
299 * if the string is not in the table return the default
303 cnvt_str2int (stringVal, dispatcher, defaultVal)
305 STRDISP_P dispatcher;
308 int retVal = defaultVal;
311 for (disp = dispatcher; disp->stringVal; disp++) {
313 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
315 retVal = disp->intVal;
325 #endif /* LOG_LOCAL4 */