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( char *, STRDISP_P, int );
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;
72 int serverMode = SLAP_SERVER_MODE;
74 configfile = SLAPD_DEFAULT_CONFIGFILE;
80 while ( (i = getopt( argc, argv, "d:f:ip:s:ut" )) != EOF ) {
82 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 */
157 case 't': /* timed server */
158 serverMode = SLAP_TIMEDSERVER_MODE;
168 lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
169 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
170 ldif_debug = slap_debug;
172 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
174 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
175 serverName = ch_strdup( argv[0] );
177 serverName = ch_strdup( serverName + 1 );
181 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
183 openlog( serverName, OPENLOG_OPTIONS );
186 if ( slap_init( serverMode, serverName ) != 0 ) {
191 if ( read_config( configfile ) != 0 ) {
196 if ( slap_startup(-1) != 0 ) {
204 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
205 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
207 (void) SIGNAL( SIGPIPE, SIG_IGN );
210 (void) SIGNAL( SIGHUP, slap_set_shutdown );
212 (void) SIGNAL( SIGINT, slap_set_shutdown );
213 (void) SIGNAL( SIGTERM, slap_set_shutdown );
216 lutil_detach( ldap_debug, 0 );
218 lutil_detach( 0, 0 );
223 status = ldap_pvt_thread_create( &listener_tid, 0,
224 slapd_daemon, (void *) port );
227 Debug( LDAP_DEBUG_ANY,
228 "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
233 /* wait for the listener thread to complete */
234 ldap_pvt_thread_join( listener_tid, (void *) NULL );
241 unsigned long len, tag;
244 struct sockaddr_in from;
251 lber_pvt_sb_init( &c.c_sb );
252 lber_pvt_sb_set_desc( &c.c_sb, 0 );
253 lber_pvt_sb_set_io( &c.c_sb,
254 (udp) ? &lber_pvt_sb_io_udp : &lber_pvt_sb_io_tcp,
256 /* FIXME: handle udp here */
258 ldap_pvt_thread_mutex_init( &c.c_dnmutex );
259 ldap_pvt_thread_mutex_init( &c.c_opsmutex );
260 ldap_pvt_thread_mutex_init( &c.c_pdumutex );
262 c.c_sb.sb_addrs = (void **) saddrlist;
263 c.c_sb.sb_fromaddr = &faddr;
264 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
267 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
268 #ifdef SLAPD_RLOOKUPS
269 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
270 sizeof(from.sin_addr.s_addr), AF_INET );
275 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
276 hp == NULL ? "unknown" : hp->h_name,
277 inet_ntoa( from.sin_addr ), 0 );
279 c.c_addr = inet_ntoa( from.sin_addr );
280 c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
282 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
286 ber_init_w_nullc( &ber, 0 );
288 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
289 == LDAP_TAG_MESSAGE ) {
290 ldap_pvt_thread_mutex_lock( ¤ttime_mutex );
291 time( ¤ttime );
292 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
294 if ( (tag = ber_get_int( &ber, &msgid ))
295 != LDAP_TAG_MSGID ) {
296 /* log and send error */
297 Debug( LDAP_DEBUG_ANY,
298 "ber_get_int returns 0x%lx\n", tag, 0, 0 );
303 if ( (tag = ber_peek_tag( &ber, &len ))
305 /* log, close and send error */
306 Debug( LDAP_DEBUG_ANY,
307 "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
309 lber_pvt_sb_close( &c.c_sb );
310 lber_pvt_sb_destroy( &c.c_sb );
314 connection_activity( &c );
321 /* remember an error during shutdown */
322 rc |= slap_shutdown(-1);
324 /* remember an error during destroy */
325 rc |= slap_destroy();
327 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
336 * Convert a string to an integer by means of a dispatcher table
337 * if the string is not in the table return the default
341 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
343 int retVal = defaultVal;
346 for (disp = dispatcher; disp->stringVal; disp++) {
348 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
350 retVal = disp->intVal;
360 #endif /* LOG_LOCAL4 */