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 */
52 * the server's name must be accessible from the daemon module,
53 * to construct the pid/args file names
55 char *serverName = NULL;
61 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
63 fprintf( stderr, " [-l sysloguser]" );
65 fprintf( stderr, "\n" );
69 main( int argc, char **argv )
79 int syslogUser = DEFAULT_SYSLOG_USER;
82 configfile = SLAPD_DEFAULT_CONFIGFILE;
87 while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
90 case 'd': /* turn on debugging */
91 if ( optarg[0] == '?' ) {
92 printf( "Debug levels:\n" );
93 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
95 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
97 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
99 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
101 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
103 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
105 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
107 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
109 printf( "\tLDAP_DEBUG_STATS\t%d\n",
111 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
113 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
115 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
117 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
121 ldap_debug |= atoi( optarg );
122 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
126 case 'd': /* turn on debugging */
128 "must compile with LDAP_DEBUG for debugging\n" );
132 case 'f': /* read config file */
133 configfile = ch_strdup( optarg );
136 case 'i': /* run from inetd */
140 case 'p': /* port on which to listen */
141 port = atoi( optarg );
144 case 's': /* set syslog level */
145 ldap_syslog = atoi( optarg );
150 case 'l': /* set syslog local user */
151 syslogUser = cnvt_str2int( optarg, syslog_types,
152 DEFAULT_SYSLOG_USER );
157 case 'u': /* do udp */
167 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
169 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
170 serverName = ch_strdup( argv[0] );
172 serverName = ch_strdup( serverName + 1 );
176 /* pre-open config file before detach in case it is a relative path */
177 fp = fopen( configfile, "r" );
179 lutil_detach( ldap_debug, 0 );
181 lutil_detach( 0, 0 );
186 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
188 openlog( serverName, OPENLOG_OPTIONS );
192 read_config( configfile, &be, fp );
199 if ( pthread_create( &listener_tid, NULL, slapd_daemon,
200 (void *) port ) != 0 ) {
201 Debug( LDAP_DEBUG_ANY,
202 "listener pthread_create failed\n", 0, 0, 0 );
206 #ifdef HAVE_PHREADS_FINAL
207 pthread_join( listener_tid, (void *) NULL );
209 pthread_join( listener_tid, (void *) &status );
218 unsigned long len, tag;
221 struct sockaddr_in from;
228 c.c_sb.sb_options = 0;
229 c.c_sb.sb_naddr = udp ? 1 : 0;
230 c.c_sb.sb_ber.ber_buf = NULL;
231 c.c_sb.sb_ber.ber_ptr = NULL;
232 c.c_sb.sb_ber.ber_end = NULL;
233 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
234 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
235 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
237 c.c_sb.sb_addrs = (void **) saddrlist;
238 c.c_sb.sb_fromaddr = &faddr;
239 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
242 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
243 #ifdef SLAPD_RLOOKUPS
244 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
245 sizeof(from.sin_addr.s_addr), AF_INET );
250 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
251 hp == NULL ? "unknown" : hp->h_name,
252 inet_ntoa( from.sin_addr ), 0 );
254 c.c_addr = inet_ntoa( from.sin_addr );
255 c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
257 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
262 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
263 == LDAP_TAG_MESSAGE ) {
264 pthread_mutex_lock( ¤ttime_mutex );
265 time( ¤ttime );
266 pthread_mutex_unlock( ¤ttime_mutex );
268 if ( (tag = ber_get_int( &ber, &msgid ))
269 != LDAP_TAG_MSGID ) {
270 /* log and send error */
271 Debug( LDAP_DEBUG_ANY,
272 "ber_get_int returns 0x%lx\n", tag, 0, 0 );
277 if ( (tag = ber_peek_tag( &ber, &len ))
279 /* log, close and send error */
280 Debug( LDAP_DEBUG_ANY,
281 "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
283 close( c.c_sb.sb_sd );
288 connection_activity( &c );
300 * Convert a string to an integer by means of a dispatcher table
301 * if the string is not in the table return the default
305 cnvt_str2int (stringVal, dispatcher, defaultVal)
307 STRDISP_P dispatcher;
310 int retVal = defaultVal;
313 for (disp = dispatcher; disp->stringVal; disp++) {
315 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
317 retVal = disp->intVal;
327 #endif /* LOG_LOCAL4 */