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;
117 int slapd_register_slp = 0;
119 void slapd_slp_init( const char* urls ) {
122 slapd_srvurls = ldap_str2charray( urls, " " );
124 if( slapd_srvurls == NULL ) return;
126 /* find and expand INADDR_ANY URLs */
127 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
128 if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
129 char *host = ldap_pvt_get_fqdn( NULL );
130 if ( host != NULL ) {
131 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
133 sizeof( LDAP_SRVTYPE_PREFIX ) );
134 strcpy( lutil_strcopy(slapd_srvurls[i],
135 LDAP_SRVTYPE_PREFIX ), host );
140 } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
141 char *host = ldap_pvt_get_fqdn( NULL );
142 if ( host != NULL ) {
143 slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
145 sizeof( LDAPS_SRVTYPE_PREFIX ) );
146 strcpy( lutil_strcopy(slapd_srvurls[i],
147 LDAPS_SRVTYPE_PREFIX ), host );
154 /* open the SLP handle */
155 SLPOpen( "en", 0, &slapd_hslp );
158 void slapd_slp_deinit() {
159 if( slapd_srvurls == NULL ) return;
161 ldap_charray_free( slapd_srvurls );
162 slapd_srvurls = NULL;
164 /* close the SLP handle */
165 SLPClose( slapd_hslp );
168 void slapd_slp_regreport(
176 void slapd_slp_reg() {
179 if( slapd_srvurls == NULL ) return;
181 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
182 if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
183 sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
184 strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
185 sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
189 SLP_LIFETIME_MAXIMUM,
199 void slapd_slp_dereg() {
202 if( slapd_srvurls == NULL ) return;
204 for( i=0; slapd_srvurls[i] != NULL; i++ ) {
205 SLPDereg( slapd_hslp,
211 #endif /* HAVE_SLP */
214 * Add a descriptor to daemon control
216 * If isactive, the descriptor is a live server session and is subject
217 * to idletimeout control. Otherwise, the descriptor is a passive
218 * listener or an outbound client session, and not subject to
221 static void slapd_add(ber_socket_t s, int isactive) {
222 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
224 assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
225 assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
226 assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
229 if (s >= slap_daemon.sd_nfds) {
230 slap_daemon.sd_nfds = s + 1;
235 slap_daemon.sd_nactives++;
238 FD_SET( s, &slap_daemon.sd_actives );
239 FD_SET( s, &slap_daemon.sd_readers );
242 LDAP_LOG( CONNECTION, DETAIL1,
243 "slapd_add: added %ld%s%s\n", (long)s,
244 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
245 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
247 Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
249 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
250 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
252 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
256 * Remove the descriptor from daemon control
258 void slapd_remove(ber_socket_t s, int wasactive, int wake) {
259 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
262 slap_daemon.sd_nactives--;
266 LDAP_LOG( CONNECTION, DETAIL1,
267 "slapd_remove: removing %ld%s%s\n", (long) s,
268 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
269 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
271 Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
273 FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
274 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
276 FD_CLR( s, &slap_daemon.sd_actives );
277 FD_CLR( s, &slap_daemon.sd_readers );
278 FD_CLR( s, &slap_daemon.sd_writers );
280 /* If we ran out of file descriptors, we dropped a listener from
281 * the select() loop. Now that we're removing a session from our
282 * control, we can try to resume a dropped listener to use.
286 for ( i = 0; slap_listeners[i] != NULL; i++ ) {
287 if ( slap_listeners[i]->sl_sd != AC_SOCKET_INVALID ) {
288 if ( slap_listeners[i]->sl_sd == s ) continue;
289 if ( slap_listeners[i]->sl_is_mute ) {
290 slap_listeners[i]->sl_is_mute = 0;
296 /* Walked the entire list without enabling anything; emfile
297 * counter is stale. Reset it.
299 if ( slap_listeners[i] == NULL )
302 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
303 WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
306 void slapd_clr_write(ber_socket_t s, int wake) {
307 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
309 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
310 FD_CLR( s, &slap_daemon.sd_writers );
312 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
316 void slapd_set_write(ber_socket_t s, int wake) {
317 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
319 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
320 if (!FD_ISSET(s, &slap_daemon.sd_writers))
321 FD_SET( (unsigned) s, &slap_daemon.sd_writers );
323 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
327 void slapd_clr_read(ber_socket_t s, int wake) {
328 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
330 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
331 FD_CLR( s, &slap_daemon.sd_readers );
333 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
337 void slapd_set_read(ber_socket_t s, int wake) {
338 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
340 assert( FD_ISSET( s, &slap_daemon.sd_actives) );
341 if (!FD_ISSET(s, &slap_daemon.sd_readers))
342 FD_SET( s, &slap_daemon.sd_readers );
344 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
348 static void slapd_close(ber_socket_t s) {
350 LDAP_LOG( CONNECTION, DETAIL1, "slapd_close: closing %ld\n", (long)s, 0, 0);
352 Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
358 static void slap_free_listener_addresses(struct sockaddr **sal)
360 struct sockaddr **sap;
366 for (sap = sal; *sap != NULL; sap++) {
373 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
374 static int get_url_perms(
386 for ( i = 0; exts[ i ]; i++ ) {
387 char *type = exts[ i ];
390 if ( type[ 0 ] == '!' ) {
395 if ( strncasecmp( type, LDAPI_MOD_URLEXT "=", sizeof(LDAPI_MOD_URLEXT "=") - 1 ) == 0 ) {
397 + ( sizeof(LDAPI_MOD_URLEXT "=") - 1 );
401 switch (strlen(value)) {
403 /* skip leading '0' */
404 if ( value[ 0 ] != '0' ) {
410 for ( j = 0; j < 3; j++) {
413 v = value[ j ] - '0';
415 if ( v < 0 || v > 7 ) {
424 for ( j = 1; j < 10; j++ ) {
425 static mode_t m[] = { 0,
426 S_IRUSR, S_IWUSR, S_IXUSR,
427 S_IRGRP, S_IWGRP, S_IXGRP,
428 S_IROTH, S_IWOTH, S_IXOTH
430 static char c[] = "-rwxrwxrwx";
432 if ( value[ j ] == c[ j ] ) {
435 } else if ( value[ j ] != '-' ) {
454 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
456 /* port = 0 indicates AF_LOCAL */
457 static int slap_get_listener_addresses(
460 struct sockaddr ***sal)
462 struct sockaddr **sap;
466 *sal = ch_malloc(2 * sizeof(void *));
472 *sap = ch_malloc(sizeof(struct sockaddr_un));
478 (sizeof(((struct sockaddr_un *)*sap)->sun_path) - 1) ) {
480 LDAP_LOG( CONNECTION, INFO,
481 "slap_get_listener_addresses: domain socket path (%s) "
482 "too long in URL\n", host, 0, 0 );
484 Debug( LDAP_DEBUG_ANY,
485 "daemon: domain socket path (%s) too long in URL",
491 (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_un) );
492 (*sap)->sa_family = AF_LOCAL;
493 strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
497 #ifdef HAVE_GETADDRINFO
498 struct addrinfo hints, *res, *sai;
502 memset( &hints, '\0', sizeof(hints) );
503 hints.ai_flags = AI_PASSIVE;
504 hints.ai_socktype = SOCK_STREAM;
505 hints.ai_family = slap_inet4or6;
506 snprintf(serv, sizeof serv, "%d", port);
508 if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
510 LDAP_LOG( CONNECTION, INFO,
511 "slap_get_listener_addresses: getaddrinfo failed: %s\n",
512 AC_GAI_STRERROR(err), 0, 0 );
514 Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
515 AC_GAI_STRERROR(err), 0, 0);
521 for (n=2; (sai = sai->ai_next) != NULL; n++) {
524 *sal = ch_calloc(n, sizeof(void *));
532 for ( sai=res; sai; sai=sai->ai_next ) {
533 if( sai->ai_addr == NULL ) {
535 LDAP_LOG( CONNECTION, INFO,
536 "slap_get_listener_addresses: "
537 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
539 Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
540 "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
546 switch (sai->ai_family) {
547 # ifdef LDAP_PF_INET6
549 *sap = ch_malloc(sizeof(struct sockaddr_in6));
554 *(struct sockaddr_in6 *)*sap =
555 *((struct sockaddr_in6 *)sai->ai_addr);
559 *sap = ch_malloc(sizeof(struct sockaddr_in));
564 *(struct sockaddr_in *)*sap =
565 *((struct sockaddr_in *)sai->ai_addr);
573 (*sap)->sa_family = sai->ai_family;
583 struct hostent *he = NULL;
585 if ( host == NULL ) {
586 in.s_addr = htonl(INADDR_ANY);
588 } else if ( !inet_aton( host, &in ) ) {
589 he = gethostbyname( host );
592 LDAP_LOG( CONNECTION, INFO,
593 "slap_get_listener_addresses: invalid host %s\n", host, 0, 0 );
595 Debug( LDAP_DEBUG_ANY,
596 "daemon: invalid host %s", host, 0, 0);
600 for (n = 0; he->h_addr_list[n]; n++) ;
603 *sal = ch_malloc((n+1) * sizeof(void *));
609 for ( i = 0; i<n; i++ ) {
610 sap[i] = ch_malloc(sizeof(struct sockaddr_in));
614 (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
615 sap[i]->sa_family = AF_INET;
616 ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
618 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, he->h_addr_list[i], sizeof(struct in_addr) );
620 AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, &in, sizeof(struct in_addr) );
630 slap_free_listener_addresses(*sal);
634 static int slap_open_listener(
645 int err, addrlen = 0;
646 struct sockaddr **sal, **psal;
647 int socktype = SOCK_STREAM; /* default to COTS */
649 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
654 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
656 rc = ldap_url_parse( url, &lud );
658 if( rc != LDAP_URL_SUCCESS ) {
660 LDAP_LOG( CONNECTION, ERR,
661 "slap_open_listener: listen URL \"%s\" parse error %d\n",
664 Debug( LDAP_DEBUG_ANY,
665 "daemon: listen URL \"%s\" parse error=%d\n",
671 l.sl_url.bv_val = NULL;
675 if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
677 LDAP_LOG( CONNECTION, INFO,
678 "slap_open_listener: TLS is not supported (%s)\n", url, 0, 0 );
680 Debug( LDAP_DEBUG_ANY,
681 "daemon: TLS not supported (%s)\n",
684 ldap_free_urldesc( lud );
688 if(! lud->lud_port ) {
689 lud->lud_port = LDAP_PORT;
693 l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
695 if(! lud->lud_port ) {
696 lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
700 port = (unsigned short) lud->lud_port;
702 tmp = ldap_pvt_url_scheme2proto(lud->lud_scheme);
703 if ( tmp == LDAP_PROTO_IPC ) {
705 if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
706 err = slap_get_listener_addresses(LDAPI_SOCK, 0, &sal);
708 err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
713 LDAP_LOG( CONNECTION, INFO,
714 "slap_open_listener: URL scheme is not supported: %s\n", url, 0, 0 );
716 Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
719 ldap_free_urldesc( lud );
723 if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
724 || strcmp(lud->lud_host, "*") == 0 )
726 err = slap_get_listener_addresses(NULL, port, &sal);
728 err = slap_get_listener_addresses(lud->lud_host, port, &sal);
731 #ifdef LDAP_CONNECTIONLESS
732 l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
735 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
736 if ( lud->lud_exts ) {
737 err = get_url_perms( lud->lud_exts, &l.sl_perms, &crit );
739 l.sl_perms = S_IRWXU | S_IRWXO;
741 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
743 ldap_free_urldesc( lud );
748 /* If we got more than one address returned, we need to make space
749 * for it in the slap_listeners array.
751 for ( num=0; sal[num]; num++ );
754 slap_listeners = ch_realloc( slap_listeners, (*listeners + 1) * sizeof(Listener *) );
758 while ( *sal != NULL ) {
760 switch( (*sal)->sa_family ) {
778 #ifdef LDAP_CONNECTIONLESS
779 if( l.sl_is_udp ) socktype = SOCK_DGRAM;
781 l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
782 if ( l.sl_sd == AC_SOCKET_INVALID ) {
783 int err = sock_errno();
785 LDAP_LOG( CONNECTION, ERR,
786 "slap_open_listener: %s socket() failed errno=%d (%s)\n",
787 af, err, sock_errstr(err) );
789 Debug( LDAP_DEBUG_ANY,
790 "daemon: %s socket() failed errno=%d (%s)\n",
791 af, err, sock_errstr(err) );
797 if ( l.sl_sd >= dtblsize ) {
799 LDAP_LOG( CONNECTION, ERR,
800 "slap_open_listener: listener descriptor %ld is too "
801 "great %ld\n", (long)l.sl_sd, (long)dtblsize, 0 );
803 Debug( LDAP_DEBUG_ANY,
804 "daemon: listener descriptor %ld is too great %ld\n",
805 (long) l.sl_sd, (long) dtblsize, 0 );
807 tcp_close( l.sl_sd );
813 if ( (*sal)->sa_family == AF_LOCAL ) {
814 unlink ( ((struct sockaddr_un *)*sal)->sun_path );
819 /* enable address reuse */
821 rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
822 (char *) &tmp, sizeof(tmp) );
823 if ( rc == AC_SOCKET_ERROR ) {
824 int err = sock_errno();
826 LDAP_LOG( CONNECTION, INFO,
827 "slap_open_listener: setsockopt( %ld, SO_REUSEADDR ) "
828 "failed errno %d (%s)\n", (long)l.sl_sd, err,
831 Debug( LDAP_DEBUG_ANY,
832 "slapd(%ld): setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
833 (long) l.sl_sd, err, sock_errstr(err) );
839 switch( (*sal)->sa_family ) {
841 addrlen = sizeof(struct sockaddr_in);
846 /* Try to use IPv6 sockets for IPv6 only */
848 rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY,
849 (char *) &tmp, sizeof(tmp) );
850 if ( rc == AC_SOCKET_ERROR ) {
851 int err = sock_errno();
853 LDAP_LOG( CONNECTION, INFO,
854 "slap_open_listener: setsockopt( %ld, IPV6_V6ONLY ) failed errno %d (%s)\n",
855 (long)l.sl_sd, err, sock_errstr(err) );
857 Debug( LDAP_DEBUG_ANY,
858 "slapd(%ld): setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
859 (long) l.sl_sd, err, sock_errstr(err) );
863 addrlen = sizeof(struct sockaddr_in6);
868 addrlen = sizeof(struct sockaddr_un);
873 if (bind(l.sl_sd, *sal, addrlen)) {
876 LDAP_LOG( CONNECTION, INFO,
877 "slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
878 (long)l.sl_sd, err, sock_errstr(err) );
880 Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
881 (long) l.sl_sd, err, sock_errstr(err) );
883 tcp_close( l.sl_sd );
888 switch ( (*sal)->sa_family ) {
891 char *addr = ((struct sockaddr_un *)*sal)->sun_path;
892 #if 0 /* don't muck with socket perms */
893 if ( chmod( addr, l.sl_perms ) < 0 && crit ) {
894 int err = sock_errno();
896 LDAP_LOG( CONNECTION, INFO,
897 "slap_open_listener: fchmod(%ld) failed errno=%d (%s)\n",
898 (long)l.sl_sd, err, sock_errstr(err) );
900 Debug( LDAP_DEBUG_ANY, "daemon: fchmod(%ld) failed errno=%d (%s)",
901 (long) l.sl_sd, err, sock_errstr(err) );
903 tcp_close( l.sl_sd );
904 slap_free_listener_addresses(psal);
908 l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1;
909 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 );
910 snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1,
913 #endif /* LDAP_PF_LOCAL */
917 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
918 char addr[INET_ADDRSTRLEN];
919 inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
920 addr, sizeof(addr) );
923 s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
925 port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
926 l.sl_name.bv_val = ber_memalloc( sizeof("IP=255.255.255.255:65535") );
927 snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"),
929 s != NULL ? s : SLAP_STRING_UNKNOWN, port );
930 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
935 char addr[INET6_ADDRSTRLEN];
936 inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr,
938 port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port );
939 l.sl_name.bv_len = strlen(addr) + sizeof("IP= 65535");
940 l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len );
941 snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=%s %d",
943 l.sl_name.bv_len = strlen( l.sl_name.bv_val );
945 #endif /* LDAP_PF_INET6 */
949 LDAP_LOG( CONNECTION, INFO,
950 "slap_open_listener: unsupported address family (%d)\n",
951 (int)(*sal)->sa_family, 0, 0 );
953 Debug( LDAP_DEBUG_ANY, "daemon: unsupported address family (%d)\n",
954 (int) (*sal)->sa_family, 0, 0 );
959 AC_MEMCPY(&l.sl_sa, *sal, addrlen);
960 ber_str2bv( url, 0, 1, &l.sl_url);
961 li = ch_malloc( sizeof( Listener ) );
963 slap_listeners[*cur] = li;
967 } /* while ( *sal != NULL ) */
969 slap_free_listener_addresses(psal);
971 if ( l.sl_url.bv_val == NULL )
974 LDAP_LOG( CONNECTION, RESULTS,
975 "slap_open_listener: failed on %s\n", url, 0, 0 );
977 Debug( LDAP_DEBUG_TRACE,
978 "slap_open_listener: failed on %s\n", url, 0, 0 );
984 LDAP_LOG( CONNECTION, RESULTS,
985 "slap_open_listener: daemon initialized %s\n",
986 l.sl_url.bv_val, 0, 0 );
988 Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
989 l.sl_url.bv_val, 0, 0 );
994 static int sockinit(void);
995 static int sockdestroy(void);
997 int slapd_daemon_init( const char *urls )
1003 LDAP_LOG( CONNECTION, ARGS,
1004 "slapd_daemon_init: %s\n", urls ? urls : "<null>", 0, 0 );
1006 Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
1007 urls ? urls : "<null>", 0, 0 );
1009 if( (rc = sockinit()) != 0 ) {
1014 dtblsize = sysconf( _SC_OPEN_MAX );
1015 #elif HAVE_GETDTABLESIZE
1016 dtblsize = getdtablesize();
1018 dtblsize = FD_SETSIZE;
1022 if(dtblsize > FD_SETSIZE) {
1023 dtblsize = FD_SETSIZE;
1025 #endif /* !FD_SETSIZE */
1027 /* open a pipe (or something equivalent connected to itself).
1028 * we write a byte on this fd whenever we catch a signal. The main
1029 * loop will be select'ing on this socket, and will wake up when
1030 * this byte arrives.
1032 if( (rc = lutil_pair( wake_sds )) < 0 ) {
1034 LDAP_LOG( CONNECTION, ERR,
1035 "slap_daemon_init: lutil_pair() failed rc=%d\n", rc, 0, 0);
1037 Debug( LDAP_DEBUG_ANY,
1038 "daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
1043 FD_ZERO( &slap_daemon.sd_readers );
1044 FD_ZERO( &slap_daemon.sd_writers );
1046 if( urls == NULL ) {
1050 u = ldap_str2charray( urls, " " );
1052 if( u == NULL || u[0] == NULL ) {
1054 LDAP_LOG( CONNECTION, ERR,
1055 "slap_daemon_init: no urls (%s) provided.\n", urls, 0, 0 );
1057 Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
1063 for( i=0; u[i] != NULL; i++ ) {
1065 LDAP_LOG( CONNECTION, DETAIL1,
1066 "slap_daemon_init: listen on %s\n", u[i], 0, 0 );
1068 Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
1075 LDAP_LOG( CONNECTION, INFO,
1076 "slap_daemon_init: no listeners to open (%s)\n", urls, 0, 0 );
1078 Debug( LDAP_DEBUG_ANY, "daemon_init: no listeners to open (%s)\n",
1081 ldap_charray_free( u );
1086 LDAP_LOG( CONNECTION, INFO,
1087 "slap_daemon_init: %d listeners to open...\n", i, 0, 0 );
1089 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners to open...\n",
1092 slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
1094 for(n = 0, j = 0; u[n]; n++ ) {
1095 if ( slap_open_listener( u[n], &i, &j ) ) {
1096 ldap_charray_free( u );
1100 slap_listeners[j] = NULL;
1103 LDAP_LOG( CONNECTION, DETAIL1,
1104 "slap_daemon_init: %d listeners opened\n", i, 0, 0 );
1106 Debug( LDAP_DEBUG_TRACE, "daemon_init: %d listeners opened\n",
1111 if( slapd_register_slp ) {
1112 slapd_slp_init( urls );
1117 ldap_charray_free( u );
1118 ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
1124 slapd_daemon_destroy(void)
1126 connections_destroy();
1127 tcp_close( wake_sds[1] );
1128 tcp_close( wake_sds[0] );
1132 if( slapd_register_slp ) {
1149 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1150 if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
1152 slapd_remove( slap_listeners[l]->sl_sd, 0, 0 );
1153 #ifdef LDAP_PF_LOCAL
1154 if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
1155 unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
1157 #endif /* LDAP_PF_LOCAL */
1158 slapd_close( slap_listeners[l]->sl_sd );
1160 if ( slap_listeners[l]->sl_url.bv_val )
1161 ber_memfree( slap_listeners[l]->sl_url.bv_val );
1162 if ( slap_listeners[l]->sl_name.bv_val )
1163 ber_memfree( slap_listeners[l]->sl_name.bv_val );
1164 free ( slap_listeners[l] );
1165 slap_listeners[l] = NULL;
1176 time_t last_idle_check = 0;
1177 struct timeval idle;
1179 #define SLAPD_IDLE_CHECK_LIMIT 4
1181 if ( global_idletimeout > 0 ) {
1182 last_idle_check = slap_get_time();
1183 /* Set the select timeout.
1184 * Don't just truncate, preserve the fractions of
1185 * seconds to prevent sleeping for zero time.
1187 idle.tv_sec = global_idletimeout/SLAPD_IDLE_CHECK_LIMIT;
1188 idle.tv_usec = global_idletimeout - idle.tv_sec * SLAPD_IDLE_CHECK_LIMIT;
1189 idle.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
1195 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1196 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1198 #ifdef LDAP_CONNECTIONLESS
1199 /* Since this is connectionless, the data port is the
1200 * listening port. The listen() and accept() calls
1203 if ( slap_listeners[l]->sl_is_udp ) {
1204 slapd_add( slap_listeners[l]->sl_sd, 1 );
1209 if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
1210 int err = sock_errno();
1212 #ifdef LDAP_PF_INET6
1213 /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
1214 * we are already listening to in6addr_any, then we want to ignore
1215 * this and continue.
1217 if ( err == EADDRINUSE ) {
1219 struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
1220 struct sockaddr_in6 sa6;
1222 if ( sa.sin_family == AF_INET &&
1223 sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
1224 for ( i = 0 ; i < l; i++ ) {
1225 sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
1226 if ( sa6.sin6_family == AF_INET6 &&
1227 !memcmp( &sa6.sin6_addr, &in6addr_any, sizeof(struct in6_addr) ) )
1232 /* We are already listening to in6addr_any */
1234 LDAP_LOG(CONNECTION, WARNING,
1235 "slapd_daemon_task: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n", 0, 0, 0 );
1237 Debug( LDAP_DEBUG_CONNS,
1238 "daemon: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n",
1241 slapd_close( slap_listeners[l]->sl_sd );
1242 slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
1249 LDAP_LOG( CONNECTION, ERR,
1250 "slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
1251 slap_listeners[l]->sl_url.bv_val, err, sock_errstr(err) );
1253 Debug( LDAP_DEBUG_ANY,
1254 "daemon: listen(%s, 5) failed errno=%d (%s)\n",
1255 slap_listeners[l]->sl_url.bv_val, err,
1258 return( (void*)-1 );
1261 slapd_add( slap_listeners[l]->sl_sd, 0 );
1264 #ifdef HAVE_NT_SERVICE_MANAGER
1265 if ( started_event != NULL ) {
1266 ldap_pvt_thread_cond_signal( &started_event );
1269 /* initialization complete. Here comes the loop. */
1271 while ( !slapd_shutdown ) {
1276 #define SLAPD_EBADF_LIMIT 16
1286 struct timeval *tvp;
1288 struct timeval *cat;
1291 now = slap_get_time();
1293 if( ( global_idletimeout > 0 ) &&
1294 difftime( last_idle_check +
1295 global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
1296 connections_timeout_idle( now );
1297 last_idle_check = now;
1302 if( slapd_gentle_shutdown ) {
1303 ber_socket_t active;
1305 if( slapd_gentle_shutdown == 1 ) {
1306 Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
1307 close_listeners( 1 );
1308 frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
1309 slapd_gentle_shutdown = 2;
1312 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1313 active = slap_daemon.sd_nactives;
1314 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1322 FD_ZERO( &writefds );
1323 FD_ZERO( &readfds );
1327 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1329 #ifdef FD_SET_MANUAL_COPY
1330 for( s = 0; s < nfds; s++ ) {
1331 if(FD_ISSET( &slap_sd_readers, s )) {
1332 FD_SET( s, &readfds );
1334 if(FD_ISSET( &slap_sd_writers, s )) {
1335 FD_SET( s, &writefds );
1339 AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
1340 AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
1342 assert(!FD_ISSET(wake_sds[0], &readfds));
1343 FD_SET( wake_sds[0], &readfds );
1345 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1346 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1348 if ( slap_listeners[l]->sl_is_mute )
1349 FD_CLR( slap_listeners[l]->sl_sd, &readfds );
1351 if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
1352 FD_SET( slap_listeners[l]->sl_sd, &readfds );
1355 #ifndef HAVE_WINSOCK
1356 nfds = slap_daemon.sd_nfds;
1360 if ( global_idletimeout && slap_daemon.sd_nactives )
1363 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1366 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
1367 && ( tv.tv_sec || tv.tv_usec )
1374 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1375 rtask = ldap_pvt_runqueue_next_sched( &syncrepl_rq, &cat );
1376 while ( cat && cat->tv_sec && cat->tv_sec <= now ) {
1377 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
1378 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
1380 ldap_pvt_runqueue_runtask( &syncrepl_rq, rtask );
1381 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
1382 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1383 ldap_pvt_thread_pool_submit( &connection_pool,
1384 rtask->routine, (void *) rtask );
1385 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
1387 rtask = ldap_pvt_runqueue_next_sched( &syncrepl_rq, &cat );
1389 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1391 if ( cat != NULL ) {
1392 time_t diff = difftime( cat->tv_sec, now );
1395 if ( tvp == NULL || diff < tv.tv_sec ) {
1402 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1403 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ||
1404 slap_listeners[l]->sl_is_mute )
1408 LDAP_LOG( CONNECTION, DETAIL1,
1409 "slapd_daemon_task: select: listen=%d "
1410 "active_threads=%d tvp=%s\n",
1411 slap_listeners[l]->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
1413 Debug( LDAP_DEBUG_CONNS,
1414 "daemon: select: listen=%d active_threads=%d tvp=%s\n",
1415 slap_listeners[l]->sl_sd, at,
1416 tvp == NULL ? "NULL" : "zero" );
1420 switch(ns = select( nfds, &readfds,
1422 /* don't pass empty fd_set */
1423 ( writefds.fd_count > 0 ? &writefds : NULL ),
1429 case -1: { /* failure - try again */
1430 int err = sock_errno();
1434 /* you'd think this would be EBADF */
1435 || err == WSAENOTSOCK
1438 if (++ebadf < SLAPD_EBADF_LIMIT)
1442 if( err != EINTR ) {
1444 LDAP_LOG( CONNECTION, INFO,
1445 "slapd_daemon_task: select failed (%d): %s\n",
1446 err, sock_errstr(err), 0 );
1448 Debug( LDAP_DEBUG_CONNS,
1449 "daemon: select failed (%d): %s\n",
1450 err, sock_errstr(err), 0 );
1457 case 0: /* timeout - let threads run */
1460 LDAP_LOG( CONNECTION, DETAIL2,
1461 "slapd_daemon_task: select timeout - yielding\n", 0, 0, 0 );
1463 Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
1467 ldap_pvt_thread_yield();
1470 default: /* something happened - deal with it */
1471 if( slapd_shutdown ) continue;
1475 LDAP_LOG( CONNECTION, DETAIL2,
1476 "slapd_daemon_task: activity on %d descriptors\n", ns, 0, 0 );
1478 Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
1484 if( FD_ISSET( wake_sds[0], &readfds ) ) {
1486 tcp_read( wake_sds[0], c, sizeof(c) );
1487 #if defined(NO_THREADS) || defined(HAVE_GNU_PTH)
1493 for ( l = 0; slap_listeners[l] != NULL; l++ ) {
1495 socklen_t len = sizeof(from);
1498 struct berval authid = BER_BVNULL;
1499 #ifdef SLAPD_RLOOKUPS
1500 char hbuf[NI_MAXHOST];
1503 char *dnsname = NULL;
1504 char *peeraddr = NULL;
1505 #ifdef LDAP_PF_LOCAL
1506 char peername[MAXPATHLEN + sizeof("PATH=")];
1507 #elif defined(LDAP_PF_INET6)
1508 char peername[sizeof(
1509 "IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
1511 char peername[sizeof("IP=255.255.255.255:65336")];
1512 #endif /* LDAP_PF_LOCAL */
1516 if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
1519 if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
1522 #ifdef LDAP_CONNECTIONLESS
1523 if ( slap_listeners[l]->sl_is_udp ) {
1524 /* The first time we receive a query, we set this
1525 * up as a "connection". It remains open for the life
1528 if ( slap_listeners[l]->sl_is_udp < 2 ) {
1529 id = connection_init(
1530 slap_listeners[l]->sl_sd,
1531 slap_listeners[l], "", "",
1532 CONN_IS_UDP, ssf, NULL );
1533 slap_listeners[l]->sl_is_udp++;
1539 /* Don't need to look at this in the data loops */
1540 FD_CLR( slap_listeners[l]->sl_sd, &readfds );
1541 FD_CLR( slap_listeners[l]->sl_sd, &writefds );
1543 s = accept( slap_listeners[l]->sl_sd,
1544 (struct sockaddr *) &from, &len );
1545 if ( s == AC_SOCKET_INVALID ) {
1546 int err = sock_errno();
1557 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1559 /* Stop listening until an existing session closes */
1560 slap_listeners[l]->sl_is_mute = 1;
1561 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1565 LDAP_LOG( CONNECTION, ERR,
1566 "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
1567 (long)slap_listeners[l]->sl_sd,
1568 err, sock_errstr(err) );
1570 Debug( LDAP_DEBUG_ANY,
1571 "daemon: accept(%ld) failed errno=%d (%s)\n",
1572 (long) slap_listeners[l]->sl_sd, err,
1575 ldap_pvt_thread_yield();
1579 #ifndef HAVE_WINSOCK
1580 /* make sure descriptor number isn't too great */
1581 if ( s >= dtblsize ) {
1583 LDAP_LOG( CONNECTION, ERR,
1584 "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
1585 (long)s, (long)dtblsize, 0 );
1587 Debug( LDAP_DEBUG_ANY,
1588 "daemon: %ld beyond descriptor table size %ld\n",
1589 (long) s, (long) dtblsize, 0 );
1593 ldap_pvt_thread_yield();
1599 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
1601 /* newly accepted stream should not be in any of the FD SETS */
1602 assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
1603 assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
1604 assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
1606 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
1609 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
1610 #ifdef LDAP_PF_LOCAL
1611 /* for IPv4 and IPv6 sockets only */
1612 if ( from.sa_addr.sa_family != AF_LOCAL )
1613 #endif /* LDAP_PF_LOCAL */
1618 /* enable keep alives */
1620 rc = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
1621 (char *) &tmp, sizeof(tmp) );
1622 if ( rc == AC_SOCKET_ERROR ) {
1623 int err = sock_errno();
1625 LDAP_LOG( CONNECTION, ERR,
1626 "slapd_daemon_task: setsockopt( %ld, SO_KEEPALIVE)"
1627 " failed errno=%d (%s)\n",
1628 (long)s, err, sock_errstr(err) );
1630 Debug( LDAP_DEBUG_ANY,
1631 "slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
1632 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1637 /* enable no delay */
1639 rc = setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
1640 (char *)&tmp, sizeof(tmp) );
1641 if ( rc == AC_SOCKET_ERROR ) {
1642 int err = sock_errno();
1644 LDAP_LOG( CONNECTION, ERR,
1645 "slapd_daemon_task: setsockopt( %ld, "
1646 "TCP_NODELAY) failed errno=%d (%s)\n",
1647 (long)s, err, sock_errstr(err) );
1649 Debug( LDAP_DEBUG_ANY,
1650 "slapd(%ld): setsockopt(TCP_NODELAY) failed "
1651 "errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
1659 LDAP_LOG( CONNECTION, DETAIL1,
1660 "slapd_daemon_task: new connection on %ld\n", (long)s, 0, 0 );
1662 Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
1665 switch ( from.sa_addr.sa_family ) {
1666 # ifdef LDAP_PF_LOCAL
1668 /* FIXME: apparently accept doesn't fill
1669 * the sun_path sun_path member */
1670 if ( from.sa_un_addr.sun_path[0] == '\0' ) {
1671 AC_MEMCPY( from.sa_un_addr.sun_path,
1672 slap_listeners[l]->sl_sa.sa_un_addr.sun_path,
1673 sizeof( from.sa_un_addr.sun_path ) );
1676 sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
1677 ssf = LDAP_PVT_SASL_LOCAL_SSF;
1682 if( getpeereid( s, &uid, &gid ) == 0 ) {
1683 authid.bv_val = ch_malloc(
1684 sizeof("uidnumber=4294967295+gidnumber=4294967295,"
1685 "cn=peercred,cn=external,cn=auth"));
1686 authid.bv_len = sprintf( authid.bv_val,
1687 "uidnumber=%d+gidnumber=%d,"
1688 "cn=peercred,cn=external,cn=auth",
1689 (int) uid, (int) gid);
1694 #endif /* LDAP_PF_LOCAL */
1696 # ifdef LDAP_PF_INET6
1698 if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) {
1699 peeraddr = inet_ntoa( *((struct in_addr *)
1700 &from.sa_in6_addr.sin6_addr.s6_addr[12]) );
1701 sprintf( peername, "IP=%s:%d",
1702 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1703 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1705 char addr[INET6_ADDRSTRLEN];
1707 peeraddr = (char *) inet_ntop( AF_INET6,
1708 &from.sa_in6_addr.sin6_addr,
1709 addr, sizeof addr );
1710 sprintf( peername, "IP=%s %d",
1711 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1712 (unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
1715 # endif /* LDAP_PF_INET6 */
1718 peeraddr = inet_ntoa( from.sa_in_addr.sin_addr );
1719 sprintf( peername, "IP=%s:%d",
1720 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1721 (unsigned) ntohs( from.sa_in_addr.sin_port ) );
1729 if ( ( from.sa_addr.sa_family == AF_INET )
1730 #ifdef LDAP_PF_INET6
1731 || ( from.sa_addr.sa_family == AF_INET6 )
1734 #ifdef SLAPD_RLOOKUPS
1735 if ( use_reverse_lookup ) {
1737 if (ldap_pvt_get_hname( (const struct sockaddr *)&from, len, hbuf,
1738 sizeof(hbuf), &herr ) == 0) {
1739 ldap_pvt_str2lower( hbuf );
1745 #endif /* SLAPD_RLOOKUPS */
1748 if ( !hosts_ctl("slapd",
1749 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1750 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1751 SLAP_STRING_UNKNOWN ))
1754 Statslog( LDAP_DEBUG_STATS,
1755 "fd=%ld DENIED from %s (%s)\n",
1757 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1758 peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
1763 #endif /* HAVE_TCPD */
1766 id = connection_init(s,
1768 dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
1771 slap_listeners[l]->sl_is_tls ? CONN_IS_TLS : 0,
1776 authid.bv_val ? &authid : NULL );
1778 if( authid.bv_val ) ch_free(authid.bv_val);
1782 LDAP_LOG( CONNECTION, INFO,
1783 "slapd_daemon_task: "
1784 "connection_init(%ld, %s, %s) "
1787 slap_listeners[l]->sl_name.bv_val );
1789 Debug( LDAP_DEBUG_ANY,
1790 "daemon: connection_init(%ld, %s, %s) "
1794 slap_listeners[l]->sl_name.bv_val );
1800 Statslog( LDAP_DEBUG_STATS,
1801 "conn=%ld fd=%ld ACCEPT from %s (%s)\n",
1804 slap_listeners[l]->sl_name.bv_val,
1813 LDAP_LOG( CONNECTION, DETAIL2,
1814 "slapd_daemon_task: activity on ", 0, 0, 0 );
1816 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
1819 for ( i = 0; i < readfds.fd_count; i++ ) {
1821 LDAP_LOG( CONNECTION, DETAIL2,
1822 " %d%s", readfds.fd_array[i], "r", 0, 0 );
1824 Debug( LDAP_DEBUG_CONNS, " %d%s",
1825 readfds.fd_array[i], "r", 0 );
1828 for ( i = 0; i < writefds.fd_count; i++ ) {
1830 LDAP_LOG( CONNECTION, DETAIL2,
1831 " %d%s", writefds.fd_array[i], "w" , 0 );
1833 Debug( LDAP_DEBUG_CONNS, " %d%s",
1834 writefds.fd_array[i], "w", 0 );
1839 for ( i = 0; i < nfds; i++ ) {
1842 r = FD_ISSET( i, &readfds );
1843 w = FD_ISSET( i, &writefds );
1846 LDAP_LOG( CONNECTION, DETAIL2,
1847 " %d%s%s", i, r ? "r" : "", w ? "w" : "" );
1849 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
1850 r ? "r" : "", w ? "w" : "" );
1856 LDAP_LOG( CONNECTION, DETAIL2, "\n", 0, 0, 0 );
1858 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
1863 /* loop through the writers */
1865 for ( i = 0; i < writefds.fd_count; i++ )
1867 for ( i = 0; i < nfds; i++ )
1872 wd = writefds.fd_array[i];
1874 if( ! FD_ISSET( i, &writefds ) ) {
1881 LDAP_LOG( CONNECTION, DETAIL2,
1882 "slapd_daemon_task: write active on %d\n", wd, 0, 0 );
1884 Debug( LDAP_DEBUG_CONNS,
1885 "daemon: write active on %d\n",
1889 * NOTE: it is possible that the connection was closed
1890 * and that the stream is now inactive.
1891 * connection_write() must valid the stream is still
1895 if ( connection_write( wd ) < 0 ) {
1896 FD_CLR( (unsigned) wd, &readfds );
1902 for ( i = 0; i < readfds.fd_count; i++ )
1904 for ( i = 0; i < nfds; i++ )
1909 rd = readfds.fd_array[i];
1911 if( ! FD_ISSET( i, &readfds ) ) {
1918 LDAP_LOG( CONNECTION, DETAIL2,
1919 "slapd_daemon_task: read activity on %d\n", rd, 0, 0 );
1921 Debug ( LDAP_DEBUG_CONNS,
1922 "daemon: read activity on %d\n", rd, 0, 0 );
1925 * NOTE: it is possible that the connection was closed
1926 * and that the stream is now inactive.
1927 * connection_read() must valid the stream is still
1931 if ( connection_read( rd ) < 0 ) {
1935 ldap_pvt_thread_yield();
1938 if( slapd_shutdown == 1 ) {
1940 LDAP_LOG( CONNECTION, CRIT,
1941 "slapd_daemon_task: shutdown requested and initiated.\n", 0, 0, 0 );
1943 Debug( LDAP_DEBUG_TRACE,
1944 "daemon: shutdown requested and initiated.\n",
1948 } else if ( slapd_shutdown == 2 ) {
1949 #ifdef HAVE_NT_SERVICE_MANAGER
1951 LDAP_LOG( CONNECTION, CRIT,
1952 "slapd_daemon_task: shutdown initiated by Service Manager.\n",
1955 Debug( LDAP_DEBUG_TRACE,
1956 "daemon: shutdown initiated by Service Manager.\n",
1959 #else /* !HAVE_NT_SERVICE_MANAGER */
1961 LDAP_LOG( CONNECTION, CRIT,
1962 "slapd_daemon_task: abnormal condition, "
1963 "shutdown initiated.\n", 0, 0, 0 );
1965 Debug( LDAP_DEBUG_TRACE,
1966 "daemon: abnormal condition, shutdown initiated.\n",
1969 #endif /* !HAVE_NT_SERVICE_MANAGER */
1972 LDAP_LOG( CONNECTION, CRIT,
1973 "slapd_daemon_task: no active streams, shutdown initiated.\n",
1976 Debug( LDAP_DEBUG_TRACE,
1977 "daemon: no active streams, shutdown initiated.\n",
1982 if( slapd_gentle_shutdown != 2 ) {
1983 close_listeners ( 0 );
1986 free ( slap_listeners );
1987 slap_listeners = NULL;
1989 if( !slapd_gentle_shutdown ) {
1990 slapd_abrupt_shutdown = 1;
1991 connections_shutdown();
1995 LDAP_LOG( CONNECTION, CRIT,
1996 "slapd_daemon_task: shutdown waiting for %d threads to terminate.\n",
1997 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
1999 Debug( LDAP_DEBUG_ANY,
2000 "slapd shutdown: waiting for %d threads to terminate\n",
2001 ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
2003 ldap_pvt_thread_pool_destroy(&connection_pool, 1);
2009 int slapd_daemon( void )
2015 #define SLAPD_LISTENER_THREAD 1
2016 #if defined( SLAPD_LISTENER_THREAD )
2018 ldap_pvt_thread_t listener_tid;
2020 /* listener as a separate THREAD */
2021 rc = ldap_pvt_thread_create( &listener_tid,
2022 0, slapd_daemon_task, NULL );
2026 LDAP_LOG( CONNECTION, ERR,
2027 "slapd_daemon: listener ldap_pvt_thread_create failed (%d).\n",
2030 Debug( LDAP_DEBUG_ANY,
2031 "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
2036 /* wait for the listener thread to complete */
2037 ldap_pvt_thread_join( listener_tid, (void *) NULL );
2040 /* experimental code */
2041 slapd_daemon_task( NULL );
2050 #if defined( HAVE_WINSOCK2 )
2051 WORD wVersionRequested;
2055 wVersionRequested = MAKEWORD( 2, 0 );
2057 err = WSAStartup( wVersionRequested, &wsaData );
2059 /* Tell the user that we couldn't find a usable */
2064 /* Confirm that the WinSock DLL supports 2.0.*/
2065 /* Note that if the DLL supports versions greater */
2066 /* than 2.0 in addition to 2.0, it will still return */
2067 /* 2.0 in wVersion since that is the version we */
2070 if ( LOBYTE( wsaData.wVersion ) != 2 ||
2071 HIBYTE( wsaData.wVersion ) != 0 )
2073 /* Tell the user that we couldn't find a usable */
2079 /* The WinSock DLL is acceptable. Proceed. */
2080 #elif defined( HAVE_WINSOCK )
2082 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
2089 int sockdestroy(void)
2091 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
2098 slap_sig_shutdown( int sig )
2102 LDAP_LOG( CONNECTION, CRIT,
2103 "slap_sig_shutdown: signal %d\n", sig, 0, 0 );
2105 Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
2110 * If the NT Service Manager is controlling the server, we don't
2111 * want SIGBREAK to kill the server. For some strange reason,
2112 * SIGBREAK is generated when a user logs out.
2115 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
2116 if (is_NT_Service && sig == SIGBREAK)
2121 if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0)
2122 slapd_gentle_shutdown = 1;
2129 /* reinstall self */
2130 (void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
2134 slap_sig_wake( int sig )
2138 /* reinstall self */
2139 (void) SIGNAL_REINSTALL( sig, slap_sig_wake );
2143 void slapd_add_internal(ber_socket_t s, int isactive) {
2144 slapd_add(s, isactive);
2147 Listener ** slapd_get_listeners(void) {
2148 return slap_listeners;
2151 void slap_wake_listener()