2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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;
1174 #define SLAPD_IDLE_CHECK_LIMIT 4
1176 if ( global_idletimeout > 0 ) {
1177 last_idle_check = slap_get_time();
1178 /* Set the select timeout.
1179 * Don't just truncate, preserve the fractions of
1180 * seconds to prevent sleeping for zero time.
1182 idle.tv_sec = global_idletimeout/SLAPD_IDLE_CHECK_LIMIT;
1183 idle.tv_usec = global_idletimeout - idle.tv_sec * SLAPD_IDLE_CHECK_LIMIT;
1184 idle.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
1190 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1191 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1193 #ifdef LDAP_CONNECTIONLESS
1194 /* Since this is connectionless, the data port is the
1195 * listening port. The listen() and accept() calls
1198 if ( slap_listeners[l]->sl_is_udp ) {
1199 slapd_add( slap_listeners[l]->sl_sd, 1 );
1204 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
1205 int err = sock_errno();
1207 #ifdef LDAP_PF_INET6
1208 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
1209 * we are already listening to in6addr_any, then we want to ignore
1210 * this and continue.
1212 if ( err == EADDRINUSE ) {
1214 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
1215 struct sockaddr_in6 sa6;
1217 if ( sa.sin_family == AF_INET &&
1218 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
1219 for ( i = 0 ; i < l; i++ ) {
1220 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
1221 if ( sa6.sin6_family == AF_INET6 &&
1222 !memcmp( &sa6.sin6_addr, &in6addr_any, sizeof(struct in6_addr) ) )
1227 /* We are already listening to in6addr_any */
1229 LDAP_LOG(CONNECTION, WARNING,
1230 "slapd_daemon_task: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n", 0, 0, 0 );
1232 Debug( LDAP_DEBUG_CONNS,
1233 "daemon: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n",
1236 slapd_close( slap_listeners[l]->sl_sd );
1237 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
1244 LDAP_LOG( CONNECTION, ERR,
1245 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
1246 slap_listeners[l]->sl_url.bv_val, err, sock_errstr(err) );
1248 Debug( LDAP_DEBUG_ANY,
1249 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1250 slap_listeners[l]->sl_url.bv_val, err,
1253 return( (void*)-1 );
1256 slapd_add( slap_listeners[l]->sl_sd, 0 );
1259 #ifdef HAVE_NT_SERVICE_MANAGER
1260 if ( started_event != NULL ) {
1261 ldap_pvt_thread_cond_signal( &started_event );
1264 /* initialization complete. Here comes the loop. */
1266 while ( !slapd_shutdown ) {
1271 #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 != NULL ) {
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 */
1455 LDAP_LOG( CONNECTION, DETAIL2,
1456 "slapd_daemon_task: select timeout - yielding\n", 0, 0, 0 );
1458 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1462 ldap_pvt_thread_yield();
1465 default: /* something happened - deal with it */
1466 if( slapd_shutdown ) continue;
1470 LDAP_LOG( CONNECTION, DETAIL2,
1471 "slapd_daemon_task: activity on %d descriptors\n", ns, 0, 0 );
1473 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1479 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1481 tcp_read( wake_sds[0], c, sizeof(c) );
1482 #if defined(NO_THREADS) || defined(HAVE_GNU_PTH)
1488 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1490 socklen_t len = sizeof(from);
1493 struct berval authid = { 0, NULL };
1494 #ifdef SLAPD_RLOOKUPS
1495 char hbuf[NI_MAXHOST];
1498 char *dnsname = NULL;
1499 char *peeraddr = NULL;
1500 #ifdef LDAP_PF_LOCAL
1501 char peername[MAXPATHLEN + sizeof("PATH=")];
1502 #elif defined(LDAP_PF_INET6)
1503 char peername[sizeof(
1504 "IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1506 char peername[sizeof("IP=255.255.255.255:65336")];
1507 #endif /* LDAP_PF_LOCAL */
1511 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1514 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1517 #ifdef LDAP_CONNECTIONLESS
1518 if ( slap_listeners[l]->sl_is_udp ) {
1519 /* The first time we receive a query, we set this
1520 * up as a "connection". It remains open for the life
1523 if ( slap_listeners[l]->sl_is_udp < 2 ) {
1524 id = connection_init(
1525 slap_listeners[l]->sl_sd,
1526 slap_listeners[l], "", "",
1527 CONN_IS_UDP, ssf, NULL );
1528 slap_listeners[l]->sl_is_udp++;
1534 /* Don't need to look at this in the data loops */
1535 FD_CLR( slap_listeners[l]->sl_sd, &readfds );
1536 FD_CLR( slap_listeners[l]->sl_sd, &writefds );
1538 s = accept( slap_listeners[l]->sl_sd,
1539 (struct sockaddr *) &from, &len );
1540 if ( s == AC_SOCKET_INVALID ) {
1541 int err = sock_errno();
1552 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1554 /* Stop listening until an existing session closes */
1555 slap_listeners[l]->sl_is_mute = 1;
1556 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1560 LDAP_LOG( CONNECTION, ERR,
1561 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1562 (long)slap_listeners[l]->sl_sd,
1563 err, sock_errstr(err) );
1565 Debug( LDAP_DEBUG_ANY,
1566 "daemon: accept(%ld) failed errno=%d (%s)\n",
1567 (long) slap_listeners[l]->sl_sd, err,
1570 ldap_pvt_thread_yield();
1574 #ifndef HAVE_WINSOCK
1575 /* make sure descriptor number isn't too great */
1576 if ( s >= dtblsize ) {
1578 LDAP_LOG( CONNECTION, ERR,
1579 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1580 (long)s, (long)dtblsize, 0 );
1582 Debug( LDAP_DEBUG_ANY,
1583 "daemon: %ld beyond descriptor table size %ld\n",
1584 (long) s, (long) dtblsize, 0 );
1588 ldap_pvt_thread_yield();
1594 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1596 /* newly accepted stream should not be in any of the FD SETS */
1597 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1598 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1599 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1601 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1604 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1605 #ifdef LDAP_PF_LOCAL
1606 /* for IPv4 and IPv6 sockets only */
1607 if ( from.sa_addr.sa_family != AF_LOCAL )
1608 #endif /* LDAP_PF_LOCAL */
1613 /* enable keep alives */
1615 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1616 (char *) &tmp, sizeof(tmp) );
1617 if ( rc == AC_SOCKET_ERROR ) {
1618 int err = sock_errno();
1620 LDAP_LOG( CONNECTION, ERR,
1621 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE)"
1622 " failed errno=%d (%s)\n",
1623 (long)s, err, sock_errstr(err) );
1625 Debug( LDAP_DEBUG_ANY,
1626 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1627 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1632 /* enable no delay */
1634 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1635 (char *)&tmp, sizeof(tmp) );
1636 if ( rc == AC_SOCKET_ERROR ) {
1637 int err = sock_errno();
1639 LDAP_LOG( CONNECTION, ERR,
1640 "slapd_daemon_task: setsockopt( %ld, "
1641 "TCP_NODELAY) failed errno=%d (%s)\n",
1642 (long)s, err, sock_errstr(err) );
1644 Debug( LDAP_DEBUG_ANY,
1645 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1646 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1654 LDAP_LOG( CONNECTION, DETAIL1,
1655 "slapd_daemon_task: new connection on %ld\n", (long)s, 0, 0 );
1657 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1660 switch ( from.sa_addr.sa_family ) {
1661 # ifdef LDAP_PF_LOCAL
1663 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1664 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1669 if( getpeereid( s, &uid, &gid ) == 0 ) {
1670 authid.bv_val = ch_malloc(
1671 sizeof("uidnumber=4294967295+gidnumber=4294967295,"
1672 "cn=peercred,cn=external,cn=auth"));
1673 authid.bv_len = sprintf( authid.bv_val,
1674 "uidnumber=%d+gidnumber=%d,"
1675 "cn=peercred,cn=external,cn=auth",
1676 (int) uid, (int) gid);
1681 #endif /* LDAP_PF_LOCAL */
1683 # ifdef LDAP_PF_INET6
1685 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1686 peeraddr = inet_ntoa( *((struct in_addr *)
1687 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1688 sprintf( peername, "IP=%s:%d",
1689 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1690 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1692 char addr[INET6_ADDRSTRLEN];
1694 peeraddr = (char *) inet_ntop( AF_INET6,
1695 &from.sa_in6_addr.sin6_addr,
1696 addr, sizeof addr );
1697 sprintf( peername, "IP=%s %d",
1698 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1699 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1702 # endif /* LDAP_PF_INET6 */
1705 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1706 sprintf( peername, "IP=%s:%d",
1707 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1708 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1716 if ( ( from.sa_addr.sa_family == AF_INET )
1717 #ifdef LDAP_PF_INET6
1718 || ( from.sa_addr.sa_family == AF_INET6 )
1721 #ifdef SLAPD_RLOOKUPS
1722 if ( use_reverse_lookup ) {
1724 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1725 sizeof(hbuf), &herr ) == 0) {
1726 ldap_pvt_str2lower( hbuf );
1732 #endif /* SLAPD_RLOOKUPS */
1735 if ( !hosts_ctl("slapd",
1736 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1737 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1738 SLAP_STRING_UNKNOWN ))
1741 Statslog( LDAP_DEBUG_STATS,
1742 "fd=%ld DENIED from %s (%s)\n",
1744 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1745 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1750 #endif /* HAVE_TCPD */
1753 id = connection_init(s,
1755 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1758 slap_listeners[l]->sl_is_tls ? CONN_IS_TLS : 0,
1763 authid.bv_val ? &authid : NULL );
1765 if( authid.bv_val ) ch_free(authid.bv_val);
1769 LDAP_LOG( CONNECTION, INFO,
1770 "slapd_daemon_task: "
1771 "connection_init(%ld, %s, %s) "
1774 slap_listeners[l]->sl_name.bv_val );
1776 Debug( LDAP_DEBUG_ANY,
1777 "daemon: connection_init(%ld, %s, %s) "
1781 slap_listeners[l]->sl_name.bv_val );
1787 Statslog( LDAP_DEBUG_STATS,
1788 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
1791 slap_listeners[l]->sl_name.bv_val,
1800 LDAP_LOG( CONNECTION, DETAIL2,
1801 "slapd_daemon_task: activity on ", 0, 0, 0 );
1803 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1806 for ( i = 0; i < readfds.fd_count; i++ ) {
1808 LDAP_LOG( CONNECTION, DETAIL2,
1809 " %d%s", readfds.fd_array[i], "r", 0, 0 );
1811 Debug( LDAP_DEBUG_CONNS, " %d%s",
1812 readfds.fd_array[i], "r", 0 );
1815 for ( i = 0; i < writefds.fd_count; i++ ) {
1817 LDAP_LOG( CONNECTION, DETAIL2,
1818 " %d%s", writefds.fd_array[i], "w" , 0 );
1820 Debug( LDAP_DEBUG_CONNS, " %d%s",
1821 writefds.fd_array[i], "w", 0 );
1826 for ( i = 0; i < nfds; i++ ) {
1829 r = FD_ISSET( i, &readfds );
1830 w = FD_ISSET( i, &writefds );
1833 LDAP_LOG( CONNECTION, DETAIL2,
1834 " %d%s%s", i, r ? "r" : "", w ? "w" : "" );
1836 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1837 r ? "r" : "", w ? "w" : "" );
1843 LDAP_LOG( CONNECTION, DETAIL2, "\n", 0, 0, 0 );
1845 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1850 /* loop through the writers */
1852 for ( i = 0; i < writefds.fd_count; i++ )
1854 for ( i = 0; i < nfds; i++ )
1859 wd = writefds.fd_array[i];
1861 if( ! FD_ISSET( i, &writefds ) ) {
1868 LDAP_LOG( CONNECTION, DETAIL2,
1869 "slapd_daemon_task: write active on %d\n", wd, 0, 0 );
1871 Debug( LDAP_DEBUG_CONNS,
1872 "daemon: write active on %d\n",
1876 * NOTE: it is possible that the connection was closed
1877 * and that the stream is now inactive.
1878 * connection_write() must valid the stream is still
1882 if ( connection_write( wd ) < 0 ) {
1883 FD_CLR( (unsigned) wd, &readfds );
1889 for ( i = 0; i < readfds.fd_count; i++ )
1891 for ( i = 0; i < nfds; i++ )
1896 rd = readfds.fd_array[i];
1898 if( ! FD_ISSET( i, &readfds ) ) {
1905 LDAP_LOG( CONNECTION, DETAIL2,
1906 "slapd_daemon_task: read activity on %d\n", rd, 0, 0 );
1908 Debug ( LDAP_DEBUG_CONNS,
1909 "daemon: read activity on %d\n", rd, 0, 0 );
1912 * NOTE: it is possible that the connection was closed
1913 * and that the stream is now inactive.
1914 * connection_read() must valid the stream is still
1918 if ( connection_read( rd ) < 0 ) {
1922 ldap_pvt_thread_yield();
1925 if( slapd_shutdown == 1 ) {
1927 LDAP_LOG( CONNECTION, CRIT,
1928 "slapd_daemon_task: shutdown requested and initiated.\n", 0, 0, 0 );
1930 Debug( LDAP_DEBUG_TRACE,
1931 "daemon: shutdown requested and initiated.\n",
1935 } else if ( slapd_shutdown == 2 ) {
1936 #ifdef HAVE_NT_SERVICE_MANAGER
1938 LDAP_LOG( CONNECTION, CRIT,
1939 "slapd_daemon_task: shutdown initiated by Service Manager.\n",
1942 Debug( LDAP_DEBUG_TRACE,
1943 "daemon: shutdown initiated by Service Manager.\n",
1946 #else /* !HAVE_NT_SERVICE_MANAGER */
1948 LDAP_LOG( CONNECTION, CRIT,
1949 "slapd_daemon_task: abnormal condition, "
1950 "shutdown initiated.\n", 0, 0, 0 );
1952 Debug( LDAP_DEBUG_TRACE,
1953 "daemon: abnormal condition, shutdown initiated.\n",
1956 #endif /* !HAVE_NT_SERVICE_MANAGER */
1959 LDAP_LOG( CONNECTION, CRIT,
1960 "slapd_daemon_task: no active streams, shutdown initiated.\n",
1963 Debug( LDAP_DEBUG_TRACE,
1964 "daemon: no active streams, shutdown initiated.\n",
1969 if( slapd_gentle_shutdown != 2 ) {
1970 close_listeners ( 0 );
1973 free ( slap_listeners );
1974 slap_listeners = NULL;
1976 if( !slapd_gentle_shutdown ) {
1977 slapd_abrupt_shutdown = 1;
1978 connections_shutdown();
1982 LDAP_LOG( CONNECTION, CRIT,
1983 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1984 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1986 Debug( LDAP_DEBUG_ANY,
1987 "slapd shutdown: waiting for %d threads to terminate\n",
1988 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1990 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
1996 int slapd_daemon( void )
2002 #define SLAPD_LISTENER_THREAD 1
2003 #if defined( SLAPD_LISTENER_THREAD )
2005 ldap_pvt_thread_t listener_tid;
2007 /* listener as a separate THREAD */
2008 rc = ldap_pvt_thread_create( &listener_tid,
2009 0, slapd_daemon_task, NULL );
2013 LDAP_LOG( CONNECTION, ERR,
2014 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n",
2017 Debug( LDAP_DEBUG_ANY,
2018 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2023 /* wait for the listener thread to complete */
2024 ldap_pvt_thread_join( listener_tid, (void *) NULL );
2027 /* experimental code */
2028 slapd_daemon_task( NULL );
2037 #if defined( HAVE_WINSOCK2 )
2038 WORD wVersionRequested;
2042 wVersionRequested = MAKEWORD( 2, 0 );
2044 err = WSAStartup( wVersionRequested, &wsaData );
2046 /* Tell the user that we couldn't find a usable */
2051 /* Confirm that the WinSock DLL supports 2.0.*/
2052 /* Note that if the DLL supports versions greater */
2053 /* than 2.0 in addition to 2.0, it will still return */
2054 /* 2.0 in wVersion since that is the version we */
2057 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2058 HIBYTE( wsaData.wVersion ) != 0 )
2060 /* Tell the user that we couldn't find a usable */
2066 /* The WinSock DLL is acceptable. Proceed. */
2067 #elif defined( HAVE_WINSOCK )
2069 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
2076 int sockdestroy(void)
2078 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2085 slap_sig_shutdown( int sig )
2089 LDAP_LOG( CONNECTION, CRIT,
2090 "slap_sig_shutdown: signal %d\n", sig, 0, 0 );
2092 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2097 * If the NT Service Manager is controlling the server, we don't
2098 * want SIGBREAK to kill the server. For some strange reason,
2099 * SIGBREAK is generated when a user logs out.
2102 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
2103 if (is_NT_Service && sig == SIGBREAK)
2108 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0)
2109 slapd_gentle_shutdown = 1;
2116 /* reinstall self */
2117 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2121 slap_sig_wake( int sig )
2125 /* reinstall self */
2126 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2130 void slapd_add_internal(ber_socket_t s, int isactive) {
2131 slapd_add(s, isactive);
2134 Listener ** slapd_get_listeners(void) {
2135 return slap_listeners;