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 LDAP_SRVTYPE_PREFIX "service:ldap://"
111 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
112 static char** slapd_srvurls = NULL;
113 static SLPHandle slapd_hslp = 0;
115 void slapd_slp_init( const char* urls ) {
118 slapd_srvurls = str2charray( urls, " " );
120 if( slapd_srv_urls == NULL ) return;
122 /* find and expand INADDR_ANY URLs */
123 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
124 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
125 char *host = ldap_pvt_get_fqdn( NULL );
126 if ( host != NULL ) {
127 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
129 sizeof( LDAP_SRVTYPE_PREFIX ) );
130 strcpy( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX );
131 strcat( slapd_srvurls[i], host );
136 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
137 char *host = ldap_pvt_get_fqdn( NULL );
138 if ( host != NULL ) {
139 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
141 sizeof( LDAPS_SRVTYPE_PREFIX ) );
142 strcpy( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX );
143 strcat( slapd_srvurls[i], host );
150 /* open the SLP handle */
151 SLPOpen( "en", 0, &slapd_hslp );
154 void slapd_slp_deinit() {
155 if( slapd_srv_urls == NULL ) return;
157 charray_free( slapd_srvurls );
158 slapd_srvurls = NULL;
160 /* close the SLP handle */
161 SLPClose( slapd_hslp );
164 void slapd_slp_regreport(
172 void slapd_slp_reg() {
175 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
176 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
177 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
178 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
179 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
183 SLP_LIFETIME_MAXIMUM,
193 void slapd_slp_dereg() {
196 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
197 SLPDereg( slapd_hslp,
203 #endif /* HAVE_SLP */
206 * Add a descriptor to daemon control
208 static void slapd_add(ber_socket_t s) {
209 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
211 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
212 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
213 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
216 if (s >= slap_daemon.sd_nfds) {
217 slap_daemon.sd_nfds = s + 1;
221 FD_SET( s, &slap_daemon.sd_actives );
222 FD_SET( s, &slap_daemon.sd_readers );
225 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
226 "slapd_add: added %ld%s%s\n",
228 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
229 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
231 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
233 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
234 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
236 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
240 * Remove the descriptor from daemon control
242 void slapd_remove(ber_socket_t s, int wake) {
243 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
246 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
247 "slapd_remove: removing %ld%s%s\n",
249 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
250 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
252 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
254 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
255 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
257 FD_CLR( s, &slap_daemon.sd_actives );
258 FD_CLR( s, &slap_daemon.sd_readers );
259 FD_CLR( s, &slap_daemon.sd_writers );
261 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
265 void slapd_clr_write(ber_socket_t s, int wake) {
266 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
268 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
269 FD_CLR( s, &slap_daemon.sd_writers );
271 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
275 void slapd_set_write(ber_socket_t s, int wake) {
276 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
278 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
279 if (!FD_ISSET(s, &slap_daemon.sd_writers))
280 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
282 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
286 void slapd_clr_read(ber_socket_t s, int wake) {
287 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
289 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
290 FD_CLR( s, &slap_daemon.sd_readers );
292 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
296 void slapd_set_read(ber_socket_t s, int wake) {
297 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
299 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
300 if (!FD_ISSET(s, &slap_daemon.sd_readers))
301 FD_SET( s, &slap_daemon.sd_readers );
303 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
307 static void slapd_close(ber_socket_t s) {
309 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
310 "slapd_close: closing %ld\n", (long)s ));
312 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
318 static void slap_free_listener_addresses(struct sockaddr **sal)
320 struct sockaddr **sap;
326 for (sap = sal; *sap != NULL; sap++) {
333 /* port = 0 indicates AF_LOCAL */
334 static int slap_get_listener_addresses(
337 struct sockaddr ***sal)
339 struct sockaddr **sap;
340 #ifdef HAVE_GETADDRINFO
341 struct addrinfo hints, *res, *sai;
344 memset( &hints, '\0', sizeof(hints) );
345 hints.ai_flags = AI_PASSIVE;
346 hints.ai_socktype = SOCK_STREAM;
347 # ifdef LDAP_PF_LOCAL
349 hints.ai_family = AF_LOCAL;
350 /* host specifies a service in this case */
351 if (err = getaddrinfo(NULL, host, &hints, &res)) {
353 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
354 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
355 AC_GAI_STRERROR(err) ));
357 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
358 AC_GAI_STRERROR(err), 0, 0);
367 snprintf(serv, sizeof serv, "%d", port);
368 hints.ai_family = AF_UNSPEC;
369 if (err = getaddrinfo(host, serv, &hints, &res)) {
371 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
372 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
373 AC_GAI_STRERROR(err) ));
375 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
376 AC_GAI_STRERROR(err), 0, 0);
383 for (n=2; (sai = sai->ai_next) != NULL; n++) {
386 *sal = ch_malloc(n * sizeof(*sal));
394 switch (sai->ai_family) {
395 # ifdef LDAP_PF_LOCAL
397 *sap = ch_malloc(sizeof(struct sockaddr_un));
402 *(struct sockaddr_un *)*sap =
403 *((struct sockaddr_un *)sai->ai_addr);
406 # ifdef LDAP_PF_INET6
408 *sap = ch_malloc(sizeof(struct sockaddr_in6));
413 *(struct sockaddr_in6 *)*sap =
414 *((struct sockaddr_in6 *)sai->ai_addr);
418 *sap = ch_malloc(sizeof(struct sockaddr_in));
423 *(struct sockaddr_in *)*sap =
424 *((struct sockaddr_in *)sai->ai_addr);
431 (*sap)->sa_family = sai->ai_family;
434 } while ((sai = sai->ai_next) != NULL);
439 # ifdef LDAP_PF_LOCAL
441 *sal = ch_malloc(2 * sizeof(*sal));
446 *sap = ch_malloc(sizeof(struct sockaddr_un));
449 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
450 (*sap)->sa_family = AF_LOCAL;
452 (sizeof(((struct sockaddr_un *)*sal)->sun_path) - 1) ) {
454 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
455 "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
458 Debug( LDAP_DEBUG_ANY,
459 "daemon: domain socket path (%s) too long in URL",
464 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
470 if ( host == NULL ) {
471 in.s_addr = htonl(INADDR_ANY);
473 } else if ( !inet_aton( host, &in ) ) {
474 struct hostent *he = gethostbyname( host );
477 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
478 "slap_get_listener_addresses: invalid host %s\n",
481 Debug( LDAP_DEBUG_ANY,
482 "daemon: invalid host %s", host, 0, 0);
486 AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
489 *sal = ch_malloc(2 * sizeof(*sal));
495 *sap = ch_malloc(sizeof(struct sockaddr_in));
500 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
501 (*sap)->sa_family = AF_INET;
502 ((struct sockaddr_in *)*sap)->sin_port = htons(port);
503 ((struct sockaddr_in *)*sap)->sin_addr = in;
512 slap_free_listener_addresses(*sal);
516 static Listener * slap_open_listener(
525 struct sockaddr **sal, **psal;
527 rc = ldap_url_parse( url, &lud );
529 if( rc != LDAP_URL_SUCCESS ) {
531 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
532 "slap_open_listener: listen URL \"%s\" parse error %d\n",
535 Debug( LDAP_DEBUG_ANY,
536 "daemon: listen URL \"%s\" parse error=%d\n",
543 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
545 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
546 "slap_open_listener: TLS is not supported (%s)\n",
549 Debug( LDAP_DEBUG_ANY,
550 "daemon: TLS not supported (%s)\n",
553 ldap_free_urldesc( lud );
557 if(! lud->lud_port ) {
558 lud->lud_port = LDAP_PORT;
562 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
564 if(! lud->lud_port ) {
565 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
569 port = (unsigned short) lud->lud_port;
571 if ( ldap_pvt_url_scheme2proto(lud->lud_scheme) == LDAP_PROTO_IPC ) {
573 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
574 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
576 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
581 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
582 "slap_open_listener: URL scheme is not supported: %s\n",
585 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
588 ldap_free_urldesc( lud );
592 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
593 || strcmp(lud->lud_host, "*") == 0 )
595 err = slap_get_listener_addresses(NULL, port, &sal);
597 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
601 ldap_free_urldesc( lud );
607 while ( *sal != NULL ) {
608 switch( (*sal)->sa_family ) {
621 l.sl_sd = socket( (*sal)->sa_family, SOCK_STREAM, 0);
622 if ( l.sl_sd == AC_SOCKET_INVALID ) {
623 int err = sock_errno();
625 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
626 "slap_open_listener: socket() failed errno=%d (%s)\n",
627 err, sock_errstr(err) ));
629 Debug( LDAP_DEBUG_ANY,
630 "daemon: socket() failed errno=%d (%s)\n", err,
631 sock_errstr(err), 0 );
637 if ( l.sl_sd >= dtblsize ) {
639 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
640 "slap_open_listener: listener descriptor %ld is too great %ld\n",
641 (long)l.sl_sd, (long)dtblsize ));
643 Debug( LDAP_DEBUG_ANY,
644 "daemon: listener descriptor %ld is too great %ld\n",
645 (long) l.sl_sd, (long) dtblsize, 0 );
647 tcp_close( l.sl_sd );
653 if ( (*sal)->sa_family == AF_LOCAL ) {
654 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
659 /* enable address reuse */
661 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
662 (char *) &tmp, sizeof(tmp) );
663 if ( rc == AC_SOCKET_ERROR ) {
664 int err = sock_errno();
666 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
667 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) failed errno %d (%s)\n",
668 (long)l.sl_sd, err, sock_errstr(err) ));
670 Debug( LDAP_DEBUG_ANY,
671 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
672 (long) l.sl_sd, err, sock_errstr(err) );
678 switch( (*sal)->sa_family ) {
680 addrlen = sizeof(struct sockaddr_in);
684 addrlen = sizeof(struct sockaddr_in6);
689 addrlen = sizeof(struct sockaddr_un);
694 if (!bind(l.sl_sd, *sal, addrlen))
698 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
699 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
700 (long)l.sl_sd, err, sock_errstr(err) ));
702 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
703 (long) l.sl_sd, err, sock_errstr(err) );
705 tcp_close( l.sl_sd );
707 } /* while ( *sal != NULL ) */
709 if ( *sal == NULL ) {
711 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
712 "slap_open_listener: bind(%ld) failed.\n", (long)l.sl_sd ));
714 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
715 (long) l.sl_sd, 0, 0 );
717 slap_free_listener_addresses(psal);
721 switch ( (*sal)->sa_family ) {
724 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
725 if ( chmod( addr, S_IRWXU ) < 0 ) {
726 int err = sock_errno();
728 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
729 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
730 (long)l.sl_sd, err, sock_errstr(err) ));
732 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
733 (long) l.sl_sd, err, sock_errstr(err) );
735 tcp_close( l.sl_sd );
736 slap_free_listener_addresses(psal);
739 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
740 sprintf( l.sl_name, "PATH=%s", addr );
742 #endif /* LDAP_PF_LOCAL */
746 #if defined( HAVE_GETADDRINFO ) && defined( INET_NTOP )
747 char addr[INET_ADDRSTRLEN];
748 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
749 addr, sizeof(addr) );
751 port = ((struct sockaddr_in *)*sal) ->sin_port;
753 s = inet_ntoa( l.sl_addr.sin_addr );
754 port = l.sl_addr.sin_port;
756 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
757 sprintf( l.sl_name, "IP=%s:%d",
758 s != NULL ? s : "unknown" , port );
763 char addr[INET6_ADDRSTRLEN];
764 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
766 port = ((struct sockaddr_in6 *)*sal)->sin6_port;
767 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
768 sprintf( l.sl_name, "IP=%s %d", addr, port );
770 #endif /* LDAP_PF_INET6 */
774 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
775 "slap_open_listener: unsupported address family (%d)\n",
776 (int)(*sal)->sa_family ));
778 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
779 (int) (*sal)->sa_family, 0, 0 );
784 slap_free_listener_addresses(psal);
786 l.sl_url = ch_strdup( url );
787 li = ch_malloc( sizeof( Listener ) );
791 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
792 "slap_open_listener: daemon initialzed %s\n", l.sl_url ));
794 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
800 static int sockinit(void);
801 static int sockdestroy(void);
803 int slapd_daemon_init( const char *urls )
809 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
810 "slapd_daemon_init: %s\n",
811 urls ? urls : "<null>" ));
813 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
814 urls ? urls : "<null>", 0, 0 );
816 if( (rc = sockinit()) != 0 ) {
821 dtblsize = sysconf( _SC_OPEN_MAX );
822 #elif HAVE_GETDTABLESIZE
823 dtblsize = getdtablesize();
825 dtblsize = FD_SETSIZE;
829 if(dtblsize > FD_SETSIZE) {
830 dtblsize = FD_SETSIZE;
832 #endif /* !FD_SETSIZE */
834 /* open a pipe (or something equivalent connected to itself).
835 * we write a byte on this fd whenever we catch a signal. The main
836 * loop will be select'ing on this socket, and will wake up when
839 if( (rc = lutil_pair( wake_sds )) < 0 ) {
841 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
842 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
844 Debug( LDAP_DEBUG_ANY,
845 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
850 FD_ZERO( &slap_daemon.sd_readers );
851 FD_ZERO( &slap_daemon.sd_writers );
857 u = str2charray( urls, " " );
859 if( u == NULL || u[0] == NULL ) {
861 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
862 "slap_daemon_init: no urls (%s) provided.\n", urls ));
864 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
870 for( i=0; u[i] != NULL; i++ ) {
872 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
873 "slap_daemon_init: listen on %s\n.", u[i] ));
875 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
882 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
883 "slap_daemon_init: no listeners to open (%s)\n", urls ));
885 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
893 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
894 "slap_daemon_init: %d listeners to open...\n", i ));
896 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
899 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
901 for(i = 0; u[i] != NULL; i++ ) {
902 slap_listeners[i] = slap_open_listener( u[i] );
904 if( slap_listeners[i] == NULL ) {
909 slap_listeners[i] = NULL;
912 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
913 "slap_daemon_init: %d listeners opened\n", i ));
915 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
920 slapd_slp_init( urls );
925 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
931 slapd_daemon_destroy(void)
933 connections_destroy();
934 tcp_close( wake_sds[1] );
935 tcp_close( wake_sds[0] );
953 time_t last_idle_check = slap_get_time();
956 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
957 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
960 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
961 int err = sock_errno();
963 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
964 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
965 slap_listeners[l]->sl_url, err, sock_errstr(err) ));
967 Debug( LDAP_DEBUG_ANY,
968 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
969 slap_listeners[l]->sl_url, err,
975 slapd_add( slap_listeners[l]->sl_sd );
978 #ifdef HAVE_NT_SERVICE_MANAGER
979 if ( started_event != NULL ) {
980 ldap_pvt_thread_cond_signal( &started_event );
983 /* initialization complete. Here comes the loop. */
985 while ( !slapd_shutdown ) {
990 #define SLAPD_EBADF_LIMIT 16
993 #define SLAPD_IDLE_CHECK_LIMIT 4
994 time_t now = slap_get_time();
1001 #if defined(SLAPD_RLOOKUPS)
1004 struct timeval zero;
1005 struct timeval *tvp;
1007 if( global_idletimeout > 0 && difftime(
1008 last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
1011 connections_timeout_idle(now);
1014 FD_ZERO( &writefds );
1015 FD_ZERO( &readfds );
1020 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1022 #ifdef FD_SET_MANUAL_COPY
1023 for( s = 0; s < nfds; s++ ) {
1024 if(FD_ISSET( &slap_sd_readers, s )) {
1025 FD_SET( s, &readfds );
1027 if(FD_ISSET( &slap_sd_writers, s )) {
1028 FD_SET( s, &writefds );
1032 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1033 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1035 assert(!FD_ISSET(wake_sds[0], &readfds));
1036 FD_SET( wake_sds[0], &readfds );
1038 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1039 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1041 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1042 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1045 #ifndef HAVE_WINSOCK
1046 nfds = slap_daemon.sd_nfds;
1051 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1053 at = ldap_pvt_thread_pool_backload(&connection_pool);
1055 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1058 tvp = at ? &zero : NULL;
1061 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1062 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1066 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1067 "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
1068 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
1070 Debug( LDAP_DEBUG_CONNS,
1071 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1072 slap_listeners[l]->sl_sd, at,
1073 tvp == NULL ? "NULL" : "zero" );
1077 switch(ns = select( nfds, &readfds,
1079 /* don't pass empty fd_set */
1080 ( writefds.fd_count > 0 ? &writefds : NULL ),
1086 case -1: { /* failure - try again */
1087 int err = sock_errno();
1091 /* you'd think this would be EBADF */
1092 || err == WSAENOTSOCK
1095 if (++ebadf < SLAPD_EBADF_LIMIT)
1099 if( err != EINTR ) {
1101 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1102 "slapd_daemon_task: select failed (%d): %s\n",
1103 err, sock_errstr(err) ));
1105 Debug( LDAP_DEBUG_CONNS,
1106 "daemon: select failed (%d): %s\n",
1107 err, sock_errstr(err), 0 );
1109 slapd_shutdown = -1;
1114 case 0: /* timeout - let threads run */
1117 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1118 "slapd_daemon_task: select timeout - yielding\n" ));
1120 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1123 ldap_pvt_thread_yield();
1126 default: /* something happened - deal with it */
1127 if( slapd_shutdown ) continue;
1131 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1132 "slapd_daemon_task: activity on %d descriptors\n", ns ));
1134 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1140 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1142 tcp_read( wake_sds[0], c, sizeof(c) );
1149 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1151 socklen_t len = sizeof(from);
1154 char *authid = NULL;
1158 #ifdef LDAP_PF_LOCAL
1159 char peername[MAXPATHLEN + sizeof("PATH=")];
1160 #elif defined(LDAP_PF_INET6)
1161 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1163 char peername[sizeof("IP=255.255.255.255:65336")];
1164 #endif /* LDAP_PF_LOCAL */
1168 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1171 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1174 s = accept( slap_listeners[l]->sl_sd,
1175 (struct sockaddr *) &from, &len );
1176 if ( s == AC_SOCKET_INVALID ) {
1177 int err = sock_errno();
1179 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1180 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1181 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1183 Debug( LDAP_DEBUG_ANY,
1184 "daemon: accept(%ld) failed errno=%d (%s)\n",
1185 (long) slap_listeners[l]->sl_sd, err,
1191 #ifndef HAVE_WINSOCK
1192 /* make sure descriptor number isn't too great */
1193 if ( s >= dtblsize ) {
1195 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1196 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1197 (long)s, (long)dtblsize ));
1199 Debug( LDAP_DEBUG_ANY,
1200 "daemon: %ld beyond descriptor table size %ld\n",
1201 (long) s, (long) dtblsize, 0 );
1209 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1211 /* newly accepted stream should not be in any of the FD SETS */
1212 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1213 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1214 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1216 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1219 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1220 #ifdef LDAP_PF_LOCAL
1221 /* for IPv4 and IPv6 sockets only */
1222 if ( from.sa_addr.sa_family != AF_LOCAL )
1223 #endif /* LDAP_PF_LOCAL */
1228 /* enable keep alives */
1230 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1231 (char *) &tmp, sizeof(tmp) );
1232 if ( rc == AC_SOCKET_ERROR ) {
1233 int err = sock_errno();
1235 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1236 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1237 (long)s, err, sock_errstr(err) ));
1239 Debug( LDAP_DEBUG_ANY,
1240 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1241 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1246 /* enable no delay */
1248 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1249 (char *)&tmp, sizeof(tmp) );
1250 if ( rc == AC_SOCKET_ERROR ) {
1251 int err = sock_errno();
1253 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1254 "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1255 (long)s, err, sock_errstr(err) ));
1257 Debug( LDAP_DEBUG_ANY,
1258 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1259 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1267 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1268 "slapd_daemon_task: new connection on %ld\n", (long)s ));
1270 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1273 switch ( from.sa_addr.sa_family ) {
1274 # ifdef LDAP_PF_LOCAL
1276 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1277 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1279 #endif /* LDAP_PF_LOCAL */
1281 # ifdef LDAP_PF_INET6
1283 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1284 peeraddr = inet_ntoa( *((struct in_addr *)
1285 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1286 sprintf( peername, "IP=%s:%d",
1287 peeraddr != NULL ? peeraddr : "unknown",
1288 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1290 char addr[INET6_ADDRSTRLEN];
1291 sprintf( peername, "IP=%s %d",
1292 inet_ntop( AF_INET6,
1293 &from.sa_in6_addr.sin6_addr,
1294 addr, sizeof addr) ? addr : "unknown",
1295 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1298 # endif /* LDAP_PF_INET6 */
1301 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1302 sprintf( peername, "IP=%s:%d",
1303 peeraddr != NULL ? peeraddr : "unknown",
1304 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1312 if ( ( from.sa_addr.sa_family == AF_INET )
1313 #ifdef LDAP_PF_INET6
1314 || ( from.sa_addr.sa_family == AF_INET6 )
1317 #ifdef SLAPD_RLOOKUPS
1318 # ifdef LDAP_PF_INET6
1319 if ( from.sa_addr.sa_family == AF_INET6 )
1321 (char *)&(from.sa_in6_addr.sin6_addr),
1322 sizeof(from.sa_in6_addr.sin6_addr),
1325 # endif /* LDAP_PF_INET6 */
1327 (char *) &(from.sa_in_addr.sin_addr),
1328 sizeof(from.sa_in_addr.sin_addr),
1330 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1333 #endif /* SLAPD_RLOOKUPS */
1336 if ( !hosts_ctl("slapd",
1337 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1338 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1342 Statslog( LDAP_DEBUG_ANY,
1343 "fd=%ld host access from %s (%s) denied.\n",
1345 dnsname != NULL ? dnsname : "unknown",
1346 peeraddr != NULL ? peeraddr : "unknown",
1351 #endif /* HAVE_TCPD */
1354 id = connection_init(s,
1355 slap_listeners[l]->sl_url,
1356 dnsname != NULL ? dnsname : "unknown",
1358 slap_listeners[l]->sl_name,
1360 slap_listeners[l]->sl_is_tls,
1367 if( authid ) ch_free(authid);
1371 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1372 "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1373 (long)s, peername, slap_listeners[l]->sl_name ));
1375 Debug( LDAP_DEBUG_ANY,
1376 "daemon: connection_init(%ld, %s, %s) failed.\n",
1379 slap_listeners[l]->sl_name );
1385 Statslog( LDAP_DEBUG_STATS,
1386 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1389 slap_listeners[l]->sl_name,
1398 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1399 "slapd_daemon_task: activity on " ));
1401 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1404 for ( i = 0; i < readfds.fd_count; i++ ) {
1406 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1407 " %d%s", readfds.fd_array[i], "r", 0 ));
1409 Debug( LDAP_DEBUG_CONNS, " %d%s",
1410 readfds.fd_array[i], "r", 0 );
1413 for ( i = 0; i < writefds.fd_count; i++ ) {
1415 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1416 " %d%s", writefds.fd_array[i], "w" ));
1418 Debug( LDAP_DEBUG_CONNS, " %d%s",
1419 writefds.fd_array[i], "w", 0 );
1424 for ( i = 0; i < nfds; i++ ) {
1426 int is_listener = 0;
1428 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1429 if ( i == slap_listeners[l]->sl_sd ) {
1434 if ( is_listener ) {
1437 r = FD_ISSET( i, &readfds );
1438 w = FD_ISSET( i, &writefds );
1441 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1443 r ? "r" : "", w ? "w" : "" ));
1445 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1446 r ? "r" : "", w ? "w" : "" );
1452 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1454 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1459 /* loop through the writers */
1461 for ( i = 0; i < writefds.fd_count; i++ )
1463 for ( i = 0; i < nfds; i++ )
1467 int is_listener = 0;
1469 wd = writefds.fd_array[i];
1471 if( ! FD_ISSET( i, &writefds ) ) {
1477 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1478 if ( i == slap_listeners[l]->sl_sd ) {
1483 if ( is_listener ) {
1487 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1488 "slapd_daemon_task: write active on %d\n", wd ));
1490 Debug( LDAP_DEBUG_CONNS,
1491 "daemon: write active on %d\n",
1495 * NOTE: it is possible that the connection was closed
1496 * and that the stream is now inactive.
1497 * connection_write() must valid the stream is still
1501 if ( connection_write( wd ) < 0 ) {
1502 FD_CLR( (unsigned) wd, &readfds );
1508 for ( i = 0; i < readfds.fd_count; i++ )
1510 for ( i = 0; i < nfds; i++ )
1514 int is_listener = 0;
1517 rd = readfds.fd_array[i];
1519 if( ! FD_ISSET( i, &readfds ) ) {
1525 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1526 if ( rd == slap_listeners[l]->sl_sd ) {
1531 if ( is_listener ) {
1536 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1537 "slapd_daemon_task: read activity on %d\n", rd ));
1539 Debug ( LDAP_DEBUG_CONNS,
1540 "daemon: read activity on %d\n", rd, 0, 0 );
1543 * NOTE: it is possible that the connection was closed
1544 * and that the stream is now inactive.
1545 * connection_read() must valid the stream is still
1549 if ( connection_read( rd ) < 0 ) {
1553 ldap_pvt_thread_yield();
1556 if( slapd_shutdown > 0 ) {
1558 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1559 "slapd_daemon_task: shutdown requested and initiated.\n"));
1561 Debug( LDAP_DEBUG_TRACE,
1562 "daemon: shutdown requested and initiated.\n",
1566 } else if ( slapd_shutdown < 0 ) {
1567 #ifdef HAVE_NT_SERVICE_MANAGER
1568 if (slapd_shutdown == -1)
1571 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1572 "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1574 Debug( LDAP_DEBUG_TRACE,
1575 "daemon: shutdown initiated by Service Manager.\n",
1583 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1584 "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1586 Debug( LDAP_DEBUG_TRACE,
1587 "daemon: abnormal condition, shutdown initiated.\n",
1593 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1594 "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1596 Debug( LDAP_DEBUG_TRACE,
1597 "daemon: no active streams, shutdown initiated.\n",
1602 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1603 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1604 #ifdef LDAP_PF_LOCAL
1605 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1606 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1608 #endif /* LDAP_PF_LOCAL */
1609 slapd_close( slap_listeners[l]->sl_sd );
1615 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1616 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1617 ldap_pvt_thread_pool_backload(&connection_pool) ));
1619 Debug( LDAP_DEBUG_ANY,
1620 "slapd shutdown: waiting for %d threads to terminate\n",
1621 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1623 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1629 int slapd_daemon( void )
1635 #define SLAPD_LISTENER_THREAD 1
1636 #if defined( SLAPD_LISTENER_THREAD )
1638 ldap_pvt_thread_t listener_tid;
1640 /* listener as a separate THREAD */
1641 rc = ldap_pvt_thread_create( &listener_tid,
1642 0, slapd_daemon_task, NULL );
1646 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1647 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1649 Debug( LDAP_DEBUG_ANY,
1650 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1655 /* wait for the listener thread to complete */
1656 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1659 /* experimental code */
1660 slapd_daemon_task( NULL );
1669 #if defined( HAVE_WINSOCK2 )
1670 WORD wVersionRequested;
1674 wVersionRequested = MAKEWORD( 2, 0 );
1676 err = WSAStartup( wVersionRequested, &wsaData );
1678 /* Tell the user that we couldn't find a usable */
1683 /* Confirm that the WinSock DLL supports 2.0.*/
1684 /* Note that if the DLL supports versions greater */
1685 /* than 2.0 in addition to 2.0, it will still return */
1686 /* 2.0 in wVersion since that is the version we */
1689 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1690 HIBYTE( wsaData.wVersion ) != 0 )
1692 /* Tell the user that we couldn't find a usable */
1698 /* The WinSock DLL is acceptable. Proceed. */
1699 #elif defined( HAVE_WINSOCK )
1701 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1708 int sockdestroy(void)
1710 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1717 slap_sig_shutdown( int sig )
1720 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1721 "slap_sig_shutdown: signal %d\n", sig ));
1723 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1727 * If the NT Service Manager is controlling the server, we don't
1728 * want SIGBREAK to kill the server. For some strange reason,
1729 * SIGBREAK is generated when a user logs out.
1732 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1733 if (is_NT_Service && sig == SIGBREAK)
1735 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1736 "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1738 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1743 slapd_shutdown = sig;
1747 /* reinstall self */
1748 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1752 slap_sig_wake( int sig )
1756 /* reinstall self */
1757 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1761 void slapd_add_internal(ber_socket_t s) {