3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/signal.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
17 #include <ac/unistd.h>
26 int allow_severity = LOG_INFO;
27 int deny_severity = LOG_NOTICE;
28 #endif /* TCP Wrappers */
32 #endif /* LDAP_PF_LOCAL */
36 ber_socket_t dtblsize;
38 typedef struct slap_listener {
44 #ifdef LDAP_CONNECTIONLESS
45 int sl_is_udp; /* UDP listener is also data port */
49 #define sl_addr sl_sa.sa_in_addr
52 Listener **slap_listeners = NULL;
54 #define SLAPD_LISTEN 10
56 static ber_socket_t wake_sds[2];
60 #define WAKE_LISTENER(w) \
61 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
63 #define WAKE_LISTENER(w) \
64 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
67 #ifdef HAVE_NT_SERVICE_MANAGER
69 extern ldap_pvt_thread_cond_t started_event;
70 extern int is_NT_Service;
76 volatile sig_atomic_t slapd_shutdown = 0;
78 static struct slap_daemon {
79 ldap_pvt_thread_mutex_t sd_mutex;
84 /* In winsock, accept() returns values higher than dtblsize
85 so don't bother with this optimization */
98 * SLP related functions
102 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
103 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
104 static char** slapd_srvurls = NULL;
105 static SLPHandle slapd_hslp = 0;
107 void slapd_slp_init( const char* urls ) {
110 slapd_srvurls = str2charray( urls, " " );
112 if( slapd_srvurls == NULL ) return;
114 /* find and expand INADDR_ANY URLs */
115 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
116 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
117 char *host = ldap_pvt_get_fqdn( NULL );
118 if ( host != NULL ) {
119 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
121 sizeof( LDAP_SRVTYPE_PREFIX ) );
122 strcpy( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX );
123 strcat( slapd_srvurls[i], host );
128 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
129 char *host = ldap_pvt_get_fqdn( NULL );
130 if ( host != NULL ) {
131 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
133 sizeof( LDAPS_SRVTYPE_PREFIX ) );
134 strcpy( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX );
135 strcat( slapd_srvurls[i], host );
142 /* open the SLP handle */
143 SLPOpen( "en", 0, &slapd_hslp );
146 void slapd_slp_deinit() {
147 if( slapd_srvurls == NULL ) return;
149 charray_free( slapd_srvurls );
150 slapd_srvurls = NULL;
152 /* close the SLP handle */
153 SLPClose( slapd_hslp );
156 void slapd_slp_regreport(
164 void slapd_slp_reg() {
167 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
168 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
169 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
170 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
171 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
175 SLP_LIFETIME_MAXIMUM,
185 void slapd_slp_dereg() {
188 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
189 SLPDereg( slapd_hslp,
195 #endif /* HAVE_SLP */
198 * Add a descriptor to daemon control
200 static void slapd_add(ber_socket_t s) {
201 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
203 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
204 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
205 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
208 if (s >= slap_daemon.sd_nfds) {
209 slap_daemon.sd_nfds = s + 1;
213 FD_SET( s, &slap_daemon.sd_actives );
214 FD_SET( s, &slap_daemon.sd_readers );
217 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
218 "slapd_add: added %ld%s%s\n",
220 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
221 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
223 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
225 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
226 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
228 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
232 * Remove the descriptor from daemon control
234 void slapd_remove(ber_socket_t s, int wake) {
235 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
238 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
239 "slapd_remove: removing %ld%s%s\n",
241 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
242 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
244 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
246 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
247 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
249 FD_CLR( s, &slap_daemon.sd_actives );
250 FD_CLR( s, &slap_daemon.sd_readers );
251 FD_CLR( s, &slap_daemon.sd_writers );
253 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
257 void slapd_clr_write(ber_socket_t s, int wake) {
258 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
260 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
261 FD_CLR( s, &slap_daemon.sd_writers );
263 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
267 void slapd_set_write(ber_socket_t s, int wake) {
268 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
270 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
271 if (!FD_ISSET(s, &slap_daemon.sd_writers))
272 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
274 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
278 void slapd_clr_read(ber_socket_t s, int wake) {
279 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
281 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
282 FD_CLR( s, &slap_daemon.sd_readers );
284 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
288 void slapd_set_read(ber_socket_t s, int wake) {
289 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
291 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
292 if (!FD_ISSET(s, &slap_daemon.sd_readers))
293 FD_SET( s, &slap_daemon.sd_readers );
295 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
299 static void slapd_close(ber_socket_t s) {
301 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
302 "slapd_close: closing %ld\n", (long)s ));
304 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
310 static void slap_free_listener_addresses(struct sockaddr **sal)
312 struct sockaddr **sap;
318 for (sap = sal; *sap != NULL; sap++) {
325 /* port = 0 indicates AF_LOCAL */
326 static int slap_get_listener_addresses(
329 struct sockaddr ***sal)
331 struct sockaddr **sap;
335 *sal = ch_malloc(2 * sizeof(void *));
341 *sap = ch_malloc(sizeof(struct sockaddr_un));
347 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
349 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
350 "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
353 Debug( LDAP_DEBUG_ANY,
354 "daemon: domain socket path (%s) too long in URL",
360 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
361 (*sap)->sa_family = AF_LOCAL;
362 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
366 #ifdef HAVE_GETADDRINFO
367 struct addrinfo hints, *res, *sai;
371 memset( &hints, '\0', sizeof(hints) );
372 hints.ai_flags = AI_PASSIVE;
373 hints.ai_socktype = SOCK_STREAM;
374 hints.ai_family = AF_UNSPEC;
375 snprintf(serv, sizeof serv, "%d", port);
377 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
379 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
380 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
381 AC_GAI_STRERROR(err) ));
383 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
384 AC_GAI_STRERROR(err), 0, 0);
390 for (n=2; (sai = sai->ai_next) != NULL; n++) {
393 *sal = ch_malloc(n * sizeof(void *));
401 for ( sai=res; sai; sai=sai->ai_next ) {
402 if( sai->ai_addr == NULL ) {
404 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
405 "slap_get_listener_addresses: "
406 "getaddrinfo ai_addr is NULL?\n" ));
408 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
409 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
415 switch (sai->ai_family) {
416 # ifdef LDAP_PF_INET6
418 *sap = ch_malloc(sizeof(struct sockaddr_in6));
423 *(struct sockaddr_in6 *)*sap =
424 *((struct sockaddr_in6 *)sai->ai_addr);
428 *sap = ch_malloc(sizeof(struct sockaddr_in));
433 *(struct sockaddr_in *)*sap =
434 *((struct sockaddr_in *)sai->ai_addr);
442 (*sap)->sa_family = sai->ai_family;
452 if ( host == NULL ) {
453 in.s_addr = htonl(INADDR_ANY);
455 } else if ( !inet_aton( host, &in ) ) {
456 struct hostent *he = gethostbyname( host );
459 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
460 "slap_get_listener_addresses: invalid host %s\n",
463 Debug( LDAP_DEBUG_ANY,
464 "daemon: invalid host %s", host, 0, 0);
468 AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
471 *sal = ch_malloc(2 * sizeof(void *));
477 *sap = ch_malloc(sizeof(struct sockaddr_in));
483 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
484 (*sap)->sa_family = AF_INET;
485 ((struct sockaddr_in *)*sap)->sin_port = htons(port);
486 ((struct sockaddr_in *)*sap)->sin_addr = in;
493 slap_free_listener_addresses(*sal);
497 static Listener * slap_open_listener(
505 int err, addrlen = 0;
506 struct sockaddr **sal, **psal;
507 int socktype = SOCK_STREAM; /* default to COTS */
509 rc = ldap_url_parse( url, &lud );
511 if( rc != LDAP_URL_SUCCESS ) {
513 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
514 "slap_open_listener: listen URL \"%s\" parse error %d\n",
517 Debug( LDAP_DEBUG_ANY,
518 "daemon: listen URL \"%s\" parse error=%d\n",
525 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
527 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
528 "slap_open_listener: TLS is not supported (%s)\n",
531 Debug( LDAP_DEBUG_ANY,
532 "daemon: TLS not supported (%s)\n",
535 ldap_free_urldesc( lud );
539 if(! lud->lud_port ) {
540 lud->lud_port = LDAP_PORT;
544 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
546 if(! lud->lud_port ) {
547 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
551 port = (unsigned short) lud->lud_port;
553 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
554 if ( tmp == LDAP_PROTO_IPC ) {
556 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
557 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
559 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
564 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
565 "slap_open_listener: URL scheme is not supported: %s\n",
568 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
571 ldap_free_urldesc( lud );
575 #ifdef LDAP_CONNECTIONLESS
576 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
578 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
579 || strcmp(lud->lud_host, "*") == 0 )
581 err = slap_get_listener_addresses(NULL, port, &sal);
583 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
587 ldap_free_urldesc( lud );
593 while ( *sal != NULL ) {
594 switch( (*sal)->sa_family ) {
607 #ifdef LDAP_CONNECTIONLESS
609 socktype = SOCK_DGRAM;
611 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
612 if ( l.sl_sd == AC_SOCKET_INVALID ) {
613 int err = sock_errno();
615 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
616 "slap_open_listener: socket() failed errno=%d (%s)\n",
617 err, sock_errstr(err) ));
619 Debug( LDAP_DEBUG_ANY,
620 "daemon: socket() failed errno=%d (%s)\n", err,
621 sock_errstr(err), 0 );
627 if ( l.sl_sd >= dtblsize ) {
629 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
630 "slap_open_listener: listener descriptor %ld is too great %ld\n",
631 (long)l.sl_sd, (long)dtblsize ));
633 Debug( LDAP_DEBUG_ANY,
634 "daemon: listener descriptor %ld is too great %ld\n",
635 (long) l.sl_sd, (long) dtblsize, 0 );
637 tcp_close( l.sl_sd );
643 if ( (*sal)->sa_family == AF_LOCAL ) {
644 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
649 /* enable address reuse */
651 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
652 (char *) &tmp, sizeof(tmp) );
653 if ( rc == AC_SOCKET_ERROR ) {
654 int err = sock_errno();
656 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
657 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) failed errno %d (%s)\n",
658 (long)l.sl_sd, err, sock_errstr(err) ));
660 Debug( LDAP_DEBUG_ANY,
661 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
662 (long) l.sl_sd, err, sock_errstr(err) );
668 switch( (*sal)->sa_family ) {
670 addrlen = sizeof(struct sockaddr_in);
674 addrlen = sizeof(struct sockaddr_in6);
679 addrlen = sizeof(struct sockaddr_un);
684 if (!bind(l.sl_sd, *sal, addrlen))
688 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
689 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
690 (long)l.sl_sd, err, sock_errstr(err) ));
692 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
693 (long) l.sl_sd, err, sock_errstr(err) );
695 tcp_close( l.sl_sd );
697 } /* while ( *sal != NULL ) */
699 if ( *sal == NULL ) {
701 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
702 "slap_open_listener: bind(%ld) failed.\n", (long)l.sl_sd ));
704 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
705 (long) l.sl_sd, 0, 0 );
707 slap_free_listener_addresses(psal);
711 switch ( (*sal)->sa_family ) {
714 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
715 if ( chmod( addr, S_IRWXU ) < 0 ) {
716 int err = sock_errno();
718 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
719 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
720 (long)l.sl_sd, err, sock_errstr(err) ));
722 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
723 (long) l.sl_sd, err, sock_errstr(err) );
725 tcp_close( l.sl_sd );
726 slap_free_listener_addresses(psal);
729 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
730 sprintf( l.sl_name, "PATH=%s", addr );
732 #endif /* LDAP_PF_LOCAL */
736 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
737 char addr[INET_ADDRSTRLEN];
738 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
739 addr, sizeof(addr) );
741 port = ((struct sockaddr_in *)*sal) ->sin_port;
743 s = inet_ntoa( l.sl_addr.sin_addr );
744 port = l.sl_addr.sin_port;
746 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
747 sprintf( l.sl_name, "IP=%s:%d",
748 s != NULL ? s : "unknown" , port );
753 char addr[INET6_ADDRSTRLEN];
754 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
756 port = ((struct sockaddr_in6 *)*sal)->sin6_port;
757 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
758 sprintf( l.sl_name, "IP=%s %d", addr, port );
760 #endif /* LDAP_PF_INET6 */
764 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
765 "slap_open_listener: unsupported address family (%d)\n",
766 (int)(*sal)->sa_family ));
768 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
769 (int) (*sal)->sa_family, 0, 0 );
774 slap_free_listener_addresses(psal);
776 l.sl_url = ch_strdup( url );
777 li = ch_malloc( sizeof( Listener ) );
781 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
782 "slap_open_listener: daemon initialzed %s\n", l.sl_url ));
784 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
790 static int sockinit(void);
791 static int sockdestroy(void);
793 int slapd_daemon_init( const char *urls )
799 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
800 "slapd_daemon_init: %s\n",
801 urls ? urls : "<null>" ));
803 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
804 urls ? urls : "<null>", 0, 0 );
806 if( (rc = sockinit()) != 0 ) {
811 dtblsize = sysconf( _SC_OPEN_MAX );
812 #elif HAVE_GETDTABLESIZE
813 dtblsize = getdtablesize();
815 dtblsize = FD_SETSIZE;
819 if(dtblsize > FD_SETSIZE) {
820 dtblsize = FD_SETSIZE;
822 #endif /* !FD_SETSIZE */
824 /* open a pipe (or something equivalent connected to itself).
825 * we write a byte on this fd whenever we catch a signal. The main
826 * loop will be select'ing on this socket, and will wake up when
829 if( (rc = lutil_pair( wake_sds )) < 0 ) {
831 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
832 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
834 Debug( LDAP_DEBUG_ANY,
835 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
840 FD_ZERO( &slap_daemon.sd_readers );
841 FD_ZERO( &slap_daemon.sd_writers );
847 u = str2charray( urls, " " );
849 if( u == NULL || u[0] == NULL ) {
851 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
852 "slap_daemon_init: no urls (%s) provided.\n", urls ));
854 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
860 for( i=0; u[i] != NULL; i++ ) {
862 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
863 "slap_daemon_init: listen on %s\n.", u[i] ));
865 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
872 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
873 "slap_daemon_init: no listeners to open (%s)\n", urls ));
875 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
883 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
884 "slap_daemon_init: %d listeners to open...\n", i ));
886 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
889 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
891 for(i = 0; u[i] != NULL; i++ ) {
892 slap_listeners[i] = slap_open_listener( u[i] );
894 if( slap_listeners[i] == NULL ) {
899 slap_listeners[i] = NULL;
902 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
903 "slap_daemon_init: %d listeners opened\n", i ));
905 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
910 slapd_slp_init( urls );
915 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
921 slapd_daemon_destroy(void)
923 connections_destroy();
924 tcp_close( wake_sds[1] );
925 tcp_close( wake_sds[0] );
943 time_t last_idle_check = 0;
946 if ( global_idletimeout > 0 ) {
947 last_idle_check = slap_get_time();
949 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
950 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
952 #ifdef LDAP_CONNECTIONLESS
953 /* Since this is connectionless, the data port is the
954 * listening port. The listen() and accept() calls
957 if ( slap_listeners[l]->sl_is_udp )
959 slapd_add( slap_listeners[l]->sl_sd );
964 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
965 int err = sock_errno();
967 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
968 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
969 slap_listeners[l]->sl_url, err, sock_errstr(err) ));
971 Debug( LDAP_DEBUG_ANY,
972 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
973 slap_listeners[l]->sl_url, err,
979 slapd_add( slap_listeners[l]->sl_sd );
982 #ifdef HAVE_NT_SERVICE_MANAGER
983 if ( started_event != NULL ) {
984 ldap_pvt_thread_cond_signal( &started_event );
987 /* initialization complete. Here comes the loop. */
989 while ( !slapd_shutdown ) {
994 #define SLAPD_EBADF_LIMIT 16
998 #define SLAPD_IDLE_CHECK_LIMIT 4
1006 #if defined(SLAPD_RLOOKUPS)
1009 struct timeval zero;
1010 struct timeval *tvp;
1013 now = slap_get_time();
1014 connections_timeout_idle( now );
1016 else if ( global_idletimeout > 0 ) {
1017 now = slap_get_time();
1018 if ( difftime( last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1019 connections_timeout_idle( now );
1023 FD_ZERO( &writefds );
1024 FD_ZERO( &readfds );
1029 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1031 #ifdef FD_SET_MANUAL_COPY
1032 for( s = 0; s < nfds; s++ ) {
1033 if(FD_ISSET( &slap_sd_readers, s )) {
1034 FD_SET( s, &readfds );
1036 if(FD_ISSET( &slap_sd_writers, s )) {
1037 FD_SET( s, &writefds );
1041 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1042 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1044 assert(!FD_ISSET(wake_sds[0], &readfds));
1045 FD_SET( wake_sds[0], &readfds );
1047 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1048 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1050 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1051 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1054 #ifndef HAVE_WINSOCK
1055 nfds = slap_daemon.sd_nfds;
1060 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1062 at = ldap_pvt_thread_pool_backload(&connection_pool);
1064 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1067 tvp = at ? &zero : NULL;
1070 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1071 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1075 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1076 "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
1077 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
1079 Debug( LDAP_DEBUG_CONNS,
1080 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1081 slap_listeners[l]->sl_sd, at,
1082 tvp == NULL ? "NULL" : "zero" );
1086 switch(ns = select( nfds, &readfds,
1088 /* don't pass empty fd_set */
1089 ( writefds.fd_count > 0 ? &writefds : NULL ),
1095 case -1: { /* failure - try again */
1096 int err = sock_errno();
1100 /* you'd think this would be EBADF */
1101 || err == WSAENOTSOCK
1104 if (++ebadf < SLAPD_EBADF_LIMIT)
1108 if( err != EINTR ) {
1110 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1111 "slapd_daemon_task: select failed (%d): %s\n",
1112 err, sock_errstr(err) ));
1114 Debug( LDAP_DEBUG_CONNS,
1115 "daemon: select failed (%d): %s\n",
1116 err, sock_errstr(err), 0 );
1118 slapd_shutdown = -1;
1123 case 0: /* timeout - let threads run */
1126 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1127 "slapd_daemon_task: select timeout - yielding\n" ));
1129 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1132 ldap_pvt_thread_yield();
1135 default: /* something happened - deal with it */
1136 if( slapd_shutdown ) continue;
1140 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1141 "slapd_daemon_task: activity on %d descriptors\n", ns ));
1143 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1149 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1151 tcp_read( wake_sds[0], c, sizeof(c) );
1158 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1160 socklen_t len = sizeof(from);
1163 char *authid = NULL;
1165 char *dnsname = NULL;
1167 #ifdef LDAP_PF_LOCAL
1168 char peername[MAXPATHLEN + sizeof("PATH=")];
1169 #elif defined(LDAP_PF_INET6)
1170 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1172 char peername[sizeof("IP=255.255.255.255:65336")];
1173 #endif /* LDAP_PF_LOCAL */
1177 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1180 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1183 #ifdef LDAP_CONNECTIONLESS
1184 if ( slap_listeners[l]->sl_is_udp )
1186 /* The first time we receive a query, we set this
1187 * up as a "connection". It remains open for the life
1190 if ( slap_listeners[l]->sl_is_udp < 2 )
1192 id = connection_init(
1193 slap_listeners[l]->sl_sd,
1194 slap_listeners[l]->sl_url, "", "",
1195 slap_listeners[l]->sl_name,
1197 slap_listeners[l]->sl_is_udp++;
1203 s = accept( slap_listeners[l]->sl_sd,
1204 (struct sockaddr *) &from, &len );
1205 if ( s == AC_SOCKET_INVALID ) {
1206 int err = sock_errno();
1209 if( err == EMFILE ) {
1214 if( err == ENFILE ) {
1224 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1225 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1226 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1228 Debug( LDAP_DEBUG_ANY,
1229 "daemon: accept(%ld) failed errno=%d (%s)\n",
1230 (long) slap_listeners[l]->sl_sd, err,
1234 /* prevent busy loop */
1236 if( emfile % 4 == 3 ) usleep( 250 );
1238 if( emfile % 8 == 7 ) sleep( 1 );
1242 ldap_pvt_thread_yield();
1247 #ifndef HAVE_WINSOCK
1248 /* make sure descriptor number isn't too great */
1249 if ( s >= dtblsize ) {
1251 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1252 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1253 (long)s, (long)dtblsize ));
1255 Debug( LDAP_DEBUG_ANY,
1256 "daemon: %ld beyond descriptor table size %ld\n",
1257 (long) s, (long) dtblsize, 0 );
1261 ldap_pvt_thread_yield();
1267 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1269 /* newly accepted stream should not be in any of the FD SETS */
1270 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1271 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1272 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1274 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1277 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1278 #ifdef LDAP_PF_LOCAL
1279 /* for IPv4 and IPv6 sockets only */
1280 if ( from.sa_addr.sa_family != AF_LOCAL )
1281 #endif /* LDAP_PF_LOCAL */
1286 /* enable keep alives */
1288 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1289 (char *) &tmp, sizeof(tmp) );
1290 if ( rc == AC_SOCKET_ERROR ) {
1291 int err = sock_errno();
1293 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1294 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1295 (long)s, err, sock_errstr(err) ));
1297 Debug( LDAP_DEBUG_ANY,
1298 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1299 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1304 /* enable no delay */
1306 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1307 (char *)&tmp, sizeof(tmp) );
1308 if ( rc == AC_SOCKET_ERROR ) {
1309 int err = sock_errno();
1311 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1312 "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1313 (long)s, err, sock_errstr(err) ));
1315 Debug( LDAP_DEBUG_ANY,
1316 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1317 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1325 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1326 "slapd_daemon_task: new connection on %ld\n", (long)s ));
1328 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1331 switch ( from.sa_addr.sa_family ) {
1332 # ifdef LDAP_PF_LOCAL
1334 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1335 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1337 #endif /* LDAP_PF_LOCAL */
1339 # ifdef LDAP_PF_INET6
1341 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1342 peeraddr = inet_ntoa( *((struct in_addr *)
1343 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1344 sprintf( peername, "IP=%s:%d",
1345 peeraddr != NULL ? peeraddr : "unknown",
1346 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1348 char addr[INET6_ADDRSTRLEN];
1349 sprintf( peername, "IP=%s %d",
1350 inet_ntop( AF_INET6,
1351 &from.sa_in6_addr.sin6_addr,
1352 addr, sizeof addr) ? addr : "unknown",
1353 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1356 # endif /* LDAP_PF_INET6 */
1359 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1360 sprintf( peername, "IP=%s:%d",
1361 peeraddr != NULL ? peeraddr : "unknown",
1362 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1370 if ( ( from.sa_addr.sa_family == AF_INET )
1371 #ifdef LDAP_PF_INET6
1372 || ( from.sa_addr.sa_family == AF_INET6 )
1375 #ifdef SLAPD_RLOOKUPS
1376 # ifdef LDAP_PF_INET6
1377 if ( from.sa_addr.sa_family == AF_INET6 )
1379 (char *)&(from.sa_in6_addr.sin6_addr),
1380 sizeof(from.sa_in6_addr.sin6_addr),
1383 # endif /* LDAP_PF_INET6 */
1385 (char *) &(from.sa_in_addr.sin_addr),
1386 sizeof(from.sa_in_addr.sin_addr),
1388 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1391 #endif /* SLAPD_RLOOKUPS */
1394 if ( !hosts_ctl("slapd",
1395 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1396 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1400 Statslog( LDAP_DEBUG_ANY,
1401 "fd=%ld host access from %s (%s) denied.\n",
1403 dnsname != NULL ? dnsname : "unknown",
1404 peeraddr != NULL ? peeraddr : "unknown",
1409 #endif /* HAVE_TCPD */
1412 id = connection_init(s,
1413 slap_listeners[l]->sl_url,
1414 dnsname != NULL ? dnsname : "unknown",
1416 slap_listeners[l]->sl_name,
1418 slap_listeners[l]->sl_is_tls,
1425 if( authid ) ch_free(authid);
1429 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1430 "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1431 (long)s, peername, slap_listeners[l]->sl_name ));
1433 Debug( LDAP_DEBUG_ANY,
1434 "daemon: connection_init(%ld, %s, %s) failed.\n",
1437 slap_listeners[l]->sl_name );
1443 Statslog( LDAP_DEBUG_STATS,
1444 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1447 slap_listeners[l]->sl_name,
1456 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1457 "slapd_daemon_task: activity on " ));
1459 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1462 for ( i = 0; i < readfds.fd_count; i++ ) {
1464 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1465 " %d%s", readfds.fd_array[i], "r", 0 ));
1467 Debug( LDAP_DEBUG_CONNS, " %d%s",
1468 readfds.fd_array[i], "r", 0 );
1471 for ( i = 0; i < writefds.fd_count; i++ ) {
1473 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1474 " %d%s", writefds.fd_array[i], "w" ));
1476 Debug( LDAP_DEBUG_CONNS, " %d%s",
1477 writefds.fd_array[i], "w", 0 );
1482 for ( i = 0; i < nfds; i++ ) {
1484 int is_listener = 0;
1486 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1487 if ( i == slap_listeners[l]->sl_sd ) {
1488 #ifdef LDAP_CONNECTIONLESS
1489 /* The listener is the data port. Don't
1492 if (slap_listeners[l]->sl_is_udp)
1499 if ( is_listener ) {
1502 r = FD_ISSET( i, &readfds );
1503 w = FD_ISSET( i, &writefds );
1506 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1508 r ? "r" : "", w ? "w" : "" ));
1510 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1511 r ? "r" : "", w ? "w" : "" );
1517 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1519 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1524 /* loop through the writers */
1526 for ( i = 0; i < writefds.fd_count; i++ )
1528 for ( i = 0; i < nfds; i++ )
1532 int is_listener = 0;
1534 wd = writefds.fd_array[i];
1536 if( ! FD_ISSET( i, &writefds ) ) {
1542 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1543 if ( i == slap_listeners[l]->sl_sd ) {
1544 #ifdef LDAP_CONNECTIONLESS
1545 if (slap_listeners[l]->sl_is_udp)
1552 if ( is_listener ) {
1556 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1557 "slapd_daemon_task: write active on %d\n", wd ));
1559 Debug( LDAP_DEBUG_CONNS,
1560 "daemon: write active on %d\n",
1564 * NOTE: it is possible that the connection was closed
1565 * and that the stream is now inactive.
1566 * connection_write() must valid the stream is still
1570 if ( connection_write( wd ) < 0 ) {
1571 FD_CLR( (unsigned) wd, &readfds );
1577 for ( i = 0; i < readfds.fd_count; i++ )
1579 for ( i = 0; i < nfds; i++ )
1583 int is_listener = 0;
1586 rd = readfds.fd_array[i];
1588 if( ! FD_ISSET( i, &readfds ) ) {
1594 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1595 if ( rd == slap_listeners[l]->sl_sd ) {
1596 #ifdef LDAP_CONNECTIONLESS
1597 if (slap_listeners[l]->sl_is_udp)
1604 if ( is_listener ) {
1609 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1610 "slapd_daemon_task: read activity on %d\n", rd ));
1612 Debug ( LDAP_DEBUG_CONNS,
1613 "daemon: read activity on %d\n", rd, 0, 0 );
1616 * NOTE: it is possible that the connection was closed
1617 * and that the stream is now inactive.
1618 * connection_read() must valid the stream is still
1622 if ( connection_read( rd ) < 0 ) {
1626 ldap_pvt_thread_yield();
1629 if( slapd_shutdown > 0 ) {
1631 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1632 "slapd_daemon_task: shutdown requested and initiated.\n"));
1634 Debug( LDAP_DEBUG_TRACE,
1635 "daemon: shutdown requested and initiated.\n",
1639 } else if ( slapd_shutdown < 0 ) {
1640 #ifdef HAVE_NT_SERVICE_MANAGER
1641 if (slapd_shutdown == -1)
1644 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1645 "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1647 Debug( LDAP_DEBUG_TRACE,
1648 "daemon: shutdown initiated by Service Manager.\n",
1656 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1657 "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1659 Debug( LDAP_DEBUG_TRACE,
1660 "daemon: abnormal condition, shutdown initiated.\n",
1666 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1667 "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1669 Debug( LDAP_DEBUG_TRACE,
1670 "daemon: no active streams, shutdown initiated.\n",
1675 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1676 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1677 #ifdef LDAP_PF_LOCAL
1678 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1679 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1681 #endif /* LDAP_PF_LOCAL */
1682 slapd_close( slap_listeners[l]->sl_sd );
1688 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1689 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1690 ldap_pvt_thread_pool_backload(&connection_pool) ));
1692 Debug( LDAP_DEBUG_ANY,
1693 "slapd shutdown: waiting for %d threads to terminate\n",
1694 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1696 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1702 int slapd_daemon( void )
1708 #define SLAPD_LISTENER_THREAD 1
1709 #if defined( SLAPD_LISTENER_THREAD )
1711 ldap_pvt_thread_t listener_tid;
1713 /* listener as a separate THREAD */
1714 rc = ldap_pvt_thread_create( &listener_tid,
1715 0, slapd_daemon_task, NULL );
1719 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1720 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1722 Debug( LDAP_DEBUG_ANY,
1723 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1728 /* wait for the listener thread to complete */
1729 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1732 /* experimental code */
1733 slapd_daemon_task( NULL );
1742 #if defined( HAVE_WINSOCK2 )
1743 WORD wVersionRequested;
1747 wVersionRequested = MAKEWORD( 2, 0 );
1749 err = WSAStartup( wVersionRequested, &wsaData );
1751 /* Tell the user that we couldn't find a usable */
1756 /* Confirm that the WinSock DLL supports 2.0.*/
1757 /* Note that if the DLL supports versions greater */
1758 /* than 2.0 in addition to 2.0, it will still return */
1759 /* 2.0 in wVersion since that is the version we */
1762 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1763 HIBYTE( wsaData.wVersion ) != 0 )
1765 /* Tell the user that we couldn't find a usable */
1771 /* The WinSock DLL is acceptable. Proceed. */
1772 #elif defined( HAVE_WINSOCK )
1774 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1781 int sockdestroy(void)
1783 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1790 slap_sig_shutdown( int sig )
1793 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1794 "slap_sig_shutdown: signal %d\n", sig ));
1796 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1800 * If the NT Service Manager is controlling the server, we don't
1801 * want SIGBREAK to kill the server. For some strange reason,
1802 * SIGBREAK is generated when a user logs out.
1805 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1806 if (is_NT_Service && sig == SIGBREAK)
1808 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1809 "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1811 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1816 slapd_shutdown = sig;
1820 /* reinstall self */
1821 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1825 slap_sig_wake( int sig )
1829 /* reinstall self */
1830 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1834 void slapd_add_internal(ber_socket_t s) {