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 /* externs are frowned upon, but so is NT :-) */
70 extern ldap_pvt_thread_cond_t started_event;
71 extern int is_NT_Service;
77 volatile sig_atomic_t slapd_shutdown = 0;
79 static struct slap_daemon {
80 ldap_pvt_thread_mutex_t sd_mutex;
85 /* In winsock, accept() returns values higher than dtblsize
86 so don't bother with this optimization */
99 * SLP related functions
103 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
104 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
105 static char** slapd_srvurls = NULL;
106 static SLPHandle slapd_hslp = 0;
108 void slapd_slp_init( const char* urls ) {
111 slapd_srvurls = str2charray( urls, " " );
113 if( slapd_srvurls == NULL ) return;
115 /* find and expand INADDR_ANY URLs */
116 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
117 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
118 char *host = ldap_pvt_get_fqdn( NULL );
119 if ( host != NULL ) {
120 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
122 sizeof( LDAP_SRVTYPE_PREFIX ) );
123 strcpy( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX );
124 strcat( slapd_srvurls[i], host );
129 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
130 char *host = ldap_pvt_get_fqdn( NULL );
131 if ( host != NULL ) {
132 slapd_srvurls[i] = (char *) realloc( slapd_srvurls[i],
134 sizeof( LDAPS_SRVTYPE_PREFIX ) );
135 strcpy( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX );
136 strcat( slapd_srvurls[i], host );
143 /* open the SLP handle */
144 SLPOpen( "en", 0, &slapd_hslp );
147 void slapd_slp_deinit() {
148 if( slapd_srvurls == NULL ) return;
150 charray_free( slapd_srvurls );
151 slapd_srvurls = NULL;
153 /* close the SLP handle */
154 SLPClose( slapd_hslp );
157 void slapd_slp_regreport(
165 void slapd_slp_reg() {
168 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
169 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
170 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
171 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
172 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
176 SLP_LIFETIME_MAXIMUM,
186 void slapd_slp_dereg() {
189 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
190 SLPDereg( slapd_hslp,
196 #endif /* HAVE_SLP */
199 * Add a descriptor to daemon control
201 static void slapd_add(ber_socket_t s) {
202 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
204 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
205 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
206 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
209 if (s >= slap_daemon.sd_nfds) {
210 slap_daemon.sd_nfds = s + 1;
214 FD_SET( s, &slap_daemon.sd_actives );
215 FD_SET( s, &slap_daemon.sd_readers );
218 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
219 "slapd_add: added %ld%s%s\n",
221 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
222 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
224 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
226 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
227 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
229 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
233 * Remove the descriptor from daemon control
235 void slapd_remove(ber_socket_t s, int wake) {
236 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
239 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
240 "slapd_remove: removing %ld%s%s\n",
242 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
243 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" ));
245 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
247 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
248 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
250 FD_CLR( s, &slap_daemon.sd_actives );
251 FD_CLR( s, &slap_daemon.sd_readers );
252 FD_CLR( s, &slap_daemon.sd_writers );
254 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
258 void slapd_clr_write(ber_socket_t s, int wake) {
259 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
261 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
262 FD_CLR( s, &slap_daemon.sd_writers );
264 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
268 void slapd_set_write(ber_socket_t s, int wake) {
269 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
271 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
272 if (!FD_ISSET(s, &slap_daemon.sd_writers))
273 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
275 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
279 void slapd_clr_read(ber_socket_t s, int wake) {
280 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
282 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
283 FD_CLR( s, &slap_daemon.sd_readers );
285 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
289 void slapd_set_read(ber_socket_t s, int wake) {
290 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
292 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
293 if (!FD_ISSET(s, &slap_daemon.sd_readers))
294 FD_SET( s, &slap_daemon.sd_readers );
296 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
300 static void slapd_close(ber_socket_t s) {
302 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
303 "slapd_close: closing %ld\n", (long)s ));
305 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
311 static void slap_free_listener_addresses(struct sockaddr **sal)
313 struct sockaddr **sap;
319 for (sap = sal; *sap != NULL; sap++) {
326 /* port = 0 indicates AF_LOCAL */
327 static int slap_get_listener_addresses(
330 struct sockaddr ***sal)
332 struct sockaddr **sap;
336 *sal = ch_malloc(2 * sizeof(void *));
342 *sap = ch_malloc(sizeof(struct sockaddr_un));
348 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
350 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
351 "slap_get_listener_addresses: domain socket path (%s) too long in URL\n",
354 Debug( LDAP_DEBUG_ANY,
355 "daemon: domain socket path (%s) too long in URL",
361 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
362 (*sap)->sa_family = AF_LOCAL;
363 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
367 #ifdef HAVE_GETADDRINFO
368 struct addrinfo hints, *res, *sai;
372 memset( &hints, '\0', sizeof(hints) );
373 hints.ai_flags = AI_PASSIVE;
374 hints.ai_socktype = SOCK_STREAM;
375 hints.ai_family = AF_UNSPEC;
376 snprintf(serv, sizeof serv, "%d", port);
378 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
380 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
381 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
382 AC_GAI_STRERROR(err) ));
384 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
385 AC_GAI_STRERROR(err), 0, 0);
391 for (n=2; (sai = sai->ai_next) != NULL; n++) {
394 *sal = ch_malloc(n * sizeof(void *));
402 for ( sai=res; sai; sai=sai->ai_next ) {
403 if( sai->ai_addr == NULL ) {
405 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
406 "slap_get_listener_addresses: "
407 "getaddrinfo ai_addr is NULL?\n" ));
409 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
410 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
416 switch (sai->ai_family) {
417 # ifdef LDAP_PF_INET6
419 *sap = ch_malloc(sizeof(struct sockaddr_in6));
424 *(struct sockaddr_in6 *)*sap =
425 *((struct sockaddr_in6 *)sai->ai_addr);
429 *sap = ch_malloc(sizeof(struct sockaddr_in));
434 *(struct sockaddr_in *)*sap =
435 *((struct sockaddr_in *)sai->ai_addr);
443 (*sap)->sa_family = sai->ai_family;
453 if ( host == NULL ) {
454 in.s_addr = htonl(INADDR_ANY);
456 } else if ( !inet_aton( host, &in ) ) {
457 struct hostent *he = gethostbyname( host );
460 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
461 "slap_get_listener_addresses: invalid host %s\n",
464 Debug( LDAP_DEBUG_ANY,
465 "daemon: invalid host %s", host, 0, 0);
469 AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
472 *sal = ch_malloc(2 * sizeof(void *));
478 *sap = ch_malloc(sizeof(struct sockaddr_in));
484 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
485 (*sap)->sa_family = AF_INET;
486 ((struct sockaddr_in *)*sap)->sin_port = htons(port);
487 ((struct sockaddr_in *)*sap)->sin_addr = in;
494 slap_free_listener_addresses(*sal);
498 static Listener * slap_open_listener(
506 int err, addrlen = 0;
507 struct sockaddr **sal, **psal;
508 int socktype = SOCK_STREAM; /* default to COTS */
510 rc = ldap_url_parse( url, &lud );
512 if( rc != LDAP_URL_SUCCESS ) {
514 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
515 "slap_open_listener: listen URL \"%s\" parse error %d\n",
518 Debug( LDAP_DEBUG_ANY,
519 "daemon: listen URL \"%s\" parse error=%d\n",
526 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
528 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
529 "slap_open_listener: TLS is not supported (%s)\n",
532 Debug( LDAP_DEBUG_ANY,
533 "daemon: TLS not supported (%s)\n",
536 ldap_free_urldesc( lud );
540 if(! lud->lud_port ) {
541 lud->lud_port = LDAP_PORT;
545 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
547 if(! lud->lud_port ) {
548 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
552 port = (unsigned short) lud->lud_port;
554 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
555 if ( tmp == LDAP_PROTO_IPC ) {
557 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
558 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
560 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
565 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
566 "slap_open_listener: URL scheme is not supported: %s\n",
569 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
572 ldap_free_urldesc( lud );
576 #ifdef LDAP_CONNECTIONLESS
577 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
579 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
580 || strcmp(lud->lud_host, "*") == 0 )
582 err = slap_get_listener_addresses(NULL, port, &sal);
584 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
588 ldap_free_urldesc( lud );
594 while ( *sal != NULL ) {
595 switch( (*sal)->sa_family ) {
608 #ifdef LDAP_CONNECTIONLESS
610 socktype = SOCK_DGRAM;
612 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
613 if ( l.sl_sd == AC_SOCKET_INVALID ) {
614 int err = sock_errno();
616 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
617 "slap_open_listener: socket() failed errno=%d (%s)\n",
618 err, sock_errstr(err) ));
620 Debug( LDAP_DEBUG_ANY,
621 "daemon: socket() failed errno=%d (%s)\n", err,
622 sock_errstr(err), 0 );
628 if ( l.sl_sd >= dtblsize ) {
630 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
631 "slap_open_listener: listener descriptor %ld is too great %ld\n",
632 (long)l.sl_sd, (long)dtblsize ));
634 Debug( LDAP_DEBUG_ANY,
635 "daemon: listener descriptor %ld is too great %ld\n",
636 (long) l.sl_sd, (long) dtblsize, 0 );
638 tcp_close( l.sl_sd );
644 if ( (*sal)->sa_family == AF_LOCAL ) {
645 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
650 /* enable address reuse */
652 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
653 (char *) &tmp, sizeof(tmp) );
654 if ( rc == AC_SOCKET_ERROR ) {
655 int err = sock_errno();
657 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
658 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) failed errno %d (%s)\n",
659 (long)l.sl_sd, err, sock_errstr(err) ));
661 Debug( LDAP_DEBUG_ANY,
662 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
663 (long) l.sl_sd, err, sock_errstr(err) );
669 switch( (*sal)->sa_family ) {
671 addrlen = sizeof(struct sockaddr_in);
675 addrlen = sizeof(struct sockaddr_in6);
680 addrlen = sizeof(struct sockaddr_un);
685 if (!bind(l.sl_sd, *sal, addrlen))
689 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
690 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
691 (long)l.sl_sd, err, sock_errstr(err) ));
693 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
694 (long) l.sl_sd, err, sock_errstr(err) );
696 tcp_close( l.sl_sd );
698 } /* while ( *sal != NULL ) */
700 if ( *sal == NULL ) {
702 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
703 "slap_open_listener: bind(%ld) failed.\n", (long)l.sl_sd ));
705 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
706 (long) l.sl_sd, 0, 0 );
708 slap_free_listener_addresses(psal);
712 switch ( (*sal)->sa_family ) {
715 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
716 if ( chmod( addr, S_IRWXU ) < 0 ) {
717 int err = sock_errno();
719 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
720 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
721 (long)l.sl_sd, err, sock_errstr(err) ));
723 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
724 (long) l.sl_sd, err, sock_errstr(err) );
726 tcp_close( l.sl_sd );
727 slap_free_listener_addresses(psal);
730 l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
731 sprintf( l.sl_name, "PATH=%s", addr );
733 #endif /* LDAP_PF_LOCAL */
737 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
738 char addr[INET_ADDRSTRLEN];
739 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
740 addr, sizeof(addr) );
742 port = ((struct sockaddr_in *)*sal) ->sin_port;
744 s = inet_ntoa( l.sl_addr.sin_addr );
745 port = l.sl_addr.sin_port;
747 l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
748 sprintf( l.sl_name, "IP=%s:%d",
749 s != NULL ? s : "unknown" , port );
754 char addr[INET6_ADDRSTRLEN];
755 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
757 port = ((struct sockaddr_in6 *)*sal)->sin6_port;
758 l.sl_name = ch_malloc( strlen(addr) + sizeof("IP= 65535") );
759 sprintf( l.sl_name, "IP=%s %d", addr, port );
761 #endif /* LDAP_PF_INET6 */
765 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
766 "slap_open_listener: unsupported address family (%d)\n",
767 (int)(*sal)->sa_family ));
769 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
770 (int) (*sal)->sa_family, 0, 0 );
775 slap_free_listener_addresses(psal);
777 l.sl_url = ch_strdup( url );
778 li = ch_malloc( sizeof( Listener ) );
782 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
783 "slap_open_listener: daemon initialzed %s\n", l.sl_url ));
785 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
791 static int sockinit(void);
792 static int sockdestroy(void);
794 int slapd_daemon_init( const char *urls )
800 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
801 "slapd_daemon_init: %s\n",
802 urls ? urls : "<null>" ));
804 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
805 urls ? urls : "<null>", 0, 0 );
807 if( (rc = sockinit()) != 0 ) {
812 dtblsize = sysconf( _SC_OPEN_MAX );
813 #elif HAVE_GETDTABLESIZE
814 dtblsize = getdtablesize();
816 dtblsize = FD_SETSIZE;
820 if(dtblsize > FD_SETSIZE) {
821 dtblsize = FD_SETSIZE;
823 #endif /* !FD_SETSIZE */
825 /* open a pipe (or something equivalent connected to itself).
826 * we write a byte on this fd whenever we catch a signal. The main
827 * loop will be select'ing on this socket, and will wake up when
830 if( (rc = lutil_pair( wake_sds )) < 0 ) {
832 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
833 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc ));
835 Debug( LDAP_DEBUG_ANY,
836 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
841 FD_ZERO( &slap_daemon.sd_readers );
842 FD_ZERO( &slap_daemon.sd_writers );
848 u = str2charray( urls, " " );
850 if( u == NULL || u[0] == NULL ) {
852 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
853 "slap_daemon_init: no urls (%s) provided.\n", urls ));
855 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
861 for( i=0; u[i] != NULL; i++ ) {
863 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
864 "slap_daemon_init: listen on %s\n.", u[i] ));
866 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
873 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
874 "slap_daemon_init: no listeners to open (%s)\n", urls ));
876 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
884 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
885 "slap_daemon_init: %d listeners to open...\n", i ));
887 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
890 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
892 for(i = 0; u[i] != NULL; i++ ) {
893 slap_listeners[i] = slap_open_listener( u[i] );
895 if( slap_listeners[i] == NULL ) {
900 slap_listeners[i] = NULL;
903 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
904 "slap_daemon_init: %d listeners opened\n", i ));
906 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
911 slapd_slp_init( urls );
916 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
922 slapd_daemon_destroy(void)
924 connections_destroy();
925 tcp_close( wake_sds[1] );
926 tcp_close( wake_sds[0] );
944 time_t last_idle_check = 0;
947 if ( global_idletimeout > 0 ) {
948 last_idle_check = slap_get_time();
950 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
951 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
953 #ifdef LDAP_CONNECTIONLESS
954 /* Since this is connectionless, the data port is the
955 * listening port. The listen() and accept() calls
958 if ( slap_listeners[l]->sl_is_udp )
960 slapd_add( slap_listeners[l]->sl_sd );
965 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
966 int err = sock_errno();
968 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
969 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
970 slap_listeners[l]->sl_url, err, sock_errstr(err) ));
972 Debug( LDAP_DEBUG_ANY,
973 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
974 slap_listeners[l]->sl_url, err,
980 slapd_add( slap_listeners[l]->sl_sd );
983 #ifdef HAVE_NT_SERVICE_MANAGER
984 if ( started_event != NULL ) {
985 ldap_pvt_thread_cond_signal( &started_event );
988 /* initialization complete. Here comes the loop. */
990 while ( !slapd_shutdown ) {
995 #define SLAPD_EBADF_LIMIT 16
999 #define SLAPD_IDLE_CHECK_LIMIT 4
1007 #if defined(SLAPD_RLOOKUPS)
1010 struct timeval zero;
1011 struct timeval *tvp;
1014 now = slap_get_time();
1015 connections_timeout_idle( now );
1017 else if ( global_idletimeout > 0 ) {
1018 now = slap_get_time();
1019 if ( difftime( last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1020 connections_timeout_idle( now );
1024 FD_ZERO( &writefds );
1025 FD_ZERO( &readfds );
1030 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1032 #ifdef FD_SET_MANUAL_COPY
1033 for( s = 0; s < nfds; s++ ) {
1034 if(FD_ISSET( &slap_sd_readers, s )) {
1035 FD_SET( s, &readfds );
1037 if(FD_ISSET( &slap_sd_writers, s )) {
1038 FD_SET( s, &writefds );
1042 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1043 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1045 assert(!FD_ISSET(wake_sds[0], &readfds));
1046 FD_SET( wake_sds[0], &readfds );
1048 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1049 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1051 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1052 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1055 #ifndef HAVE_WINSOCK
1056 nfds = slap_daemon.sd_nfds;
1061 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1063 at = ldap_pvt_thread_pool_backload(&connection_pool);
1065 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
1068 tvp = at ? &zero : NULL;
1071 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1072 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1076 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1077 "slapd_daemon_task: select: listen=%d active_threads=%d tvp=%s\n",
1078 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" ));
1080 Debug( LDAP_DEBUG_CONNS,
1081 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1082 slap_listeners[l]->sl_sd, at,
1083 tvp == NULL ? "NULL" : "zero" );
1087 switch(ns = select( nfds, &readfds,
1089 /* don't pass empty fd_set */
1090 ( writefds.fd_count > 0 ? &writefds : NULL ),
1096 case -1: { /* failure - try again */
1097 int err = sock_errno();
1101 /* you'd think this would be EBADF */
1102 || err == WSAENOTSOCK
1105 if (++ebadf < SLAPD_EBADF_LIMIT)
1109 if( err != EINTR ) {
1111 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1112 "slapd_daemon_task: select failed (%d): %s\n",
1113 err, sock_errstr(err) ));
1115 Debug( LDAP_DEBUG_CONNS,
1116 "daemon: select failed (%d): %s\n",
1117 err, sock_errstr(err), 0 );
1119 slapd_shutdown = -1;
1124 case 0: /* timeout - let threads run */
1127 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1128 "slapd_daemon_task: select timeout - yielding\n" ));
1130 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1133 ldap_pvt_thread_yield();
1136 default: /* something happened - deal with it */
1137 if( slapd_shutdown ) continue;
1141 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1142 "slapd_daemon_task: activity on %d descriptors\n", ns ));
1144 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1150 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1152 tcp_read( wake_sds[0], c, sizeof(c) );
1159 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1161 socklen_t len = sizeof(from);
1164 char *authid = NULL;
1166 char *dnsname = NULL;
1168 #ifdef LDAP_PF_LOCAL
1169 char peername[MAXPATHLEN + sizeof("PATH=")];
1170 #elif defined(LDAP_PF_INET6)
1171 char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1173 char peername[sizeof("IP=255.255.255.255:65336")];
1174 #endif /* LDAP_PF_LOCAL */
1178 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1181 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1184 #ifdef LDAP_CONNECTIONLESS
1185 if ( slap_listeners[l]->sl_is_udp )
1187 /* The first time we receive a query, we set this
1188 * up as a "connection". It remains open for the life
1191 if ( slap_listeners[l]->sl_is_udp < 2 )
1193 id = connection_init(
1194 slap_listeners[l]->sl_sd,
1195 slap_listeners[l]->sl_url, "", "",
1196 slap_listeners[l]->sl_name,
1198 slap_listeners[l]->sl_is_udp++;
1204 s = accept( slap_listeners[l]->sl_sd,
1205 (struct sockaddr *) &from, &len );
1206 if ( s == AC_SOCKET_INVALID ) {
1207 int err = sock_errno();
1210 if( err == EMFILE ) {
1215 if( err == ENFILE ) {
1225 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1226 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1227 (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
1229 Debug( LDAP_DEBUG_ANY,
1230 "daemon: accept(%ld) failed errno=%d (%s)\n",
1231 (long) slap_listeners[l]->sl_sd, err,
1235 /* prevent busy loop */
1237 if( emfile % 4 == 3 ) usleep( 250 );
1239 if( emfile % 8 == 7 ) sleep( 1 );
1243 ldap_pvt_thread_yield();
1248 #ifndef HAVE_WINSOCK
1249 /* make sure descriptor number isn't too great */
1250 if ( s >= dtblsize ) {
1252 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1253 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1254 (long)s, (long)dtblsize ));
1256 Debug( LDAP_DEBUG_ANY,
1257 "daemon: %ld beyond descriptor table size %ld\n",
1258 (long) s, (long) dtblsize, 0 );
1262 ldap_pvt_thread_yield();
1268 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1270 /* newly accepted stream should not be in any of the FD SETS */
1271 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1272 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1273 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1275 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1278 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1279 #ifdef LDAP_PF_LOCAL
1280 /* for IPv4 and IPv6 sockets only */
1281 if ( from.sa_addr.sa_family != AF_LOCAL )
1282 #endif /* LDAP_PF_LOCAL */
1287 /* enable keep alives */
1289 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1290 (char *) &tmp, sizeof(tmp) );
1291 if ( rc == AC_SOCKET_ERROR ) {
1292 int err = sock_errno();
1294 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1295 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE) failed errno=%d (%s)\n",
1296 (long)s, err, sock_errstr(err) ));
1298 Debug( LDAP_DEBUG_ANY,
1299 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1300 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1305 /* enable no delay */
1307 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1308 (char *)&tmp, sizeof(tmp) );
1309 if ( rc == AC_SOCKET_ERROR ) {
1310 int err = sock_errno();
1312 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1313 "slapd_daemon_task: setsockopt( %ld, TCP_NODELAY) failed errno=%d (%s)\n",
1314 (long)s, err, sock_errstr(err) ));
1316 Debug( LDAP_DEBUG_ANY,
1317 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1318 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1326 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1327 "slapd_daemon_task: new connection on %ld\n", (long)s ));
1329 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1332 switch ( from.sa_addr.sa_family ) {
1333 # ifdef LDAP_PF_LOCAL
1335 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1336 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1338 #endif /* LDAP_PF_LOCAL */
1340 # ifdef LDAP_PF_INET6
1342 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1343 peeraddr = inet_ntoa( *((struct in_addr *)
1344 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1345 sprintf( peername, "IP=%s:%d",
1346 peeraddr != NULL ? peeraddr : "unknown",
1347 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1349 char addr[INET6_ADDRSTRLEN];
1350 sprintf( peername, "IP=%s %d",
1351 inet_ntop( AF_INET6,
1352 &from.sa_in6_addr.sin6_addr,
1353 addr, sizeof addr) ? addr : "unknown",
1354 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1357 # endif /* LDAP_PF_INET6 */
1360 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1361 sprintf( peername, "IP=%s:%d",
1362 peeraddr != NULL ? peeraddr : "unknown",
1363 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1371 if ( ( from.sa_addr.sa_family == AF_INET )
1372 #ifdef LDAP_PF_INET6
1373 || ( from.sa_addr.sa_family == AF_INET6 )
1376 #ifdef SLAPD_RLOOKUPS
1377 # ifdef LDAP_PF_INET6
1378 if ( from.sa_addr.sa_family == AF_INET6 )
1380 (char *)&(from.sa_in6_addr.sin6_addr),
1381 sizeof(from.sa_in6_addr.sin6_addr),
1384 # endif /* LDAP_PF_INET6 */
1386 (char *) &(from.sa_in_addr.sin_addr),
1387 sizeof(from.sa_in_addr.sin_addr),
1389 dnsname = hp ? ldap_pvt_str2lower( hp->h_name ) : NULL;
1392 #endif /* SLAPD_RLOOKUPS */
1395 if ( !hosts_ctl("slapd",
1396 dnsname != NULL ? dnsname : STRING_UNKNOWN,
1397 peeraddr != NULL ? peeraddr : STRING_UNKNOWN,
1401 Statslog( LDAP_DEBUG_ANY,
1402 "fd=%ld host access from %s (%s) denied.\n",
1404 dnsname != NULL ? dnsname : "unknown",
1405 peeraddr != NULL ? peeraddr : "unknown",
1410 #endif /* HAVE_TCPD */
1413 id = connection_init(s,
1414 slap_listeners[l]->sl_url,
1415 dnsname != NULL ? dnsname : "unknown",
1417 slap_listeners[l]->sl_name,
1419 slap_listeners[l]->sl_is_tls,
1426 if( authid ) ch_free(authid);
1430 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1431 "slapd_daemon_task: connection_init(%ld, %s, %s) failed.\n",
1432 (long)s, peername, slap_listeners[l]->sl_name ));
1434 Debug( LDAP_DEBUG_ANY,
1435 "daemon: connection_init(%ld, %s, %s) failed.\n",
1438 slap_listeners[l]->sl_name );
1444 Statslog( LDAP_DEBUG_STATS,
1445 "daemon: conn=%ld fd=%ld connection from %s (%s) accepted.\n",
1448 slap_listeners[l]->sl_name,
1457 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1458 "slapd_daemon_task: activity on " ));
1460 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1463 for ( i = 0; i < readfds.fd_count; i++ ) {
1465 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1466 " %d%s", readfds.fd_array[i], "r", 0 ));
1468 Debug( LDAP_DEBUG_CONNS, " %d%s",
1469 readfds.fd_array[i], "r", 0 );
1472 for ( i = 0; i < writefds.fd_count; i++ ) {
1474 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1475 " %d%s", writefds.fd_array[i], "w" ));
1477 Debug( LDAP_DEBUG_CONNS, " %d%s",
1478 writefds.fd_array[i], "w", 0 );
1483 for ( i = 0; i < nfds; i++ ) {
1485 int is_listener = 0;
1487 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1488 if ( i == slap_listeners[l]->sl_sd ) {
1489 #ifdef LDAP_CONNECTIONLESS
1490 /* The listener is the data port. Don't
1493 if (slap_listeners[l]->sl_is_udp)
1500 if ( is_listener ) {
1503 r = FD_ISSET( i, &readfds );
1504 w = FD_ISSET( i, &writefds );
1507 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1509 r ? "r" : "", w ? "w" : "" ));
1511 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1512 r ? "r" : "", w ? "w" : "" );
1518 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2, "\n" ));
1520 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1525 /* loop through the writers */
1527 for ( i = 0; i < writefds.fd_count; i++ )
1529 for ( i = 0; i < nfds; i++ )
1533 int is_listener = 0;
1535 wd = writefds.fd_array[i];
1537 if( ! FD_ISSET( i, &writefds ) ) {
1543 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1544 if ( i == slap_listeners[l]->sl_sd ) {
1545 #ifdef LDAP_CONNECTIONLESS
1546 if (slap_listeners[l]->sl_is_udp)
1553 if ( is_listener ) {
1557 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1558 "slapd_daemon_task: write active on %d\n", wd ));
1560 Debug( LDAP_DEBUG_CONNS,
1561 "daemon: write active on %d\n",
1565 * NOTE: it is possible that the connection was closed
1566 * and that the stream is now inactive.
1567 * connection_write() must valid the stream is still
1571 if ( connection_write( wd ) < 0 ) {
1572 FD_CLR( (unsigned) wd, &readfds );
1578 for ( i = 0; i < readfds.fd_count; i++ )
1580 for ( i = 0; i < nfds; i++ )
1584 int is_listener = 0;
1587 rd = readfds.fd_array[i];
1589 if( ! FD_ISSET( i, &readfds ) ) {
1595 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1596 if ( rd == slap_listeners[l]->sl_sd ) {
1597 #ifdef LDAP_CONNECTIONLESS
1598 if (slap_listeners[l]->sl_is_udp)
1605 if ( is_listener ) {
1610 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL2,
1611 "slapd_daemon_task: read activity on %d\n", rd ));
1613 Debug ( LDAP_DEBUG_CONNS,
1614 "daemon: read activity on %d\n", rd, 0, 0 );
1617 * NOTE: it is possible that the connection was closed
1618 * and that the stream is now inactive.
1619 * connection_read() must valid the stream is still
1623 if ( connection_read( rd ) < 0 ) {
1627 ldap_pvt_thread_yield();
1630 if( slapd_shutdown > 0 ) {
1632 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1633 "slapd_daemon_task: shutdown requested and initiated.\n"));
1635 Debug( LDAP_DEBUG_TRACE,
1636 "daemon: shutdown requested and initiated.\n",
1640 } else if ( slapd_shutdown < 0 ) {
1641 #ifdef HAVE_NT_SERVICE_MANAGER
1642 if (slapd_shutdown == -1)
1645 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1646 "slapd_daemon_task: shutdown initiated by Service Manager.\n"));
1648 Debug( LDAP_DEBUG_TRACE,
1649 "daemon: shutdown initiated by Service Manager.\n",
1657 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1658 "slapd_daemon_task: abnormal condition, shutdown initiated.\n" ));
1660 Debug( LDAP_DEBUG_TRACE,
1661 "daemon: abnormal condition, shutdown initiated.\n",
1667 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1668 "slapd_daemon_task: no active streams, shutdown initiated.\n" ));
1670 Debug( LDAP_DEBUG_TRACE,
1671 "daemon: no active streams, shutdown initiated.\n",
1676 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1677 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1678 #ifdef LDAP_PF_LOCAL
1679 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1680 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1682 #endif /* LDAP_PF_LOCAL */
1683 slapd_close( slap_listeners[l]->sl_sd );
1689 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1690 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1691 ldap_pvt_thread_pool_backload(&connection_pool) ));
1693 Debug( LDAP_DEBUG_ANY,
1694 "slapd shutdown: waiting for %d threads to terminate\n",
1695 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1697 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1703 int slapd_daemon( void )
1709 #define SLAPD_LISTENER_THREAD 1
1710 #if defined( SLAPD_LISTENER_THREAD )
1712 ldap_pvt_thread_t listener_tid;
1714 /* listener as a separate THREAD */
1715 rc = ldap_pvt_thread_create( &listener_tid,
1716 0, slapd_daemon_task, NULL );
1720 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1721 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n", rc ));
1723 Debug( LDAP_DEBUG_ANY,
1724 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
1729 /* wait for the listener thread to complete */
1730 ldap_pvt_thread_join( listener_tid, (void *) NULL );
1733 /* experimental code */
1734 slapd_daemon_task( NULL );
1743 #if defined( HAVE_WINSOCK2 )
1744 WORD wVersionRequested;
1748 wVersionRequested = MAKEWORD( 2, 0 );
1750 err = WSAStartup( wVersionRequested, &wsaData );
1752 /* Tell the user that we couldn't find a usable */
1757 /* Confirm that the WinSock DLL supports 2.0.*/
1758 /* Note that if the DLL supports versions greater */
1759 /* than 2.0 in addition to 2.0, it will still return */
1760 /* 2.0 in wVersion since that is the version we */
1763 if ( LOBYTE( wsaData.wVersion ) != 2 ||
1764 HIBYTE( wsaData.wVersion ) != 0 )
1766 /* Tell the user that we couldn't find a usable */
1772 /* The WinSock DLL is acceptable. Proceed. */
1773 #elif defined( HAVE_WINSOCK )
1775 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
1782 int sockdestroy(void)
1784 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
1791 slap_sig_shutdown( int sig )
1794 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1795 "slap_sig_shutdown: signal %d\n", sig ));
1797 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
1801 * If the NT Service Manager is controlling the server, we don't
1802 * want SIGBREAK to kill the server. For some strange reason,
1803 * SIGBREAK is generated when a user logs out.
1806 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
1807 if (is_NT_Service && sig == SIGBREAK)
1809 LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,
1810 "slap_sig_shutdown: SIGBREAK ignored.\n" ));
1812 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
1817 slapd_shutdown = sig;
1821 /* reinstall self */
1822 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
1826 slap_sig_wake( int sig )
1830 /* reinstall self */
1831 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
1835 void slapd_add_internal(ber_socket_t s) {