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 union slap_sockaddr {
39 struct sockaddr sa_addr;
40 struct sockaddr_in sa_in_addr;
42 struct sockaddr_in6 sa_in6_addr;
45 struct sockaddr_un sa_un_addr;
49 typedef struct slap_listener {
57 #define sl_addr sl_sa.sa_in_addr
60 Listener **slap_listeners = NULL;
62 #define SLAPD_LISTEN 10
64 static ber_socket_t wake_sds[2];
68 #define WAKE_LISTENER(w) \
69 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
71 #define WAKE_LISTENER(w) \
72 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
75 #ifdef HAVE_NT_SERVICE_MANAGER
77 extern ldap_pvt_thread_cond_t started_event;
78 extern int is_NT_Service;
84 volatile sig_atomic_t slapd_shutdown = 0;
86 static struct slap_daemon {
87 ldap_pvt_thread_mutex_t sd_mutex;
92 /* In winsock, accept() returns values higher than dtblsize
93 so don't bother with this optimization */
106 * SLP related functions
110 #define MAX_HOSTNAME_LEN 256
111 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
112 static char** slapd_srvurls = 0;
113 static SLPHandle slapd_hslp = 0;
115 void slapd_slp_init( const char* urls ) {
118 char hn[MAX_HOSTNAME_LEN];
120 slapd_srvurls = str2charray( urls, " " );
122 for( i=0; slapd_srvurls[i]!=NULL; i++ ) {
123 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
124 /* INADDR_ANY urls should be marked up with host.domainname */
125 if ( gethostname( hn, MAX_HOSTNAME_LEN ) == 0) {
126 he = gethostbyname( hn );
128 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
129 strlen( he->h_name ) +
130 strlen( LDAP_SRVTYPE_PREFIX ) + 1);
131 strcpy( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX );
132 strcat( slapd_srvurls[i], he->h_name );
138 /* open the SLP handle */
139 SLPOpen("en", 0, &slapd_hslp);
142 void slapd_slp_deinit() {
143 if ( slapd_srvurls ) {
144 charray_free( slapd_srvurls );
147 /* close the SLP handle */
148 SLPClose( slapd_hslp );
151 void slapd_slp_regreport(
159 void slapd_slp_reg() {
162 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
165 SLP_LIFETIME_MAXIMUM,
174 void slapd_slp_dereg() {
177 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
178 SLPDereg( slapd_hslp,
184 #endif /* HAVE_SLP */
187 * Add a descriptor to daemon control
189 static void slapd_add(ber_socket_t s) {
190 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
192 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
193 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
194 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
197 if (s >= slap_daemon.sd_nfds) {
198 slap_daemon.sd_nfds = s + 1;
202 FD_SET( s, &slap_daemon.sd_actives );
203 FD_SET( s, &slap_daemon.sd_readers );
206 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
207 "slapd_add: added %ld%s%s\n",
209 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
210 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
212 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
214 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
215 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
217 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
221 * Remove the descriptor from daemon control
223 void slapd_remove(ber_socket_t s, int wake) {
224 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
227 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
228 "slapd_remove: removing %ld%s%s\n",
230 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
231 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
233 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
235 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
236 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
238 FD_CLR( s, &slap_daemon.sd_actives );
239 FD_CLR( s, &slap_daemon.sd_readers );
240 FD_CLR( s, &slap_daemon.sd_writers );
242 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
246 void slapd_clr_write(ber_socket_t s, int wake) {
247 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
249 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
250 FD_CLR( s, &slap_daemon.sd_writers );
252 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
256 void slapd_set_write(ber_socket_t s, int wake) {
257 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
259 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
260 if (!FD_ISSET(s, &slap_daemon.sd_writers))
261 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
263 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
267 void slapd_clr_read(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 FD_CLR( s, &slap_daemon.sd_readers );
273 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
277 void slapd_set_read(ber_socket_t s, int wake) {
278 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
280 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
281 if (!FD_ISSET(s, &slap_daemon.sd_readers))
282 FD_SET( s, &slap_daemon.sd_readers );
284 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
288 static void slapd_close(ber_socket_t s) {
290 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
291 "slapd_close: closing %ld\n", (long)s ));
293 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
299 static void slap_free_listener_addresses(struct sockaddr **sal)
301 struct sockaddr **sap;
307 for (sap = sal; *sap != NULL; sap++) {
314 /* port = 0 indicates AF_LOCAL */
315 static int slap_get_listener_addresses(
318 struct sockaddr ***sal)
320 struct sockaddr **sap;
321 #ifdef HAVE_GETADDRINFO
322 struct addrinfo hints, *res, *sai;
325 memset( &hints, '\0', sizeof(hints) );
326 hints.ai_flags = AI_PASSIVE;
327 hints.ai_socktype = SOCK_STREAM;
328 # ifdef LDAP_PF_LOCAL
330 hints.ai_family = AF_LOCAL;
331 /* host specifies a service in this case */
332 if (err = getaddrinfo(NULL, host, &hints, &res)) {
334 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
335 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
336 AC_GAI_STRERROR(err) ));
338 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
339 AC_GAI_STRERROR(err), 0, 0);
348 snprintf(serv, sizeof serv, "%d", port);
349 hints.ai_family = AF_UNSPEC;
350 if (err = getaddrinfo(host, serv, &hints, &res)) {
352 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
353 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
354 AC_GAI_STRERROR(err) ));
356 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
357 AC_GAI_STRERROR(err), 0, 0);
364 for (n=2; (sai = sai->ai_next) != NULL; n++) {
367 *sal = ch_malloc(n * sizeof(*sal));
375 switch (sai->ai_family) {
376 # ifdef LDAP_PF_LOCAL
378 *sap = ch_malloc(sizeof(struct sockaddr_un));
383 *(struct sockaddr_un *)*sap =
384 *((struct sockaddr_un *)sai->ai_addr);
387 # ifdef LDAP_PF_INET6
389 *sap = ch_malloc(sizeof(struct sockaddr_in6));
394 *(struct sockaddr_in6 *)*sap =
395 *((struct sockaddr_in6 *)sai->ai_addr);
399 *sap = ch_malloc(sizeof(struct sockaddr_in));
404 *(struct sockaddr_in *)*sap =
405 *((struct sockaddr_in *)sai->ai_addr);
412 (*sap)->sa_family = sai->ai_family;
415 } while ((sai = sai->ai_next) != NULL);
420 # ifdef LDAP_PF_LOCAL
422 *sal = ch_malloc(2 * sizeof(*sal));
427 *sap = ch_malloc(sizeof(struct sockaddr_un));
430 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
431 (*sap)->sa_family = AF_LOCAL;
433 (sizeof(((struct sockaddr_un *)*sal)->sun_path) - 1) ) {
435 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
436 "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
439 Debug( LDAP_DEBUG_ANY,
440 "daemon: domain socket path (%s) too long in URL",
445 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
451 if ( host == NULL ) {
452 in.s_addr = htonl(INADDR_ANY);
454 } else if ( !inet_aton( host, &in ) ) {
455 struct hostent *he = gethostbyname( host );
458 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
459 "slap_get_listener_addresses: invalid host %s\n",
462 Debug( LDAP_DEBUG_ANY,
463 "daemon: invalid host %s", host, 0, 0);
467 AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
470 *sal = ch_malloc(2 * sizeof(*sal));
476 *sap = ch_malloc(sizeof(struct sockaddr_in));
481 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
482 (*sap)->sa_family = AF_INET;
483 ((struct sockaddr_in *)*sap)->sin_port = htons(port);
484 ((struct sockaddr_in *)*sap)->sin_addr = in;
493 slap_free_listener_addresses(*sal);
497 static Listener * slap_open_listener(
506 struct sockaddr **sal, **psal;
508 rc = ldap_url_parse( url, &lud );
510 if( rc != LDAP_URL_SUCCESS ) {
512 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
513 "slap_open_listener: listen URL \"%s\" parse error %d\n",
516 Debug( LDAP_DEBUG_ANY,
517 "daemon: listen URL \"%s\" parse error=%d\n",
524 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
526 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
527 "slap_open_listener: TLS is not supported (%s)\n",
530 Debug( LDAP_DEBUG_ANY,
531 "daemon: TLS not supported (%s)\n",
534 ldap_free_urldesc( lud );
538 if(! lud->lud_port ) {
539 lud->lud_port = LDAP_PORT;
543 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
545 if(! lud->lud_port ) {
546 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
550 port = (unsigned short) lud->lud_port;
552 if ( ldap_pvt_url_scheme2proto(lud->lud_scheme) == LDAP_PROTO_IPC ) {
554 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
555 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
557 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
562 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
563 "slap_open_listener: URL scheme is not supported: %s\n",
566 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
569 ldap_free_urldesc( lud );
573 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
574 || strcmp(lud->lud_host, "*") == 0 )
576 err = slap_get_listener_addresses(NULL, port, &sal);
578 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
582 ldap_free_urldesc( lud );
588 while ( *sal != NULL ) {
589 switch( (*sal)->sa_family ) {
602 l.sl_sd = socket( (*sal)->sa_family, SOCK_STREAM, 0);
603 if ( l.sl_sd == AC_SOCKET_INVALID ) {
604 int err = sock_errno();
606 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
607 "slap_open_listener: socket() failed errno=%d (%s)\n",
608 err, sock_errstr(err) ));
610 Debug( LDAP_DEBUG_ANY,
611 "daemon: socket() failed errno=%d (%s)\n", err,
612 sock_errstr(err), 0 );
618 if ( l.sl_sd >= dtblsize ) {
620 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
621 "slap_open_listener: listener descriptor %ld is too great %ld\n",
622 (long)l.sl_sd, (long)dtblsize ));
624 Debug( LDAP_DEBUG_ANY,
625 "daemon: listener descriptor %ld is too great %ld\n",
626 (long) l.sl_sd, (long) dtblsize, 0 );
628 tcp_close( l.sl_sd );
634 if ( (*sal)->sa_family == AF_LOCAL ) {
635 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
640 /* enable address reuse */
642 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
643 (char *) &tmp, sizeof(tmp) );
644 if ( rc == AC_SOCKET_ERROR ) {
645 int err = sock_errno();
647 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
648 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) failed errno %d (%s)\n",
649 (long)l.sl_sd, err, sock_errstr(err) ));
651 Debug( LDAP_DEBUG_ANY,
652 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
653 (long) l.sl_sd, err, sock_errstr(err) );
659 switch( (*sal)->sa_family ) {
661 addrlen = sizeof(struct sockaddr_in);
665 addrlen = sizeof(struct sockaddr_in6);
670 addrlen = sizeof(struct sockaddr_un);
675 if (!bind(l.sl_sd, *sal, addrlen))
679 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
680 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
681 (long)l.sl_sd, err, sock_errstr(err) ));
683 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
684 (long) l.sl_sd, err, sock_errstr(err) );
686 tcp_close( l.sl_sd );
688 } /* while ( *sal != NULL ) */
690 if ( *sal == NULL ) {
692 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
693 "slap_open_listener: bind(%ld) failed.\n", (long)l.sl_sd ));
695 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
696 (long) l.sl_sd, 0, 0 );
698 slap_free_listener_addresses(psal);
702 switch ( (*sal)->sa_family ) {
705 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
706 if ( chmod( addr, S_IRWXU ) < 0 ) {
707 int err = sock_errno();
709 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
710 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
711 (long)l.sl_sd, err, sock_errstr(err) ));
713 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
714 (long) l.sl_sd, err, sock_errstr(err) );
716 tcp_close( l.sl_sd );
717 slap_free_listener_addresses(psal);
720 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
721 sprintf( l.sl_name, "PATH=%s", addr );
723 #endif /* LDAP_PF_LOCAL */
727 #ifdef HAVE_GETADDRINFO
728 char addr[INET_ADDRSTRLEN];
729 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
730 addr, sizeof(addr) );
732 port = ((struct sockaddr_in *)*sal) ->sin_port;
734 s = inet_ntoa( l.sl_addr.sin_addr );
735 port = l.sl_addr.sin_port;
737 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
738 sprintf( l.sl_name, "IP=%s:%d",
739 s != NULL ? s : "unknown" , port );
744 char addr[INET6_ADDRSTRLEN];
745 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
747 port = ((struct sockaddr_in6 *)*sal)->sin6_port;
748 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
749 sprintf( l.sl_name, "IP=%s %d", addr, port );
751 #endif /* LDAP_PF_INET6 */
755 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
756 "slap_open_listener: unsupported address family (%d)\n",
757 (int)(*sal)->sa_family ));
759 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
760 (int) (*sal)->sa_family, 0, 0 );
765 slap_free_listener_addresses(psal);
767 l.sl_url = ch_strdup( url );
768 li = ch_malloc( sizeof( Listener ) );
772 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
773 "slap_open_listener: daemon initialzed %s\n", l.sl_url ));
775 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
781 static int sockinit(void);
782 static int sockdestroy(void);
784 int slapd_daemon_init( const char *urls )
790 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
791 "slapd_daemon_init: %s\n",
792 urls ? urls : "<null>" ));
794 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
795 urls ? urls : "<null>", 0, 0 );
797 if( (rc = sockinit()) != 0 ) {
802 dtblsize = sysconf( _SC_OPEN_MAX );
803 #elif HAVE_GETDTABLESIZE
804 dtblsize = getdtablesize();
806 dtblsize = FD_SETSIZE;
810 if(dtblsize > FD_SETSIZE) {
811 dtblsize = FD_SETSIZE;
813 #endif /* !FD_SETSIZE */
815 /* open a pipe (or something equivalent connected to itself).
816 * we write a byte on this fd whenever we catch a signal. The main
817 * loop will be select'ing on this socket, and will wake up when
820 if( (rc = lutil_pair( wake_sds )) < 0 ) {
822 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
823 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
825 Debug( LDAP_DEBUG_ANY,
826 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
831 FD_ZERO( &slap_daemon.sd_readers );
832 FD_ZERO( &slap_daemon.sd_writers );
838 u = str2charray( urls, " " );
840 if( u == NULL || u[0] == NULL ) {
842 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
843 "slap_daemon_init: no urls (%s) provided.\n", urls ));
845 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
851 for( i=0; u[i] != NULL; i++ ) {
853 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
854 "slap_daemon_init: listen on %s\n.", u[i] ));
856 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
863 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
864 "slap_daemon_init: no listeners to open (%s)\n", urls ));
866 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
874 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
875 "slap_daemon_init: %d listeners to open...\n", i ));
877 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
880 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
882 for(i = 0; u[i] != NULL; i++ ) {
883 slap_listeners[i] = slap_open_listener( u[i] );
885 if( slap_listeners[i] == NULL ) {
890 slap_listeners[i] = NULL;
893 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
894 "slap_daemon_init: %d listeners opened\n", i ));
896 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
901 slapd_slp_init( urls );
906 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
912 slapd_daemon_destroy(void)
914 connections_destroy();
915 tcp_close( wake_sds[1] );
916 tcp_close( wake_sds[0] );
934 time_t last_idle_check = slap_get_time();
937 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
938 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
941 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
942 int err = sock_errno();
944 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
945 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
946 slap_listeners[l]->sl_url, err, sock_errstr(err) ));
948 Debug( LDAP_DEBUG_ANY,
949 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
950 slap_listeners[l]->sl_url, err,
956 slapd_add( slap_listeners[l]->sl_sd );
959 #ifdef HAVE_NT_SERVICE_MANAGER
960 if ( started_event != NULL ) {
961 ldap_pvt_thread_cond_signal( &started_event );
964 /* initialization complete. Here comes the loop. */
966 while ( !slapd_shutdown ) {
971 #define SLAPD_EBADF_LIMIT 16
974 #define SLAPD_IDLE_CHECK_LIMIT 4
975 time_t now = slap_get_time();
982 #if defined(SLAPD_RLOOKUPS)
988 if( global_idletimeout > 0 && difftime(
989 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
992 connections_timeout_idle(now);
995 FD_ZERO( &writefds );
1001 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1003 #ifdef FD_SET_MANUAL_COPY
1004 for( s = 0; s < nfds; s++ ) {
1005 if(FD_ISSET( &slap_sd_readers, s )) {
1006 FD_SET( s, &readfds );
1008 if(FD_ISSET( &slap_sd_writers, s )) {
1009 FD_SET( s, &writefds );
1013 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1014 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1016 assert(!FD_ISSET(wake_sds[0], &readfds));
1017 FD_SET( wake_sds[0], &readfds );
1019 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1020 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1022 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1023 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1026 #ifndef HAVE_WINSOCK
1027 nfds = slap_daemon.sd_nfds;
1032 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1034 at = ldap_pvt_thread_pool_backload(&connection_pool);
1036 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1039 tvp = at ? &zero : NULL;
1042 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1043 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1047 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1048 "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
1049 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
1051 Debug( LDAP_DEBUG_CONNS,
1052 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1053 slap_listeners[l]->sl_sd, at,
1054 tvp == NULL ? "NULL" : "zero" );
1058 switch(ns = select( nfds, &readfds,
1060 /* don't pass empty fd_set */
1061 ( writefds.fd_count > 0 ? &writefds : NULL ),
1067 case -1: { /* failure - try again */
1068 int err = sock_errno();
1072 /* you'd think this would be EBADF */
1073 || err == WSAENOTSOCK
1076 if (++ebadf < SLAPD_EBADF_LIMIT)
1080 if( err != EINTR ) {
1082 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1083 "slapd_daemon_task: select failed (%d): %s\n",
1084 err, sock_errstr(err) ));
1086 Debug( LDAP_DEBUG_CONNS,
1087 "daemon: select failed (%d): %s\n",
1088 err, sock_errstr(err), 0 );
1090 slapd_shutdown = -1;
1095 case 0: /* timeout - let threads run */
1098 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1099 "slapd_daemon_task: select timeout - yielding\n" ));
1101 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1104 ldap_pvt_thread_yield();
1107 default: /* something happened - deal with it */
1108 if( slapd_shutdown ) continue;
1112 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1113 "slapd_daemon_task: activity on %d descriptors\n", ns ));
1115 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1121 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1123 tcp_read( wake_sds[0], c, sizeof(c) );
1130 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1132 socklen_t len = sizeof(from);
1135 char *authid = NULL;
1139 #ifdef LDAP_PF_LOCAL
1140 char peername[MAXPATHLEN + sizeof("PATH=")];
1141 #elif defined(LDAP_PF_INET6)
1142 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1144 char peername[sizeof("IP=255.255.255.255:65336")];
1145 #endif /* LDAP_PF_LOCAL */
1149 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1152 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1155 s = accept( slap_listeners[l]->sl_sd,
1156 (struct sockaddr *) &from, &len );
1157 if ( s == AC_SOCKET_INVALID ) {
1158 int err = sock_errno();
1160 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1161 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1162 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1164 Debug( LDAP_DEBUG_ANY,
1165 "daemon: accept(%ld) failed errno=%d (%s)\n",
1166 (long) slap_listeners[l]->sl_sd, err,
1172 #ifndef HAVE_WINSOCK
1173 /* make sure descriptor number isn't too great */
1174 if ( s >= dtblsize ) {
1176 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1177 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1178 (long)s, (long)dtblsize ));
1180 Debug( LDAP_DEBUG_ANY,
1181 "daemon: %ld beyond descriptor table size %ld\n",
1182 (long) s, (long) dtblsize, 0 );
1190 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1192 /* newly accepted stream should not be in any of the FD SETS */
1193 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1194 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1195 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1197 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1200 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1201 #ifdef LDAP_PF_LOCAL
1202 /* for IPv4 and IPv6 sockets only */
1203 if ( from.sa_addr.sa_family != AF_LOCAL )
1204 #endif /* LDAP_PF_LOCAL */
1209 /* enable keep alives */
1211 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1212 (char *) &tmp, sizeof(tmp) );
1213 if ( rc == AC_SOCKET_ERROR ) {
1214 int err = sock_errno();
1216 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1217 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1218 (long)s, err, sock_errstr(err) ));
1220 Debug( LDAP_DEBUG_ANY,
1221 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1222 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1227 /* enable no delay */
1229 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1230 (char *)&tmp, sizeof(tmp) );
1231 if ( rc == AC_SOCKET_ERROR ) {
1232 int err = sock_errno();
1234 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1235 "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1236 (long)s, err, sock_errstr(err) ));
1238 Debug( LDAP_DEBUG_ANY,
1239 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1240 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1248 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1249 "slapd_daemon_task: new connection on %ld\n", (long)s ));
1251 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1254 switch ( from.sa_addr.sa_family ) {
1255 # ifdef LDAP_PF_LOCAL
1257 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1258 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1260 #endif /* LDAP_PF_LOCAL */
1262 # ifdef LDAP_PF_INET6
1264 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1265 peeraddr = inet_ntoa( *((struct in_addr *)
1266 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1267 sprintf( peername, "IP=%s:%d",
1268 peeraddr != NULL ? peeraddr : "unknown",
1269 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1271 char addr[INET6_ADDRSTRLEN];
1272 sprintf( peername, "IP=%s %d",
1273 inet_ntop( AF_INET6,
1274 &from.sa_in6_addr.sin6_addr,
1275 addr, sizeof addr) ? addr : "unknown",
1276 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1279 # endif /* LDAP_PF_INET6 */
1282 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1283 sprintf( peername, "IP=%s:%d",
1284 peeraddr != NULL ? peeraddr : "unknown",
1285 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1293 if ( ( from.sa_addr.sa_family == AF_INET )
1294 #ifdef LDAP_PF_INET6
1295 || ( from.sa_addr.sa_family == AF_INET6 )
1298 #ifdef SLAPD_RLOOKUPS
1299 # ifdef LDAP_PF_INET6
1300 if ( from.sa_addr.sa_family == AF_INET6 )
1302 (char *)&(from.sa_in6_addr.sin6_addr),
1303 sizeof(from.sa_in6_addr.sin6_addr),
1306 # endif /* LDAP_PF_INET6 */
1308 (char *) &(from.sa_in_addr.sin_addr),
1309 sizeof(from.sa_in_addr.sin_addr),
1311 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1314 #endif /* SLAPD_RLOOKUPS */
1317 if ( !hosts_ctl("slapd",
1318 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1319 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1323 Statslog( LDAP_DEBUG_ANY,
1324 "fd=%ld host access from %s (%s) denied.\n",
1326 dnsname != NULL ? dnsname : "unknown",
1327 peeraddr != NULL ? peeraddr : "unknown",
1332 #endif /* HAVE_TCPD */
1335 id = connection_init(s,
1336 slap_listeners[l]->sl_url,
1337 dnsname != NULL ? dnsname : "unknown",
1339 slap_listeners[l]->sl_name,
1341 slap_listeners[l]->sl_is_tls,
1348 if( authid ) ch_free(authid);
1352 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1353 "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1354 (long)s, peername, slap_listeners[l]->sl_name ));
1356 Debug( LDAP_DEBUG_ANY,
1357 "daemon: connection_init(%ld, %s, %s) failed.\n",
1360 slap_listeners[l]->sl_name );
1366 Statslog( LDAP_DEBUG_STATS,
1367 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1370 slap_listeners[l]->sl_name,
1379 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1380 "slapd_daemon_task: activity on " ));
1382 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1385 for ( i = 0; i < readfds.fd_count; i++ ) {
1387 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1388 " %d%s", readfds.fd_array[i], "r", 0 ));
1390 Debug( LDAP_DEBUG_CONNS, " %d%s",
1391 readfds.fd_array[i], "r", 0 );
1394 for ( i = 0; i < writefds.fd_count; i++ ) {
1396 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1397 " %d%s", writefds.fd_array[i], "w" ));
1399 Debug( LDAP_DEBUG_CONNS, " %d%s",
1400 writefds.fd_array[i], "w", 0 );
1405 for ( i = 0; i < nfds; i++ ) {
1407 int is_listener = 0;
1409 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1410 if ( i == slap_listeners[l]->sl_sd ) {
1415 if ( is_listener ) {
1418 r = FD_ISSET( i, &readfds );
1419 w = FD_ISSET( i, &writefds );
1422 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1424 r ? "r" : "", w ? "w" : "" ));
1426 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1427 r ? "r" : "", w ? "w" : "" );
1433 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1435 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1440 /* loop through the writers */
1442 for ( i = 0; i < writefds.fd_count; i++ )
1444 for ( i = 0; i < nfds; i++ )
1448 int is_listener = 0;
1450 wd = writefds.fd_array[i];
1452 if( ! FD_ISSET( i, &writefds ) ) {
1458 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1459 if ( i == slap_listeners[l]->sl_sd ) {
1464 if ( is_listener ) {
1468 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1469 "slapd_daemon_task: write active on %d\n", wd ));
1471 Debug( LDAP_DEBUG_CONNS,
1472 "daemon: write active on %d\n",
1476 * NOTE: it is possible that the connection was closed
1477 * and that the stream is now inactive.
1478 * connection_write() must valid the stream is still
1482 if ( connection_write( wd ) < 0 ) {
1483 FD_CLR( (unsigned) wd, &readfds );
1489 for ( i = 0; i < readfds.fd_count; i++ )
1491 for ( i = 0; i < nfds; i++ )
1495 int is_listener = 0;
1498 rd = readfds.fd_array[i];
1500 if( ! FD_ISSET( i, &readfds ) ) {
1506 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1507 if ( rd == slap_listeners[l]->sl_sd ) {
1512 if ( is_listener ) {
1517 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1518 "slapd_daemon_task: read activity on %d\n", rd ));
1520 Debug ( LDAP_DEBUG_CONNS,
1521 "daemon: read activity on %d\n", rd, 0, 0 );
1524 * NOTE: it is possible that the connection was closed
1525 * and that the stream is now inactive.
1526 * connection_read() must valid the stream is still
1530 if ( connection_read( rd ) < 0 ) {
1534 ldap_pvt_thread_yield();
1537 if( slapd_shutdown > 0 ) {
1539 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1540 "slapd_daemon_task: shutdown requested and initiated.\n"));
1542 Debug( LDAP_DEBUG_TRACE,
1543 "daemon: shutdown requested and initiated.\n",
1547 } else if ( slapd_shutdown < 0 ) {
1548 #ifdef HAVE_NT_SERVICE_MANAGER
1549 if (slapd_shutdown == -1)
1552 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1553 "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1555 Debug( LDAP_DEBUG_TRACE,
1556 "daemon: shutdown initiated by Service Manager.\n",
1564 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1565 "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1567 Debug( LDAP_DEBUG_TRACE,
1568 "daemon: abnormal condition, shutdown initiated.\n",
1574 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1575 "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1577 Debug( LDAP_DEBUG_TRACE,
1578 "daemon: no active streams, shutdown initiated.\n",
1583 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1584 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1585 #ifdef LDAP_PF_LOCAL
1586 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1587 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1589 #endif /* LDAP_PF_LOCAL */
1590 slapd_close( slap_listeners[l]->sl_sd );
1596 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1597 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1598 ldap_pvt_thread_pool_backload(&connection_pool) ));
1600 Debug( LDAP_DEBUG_ANY,
1601 "slapd shutdown: waiting for %d threads to terminate\n",
1602 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1604 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1610 int slapd_daemon( void )
1616 #define SLAPD_LISTENER_THREAD 1
1617 #if defined( SLAPD_LISTENER_THREAD )
1619 ldap_pvt_thread_t listener_tid;
1621 /* listener as a separate THREAD */
1622 rc = ldap_pvt_thread_create( &listener_tid,
1623 0, slapd_daemon_task, NULL );
1627 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1628 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1630 Debug( LDAP_DEBUG_ANY,
1631 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1636 /* wait for the listener thread to complete */
1637 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1640 /* experimental code */
1641 slapd_daemon_task( NULL );
1650 #if defined( HAVE_WINSOCK2 )
1651 WORD wVersionRequested;
1655 wVersionRequested = MAKEWORD( 2, 0 );
1657 err = WSAStartup( wVersionRequested, &wsaData );
1659 /* Tell the user that we couldn't find a usable */
1664 /* Confirm that the WinSock DLL supports 2.0.*/
1665 /* Note that if the DLL supports versions greater */
1666 /* than 2.0 in addition to 2.0, it will still return */
1667 /* 2.0 in wVersion since that is the version we */
1670 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1671 HIBYTE( wsaData.wVersion ) != 0 )
1673 /* Tell the user that we couldn't find a usable */
1679 /* The WinSock DLL is acceptable. Proceed. */
1680 #elif defined( HAVE_WINSOCK )
1682 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1689 int sockdestroy(void)
1691 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1698 slap_sig_shutdown( int sig )
1701 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1702 "slap_sig_shutdown: signal %d\n", sig ));
1704 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1708 * If the NT Service Manager is controlling the server, we don't
1709 * want SIGBREAK to kill the server. For some strange reason,
1710 * SIGBREAK is generated when a user logs out.
1713 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1714 if (is_NT_Service && sig == SIGBREAK)
1716 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1717 "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1719 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1724 slapd_shutdown = sig;
1728 /* reinstall self */
1729 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1733 slap_sig_wake( int sig )
1737 /* reinstall self */
1738 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1742 void slapd_add_internal(ber_socket_t s) {