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 */
53 * the server's name must be accessible from the daemon module,
54 * to construct the pid/args file names
56 char *serverName = NULL;
62 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
64 fprintf( stderr, " [-l sysloguser]" );
66 fprintf( stderr, "\n" );
70 main( int argc, char **argv )
79 int syslogUser = DEFAULT_SYSLOG_USER;
83 configfile = SLAPD_DEFAULT_CONFIGFILE;
88 while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
91 case 'd': /* turn on debugging */
92 if ( optarg[0] == '?' ) {
93 printf( "Debug levels:\n" );
94 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
96 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
98 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
100 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
102 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
104 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
106 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
108 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
110 printf( "\tLDAP_DEBUG_STATS\t%d\n",
112 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
114 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
116 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
118 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
122 slap_debug |= atoi( optarg );
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 lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
168 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
169 ldif_debug = slap_debug;
171 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
173 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
174 serverName = ch_strdup( argv[0] );
176 serverName = ch_strdup( serverName + 1 );
180 /* pre-open config file before detach in case it is a relative path */
181 fp = fopen( configfile, "r" );
183 lutil_detach( ldap_debug, 0 );
185 lutil_detach( 0, 0 );
190 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
192 openlog( serverName, OPENLOG_OPTIONS );
196 read_config( configfile, &be, fp );
203 if ( status = pthread_create( &listener_tid, NULL,
204 slapd_daemon, (void *) port ) != 0 )
206 Debug( LDAP_DEBUG_ANY,
207 "listener pthread_create failed (%d)\n", status, 0, 0 );
211 #ifdef HAVE_PHREADS_FINAL
212 pthread_join( listener_tid, (void *) NULL );
214 pthread_join( listener_tid, (void *) &status );
223 unsigned long len, tag;
226 struct sockaddr_in from;
233 c.c_sb.sb_options = 0;
234 c.c_sb.sb_naddr = udp ? 1 : 0;
235 c.c_sb.sb_ber.ber_buf = NULL;
236 c.c_sb.sb_ber.ber_ptr = NULL;
237 c.c_sb.sb_ber.ber_end = NULL;
238 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
239 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
240 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
242 c.c_sb.sb_addrs = (void **) saddrlist;
243 c.c_sb.sb_fromaddr = &faddr;
244 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
247 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
248 #ifdef SLAPD_RLOOKUPS
249 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
250 sizeof(from.sin_addr.s_addr), AF_INET );
255 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
256 hp == NULL ? "unknown" : hp->h_name,
257 inet_ntoa( from.sin_addr ), 0 );
259 c.c_addr = inet_ntoa( from.sin_addr );
260 c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
262 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
266 ber_init_w_nullc( &ber, 0 );
268 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
269 == LDAP_TAG_MESSAGE ) {
270 pthread_mutex_lock( ¤ttime_mutex );
271 time( ¤ttime );
272 pthread_mutex_unlock( ¤ttime_mutex );
274 if ( (tag = ber_get_int( &ber, &msgid ))
275 != LDAP_TAG_MSGID ) {
276 /* log and send error */
277 Debug( LDAP_DEBUG_ANY,
278 "ber_get_int returns 0x%lx\n", tag, 0, 0 );
283 if ( (tag = ber_peek_tag( &ber, &len ))
285 /* log, close and send error */
286 Debug( LDAP_DEBUG_ANY,
287 "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
289 close( c.c_sb.sb_sd );
294 connection_activity( &c );
306 * Convert a string to an integer by means of a dispatcher table
307 * if the string is not in the table return the default
311 cnvt_str2int (stringVal, dispatcher, defaultVal)
313 STRDISP_P dispatcher;
316 int retVal = defaultVal;
319 for (disp = dispatcher; disp->stringVal; disp++) {
321 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
323 retVal = disp->intVal;
333 #endif /* LOG_LOCAL4 */