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 )
65 struct sockaddr_in bind_addr;
68 int syslogUser = DEFAULT_SYSLOG_USER;
72 int serverMode = SLAP_SERVER_MODE;
74 configfile = SLAPD_DEFAULT_CONFIGFILE;
76 (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
77 bind_addr.sin_family = AF_INET;
78 bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
79 bind_addr.sin_port = htons(LDAP_PORT);
85 while ( (i = getopt( argc, argv, "d:f:ia:p:s:ut" )) != EOF ) {
87 while ( (i = getopt( argc, argv, "d:f:ia:p:s:u" )) != EOF ) {
90 case 'a': /* bind address */
91 if(!inet_aton(optarg, &bind_addr.sin_addr)) {
92 fprintf(stderr, "invalid address (%s) for -a option", optarg);
97 case 'd': /* turn on debugging */
98 if ( optarg[0] == '?' ) {
99 printf( "Debug levels:\n" );
100 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
102 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
103 LDAP_DEBUG_PACKETS );
104 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
106 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
108 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
110 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
112 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
114 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
116 printf( "\tLDAP_DEBUG_STATS\t%d\n",
118 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
120 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
122 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
124 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
128 slap_debug |= atoi( optarg );
132 case 'd': /* turn on debugging */
134 "must compile with LDAP_DEBUG for debugging\n" );
138 case 'f': /* read config file */
139 configfile = ch_strdup( optarg );
142 case 'i': /* run from inetd */
146 case 'p': { /* port on which to listen */
147 int port = atoi( optarg );
149 fprintf(stderr, "-p %s must be numeric\n", optarg);
151 bind_addr.sin_port = htons(port);
155 case 's': /* set syslog level */
156 ldap_syslog = atoi( optarg );
160 case 'l': /* set syslog local user */
161 syslogUser = cnvt_str2int( optarg, syslog_types,
162 DEFAULT_SYSLOG_USER );
166 case 'u': /* do udp */
171 case 't': /* timed server */
172 serverMode = SLAP_TIMEDSERVER_MODE;
182 lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
183 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
184 ldif_debug = slap_debug;
186 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
188 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
189 serverName = ch_strdup( argv[0] );
191 serverName = ch_strdup( serverName + 1 );
195 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
197 openlog( serverName, OPENLOG_OPTIONS );
200 if ( slap_init( serverMode, serverName ) != 0 ) {
205 if ( read_config( configfile ) != 0 ) {
210 if ( slap_startup(-1) != 0 ) {
218 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
219 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
221 (void) SIGNAL( SIGPIPE, SIG_IGN );
224 (void) SIGNAL( SIGHUP, slap_set_shutdown );
226 (void) SIGNAL( SIGINT, slap_set_shutdown );
227 (void) SIGNAL( SIGTERM, slap_set_shutdown );
230 lutil_detach( ldap_debug, 0 );
232 lutil_detach( 0, 0 );
237 status = ldap_pvt_thread_create( &listener_tid, 0,
238 slapd_daemon, &bind_addr );
241 Debug( LDAP_DEBUG_ANY,
242 "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
247 /* wait for the listener thread to complete */
248 ldap_pvt_thread_join( listener_tid, (void *) NULL );
254 unsigned long len, tag;
257 struct sockaddr_in from;
264 lber_pvt_sb_init( &c.c_sb );
265 lber_pvt_sb_set_desc( &c.c_sb, 0 );
266 lber_pvt_sb_set_io( &c.c_sb,
267 (udp) ? &lber_pvt_sb_io_udp : &lber_pvt_sb_io_tcp,
269 /* FIXME: handle udp here */
271 ldap_pvt_thread_mutex_init( &c.c_dnmutex );
272 ldap_pvt_thread_mutex_init( &c.c_opsmutex );
273 ldap_pvt_thread_mutex_init( &c.c_pdumutex );
275 c.c_sb.sb_addrs = (void **) saddrlist;
276 c.c_sb.sb_fromaddr = &faddr;
277 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
280 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
281 #ifdef SLAPD_RLOOKUPS
282 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
283 sizeof(from.sin_addr.s_addr), AF_INET );
288 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
289 hp == NULL ? "unknown" : hp->h_name,
290 inet_ntoa( from.sin_addr ), 0 );
292 c.c_addr = inet_ntoa( from.sin_addr );
293 c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
295 Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
299 c.c_state = SLAP_C_ACTIVE;
301 ber_init_w_nullc( &ber, 0 );
303 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
304 == LDAP_TAG_MESSAGE ) {
305 ldap_pvt_thread_mutex_lock( ¤ttime_mutex );
306 time( ¤ttime );
307 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
309 if ( (tag = ber_get_int( &ber, &msgid ))
310 != LDAP_TAG_MSGID ) {
311 /* log and send error */
312 Debug( LDAP_DEBUG_ANY,
313 "ber_get_int returns 0x%lx\n", tag, 0, 0 );
318 if ( (tag = ber_peek_tag( &ber, &len ))
320 /* log, close and send error */
321 Debug( LDAP_DEBUG_ANY,
322 "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
324 lber_pvt_sb_close( &c.c_sb );
325 lber_pvt_sb_destroy( &c.c_sb );
329 connection_activity( &c );
336 /* remember an error during shutdown */
337 rc |= slap_shutdown(-1);
339 /* remember an error during destroy */
340 rc |= slap_destroy();
342 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
351 * Convert a string to an integer by means of a dispatcher table
352 * if the string is not in the table return the default
356 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
358 int retVal = defaultVal;
361 for (disp = dispatcher; disp->stringVal; disp++) {
363 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
365 retVal = disp->intVal;
375 #endif /* LOG_LOCAL4 */