11 #include "ldapconfig.h"
13 #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 {
28 } STRDISP, *STRDISP_P;
31 /* table to compute syslog-options to integer */
32 static STRDISP syslog_types[] = {
33 { "LOCAL0", 6, LOG_LOCAL0 },
34 { "LOCAL1", 6, LOG_LOCAL1 },
35 { "LOCAL2", 6, LOG_LOCAL2 },
36 { "LOCAL3", 6, LOG_LOCAL3 },
37 { "LOCAL4", 6, LOG_LOCAL4 },
38 { "LOCAL5", 6, LOG_LOCAL5 },
39 { "LOCAL6", 6, LOG_LOCAL6 },
40 { "LOCAL7", 6, LOG_LOCAL7 },
44 static int cnvt_str2int();
46 #endif /* LOG_LOCAL4 */
52 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
54 fprintf( stderr, " [-l sysloguser]" );
56 fprintf( stderr, "\n" );
60 main( int argc, char **argv )
68 int syslogUser = DEFAULT_SYSLOG_USER;
73 configfile = SLAPD_DEFAULT_CONFIGFILE;
78 while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
81 case 'd': /* turn on debugging */
82 if ( optarg[0] == '?' ) {
83 printf( "Debug levels:\n" );
84 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
86 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
88 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
90 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
92 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
94 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
96 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
98 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
100 printf( "\tLDAP_DEBUG_STATS\t%d\n",
102 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
104 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
106 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
108 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
112 slap_debug |= atoi( optarg );
116 case 'd': /* turn on debugging */
118 "must compile with LDAP_DEBUG for debugging\n" );
122 case 'f': /* read config file */
123 configfile = ch_strdup( optarg );
126 case 'i': /* run from inetd */
130 case 'p': /* port on which to listen */
131 port = atoi( optarg );
134 case 's': /* set syslog level */
135 ldap_syslog = atoi( optarg );
140 case 'l': /* set syslog local user */
141 syslogUser = cnvt_str2int( optarg, syslog_types,
142 DEFAULT_SYSLOG_USER );
147 case 'u': /* do udp */
157 lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
158 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
159 ldif_debug = slap_debug;
161 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
163 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
164 serverName = ch_strdup( argv[0] );
166 serverName = ch_strdup( serverName + 1 );
170 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
172 openlog( serverName, OPENLOG_OPTIONS );
179 if ( slap_init( SLAP_SERVER_MODE, serverName ) != 0 ) {
184 if ( read_config( configfile ) != 0 ) {
189 if ( slap_startup(-1) != 0 ) {
197 (void) SIGNAL( SIGPIPE, SIG_IGN );
198 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
199 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
200 (void) SIGNAL( SIGTERM, slap_set_shutdown );
201 (void) SIGNAL( SIGINT, slap_set_shutdown );
202 (void) SIGNAL( SIGHUP, slap_set_shutdown );
205 lutil_detach( ldap_debug, 0 );
207 lutil_detach( 0, 0 );
212 if ( status = ldap_pvt_thread_create( &listener_tid, 0,
213 slapd_daemon, (void *) port ) != 0 )
215 Debug( LDAP_DEBUG_ANY,
216 "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
221 /* wait for the listener thread to complete */
222 ldap_pvt_thread_join( listener_tid, (void *) NULL );
229 unsigned long len, tag;
232 struct sockaddr_in from;
239 c.c_sb.sb_options = 0;
240 c.c_sb.sb_naddr = udp ? 1 : 0;
241 c.c_sb.sb_ber.ber_buf = NULL;
242 c.c_sb.sb_ber.ber_ptr = NULL;
243 c.c_sb.sb_ber.ber_end = NULL;
244 ldap_pvt_thread_mutex_init( &c.c_dnmutex );
245 ldap_pvt_thread_mutex_init( &c.c_opsmutex );
246 ldap_pvt_thread_mutex_init( &c.c_pdumutex );
248 c.c_sb.sb_addrs = (void **) saddrlist;
249 c.c_sb.sb_fromaddr = &faddr;
250 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
253 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
254 #ifdef SLAPD_RLOOKUPS
255 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
256 sizeof(from.sin_addr.s_addr), AF_INET );
261 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
262 hp == NULL ? "unknown" : hp->h_name,
263 inet_ntoa( from.sin_addr ), 0 );
265 c.c_addr = inet_ntoa( from.sin_addr );
266 c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
268 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
272 ber_init_w_nullc( &ber, 0 );
274 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
275 == LDAP_TAG_MESSAGE ) {
276 ldap_pvt_thread_mutex_lock( ¤ttime_mutex );
277 time( ¤ttime );
278 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
280 if ( (tag = ber_get_int( &ber, &msgid ))
281 != LDAP_TAG_MSGID ) {
282 /* log and send error */
283 Debug( LDAP_DEBUG_ANY,
284 "ber_get_int returns 0x%lx\n", tag, 0, 0 );
289 if ( (tag = ber_peek_tag( &ber, &len ))
291 /* log, close and send error */
292 Debug( LDAP_DEBUG_ANY,
293 "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
295 close( c.c_sb.sb_sd );
300 connection_activity( &c );
307 /* remember an error during shutdown */
308 rc |= slap_shutdown(-1);
310 /* remember an error during destroy */
311 rc |= slap_destroy();
313 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
322 * Convert a string to an integer by means of a dispatcher table
323 * if the string is not in the table return the default
327 cnvt_str2int (stringVal, dispatcher, defaultVal)
329 STRDISP_P dispatcher;
332 int retVal = defaultVal;
335 for (disp = dispatcher; disp->stringVal; disp++) {
337 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
339 retVal = disp->intVal;
349 #endif /* LOG_LOCAL4 */