#define LDAPI_MOD_URLEXT "x-mod"
#endif /* LDAP_PF_LOCAL */
+int slap_inet4or6 = AF_UNSPEC;
+
/* globals */
time_t starttime;
ber_socket_t dtblsize;
FD_CLR( s, &slap_daemon.sd_writers );
ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
- WAKE_LISTENER(wake || slapd_gentle_shutdown < 0);
+ WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
}
void slapd_clr_write(ber_socket_t s, int wake) {
memset( &hints, '\0', sizeof(hints) );
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
- hints.ai_family = AF_UNSPEC;
+ hints.ai_family = slap_inet4or6;
snprintf(serv, sizeof serv, "%d", port);
if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
freeaddrinfo(res);
#else
+ int i, n = 1;
struct in_addr in;
+ struct hostent *he = NULL;
if ( host == NULL ) {
in.s_addr = htonl(INADDR_ANY);
} else if ( !inet_aton( host, &in ) ) {
- struct hostent *he = gethostbyname( host );
+ he = gethostbyname( host );
if( he == NULL ) {
#ifdef NEW_LOGGING
LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
#endif
return -1;
}
- AC_MEMCPY( &in, he->h_addr, sizeof( in ) );
+ for (n = 0; he->h_addr_list[n]; n++) ;
}
- *sal = ch_malloc(2 * sizeof(void *));
+ *sal = ch_malloc((n+1) * sizeof(void *));
if (*sal == NULL) {
return -1;
}
sap = *sal;
- *sap = ch_malloc(sizeof(struct sockaddr_in));
- if (*sap == NULL) {
- goto errexit;
+ for ( i = 0; i<n; i++ ) {
+ sap[i] = ch_malloc(sizeof(struct sockaddr_in));
+ if (*sap == NULL) {
+ goto errexit;
+ }
+ (void)memset( (void *)sap[i], '\0', sizeof(struct sockaddr_in) );
+ sap[i]->sa_family = AF_INET;
+ ((struct sockaddr_in *)sap[i])->sin_port = htons(port);
+ if (he) {
+ AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, he->h_addr_list[i], sizeof(struct in_addr) );
+ } else {
+ AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr, &in, sizeof(struct in_addr) );
+ }
}
- sap[1] = NULL;
-
- (void)memset( (void *)*sap, '\0', sizeof(struct sockaddr_in) );
- (*sap)->sa_family = AF_INET;
- ((struct sockaddr_in *)*sap)->sin_port = htons(port);
- ((struct sockaddr_in *)*sap)->sin_addr = in;
+ sap[i] = NULL;
#endif
}
return -1;
}
-static Listener * slap_open_listener(
- const char* url )
+static int slap_open_listener(
+ const char* url,
+ int *listeners,
+ int *cur
+ )
{
- int tmp, rc;
+ int num, tmp, rc;
Listener l;
Listener *li;
LDAPURLDesc *lud;
"daemon: listen URL \"%s\" parse error=%d\n",
url, rc, 0 );
#endif
- return NULL;
+ return rc;
}
#ifndef HAVE_TLS
url, 0, 0 );
#endif
ldap_free_urldesc( lud );
- return NULL;
+ return -1;
}
if(! lud->lud_port ) {
url, 0, 0);
#endif
ldap_free_urldesc( lud );
- return NULL;
+ return -1;
#endif
} else {
#ifdef LDAP_CONNECTIONLESS
ldap_free_urldesc( lud );
if ( err ) {
- return NULL;
+ return -1;
+ }
+
+ /* If we got more than one address returned, we need to make space
+ * for it in the slap_listeners array.
+ */
+ for ( num=0; sal[num]; num++ );
+ if ( num > 1 ) {
+ *listeners += num-1;
+ slap_listeners = ch_realloc( slap_listeners, (*listeners + 1) * sizeof(Listener *) );
}
psal = sal;
break;
#ifdef LDAP_PF_INET6
case AF_INET6:
+#ifdef IPV6_V6ONLY
+ /* Try to use IPv6 sockets for IPv6 only */
+ tmp = 1;
+ rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY,
+ (char *) &tmp, sizeof(tmp) );
+ if ( rc == AC_SOCKET_ERROR ) {
+ int err = sock_errno();
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
+ "slap_open_listener: setsockopt( %ld, IPV6_V6ONLY ) failed errno %d (%s)\n",
+ (long)l.sl_sd, err, sock_errstr(err) ));
+#else
+ Debug( LDAP_DEBUG_ANY,
+ "slapd(%ld): setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
+ (long) l.sl_sd, err, sock_errstr(err) );
+#endif
+ }
+#endif
addrlen = sizeof(struct sockaddr_in6);
break;
#endif
#endif
}
- if (!bind(l.sl_sd, *sal, addrlen))
- break;
- err = sock_errno();
+ if (bind(l.sl_sd, *sal, addrlen)) {
+ err = sock_errno();
#ifdef NEW_LOGGING
LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
"slap_open_listener: bind(%ld) failed errno=%d (%s)\n",
Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n",
(long) l.sl_sd, err, sock_errstr(err) );
#endif
- tcp_close( l.sl_sd );
- sal++;
- } /* while ( *sal != NULL ) */
-
- if ( *sal == NULL ) {
-#ifdef NEW_LOGGING
- LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
- "slap_open_listener: bind(%ld) failed.\n", (long)l.sl_sd ));
-#else
- Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed\n",
- (long) l.sl_sd, 0, 0 );
-#endif
- slap_free_listener_addresses(psal);
- return NULL;
- }
+ tcp_close( l.sl_sd );
+ sal++;
+ continue;
+ }
switch ( (*sal)->sa_family ) {
#ifdef LDAP_PF_LOCAL
#endif
tcp_close( l.sl_sd );
slap_free_listener_addresses(psal);
- return NULL;
+ return -1;
}
l.sl_name = ch_malloc( strlen(addr) + sizeof("PATH=") );
sprintf( l.sl_name, "PATH=%s", addr );
break;
}
- slap_free_listener_addresses(psal);
-
+ AC_MEMCPY(&l.sl_sa, *sal, addrlen);
l.sl_url = ch_strdup( url );
li = ch_malloc( sizeof( Listener ) );
*li = l;
+ slap_listeners[*cur] = li;
+ (*cur)++;
+ sal++;
+
+ } /* while ( *sal != NULL ) */
+
+ slap_free_listener_addresses(psal);
+
#ifdef NEW_LOGGING
LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
- "slap_open_listener: daemon initialzed %s\n", l.sl_url ));
+ "slap_open_listener: daemon initialized %s\n", l.sl_url ));
#else
Debug( LDAP_DEBUG_TRACE, "daemon: initialized %s\n",
l.sl_url, 0, 0 );
#endif
- return li;
+ return 0;
}
static int sockinit(void);
int slapd_daemon_init( const char *urls )
{
- int i, rc;
+ int i, j, n, rc;
char **u;
#ifdef NEW_LOGGING
#endif
slap_listeners = ch_malloc( (i+1)*sizeof(Listener *) );
- for(i = 0; u[i] != NULL; i++ ) {
- slap_listeners[i] = slap_open_listener( u[i] );
-
- if( slap_listeners[i] == NULL ) {
+ for(n = 0, j = 0; u[n]; n++ ) {
+ if ( slap_open_listener( u[n], &i, &j ) ) {
charray_free( u );
return -1;
}
}
- slap_listeners[i] = NULL;
+ slap_listeners[j] = NULL;
#ifdef NEW_LOGGING
LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
int l;
for ( l = 0; slap_listeners[l] != NULL; l++ ) {
- if ( remove )
- slapd_remove( slap_listeners[l]->sl_sd, 0 );
if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
+ if ( remove )
+ slapd_remove( slap_listeners[l]->sl_sd, 0 );
#ifdef LDAP_PF_LOCAL
if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN ) == -1 ) {
int err = sock_errno();
+
+#ifdef LDAP_PF_INET6
+ /* If error is EADDRINUSE, we are trying to listen to INADDR_ANY and
+ * we are already listening to in6addr_any, then we want to ignore
+ * this and continue.
+ */
+ if ( err == EADDRINUSE ) {
+ int i;
+ struct sockaddr_in sa = slap_listeners[l]->sl_sa.sa_in_addr;
+ struct sockaddr_in6 sa6;
+
+ if ( sa.sin_family == AF_INET &&
+ sa.sin_addr.s_addr == htonl(INADDR_ANY) ) {
+ for ( i = 0 ; i < l; i++ ) {
+ sa6 = slap_listeners[i]->sl_sa.sa_in6_addr;
+ if ( sa6.sin6_family == AF_INET6 &&
+ !memcmp( &sa6.sin6_addr, &in6addr_any, sizeof(struct in6_addr) ) )
+ break;
+ }
+
+ if ( i < l ) {
+ /* We are already listening to in6addr_any */
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "connection", LDAP_LEVEL_WARNING,
+ "slapd_daemon_task: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n" ));
+#else
+ Debug( LDAP_DEBUG_CONNS,
+ "daemon: Attempt to listen to 0.0.0.0 failed, already listening on ::, assuming IPv4 included\n",
+ 0, 0, 0 );
+#endif
+ slapd_close( slap_listeners[l]->sl_sd );
+ slap_listeners[l]->sl_sd = AC_SOCKET_INVALID;
+ continue;
+ }
+ }
+ }
+#endif
#ifdef NEW_LOGGING
LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
"slapd_daemon_task: listen( %s, 5 ) failed errno=%d (%s)\n",
if( slapd_gentle_shutdown ) {
ber_socket_t active;
- if( slapd_gentle_shutdown > 0 ) {
+ if( slapd_gentle_shutdown == 1 ) {
Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
close_listeners( 1 );
- slapd_gentle_shutdown = -1;
+ global_restrictops |= SLAP_RESTRICT_OP_WRITES;
+ slapd_gentle_shutdown = 2;
}
ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
active = slap_daemon.sd_nactives;
ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
if( active == 0 ) {
- slapd_shutdown = -1;
+ slapd_shutdown = 2;
break;
}
}
(unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
} else {
char addr[INET6_ADDRSTRLEN];
+
+ peeraddr = (char *) inet_ntop( AF_INET6,
+ &from.sa_in6_addr.sin6_addr,
+ addr, sizeof addr );
sprintf( peername, "IP=%s %d",
- inet_ntop( AF_INET6,
- &from.sa_in6_addr.sin6_addr,
- addr, sizeof addr) ? addr : "unknown",
+ peeraddr != NULL ? peeraddr : "unknown",
(unsigned) ntohs( from.sa_in6_addr.sin6_port ) );
}
break;
#endif
}
- if( slapd_gentle_shutdown >= 0 )
+ if( slapd_gentle_shutdown != 2 )
close_listeners ( 0 );
free ( slap_listeners );
slap_listeners = NULL;