/*
Bacula® - The Network Backup Solution
- Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
+ Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
The main author of Bacula is Kern Sibbald, with contributions from
many others, a complete list can be found in the file AUTHORS.
This program is Free Software; you can redistribute it and/or
- modify it under the terms of version two of the GNU General Public
- License as published by the Free Software Foundation plus additions
- that are listed in the file LICENSE.
+ modify it under the terms of version three of the GNU Affero General Public
+ License as published by the Free Software Foundation and included
+ in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
- You should have received a copy of the GNU General Public License
+ You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
- Bacula® is a registered trademark of John Walker.
+ Bacula® is a registered trademark of Kern Sibbald.
The licensor of Bacula is the Free Software Foundation Europe
(FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
Switzerland, email:ftf@fsfeurope.org.
* Adapted and enhanced for Bacula, originally written
* for inclusion in the Apcupsd package
*
- * Version $Id$
*/
-
#include "bacula.h"
#include "jcr.h"
#include <netdb.h>
-extern time_t watchdog_time;
-
#ifndef INADDR_NONE
#define INADDR_NONE -1
#endif
#define socketClose(fd) close(fd)
#endif
+#ifndef HAVE_GETADDRINFO
static pthread_mutex_t ip_mutex = PTHREAD_MUTEX_INITIALIZER;
+#endif
/*
* Read a nbytes from the network.
* It is possible that the total bytes require in several
* read requests
*/
-
int32_t read_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes)
{
int32_t nleft, nread;
errno = 0;
nread = socketRead(bsock->m_fd, ptr, nleft);
if (bsock->is_timed_out() || bsock->is_terminated()) {
- return nread;
+ return -1;
}
+
+#ifdef HAVE_WIN32
+ /*
+ * For Windows, we must simulate Unix errno on a socket
+ * error in order to handle errors correctly.
+ */
+ if (nread == SOCKET_ERROR) {
+ DWORD err = WSAGetLastError();
+ nread = -1;
+ if (err == WSAEINTR) {
+ errno = EINTR;
+ } else if (err == WSAEWOULDBLOCK) {
+ errno = EAGAIN;
+ } else {
+ errno = EIO; /* some other error */
+ }
+ }
+#endif
+
if (nread == -1) {
if (errno == EINTR) {
continue;
}
if (errno == EAGAIN) {
- bmicrosleep(0, 200000); /* try again in 200ms */
+ bmicrosleep(0, 20000); /* try again in 20ms */
continue;
}
}
if (nread <= 0) {
- return nread; /* error, or EOF */
+ return -1; /* error, or EOF */
}
nleft -= nread;
ptr += nread;
{
int32_t nleft, nwritten;
- if (bsock->m_spool) {
+ if (bsock->is_spooling()) {
nwritten = fwrite(ptr, 1, nbytes, bsock->m_spool_fd);
if (nwritten != nbytes) {
berrno be;
errno = 0;
nwritten = socketWrite(bsock->m_fd, ptr, nleft);
if (bsock->is_timed_out() || bsock->is_terminated()) {
- return nwritten;
+ return -1;
+ }
+
+#ifdef HAVE_WIN32
+ /*
+ * For Windows, we must simulate Unix errno on a socket
+ * error in order to handle errors correctly.
+ */
+ if (nwritten == SOCKET_ERROR) {
+ DWORD err = WSAGetLastError();
+ nwritten = -1;
+ if (err == WSAEINTR) {
+ errno = EINTR;
+ } else if (err == WSAEWOULDBLOCK) {
+ errno = EAGAIN;
+ } else {
+ errno = EIO; /* some other error */
+ }
}
+#endif
+
} while (nwritten == -1 && errno == EINTR);
/*
* If connection is non-blocking, we will get EAGAIN, so
FD_ZERO(&fdset);
FD_SET((unsigned)bsock->m_fd, &fdset);
- tv.tv_sec = 10;
+ tv.tv_sec = 1;
tv.tv_usec = 0;
select(bsock->m_fd + 1, NULL, &fdset, NULL, &tv);
continue;
}
if (nwritten <= 0) {
- return nwritten; /* error */
+ return -1; /* error */
}
nleft -= nwritten;
ptr += nwritten;
*/
bool is_bnet_stop(BSOCK * bsock)
{
- return bsock->errors || bsock->is_terminated();
+ return bsock->is_stop();
}
/*
*/
int is_bnet_error(BSOCK * bsock)
{
- errno = bsock->b_errno;
- return bsock->errors;
+ return bsock->is_error();
}
/*
bool bnet_tls_server(TLS_CONTEXT *ctx, BSOCK * bsock, alist *verify_list)
{
TLS_CONNECTION *tls;
+ JCR *jcr = bsock->jcr();
tls = new_tls_connection(ctx, bsock->m_fd);
if (!tls) {
}
if (verify_list) {
- if (!tls_postconnect_verify_cn(tls, verify_list)) {
+ if (!tls_postconnect_verify_cn(jcr, tls, verify_list)) {
Qmsg1(bsock->jcr(), M_FATAL, 0, _("TLS certificate verification failed."
" Peer certificate did not match a required commonName\n"),
bsock->host());
goto err;
}
}
+ Dmsg0(50, "TLS server negotiation established.\n");
return true;
err:
bool bnet_tls_client(TLS_CONTEXT *ctx, BSOCK * bsock, alist *verify_list)
{
TLS_CONNECTION *tls;
+ JCR *jcr = bsock->jcr();
tls = new_tls_connection(ctx, bsock->m_fd);
if (!tls) {
/* If there's an Allowed CN verify list, use that to validate the remote
* certificate's CN. Otherwise, we use standard host/CN matching. */
if (verify_list) {
- if (!tls_postconnect_verify_cn(tls, verify_list)) {
+ if (!tls_postconnect_verify_cn(jcr, tls, verify_list)) {
Qmsg1(bsock->jcr(), M_FATAL, 0, _("TLS certificate verification failed."
" Peer certificate did not match a required commonName\n"),
bsock->host());
goto err;
}
} else {
- if (!tls_postconnect_verify_host(tls, bsock->host())) {
- Qmsg1(bsock->jcr(), M_FATAL, 0, _("TLS host certificate verification failed. Host %s did not match presented certificate\n"),
+ if (!tls_postconnect_verify_host(jcr, tls, bsock->host())) {
+ Qmsg1(bsock->jcr(), M_FATAL, 0, _("TLS host certificate verification failed. Host name \"%s\" did not match presented certificate\n"),
bsock->host());
goto err;
}
}
-
+ Dmsg0(50, "TLS client negotiation established.\n");
return true;
err:
*/
int bnet_wait_data(BSOCK * bsock, int sec)
{
- fd_set fdset;
- struct timeval tv;
-
- FD_ZERO(&fdset);
- FD_SET((unsigned)bsock->m_fd, &fdset);
- for (;;) {
- tv.tv_sec = sec;
- tv.tv_usec = 0;
- switch (select(bsock->m_fd + 1, &fdset, NULL, NULL, &tv)) {
- case 0: /* timeout */
- bsock->b_errno = 0;
- return 0;
- case -1:
- bsock->b_errno = errno;
- if (errno == EINTR) {
- continue;
- }
- return -1; /* error return */
- default:
- bsock->b_errno = 0;
- return 1;
- }
- }
+ return bsock->wait_data(sec);
}
/*
*/
int bnet_wait_data_intr(BSOCK * bsock, int sec)
{
- fd_set fdset;
- struct timeval tv;
-
- FD_ZERO(&fdset);
- FD_SET((unsigned)bsock->m_fd, &fdset);
- tv.tv_sec = sec;
- tv.tv_usec = 0;
- switch (select(bsock->m_fd + 1, &fdset, NULL, NULL, &tv)) {
- case 0: /* timeout */
- bsock->b_errno = 0;
- return 0;
- case -1:
- bsock->b_errno = errno;
- return -1; /* error return */
- default:
- bsock->b_errno = 0;
- }
- return 1;
+ return bsock->wait_data_intr(sec);
}
#ifndef NETDB_INTERNAL
#define NO_DATA 4 /* Valid name, no data record of requested type. */
#endif
+#if HAVE_GETADDRINFO
+const char *resolv_host(int family, const char *host, dlist *addr_list)
+{
+ int res;
+ struct addrinfo hints;
+ struct addrinfo *ai, *rp;
+ IPADDR *addr;
+
+ memset(&hints, 0, sizeof(struct addrinfo));
+ hints.ai_family = family;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+ hints.ai_flags = 0;
+
+ res = getaddrinfo(host, NULL, &hints, &ai);
+ if (res != 0) {
+ return gai_strerror(res);
+ }
+
+ for (rp = ai; rp != NULL; rp = rp->ai_next) {
+ switch (rp->ai_addr->sa_family) {
+ case AF_INET:
+ addr = New(IPADDR(rp->ai_addr->sa_family));
+ addr->set_type(IPADDR::R_MULTIPLE);
+ /*
+ * Some serious casting to get the struct in_addr *
+ * rp->ai_addr == struct sockaddr
+ * as this is AF_INET family we can cast that
+ * to struct_sockaddr_in. Of that we need the
+ * address of the sin_addr member which contains a
+ * struct in_addr
+ */
+ addr->set_addr4(&(((struct sockaddr_in *)rp->ai_addr)->sin_addr));
+ break;
+#ifdef HAVE_IPV6
+ case AF_INET6:
+ addr = New(IPADDR(rp->ai_addr->sa_family));
+ addr->set_type(IPADDR::R_MULTIPLE);
+ /*
+ * Some serious casting to get the struct in6_addr *
+ * rp->ai_addr == struct sockaddr
+ * as this is AF_INET6 family we can cast that
+ * to struct_sockaddr_in6. Of that we need the
+ * address of the sin6_addr member which contains a
+ * struct in6_addr
+ */
+ addr->set_addr6(&(((struct sockaddr_in6 *)rp->ai_addr)->sin6_addr));
+ break;
+#endif
+ default:
+ continue;
+ }
+ addr_list->append(addr);
+ }
+ freeaddrinfo(ai);
+ return NULL;
+}
+#else
/*
* Get human readable error for gethostbyname()
*/
return msg;
}
-
-
-
-static IPADDR *add_any(int family)
-{
- IPADDR *addr = New(IPADDR(family));
- addr->set_type(IPADDR::R_MULTIPLE);
- addr->set_addr_any();
- return addr;
-}
-
-static const char *resolv_host(int family, const char *host, dlist * addr_list)
+static const char *resolv_host(int family, const char *host, dlist *addr_list)
{
struct hostent *hp;
const char *errmsg;
+ char **p;
+ IPADDR *addr;
P(ip_mutex); /* gethostbyname() is not thread safe */
#ifdef HAVE_GETHOSTBYNAME2
V(ip_mutex);
return errmsg;
} else {
- char **p;
for (p = hp->h_addr_list; *p != 0; p++) {
- IPADDR *addr = New(IPADDR(hp->h_addrtype));
- addr->set_type(IPADDR::R_MULTIPLE);
- if (addr->get_family() == AF_INET) {
- addr->set_addr4((struct in_addr*)*p);
- }
+ switch (hp->h_addrtype) {
+ case AF_INET:
+ addr = New(IPADDR(hp->h_addrtype));
+ addr->set_type(IPADDR::R_MULTIPLE);
+ addr->set_addr4((struct in_addr *)*p);
+ break;
#ifdef HAVE_IPV6
- else {
- addr->set_addr6((struct in6_addr*)*p);
- }
+ case AF_INET6:
+ addr = New(IPADDR(hp->h_addrtype));
+ addr->set_type(IPADDR::R_MULTIPLE);
+ addr->set_addr6((struct in6_addr *)*p);
+ break;
#endif
+ default:
+ continue;
+ }
addr_list->append(addr);
}
V(ip_mutex);
}
return NULL;
}
+#endif
+
+static IPADDR *add_any(int family)
+{
+ IPADDR *addr = New(IPADDR(family));
+ addr->set_type(IPADDR::R_MULTIPLE);
+ addr->set_addr_any();
+ return addr;
+}
/*
* i host = 0 mean INADDR_ANY only ipv4
return 0;
}
} else {
- errmsg = resolv_host(AF_INET, host, addr_list);
#ifdef HAVE_IPV6
- if (errmsg) {
- errmsg = resolv_host(AF_INET6, host, addr_list);
- }
+ /* We try to resolv host for ipv6 and ipv4, the connection procedure
+ * will try to reach the host for each protocols. We report only "Host
+ * not found" ipv4 message (no need to have ipv6 and ipv4 messages).
+ */
+ resolv_host(AF_INET6, host, addr_list);
#endif
- if (errmsg) {
+ errmsg = resolv_host(AF_INET, host, addr_list);
+
+ if (addr_list->size() == 0) {
*errstr = errmsg;
free_addresses(addr_list);
return 0;
}
/*
- * Open a TCP connection to the UPS network server
- * Returns NULL
- * Returns BSOCK * pointer on success
- *
- */
-static BSOCK *bnet_open(JCR *jcr, const char *name, char *host, char *service,
- int port, utime_t heart_beat, int *fatal)
-{
- int sockfd = -1;
- dlist *addr_list;
- IPADDR *ipaddr;
- bool connected = false;
- int turnon = 1;
- const char *errstr;
- int save_errno = 0;
-
- /*
- * Fill in the structure serv_addr with the address of
- * the server that we want to connect with.
- */
- if ((addr_list = bnet_host2ipaddrs(host, 0, &errstr)) == NULL) {
- /* Note errstr is not malloc'ed */
- Qmsg2(jcr, M_ERROR, 0, _("gethostbyname() for host \"%s\" failed: ERR=%s\n"),
- host, errstr);
- Dmsg2(100, "bnet_host2ipaddrs() for host %s failed: ERR=%s\n",
- host, errstr);
- *fatal = 1;
- return NULL;
- }
-
- foreach_dlist(ipaddr, addr_list) {
- ipaddr->set_port_net(htons(port));
- char allbuf[256 * 10];
- char curbuf[256];
- Dmsg2(100, "Current %sAll %s\n",
- ipaddr->build_address_str(curbuf, sizeof(curbuf)),
- build_addresses_str(addr_list, allbuf, sizeof(allbuf)));
- /* Open a TCP socket */
- if ((sockfd = socket(ipaddr->get_family(), SOCK_STREAM, 0)) < 0) {
- berrno be;
- save_errno = errno;
- *fatal = 1;
- Pmsg3(000, _("Socket open error. proto=%d port=%d. ERR=%s\n"),
- ipaddr->get_family(), ipaddr->get_port_host_order(), be.bstrerror());
- continue;
- }
- /*
- * Keep socket from timing out from inactivity
- */
- if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
- berrno be;
- Qmsg1(jcr, M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
- be.bstrerror());
- }
-#if defined(TCP_KEEPIDLE)
- if (heart_beat) {
- int opt = heart_beat
- if (setsockopt(sockfd, IPPROTO_IP, TCP_KEEPIDLE, (sockopt_val_t)&opt, sizeof(opt)) < 0) {
- berrno be;
- Qmsg1(jcr, M_WARNING, 0, _("Cannot set SO_KEEPIDLE on socket: %s\n"),
- be.bstrerror());
- }
- }
-#endif
-
- /* connect to server */
- if (connect(sockfd, ipaddr->get_sockaddr(), ipaddr->get_sockaddr_len()) < 0) {
- save_errno = errno;
- socketClose(sockfd);
- continue;
- }
- *fatal = 0;
- connected = true;
- break;
- }
-
- if (!connected) {
- free_addresses(addr_list);
- errno = save_errno | b_errno_win32;
- return NULL;
- }
- /*
- * Keep socket from timing out from inactivity
- * Do this a second time out of paranoia
- */
- if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
- berrno be;
- Qmsg1(jcr, M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
- be.bstrerror());
- }
- BSOCK* ret = init_bsock(jcr, sockfd, name, host, port, ipaddr->get_sockaddr());
- free_addresses(addr_list);
- return ret;
-}
-
-/*
- * Try to connect to host for max_retry_time at retry_time intervals.
- */
+ * This is the "old" way of opening a connection. The preferred way is
+ * now to do what this subroutine does, but inline. That allows the
+ * connect() call to return error status, ...
+ */
BSOCK *bnet_connect(JCR * jcr, int retry_interval, utime_t max_retry_time,
utime_t heart_beat,
const char *name, char *host, char *service, int port,
int verbose)
{
- int i;
- BSOCK *bsock;
- int fatal = 0;
- time_t begin_time = time(NULL);
- time_t now;
- btimer_t *tid = NULL;
-
- /* Try to trap out of OS call when time expires */
- if (max_retry_time) {
- tid = start_thread_timer(pthread_self(), (uint32_t)max_retry_time);
- }
-
- for (i = 0; (bsock = bnet_open(jcr, name, host, service, port, heart_beat, &fatal)) == NULL;
- i -= retry_interval) {
- berrno be;
- if (fatal || (jcr && job_canceled(jcr))) {
- bsock = NULL;
- goto bail_out;
- }
- Dmsg4(100, "Unable to connect to %s on %s:%d. ERR=%s\n",
- name, host, port, be.bstrerror());
- if (i < 0) {
- i = 60 * 5; /* complain again in 5 minutes */
- if (verbose)
- Qmsg4(jcr, M_WARNING, 0, _(
- "Could not connect to %s on %s:%d. ERR=%s\n"
- "Retrying ...\n"), name, host, port, be.bstrerror());
- }
- bmicrosleep(retry_interval, 0);
- now = time(NULL);
- if (begin_time + max_retry_time <= now) {
- Qmsg4(jcr, M_FATAL, 0, _("Unable to connect to %s on %s:%d. ERR=%s\n"),
- name, host, port, be.bstrerror());
- bsock = NULL;
- goto bail_out;
- }
- }
-
-bail_out:
- if (tid) {
- stop_thread_timer(tid);
+ BSOCK *bsock = new_bsock();
+ if (!bsock->connect(jcr, retry_interval, max_retry_time, heart_beat,
+ name, host, service, port, verbose)) {
+ bsock->destroy();
+ bsock = NULL;
}
return bsock;
}
-
/*
* Return the string for the error that occurred
* on the socket. Only the first error is retained.
*/
const char *bnet_strerror(BSOCK * bsock)
{
- berrno be;
- if (bsock->errmsg == NULL) {
- bsock->errmsg = get_pool_memory(PM_MESSAGE);
- }
- pm_strcpy(bsock->errmsg, be.bstrerror(bsock->b_errno));
- return bsock->errmsg;
+ return bsock->bstrerror();
}
/*
bsock->restore_blocking(flags);
}
-
/*
* Send a network "signal" to the other end
* This consists of sending a negative packet length
return "BNET_HEARTBEAT";
case BNET_HB_RESPONSE:
return "BNET_HB_RESPONSE";
- case BNET_PROMPT:
- return "BNET_PROMPT";
+ case BNET_SUB_PROMPT:
+ return "BNET_SUB_PROMPT";
+ case BNET_TEXT_INPUT:
+ return "BNET_TEXT_INPUT";
default:
sprintf(buf, _("Unknown sig %d"), (int)bs->msglen);
return buf;
}
}
-
/* Initialize internal socket structure.
* This probably should be done in net_open
*/
bsock->tls = NULL;
bsock->errors = 0;
bsock->m_blocking = 1;
- bsock->msg = get_pool_memory(PM_MESSAGE);
+ bsock->msg = get_pool_memory(PM_BSOCK);
bsock->errmsg = get_pool_memory(PM_MESSAGE);
bsock->set_who(bstrdup(who));
bsock->set_host(bstrdup(host));
return bsock;
}
-BSOCK *dup_bsock(BSOCK * osock)
+BSOCK *dup_bsock(BSOCK *osock)
{
BSOCK *bsock = (BSOCK *)malloc(sizeof(BSOCK));
memcpy(bsock, osock, sizeof(BSOCK));
- bsock->msg = get_pool_memory(PM_MESSAGE);
+ bsock->msg = get_pool_memory(PM_BSOCK);
bsock->errmsg = get_pool_memory(PM_MESSAGE);
if (osock->who()) {
bsock->set_who(bstrdup(osock->who()));
if (osock->host()) {
bsock->set_host(bstrdup(osock->host()));
}
- bsock->m_duped = true;
+ if (osock->src_addr) {
+ bsock->src_addr = New( IPADDR( *(osock->src_addr)) );
+ }
+ bsock->set_duped();
return bsock;
}
/* Close the network connection */
void bnet_close(BSOCK * bsock)
{
- bsock->close(); /* this calls destroy */
+ bsock->close();
}
void term_bsock(BSOCK * bsock)