2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
32 #include <ac/socket.h>
33 #include <ac/string.h>
35 #include <ac/unistd.h>
38 #include "ldap_pvt_thread.h"
46 #define SLAP_STRING_UNKNOWN STRING_UNKNOWN
48 int allow_severity = LOG_INFO;
49 int deny_severity = LOG_NOTICE;
50 #else /* ! TCP Wrappers */
51 #define SLAP_STRING_UNKNOWN "unknown"
52 #endif /* ! TCP Wrappers */
56 /* this should go in <ldap.h> as soon as it is accepted */
57 #define LDAPI_MOD_URLEXT "x-mod"
58 #endif /* LDAP_PF_LOCAL */
61 int slap_inet4or6 = AF_UNSPEC;
63 int slap_inet4or6 = AF_INET;
68 ber_socket_t dtblsize;
70 Listener **slap_listeners = NULL;
72 #define SLAPD_LISTEN 10
74 static ber_socket_t wake_sds[2];
77 #if defined(NO_THREADS) || defined(HAVE_GNU_PTH)
79 #define WAKE_LISTENER(w) \
80 ((w && !waking) ? tcp_write( wake_sds[1], "0", 1 ), waking=1 : 0)
82 #define WAKE_LISTENER(w) \
83 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
86 volatile sig_atomic_t slapd_shutdown = 0, slapd_gentle_shutdown = 0;
87 volatile sig_atomic_t slapd_abrupt_shutdown = 0;
89 static struct slap_daemon {
90 ldap_pvt_thread_mutex_t sd_mutex;
92 ber_socket_t sd_nactives;
95 /* In winsock, accept() returns values higher than dtblsize
96 so don't bother with this optimization */
109 * SLP related functions
113 #define LDAP_SRVTYPE_PREFIX "service:ldap://"
114 #define LDAPS_SRVTYPE_PREFIX "service:ldaps://"
115 static char** slapd_srvurls = NULL;
116 static SLPHandle slapd_hslp = 0;
118 void slapd_slp_init( const char* urls ) {
121 slapd_srvurls = ldap_str2charray( urls, " " );
123 if( slapd_srvurls == NULL ) return;
125 /* find and expand INADDR_ANY URLs */
126 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
127 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
128 char *host = ldap_pvt_get_fqdn( NULL );
129 if ( host != NULL ) {
130 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
132 sizeof( LDAP_SRVTYPE_PREFIX ) );
133 strcpy( lutil_strcopy(slapd_srvurls[i],
134 LDAP_SRVTYPE_PREFIX ), host );
139 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
140 char *host = ldap_pvt_get_fqdn( NULL );
141 if ( host != NULL ) {
142 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
144 sizeof( LDAPS_SRVTYPE_PREFIX ) );
145 strcpy( lutil_strcopy(slapd_srvurls[i],
146 LDAPS_SRVTYPE_PREFIX ), host );
153 /* open the SLP handle */
154 SLPOpen( "en", 0, &slapd_hslp );
157 void slapd_slp_deinit() {
158 if( slapd_srvurls == NULL ) return;
160 ldap_charray_free( slapd_srvurls );
161 slapd_srvurls = NULL;
163 /* close the SLP handle */
164 SLPClose( slapd_hslp );
167 void slapd_slp_regreport(
175 void slapd_slp_reg() {
178 if( slapd_srvurls == NULL ) return;
180 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
181 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
182 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
183 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
184 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
188 SLP_LIFETIME_MAXIMUM,
198 void slapd_slp_dereg() {
201 if( slapd_srvurls == NULL ) return;
203 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
204 SLPDereg( slapd_hslp,
210 #endif /* HAVE_SLP */
213 * Add a descriptor to daemon control
215 * If isactive, the descriptor is a live server session and is subject
216 * to idletimeout control. Otherwise, the descriptor is a passive
217 * listener or an outbound client session, and not subject to
220 static void slapd_add(ber_socket_t s, int isactive) {
221 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
223 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
224 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
225 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
228 if (s >= slap_daemon.sd_nfds) {
229 slap_daemon.sd_nfds = s + 1;
234 slap_daemon.sd_nactives++;
237 FD_SET( s, &slap_daemon.sd_actives );
238 FD_SET( s, &slap_daemon.sd_readers );
241 LDAP_LOG( CONNECTION, DETAIL1,
242 "slapd_add: added %ld%s%s\n", (long)s,
243 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
244 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
246 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
248 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
249 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
251 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
255 * Remove the descriptor from daemon control
257 void slapd_remove(ber_socket_t s, int wasactive, int wake) {
258 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
261 slap_daemon.sd_nactives--;
265 LDAP_LOG( CONNECTION, DETAIL1,
266 "slapd_remove: removing %ld%s%s\n", (long) s,
267 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
268 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
270 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
272 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
273 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
275 FD_CLR( s, &slap_daemon.sd_actives );
276 FD_CLR( s, &slap_daemon.sd_readers );
277 FD_CLR( s, &slap_daemon.sd_writers );
279 /* If we ran out of file descriptors, we dropped a listener from
280 * the select() loop. Now that we're removing a session from our
281 * control, we can try to resume a dropped listener to use.
285 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
286 if ( slap_listeners[i]->sl_sd != AC_SOCKET_INVALID ) {
287 if ( slap_listeners[i]->sl_sd == s ) continue;
288 if ( slap_listeners[i]->sl_is_mute ) {
289 slap_listeners[i]->sl_is_mute = 0;
295 /* Walked the entire list without enabling anything; emfile
296 * counter is stale. Reset it.
298 if ( slap_listeners[i] == NULL )
301 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
302 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
305 void slapd_clr_write(ber_socket_t s, int wake) {
306 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
308 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
309 FD_CLR( s, &slap_daemon.sd_writers );
311 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
315 void slapd_set_write(ber_socket_t s, int wake) {
316 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
318 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
319 if (!FD_ISSET(s, &slap_daemon.sd_writers))
320 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
322 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
326 void slapd_clr_read(ber_socket_t s, int wake) {
327 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
329 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
330 FD_CLR( s, &slap_daemon.sd_readers );
332 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
336 void slapd_set_read(ber_socket_t s, int wake) {
337 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
339 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
340 if (!FD_ISSET(s, &slap_daemon.sd_readers))
341 FD_SET( s, &slap_daemon.sd_readers );
343 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
347 static void slapd_close(ber_socket_t s) {
349 LDAP_LOG( CONNECTION, DETAIL1, "slapd_close: closing %ld\n", (long)s, 0, 0);
351 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
357 static void slap_free_listener_addresses(struct sockaddr **sal)
359 struct sockaddr **sap;
365 for (sap = sal; *sap != NULL; sap++) {
372 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
373 static int get_url_perms(
385 for ( i = 0; exts[ i ]; i++ ) {
386 char *type = exts[ i ];
389 if ( type[ 0 ] == '!' ) {
394 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=", sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 ) {
396 + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
400 switch (strlen(value)) {
402 /* skip leading '0' */
403 if ( value[ 0 ] != '0' ) {
409 for ( j = 0; j < 3; j++) {
412 v = value[ j ] - '0';
414 if ( v < 0 || v > 7 ) {
423 for ( j = 1; j < 10; j++ ) {
424 static mode_t m[] = { 0,
425 S_IRUSR, S_IWUSR, S_IXUSR,
426 S_IRGRP, S_IWGRP, S_IXGRP,
427 S_IROTH, S_IWOTH, S_IXOTH
429 static char c[] = "-rwxrwxrwx";
431 if ( value[ j ] == c[ j ] ) {
434 } else if ( value[ j ] != '-' ) {
453 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
455 /* port = 0 indicates AF_LOCAL */
456 static int slap_get_listener_addresses(
459 struct sockaddr ***sal)
461 struct sockaddr **sap;
465 *sal = ch_malloc(2 * sizeof(void *));
471 *sap = ch_malloc(sizeof(struct sockaddr_un));
477 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
479 LDAP_LOG( CONNECTION, INFO,
480 "slap_get_listener_addresses: domain socket path (%s) "
481 "too long in URL\n", host, 0, 0 );
483 Debug( LDAP_DEBUG_ANY,
484 "daemon: domain socket path (%s) too long in URL",
490 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
491 (*sap)->sa_family = AF_LOCAL;
492 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
496 #ifdef HAVE_GETADDRINFO
497 struct addrinfo hints, *res, *sai;
501 memset( &hints, '\0', sizeof(hints) );
502 hints.ai_flags = AI_PASSIVE;
503 hints.ai_socktype = SOCK_STREAM;
504 hints.ai_family = slap_inet4or6;
505 snprintf(serv, sizeof serv, "%d", port);
507 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
509 LDAP_LOG( CONNECTION, INFO,
510 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
511 AC_GAI_STRERROR(err), 0, 0 );
513 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
514 AC_GAI_STRERROR(err), 0, 0);
520 for (n=2; (sai = sai->ai_next) != NULL; n++) {
523 *sal = ch_calloc(n, sizeof(void *));
531 for ( sai=res; sai; sai=sai->ai_next ) {
532 if( sai->ai_addr == NULL ) {
534 LDAP_LOG( CONNECTION, INFO,
535 "slap_get_listener_addresses: "
536 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
538 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
539 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
545 switch (sai->ai_family) {
546 # ifdef LDAP_PF_INET6
548 *sap = ch_malloc(sizeof(struct sockaddr_in6));
553 *(struct sockaddr_in6 *)*sap =
554 *((struct sockaddr_in6 *)sai->ai_addr);
558 *sap = ch_malloc(sizeof(struct sockaddr_in));
563 *(struct sockaddr_in *)*sap =
564 *((struct sockaddr_in *)sai->ai_addr);
572 (*sap)->sa_family = sai->ai_family;
582 struct hostent *he = NULL;
584 if ( host == NULL ) {
585 in.s_addr = htonl(INADDR_ANY);
587 } else if ( !inet_aton( host, &in ) ) {
588 he = gethostbyname( host );
591 LDAP_LOG( CONNECTION, INFO,
592 "slap_get_listener_addresses: invalid host %s\n", host, 0, 0 );
594 Debug( LDAP_DEBUG_ANY,
595 "daemon: invalid host %s", host, 0, 0);
599 for (n = 0; he->h_addr_list[n]; n++) ;
602 *sal = ch_malloc((n+1) * sizeof(void *));
608 for ( i = 0; i<n; i++ ) {
609 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
613 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
614 sap[i]->sa_family = AF_INET;
615 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
617 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, he->h_addr_list[i], sizeof(struct in_addr) );
619 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, &in, sizeof(struct in_addr) );
629 slap_free_listener_addresses(*sal);
633 static int slap_open_listener(
644 int err, addrlen = 0;
645 struct sockaddr **sal, **psal;
646 int socktype = SOCK_STREAM; /* default to COTS */
648 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
653 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
655 rc = ldap_url_parse( url, &lud );
657 if( rc != LDAP_URL_SUCCESS ) {
659 LDAP_LOG( CONNECTION, ERR,
660 "slap_open_listener: listen URL \"%s\" parse error %d\n",
663 Debug( LDAP_DEBUG_ANY,
664 "daemon: listen URL \"%s\" parse error=%d\n",
670 l.sl_url.bv_val = NULL;
674 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
676 LDAP_LOG( CONNECTION, INFO,
677 "slap_open_listener: TLS is not supported (%s)\n", url, 0, 0 );
679 Debug( LDAP_DEBUG_ANY,
680 "daemon: TLS not supported (%s)\n",
683 ldap_free_urldesc( lud );
687 if(! lud->lud_port ) {
688 lud->lud_port = LDAP_PORT;
692 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
694 if(! lud->lud_port ) {
695 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
699 port = (unsigned short) lud->lud_port;
701 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
702 if ( tmp == LDAP_PROTO_IPC ) {
704 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
705 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
707 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
712 LDAP_LOG( CONNECTION, INFO,
713 "slap_open_listener: URL scheme is not supported: %s\n", url, 0, 0 );
715 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
718 ldap_free_urldesc( lud );
722 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
723 || strcmp(lud->lud_host, "*") == 0 )
725 err = slap_get_listener_addresses(NULL, port, &sal);
727 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
730 #ifdef LDAP_CONNECTIONLESS
731 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
734 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
735 if ( lud->lud_exts ) {
736 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
738 l.sl_perms = S_IRWXU | S_IRWXO;
740 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
742 ldap_free_urldesc( lud );
747 /* If we got more than one address returned, we need to make space
748 * for it in the slap_listeners array.
750 for ( num=0; sal[num]; num++ );
753 slap_listeners = ch_realloc( slap_listeners, (*listeners + 1) * sizeof(Listener *) );
757 while ( *sal != NULL ) {
759 switch( (*sal)->sa_family ) {
777 #ifdef LDAP_CONNECTIONLESS
778 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
780 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
781 if ( l.sl_sd == AC_SOCKET_INVALID ) {
782 int err = sock_errno();
784 LDAP_LOG( CONNECTION, ERR,
785 "slap_open_listener: %s socket() failed errno=%d (%s)\n",
786 af, err, sock_errstr(err) );
788 Debug( LDAP_DEBUG_ANY,
789 "daemon: %s socket() failed errno=%d (%s)\n",
790 af, err, sock_errstr(err) );
796 if ( l.sl_sd >= dtblsize ) {
798 LDAP_LOG( CONNECTION, ERR,
799 "slap_open_listener: listener descriptor %ld is too "
800 "great %ld\n", (long)l.sl_sd, (long)dtblsize, 0 );
802 Debug( LDAP_DEBUG_ANY,
803 "daemon: listener descriptor %ld is too great %ld\n",
804 (long) l.sl_sd, (long) dtblsize, 0 );
806 tcp_close( l.sl_sd );
812 if ( (*sal)->sa_family == AF_LOCAL ) {
813 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
818 /* enable address reuse */
820 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
821 (char *) &tmp, sizeof(tmp) );
822 if ( rc == AC_SOCKET_ERROR ) {
823 int err = sock_errno();
825 LDAP_LOG( CONNECTION, INFO,
826 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) "
827 "failed errno %d (%s)\n", (long)l.sl_sd, err,
830 Debug( LDAP_DEBUG_ANY,
831 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
832 (long) l.sl_sd, err, sock_errstr(err) );
838 switch( (*sal)->sa_family ) {
840 addrlen = sizeof(struct sockaddr_in);
845 /* Try to use IPv6 sockets for IPv6 only */
847 rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY,
848 (char *) &tmp, sizeof(tmp) );
849 if ( rc == AC_SOCKET_ERROR ) {
850 int err = sock_errno();
852 LDAP_LOG( CONNECTION, INFO,
853 "slap_open_listener: setsockopt( %ld, IPV6_V6ONLY ) failed errno %d (%s)\n",
854 (long)l.sl_sd, err, sock_errstr(err) );
856 Debug( LDAP_DEBUG_ANY,
857 "slapd(%ld): setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
858 (long) l.sl_sd, err, sock_errstr(err) );
862 addrlen = sizeof(struct sockaddr_in6);
867 addrlen = sizeof(struct sockaddr_un);
872 if (bind(l.sl_sd, *sal, addrlen)) {
875 LDAP_LOG( CONNECTION, INFO,
876 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
877 (long)l.sl_sd, err, sock_errstr(err) );
879 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
880 (long) l.sl_sd, err, sock_errstr(err) );
882 tcp_close( l.sl_sd );
887 switch ( (*sal)->sa_family ) {
890 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
891 #if 0 /* don't muck with socket perms */
892 if ( chmod( addr, l.sl_perms ) < 0 && crit ) {
893 int err = sock_errno();
895 LDAP_LOG( CONNECTION, INFO,
896 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
897 (long)l.sl_sd, err, sock_errstr(err) );
899 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
900 (long) l.sl_sd, err, sock_errstr(err) );
902 tcp_close( l.sl_sd );
903 slap_free_listener_addresses(psal);
907 l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
908 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
909 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
912 #endif /* LDAP_PF_LOCAL */
916 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
917 char addr[INET_ADDRSTRLEN];
918 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
919 addr, sizeof(addr) );
922 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
924 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
925 l.sl_name.bv_val = ber_memalloc( sizeof("IP=255.255.255.255:65535") );
926 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
928 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
929 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
934 char addr[INET6_ADDRSTRLEN];
935 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
937 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
938 l.sl_name.bv_len = strlen(addr) + sizeof("IP= 65535");
939 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
940 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=%s %d",
942 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
944 #endif /* LDAP_PF_INET6 */
948 LDAP_LOG( CONNECTION, INFO,
949 "slap_open_listener: unsupported address family (%d)\n",
950 (int)(*sal)->sa_family, 0, 0 );
952 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
953 (int) (*sal)->sa_family, 0, 0 );
958 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
959 ber_str2bv( url, 0, 1, &l.sl_url);
960 li = ch_malloc( sizeof( Listener ) );
962 slap_listeners[*cur] = li;
966 } /* while ( *sal != NULL ) */
968 slap_free_listener_addresses(psal);
970 if ( l.sl_url.bv_val == NULL )
973 LDAP_LOG( CONNECTION, RESULTS,
974 "slap_open_listener: failed on %s\n", url, 0, 0 );
976 Debug( LDAP_DEBUG_TRACE,
977 "slap_open_listener: failed on %s\n", url, 0, 0 );
983 LDAP_LOG( CONNECTION, RESULTS,
984 "slap_open_listener: daemon initialized %s\n",
985 l.sl_url.bv_val, 0, 0 );
987 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
988 l.sl_url.bv_val, 0, 0 );
993 static int sockinit(void);
994 static int sockdestroy(void);
996 int slapd_daemon_init( const char *urls )
1002 LDAP_LOG( CONNECTION, ARGS,
1003 "slapd_daemon_init: %s\n", urls ? urls : "<null>", 0, 0 );
1005 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1006 urls ? urls : "<null>", 0, 0 );
1008 if( (rc = sockinit()) != 0 ) {
1013 dtblsize = sysconf( _SC_OPEN_MAX );
1014 #elif HAVE_GETDTABLESIZE
1015 dtblsize = getdtablesize();
1017 dtblsize = FD_SETSIZE;
1021 if(dtblsize > FD_SETSIZE) {
1022 dtblsize = FD_SETSIZE;
1024 #endif /* !FD_SETSIZE */
1026 /* open a pipe (or something equivalent connected to itself).
1027 * we write a byte on this fd whenever we catch a signal. The main
1028 * loop will be select'ing on this socket, and will wake up when
1029 * this byte arrives.
1031 if( (rc = lutil_pair( wake_sds )) < 0 ) {
1033 LDAP_LOG( CONNECTION, ERR,
1034 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc, 0, 0);
1036 Debug( LDAP_DEBUG_ANY,
1037 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1042 FD_ZERO( &slap_daemon.sd_readers );
1043 FD_ZERO( &slap_daemon.sd_writers );
1045 if( urls == NULL ) {
1049 u = ldap_str2charray( urls, " " );
1051 if( u == NULL || u[0] == NULL ) {
1053 LDAP_LOG( CONNECTION, ERR,
1054 "slap_daemon_init: no urls (%s) provided.\n", urls, 0, 0 );
1056 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1062 for( i=0; u[i] != NULL; i++ ) {
1064 LDAP_LOG( CONNECTION, DETAIL1,
1065 "slap_daemon_init: listen on %s\n", u[i], 0, 0 );
1067 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1074 LDAP_LOG( CONNECTION, INFO,
1075 "slap_daemon_init: no listeners to open (%s)\n", urls, 0, 0 );
1077 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1080 ldap_charray_free( u );
1085 LDAP_LOG( CONNECTION, INFO,
1086 "slap_daemon_init: %d listeners to open...\n", i, 0, 0 );
1088 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1091 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1093 for(n = 0, j = 0; u[n]; n++ ) {
1094 if ( slap_open_listener( u[n], &i, &j ) ) {
1095 ldap_charray_free( u );
1099 slap_listeners[j] = NULL;
1102 LDAP_LOG( CONNECTION, DETAIL1,
1103 "slap_daemon_init: %d listeners opened\n", i, 0, 0 );
1105 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1110 slapd_slp_init( urls );
1114 ldap_charray_free( u );
1115 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1121 slapd_daemon_destroy(void)
1123 connections_destroy();
1124 tcp_close( wake_sds[1] );
1125 tcp_close( wake_sds[0] );
1144 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1145 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1147 slapd_remove( slap_listeners[l]->sl_sd, 0, 0 );
1148 #ifdef LDAP_PF_LOCAL
1149 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1150 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1152 #endif /* LDAP_PF_LOCAL */
1153 slapd_close( slap_listeners[l]->sl_sd );
1155 if ( slap_listeners[l]->sl_url.bv_val )
1156 ber_memfree( slap_listeners[l]->sl_url.bv_val );
1157 if ( slap_listeners[l]->sl_name.bv_val )
1158 ber_memfree( slap_listeners[l]->sl_name.bv_val );
1159 free ( slap_listeners[l] );
1160 slap_listeners[l] = NULL;
1171 time_t last_idle_check = 0;
1172 struct timeval idle;
1175 #define SLAPD_IDLE_CHECK_LIMIT 4
1177 if ( global_idletimeout > 0 ) {
1178 last_idle_check = slap_get_time();
1179 /* Set the select timeout.
1180 * Don't just truncate, preserve the fractions of
1181 * seconds to prevent sleeping for zero time.
1183 idle.tv_sec = global_idletimeout/SLAPD_IDLE_CHECK_LIMIT;
1184 idle.tv_usec = global_idletimeout - idle.tv_sec * SLAPD_IDLE_CHECK_LIMIT;
1185 idle.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
1191 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1192 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1194 #ifdef LDAP_CONNECTIONLESS
1195 /* Since this is connectionless, the data port is the
1196 * listening port. The listen() and accept() calls
1199 if ( slap_listeners[l]->sl_is_udp ) {
1200 slapd_add( slap_listeners[l]->sl_sd, 1 );
1205 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
1206 int err = sock_errno();
1208 #ifdef LDAP_PF_INET6
1209 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
1210 * we are already listening to in6addr_any, then we want to ignore
1211 * this and continue.
1213 if ( err == EADDRINUSE ) {
1215 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
1216 struct sockaddr_in6 sa6;
1218 if ( sa.sin_family == AF_INET &&
1219 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
1220 for ( i = 0 ; i < l; i++ ) {
1221 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
1222 if ( sa6.sin6_family == AF_INET6 &&
1223 !memcmp( &sa6.sin6_addr, &in6addr_any, sizeof(struct in6_addr) ) )
1228 /* We are already listening to in6addr_any */
1230 LDAP_LOG(CONNECTION, WARNING,
1231 "slapd_daemon_task: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n", 0, 0, 0 );
1233 Debug( LDAP_DEBUG_CONNS,
1234 "daemon: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n",
1237 slapd_close( slap_listeners[l]->sl_sd );
1238 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
1245 LDAP_LOG( CONNECTION, ERR,
1246 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
1247 slap_listeners[l]->sl_url.bv_val, err, sock_errstr(err) );
1249 Debug( LDAP_DEBUG_ANY,
1250 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1251 slap_listeners[l]->sl_url.bv_val, err,
1254 return( (void*)-1 );
1257 slapd_add( slap_listeners[l]->sl_sd, 0 );
1260 #ifdef HAVE_NT_SERVICE_MANAGER
1261 if ( started_event != NULL ) {
1262 ldap_pvt_thread_cond_signal( &started_event );
1265 /* initialization complete. Here comes the loop. */
1267 while ( !slapd_shutdown ) {
1272 #define SLAPD_EBADF_LIMIT 16
1281 struct timeval *tvp;
1283 struct timeval *cat;
1286 now = slap_get_time();
1288 if( ( global_idletimeout > 0 ) &&
1289 difftime( last_idle_check +
1290 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1291 connections_timeout_idle( now );
1292 last_idle_check = now;
1297 if( slapd_gentle_shutdown ) {
1298 ber_socket_t active;
1300 if( slapd_gentle_shutdown == 1 ) {
1301 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
1302 close_listeners( 1 );
1303 global_restrictops |= SLAP_RESTRICT_OP_WRITES;
1304 slapd_gentle_shutdown = 2;
1307 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1308 active = slap_daemon.sd_nactives;
1309 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1317 FD_ZERO( &writefds );
1318 FD_ZERO( &readfds );
1322 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1324 #ifdef FD_SET_MANUAL_COPY
1325 for( s = 0; s < nfds; s++ ) {
1326 if(FD_ISSET( &slap_sd_readers, s )) {
1327 FD_SET( s, &readfds );
1329 if(FD_ISSET( &slap_sd_writers, s )) {
1330 FD_SET( s, &writefds );
1334 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1335 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1337 assert(!FD_ISSET(wake_sds[0], &readfds));
1338 FD_SET( wake_sds[0], &readfds );
1340 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1341 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1343 if ( slap_listeners[l]->sl_is_mute )
1344 FD_CLR( slap_listeners[l]->sl_sd, &readfds );
1346 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1347 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1350 #ifndef HAVE_WINSOCK
1351 nfds = slap_daemon.sd_nfds;
1355 if ( global_idletimeout && slap_daemon.sd_nactives )
1358 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1361 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
1362 && ( tv.tv_sec || tv.tv_usec )
1369 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1370 rtask = ldap_pvt_runqueue_next_sched( &syncrepl_rq, &cat );
1371 while ( cat && cat->tv_sec && cat->tv_sec <= now ) {
1372 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
1373 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
1375 ldap_pvt_runqueue_runtask( &syncrepl_rq, rtask );
1376 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
1377 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1378 ldap_pvt_thread_pool_submit( &connection_pool,
1379 rtask->routine, (void *) rtask );
1380 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1382 rtask = ldap_pvt_runqueue_next_sched( &syncrepl_rq, &cat );
1384 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1386 if ( cat && cat->tv_sec ) {
1387 time_t diff = difftime( cat->tv_sec, now );
1390 if ( tvp == NULL || diff < tv.tv_sec ) {
1397 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1398 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ||
1399 slap_listeners[l]->sl_is_mute )
1403 LDAP_LOG( CONNECTION, DETAIL1,
1404 "slapd_daemon_task: select: listen=%d "
1405 "active_threads=%d tvp=%s\n",
1406 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
1408 Debug( LDAP_DEBUG_CONNS,
1409 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1410 slap_listeners[l]->sl_sd, at,
1411 tvp == NULL ? "NULL" : "zero" );
1415 switch(ns = select( nfds, &readfds,
1417 /* don't pass empty fd_set */
1418 ( writefds.fd_count > 0 ? &writefds : NULL ),
1424 case -1: { /* failure - try again */
1425 int err = sock_errno();
1429 /* you'd think this would be EBADF */
1430 || err == WSAENOTSOCK
1433 if (++ebadf < SLAPD_EBADF_LIMIT)
1437 if( err != EINTR ) {
1439 LDAP_LOG( CONNECTION, INFO,
1440 "slapd_daemon_task: select failed (%d): %s\n",
1441 err, sock_errstr(err), 0 );
1443 Debug( LDAP_DEBUG_CONNS,
1444 "daemon: select failed (%d): %s\n",
1445 err, sock_errstr(err), 0 );
1452 case 0: /* timeout - let threads run */
1454 #ifndef HAVE_YIELDING_SELECT
1456 LDAP_LOG( CONNECTION, DETAIL2,
1457 "slapd_daemon_task: select timeout - yielding\n", 0, 0, 0 );
1459 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1463 ldap_pvt_thread_yield();
1467 default: /* something happened - deal with it */
1468 if( slapd_shutdown ) continue;
1472 LDAP_LOG( CONNECTION, DETAIL2,
1473 "slapd_daemon_task: activity on %d descriptors\n", ns, 0, 0 );
1475 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1481 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1483 tcp_read( wake_sds[0], c, sizeof(c) );
1484 #if defined(NO_THREADS) || defined(HAVE_GNU_PTH)
1490 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1492 socklen_t len = sizeof(from);
1495 struct berval authid = BER_BVNULL;
1496 #ifdef SLAPD_RLOOKUPS
1497 char hbuf[NI_MAXHOST];
1500 char *dnsname = NULL;
1501 char *peeraddr = NULL;
1502 #ifdef LDAP_PF_LOCAL
1503 char peername[MAXPATHLEN + sizeof("PATH=")];
1504 #elif defined(LDAP_PF_INET6)
1505 char peername[sizeof(
1506 "IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1508 char peername[sizeof("IP=255.255.255.255:65336")];
1509 #endif /* LDAP_PF_LOCAL */
1513 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1516 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1519 #ifdef LDAP_CONNECTIONLESS
1520 if ( slap_listeners[l]->sl_is_udp ) {
1521 /* The first time we receive a query, we set this
1522 * up as a "connection". It remains open for the life
1525 if ( slap_listeners[l]->sl_is_udp < 2 ) {
1526 id = connection_init(
1527 slap_listeners[l]->sl_sd,
1528 slap_listeners[l], "", "",
1529 CONN_IS_UDP, ssf, NULL );
1530 slap_listeners[l]->sl_is_udp++;
1536 /* Don't need to look at this in the data loops */
1537 FD_CLR( slap_listeners[l]->sl_sd, &readfds );
1538 FD_CLR( slap_listeners[l]->sl_sd, &writefds );
1540 # ifdef LDAP_PF_LOCAL
1541 /* FIXME: apparently accept doesn't fill
1542 * the sun_path sun_path member */
1543 from.sa_un_addr.sun_path[0] = '\0';
1544 # endif /* LDAP_PF_LOCAL */
1545 s = accept( slap_listeners[l]->sl_sd,
1546 (struct sockaddr *) &from, &len );
1547 if ( s == AC_SOCKET_INVALID ) {
1548 int err = sock_errno();
1559 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1561 /* Stop listening until an existing session closes */
1562 slap_listeners[l]->sl_is_mute = 1;
1563 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1567 LDAP_LOG( CONNECTION, ERR,
1568 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1569 (long)slap_listeners[l]->sl_sd,
1570 err, sock_errstr(err) );
1572 Debug( LDAP_DEBUG_ANY,
1573 "daemon: accept(%ld) failed errno=%d (%s)\n",
1574 (long) slap_listeners[l]->sl_sd, err,
1577 ldap_pvt_thread_yield();
1581 #ifndef HAVE_WINSOCK
1582 /* make sure descriptor number isn't too great */
1583 if ( s >= dtblsize ) {
1585 LDAP_LOG( CONNECTION, ERR,
1586 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1587 (long)s, (long)dtblsize, 0 );
1589 Debug( LDAP_DEBUG_ANY,
1590 "daemon: %ld beyond descriptor table size %ld\n",
1591 (long) s, (long) dtblsize, 0 );
1595 ldap_pvt_thread_yield();
1601 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1603 /* newly accepted stream should not be in any of the FD SETS */
1604 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1605 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1606 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1608 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1611 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1612 #ifdef LDAP_PF_LOCAL
1613 /* for IPv4 and IPv6 sockets only */
1614 if ( from.sa_addr.sa_family != AF_LOCAL )
1615 #endif /* LDAP_PF_LOCAL */
1620 /* enable keep alives */
1622 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1623 (char *) &tmp, sizeof(tmp) );
1624 if ( rc == AC_SOCKET_ERROR ) {
1625 int err = sock_errno();
1627 LDAP_LOG( CONNECTION, ERR,
1628 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE)"
1629 " failed errno=%d (%s)\n",
1630 (long)s, err, sock_errstr(err) );
1632 Debug( LDAP_DEBUG_ANY,
1633 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1634 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1639 /* enable no delay */
1641 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1642 (char *)&tmp, sizeof(tmp) );
1643 if ( rc == AC_SOCKET_ERROR ) {
1644 int err = sock_errno();
1646 LDAP_LOG( CONNECTION, ERR,
1647 "slapd_daemon_task: setsockopt( %ld, "
1648 "TCP_NODELAY) failed errno=%d (%s)\n",
1649 (long)s, err, sock_errstr(err) );
1651 Debug( LDAP_DEBUG_ANY,
1652 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1653 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1661 LDAP_LOG( CONNECTION, DETAIL1,
1662 "slapd_daemon_task: new connection on %ld\n", (long)s, 0, 0 );
1664 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1667 switch ( from.sa_addr.sa_family ) {
1668 # ifdef LDAP_PF_LOCAL
1670 /* FIXME: apparently accept doesn't fill
1671 * the sun_path sun_path member */
1672 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1673 AC_MEMCPY( from.sa_un_addr.sun_path,
1674 slap_listeners[l]->sl_sa.sa_un_addr.sun_path,
1675 sizeof( from.sa_un_addr.sun_path ) );
1678 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1679 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1684 if( getpeereid( s, &uid, &gid ) == 0 ) {
1685 authid.bv_val = ch_malloc(
1686 sizeof("uidnumber=4294967295+gidnumber=4294967295,"
1687 "cn=peercred,cn=external,cn=auth"));
1688 authid.bv_len = sprintf( authid.bv_val,
1689 "uidnumber=%d+gidnumber=%d,"
1690 "cn=peercred,cn=external,cn=auth",
1691 (int) uid, (int) gid);
1696 #endif /* LDAP_PF_LOCAL */
1698 # ifdef LDAP_PF_INET6
1700 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1701 peeraddr = inet_ntoa( *((struct in_addr *)
1702 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1703 sprintf( peername, "IP=%s:%d",
1704 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1705 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1707 char addr[INET6_ADDRSTRLEN];
1709 peeraddr = (char *) inet_ntop( AF_INET6,
1710 &from.sa_in6_addr.sin6_addr,
1711 addr, sizeof addr );
1712 sprintf( peername, "IP=%s %d",
1713 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1714 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1717 # endif /* LDAP_PF_INET6 */
1720 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1721 sprintf( peername, "IP=%s:%d",
1722 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1723 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1731 if ( ( from.sa_addr.sa_family == AF_INET )
1732 #ifdef LDAP_PF_INET6
1733 || ( from.sa_addr.sa_family == AF_INET6 )
1736 #ifdef SLAPD_RLOOKUPS
1737 if ( use_reverse_lookup ) {
1739 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1740 sizeof(hbuf), &herr ) == 0) {
1741 ldap_pvt_str2lower( hbuf );
1747 #endif /* SLAPD_RLOOKUPS */
1750 if ( !hosts_ctl("slapd",
1751 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1752 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1753 SLAP_STRING_UNKNOWN ))
1756 Statslog( LDAP_DEBUG_STATS,
1757 "fd=%ld DENIED from %s (%s)\n",
1759 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1760 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1765 #endif /* HAVE_TCPD */
1768 id = connection_init(s,
1770 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1773 slap_listeners[l]->sl_is_tls ? CONN_IS_TLS : 0,
1778 authid.bv_val ? &authid : NULL );
1780 if( authid.bv_val ) ch_free(authid.bv_val);
1784 LDAP_LOG( CONNECTION, INFO,
1785 "slapd_daemon_task: "
1786 "connection_init(%ld, %s, %s) "
1789 slap_listeners[l]->sl_name.bv_val );
1791 Debug( LDAP_DEBUG_ANY,
1792 "daemon: connection_init(%ld, %s, %s) "
1796 slap_listeners[l]->sl_name.bv_val );
1802 Statslog( LDAP_DEBUG_STATS,
1803 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
1806 slap_listeners[l]->sl_name.bv_val,
1815 LDAP_LOG( CONNECTION, DETAIL2,
1816 "slapd_daemon_task: activity on ", 0, 0, 0 );
1818 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1821 for ( i = 0; i < readfds.fd_count; i++ ) {
1823 LDAP_LOG( CONNECTION, DETAIL2,
1824 " %d%s", readfds.fd_array[i], "r", 0, 0 );
1826 Debug( LDAP_DEBUG_CONNS, " %d%s",
1827 readfds.fd_array[i], "r", 0 );
1830 for ( i = 0; i < writefds.fd_count; i++ ) {
1832 LDAP_LOG( CONNECTION, DETAIL2,
1833 " %d%s", writefds.fd_array[i], "w" , 0 );
1835 Debug( LDAP_DEBUG_CONNS, " %d%s",
1836 writefds.fd_array[i], "w", 0 );
1841 for ( i = 0; i < nfds; i++ ) {
1844 r = FD_ISSET( i, &readfds );
1845 w = FD_ISSET( i, &writefds );
1848 LDAP_LOG( CONNECTION, DETAIL2,
1849 " %d%s%s", i, r ? "r" : "", w ? "w" : "" );
1851 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1852 r ? "r" : "", w ? "w" : "" );
1858 LDAP_LOG( CONNECTION, DETAIL2, "\n", 0, 0, 0 );
1860 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1865 /* loop through the writers */
1867 for ( i = 0; i < writefds.fd_count; i++ )
1869 for ( i = 0; i < nfds; i++ )
1874 wd = writefds.fd_array[i];
1876 if( ! FD_ISSET( i, &writefds ) ) {
1883 LDAP_LOG( CONNECTION, DETAIL2,
1884 "slapd_daemon_task: write active on %d\n", wd, 0, 0 );
1886 Debug( LDAP_DEBUG_CONNS,
1887 "daemon: write active on %d\n",
1891 * NOTE: it is possible that the connection was closed
1892 * and that the stream is now inactive.
1893 * connection_write() must valid the stream is still
1897 if ( connection_write( wd ) < 0 ) {
1898 FD_CLR( (unsigned) wd, &readfds );
1904 for ( i = 0; i < readfds.fd_count; i++ )
1906 for ( i = 0; i < nfds; i++ )
1911 rd = readfds.fd_array[i];
1913 if( ! FD_ISSET( i, &readfds ) ) {
1920 LDAP_LOG( CONNECTION, DETAIL2,
1921 "slapd_daemon_task: read activity on %d\n", rd, 0, 0 );
1923 Debug ( LDAP_DEBUG_CONNS,
1924 "daemon: read activity on %d\n", rd, 0, 0 );
1927 * NOTE: it is possible that the connection was closed
1928 * and that the stream is now inactive.
1929 * connection_read() must valid the stream is still
1933 if ( connection_read( rd ) < 0 ) {
1937 #ifndef HAVE_YIELDING_SELECT
1938 ldap_pvt_thread_yield();
1942 if( slapd_shutdown == 1 ) {
1944 LDAP_LOG( CONNECTION, CRIT,
1945 "slapd_daemon_task: shutdown requested and initiated.\n", 0, 0, 0 );
1947 Debug( LDAP_DEBUG_TRACE,
1948 "daemon: shutdown requested and initiated.\n",
1952 } else if ( slapd_shutdown == 2 ) {
1953 #ifdef HAVE_NT_SERVICE_MANAGER
1955 LDAP_LOG( CONNECTION, CRIT,
1956 "slapd_daemon_task: shutdown initiated by Service Manager.\n",
1959 Debug( LDAP_DEBUG_TRACE,
1960 "daemon: shutdown initiated by Service Manager.\n",
1963 #else /* !HAVE_NT_SERVICE_MANAGER */
1965 LDAP_LOG( CONNECTION, CRIT,
1966 "slapd_daemon_task: abnormal condition, "
1967 "shutdown initiated.\n", 0, 0, 0 );
1969 Debug( LDAP_DEBUG_TRACE,
1970 "daemon: abnormal condition, shutdown initiated.\n",
1973 #endif /* !HAVE_NT_SERVICE_MANAGER */
1976 LDAP_LOG( CONNECTION, CRIT,
1977 "slapd_daemon_task: no active streams, shutdown initiated.\n",
1980 Debug( LDAP_DEBUG_TRACE,
1981 "daemon: no active streams, shutdown initiated.\n",
1986 if( slapd_gentle_shutdown != 2 ) {
1987 close_listeners ( 0 );
1990 if( !slapd_gentle_shutdown ) {
1991 slapd_abrupt_shutdown = 1;
1992 connections_shutdown();
1996 LDAP_LOG( CONNECTION, CRIT,
1997 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1998 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
2000 Debug( LDAP_DEBUG_ANY,
2001 "slapd shutdown: waiting for %d threads to terminate\n",
2002 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
2004 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
2006 free ( slap_listeners );
2007 slap_listeners = NULL;
2013 int slapd_daemon( void )
2019 #define SLAPD_LISTENER_THREAD 1
2020 #if defined( SLAPD_LISTENER_THREAD )
2022 ldap_pvt_thread_t listener_tid;
2024 /* listener as a separate THREAD */
2025 rc = ldap_pvt_thread_create( &listener_tid,
2026 0, slapd_daemon_task, NULL );
2030 LDAP_LOG( CONNECTION, ERR,
2031 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n",
2034 Debug( LDAP_DEBUG_ANY,
2035 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2040 /* wait for the listener thread to complete */
2041 ldap_pvt_thread_join( listener_tid, (void *) NULL );
2044 /* experimental code */
2045 slapd_daemon_task( NULL );
2054 #if defined( HAVE_WINSOCK2 )
2055 WORD wVersionRequested;
2059 wVersionRequested = MAKEWORD( 2, 0 );
2061 err = WSAStartup( wVersionRequested, &wsaData );
2063 /* Tell the user that we couldn't find a usable */
2068 /* Confirm that the WinSock DLL supports 2.0.*/
2069 /* Note that if the DLL supports versions greater */
2070 /* than 2.0 in addition to 2.0, it will still return */
2071 /* 2.0 in wVersion since that is the version we */
2074 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2075 HIBYTE( wsaData.wVersion ) != 0 )
2077 /* Tell the user that we couldn't find a usable */
2083 /* The WinSock DLL is acceptable. Proceed. */
2084 #elif defined( HAVE_WINSOCK )
2086 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
2093 int sockdestroy(void)
2095 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2102 slap_sig_shutdown( int sig )
2106 LDAP_LOG( CONNECTION, CRIT,
2107 "slap_sig_shutdown: signal %d\n", sig, 0, 0 );
2109 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2114 * If the NT Service Manager is controlling the server, we don't
2115 * want SIGBREAK to kill the server. For some strange reason,
2116 * SIGBREAK is generated when a user logs out.
2119 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
2120 if (is_NT_Service && sig == SIGBREAK)
2125 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0)
2126 slapd_gentle_shutdown = 1;
2133 /* reinstall self */
2134 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2138 slap_sig_wake( int sig )
2142 /* reinstall self */
2143 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2147 void slapd_add_internal(ber_socket_t s, int isactive) {
2148 slapd_add(s, isactive);
2151 Listener ** slapd_get_listeners(void) {
2152 return slap_listeners;
2155 void slap_wake_listener()