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 ( pthread_create( &listener_tid, NULL, slapd_daemon,
204 (void *) port ) != 0 ) {
205 Debug( LDAP_DEBUG_ANY,
206 "listener pthread_create failed\n", 0, 0, 0 );
210 #ifdef HAVE_PHREADS_FINAL
211 pthread_join( listener_tid, (void *) NULL );
213 pthread_join( listener_tid, (void *) &status );
222 unsigned long len, tag;
225 struct sockaddr_in from;
231 c.c_sb.sb_options = 0;
232 c.c_sb.sb_naddr = udp ? 1 : 0;
233 c.c_sb.sb_ber.ber_buf = NULL;
234 c.c_sb.sb_ber.ber_ptr = NULL;
235 c.c_sb.sb_ber.ber_end = NULL;
236 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
237 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
238 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
240 c.c_sb.sb_addrs = (void **) saddrlist;
241 c.c_sb.sb_fromaddr = &faddr;
242 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
245 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
246 #ifdef SLAPD_RLOOKUPS
247 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
248 sizeof(from.sin_addr.s_addr), AF_INET );
253 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
254 hp == NULL ? "unknown" : hp->h_name,
255 inet_ntoa( from.sin_addr ), 0 );
257 c.c_addr = inet_ntoa( from.sin_addr );
258 c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
260 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
264 ber_init_w_nullc( &ber, 0 );
266 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
267 == LDAP_TAG_MESSAGE ) {
268 pthread_mutex_lock( ¤ttime_mutex );
269 time( ¤ttime );
270 pthread_mutex_unlock( ¤ttime_mutex );
272 if ( (tag = ber_get_int( &ber, &msgid ))
273 != LDAP_TAG_MSGID ) {
274 /* log and send error */
275 Debug( LDAP_DEBUG_ANY,
276 "ber_get_int returns 0x%lx\n", tag, 0, 0 );
281 if ( (tag = ber_peek_tag( &ber, &len ))
283 /* log, close and send error */
284 Debug( LDAP_DEBUG_ANY,
285 "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
287 close( c.c_sb.sb_sd );
292 connection_activity( &c );
304 * Convert a string to an integer by means of a dispatcher table
305 * if the string is not in the table return the default
309 cnvt_str2int (stringVal, dispatcher, defaultVal)
311 STRDISP_P dispatcher;
314 int retVal = defaultVal;
317 for (disp = dispatcher; disp->stringVal; disp++) {
319 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
321 retVal = disp->intVal;
331 #endif /* LOG_LOCAL4 */