]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/bnet.c
- Put Dmsg() on inside if() to avoid calling subroutine.
[bacula/bacula] / bacula / src / lib / bnet.c
index 5c3b638d15a355e6f7748a9f84756e83588300fa..b07838db2aec72a9aa07e0efce6547e773b2da17 100644 (file)
@@ -9,22 +9,17 @@
  *   Version $Id$
  */
 /*
-   Copyright (C) 2000-2004 Kern Sibbald
+   Copyright (C) 2000-2005 Kern Sibbald
 
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as ammended with additional clauses defined in the
+   file LICENSE in the main source directory.
 
-   This library is distributed in the hope that it will be useful,
+   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
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
-   MA 02111-1307, USA.
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+   the file LICENSE for additional details.
 
  */
 
@@ -65,6 +60,13 @@ static int32_t read_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes)
 {
    int32_t nleft, nread;
 
+#ifdef HAVE_TLS
+   if (bsock->tls) {
+      /* TLS enabled */
+      return (tls_bsock_readn(bsock, ptr, nbytes));
+   }
+#endif /* HAVE_TLS */
+
    nleft = nbytes;
    while (nleft > 0) {
       do {
@@ -105,6 +107,14 @@ static int32_t write_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes)
       }
       return nbytes;
    }
+
+#ifdef HAVE_TLS
+   if (bsock->tls) {
+      /* TLS enabled */
+      return (tls_bsock_writen(bsock, ptr, nbytes));
+   }
+#endif /* HAVE_TLS */
+
    nleft = nbytes;
    while (nleft > 0) {
       do {
@@ -370,7 +380,7 @@ bool bnet_send(BSOCK * bsock)
       if (rc < 0) {
         if (!bsock->suppress_error_msgs && !bsock->timed_out) {
            Qmsg4(bsock->jcr, M_ERROR, 0,
-                  _("Write error sending to %s:%s:%d: ERR=%s\n"), bsock->who,
+                  _("Write error sending len to %s:%s:%d: ERR=%s\n"), bsock->who,
                  bsock->host, bsock->port, bnet_strerror(bsock));
         }
       } else {
@@ -400,8 +410,9 @@ bool bnet_send(BSOCK * bsock)
       }
       if (rc < 0) {
         if (!bsock->suppress_error_msgs) {
-           Qmsg4(bsock->jcr, M_ERROR, 0,
-                  _("Write error sending to %s:%s:%d: ERR=%s\n"), bsock->who,
+           Qmsg5(bsock->jcr, M_ERROR, 0,
+                  _("Write error sending %d bytes to %s:%s:%d: ERR=%s\n"), 
+                 bsock->msglen, bsock->who,
                  bsock->host, bsock->port, bnet_strerror(bsock));
         }
       } else {
@@ -415,28 +426,81 @@ bool bnet_send(BSOCK * bsock)
 }
 
 /*
- * Establish an SSL connection -- server side
- *  Codes that ssl_need and ssl_has can take
- *    BNET_SSL_NONE     I cannot do ssl
- *    BNET_SSL_OK       I can do ssl, but it is not required on my end
- *    BNET_SSL_REQUIRED  ssl is required on my end
+ * Establish a TLS connection -- server side
+ *  Returns: 1 on success
+ *          0 failure
  */
-int bnet_ssl_server(BSOCK * bsock, char *password, int ssl_need, int ssl_has)
+#ifdef HAVE_TLS
+int bnet_tls_server(TLS_CONTEXT *ctx, BSOCK * bsock, alist *verify_list)
 {
-   /* Check to see if what we need (ssl_need) corresponds to what he has (ssl_has) */
-   /* The other side expects a response from us */
+   TLS_CONNECTION *tls;
+   
+   tls = new_tls_connection(ctx, bsock->fd);
+   if (!tls) {
+      Qmsg0(bsock->jcr, M_FATAL, 0, _("TLS connection initialization failed.\n"));
+      return 0;
+   }
+
+   bsock->tls = tls;
+
+   /* Initiate TLS Negotiation */
+   if (!tls_bsock_accept(bsock)) {
+      Qmsg0(bsock->jcr, M_FATAL, 0, _("TLS Negotiation failed.\n"));
+      goto err;
+   }
+
+   if (verify_list) {
+      if (!tls_postconnect_verify_cn(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;
+      }
+   }
    return 1;
+
+err:
+   free_tls_connection(tls);
+   bsock->tls = NULL;
+   return 0;
 }
 
 /*
- * Establish an SSL connection -- client side
+ * Establish a TLS connection -- client side
+ * Returns: 1 on success
+ *         0 failure
  */
-int bnet_ssl_client(BSOCK * bsock, char *password, int ssl_need)
+int bnet_tls_client(TLS_CONTEXT *ctx, BSOCK * bsock)
 {
-   /* We are the client so we must wait for the server to notify us */
+   TLS_CONNECTION *tls;
+
+   tls = new_tls_connection(ctx, bsock->fd);
+   if (!tls) {
+      Qmsg0(bsock->jcr, M_FATAL, 0, _("TLS connection initialization failed.\n"));
+      return 0;
+   }
+
+   bsock->tls = tls;
+
+   /* Initiate TLS Negotiation */
+   if (!tls_bsock_connect(bsock)) {
+      goto err;
+   }
+
+   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"), bsock->host);
+      goto err;
+   }
    return 1;
-}
 
+err:
+   free_tls_connection(tls);
+   bsock->tls = NULL;
+   return 0;
+}
+#endif /* HAVE_TLS */
 
 /*
  * Wait for a specified time for data to appear on
@@ -485,19 +549,17 @@ int bnet_wait_data_intr(BSOCK * bsock, int sec)
    FD_SET((unsigned)bsock->fd, &fdset);
    tv.tv_sec = sec;
    tv.tv_usec = 0;
-   for (;;) {
-      switch (select(bsock->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;
-      }
+   switch (select(bsock->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;
 }
 
 #ifndef NETDB_INTERNAL
@@ -525,9 +587,10 @@ int bnet_wait_data_intr(BSOCK * bsock, int sec)
 static const char *gethost_strerror()
 {
    const char *msg;
+   berrno be;
    switch (h_errno) {
    case NETDB_INTERNAL:
-      msg = strerror(errno);
+      msg = be.strerror();
       break;
    case NETDB_SUCCESS:
       msg = "No problem.";
@@ -898,6 +961,93 @@ bool bnet_set_buffer_size(BSOCK * bs, uint32_t size, int rw)
    return true;
 }
 
+/*
+ * Set socket non-blocking
+ * Returns previous socket flag
+ */
+int bnet_set_nonblocking (BSOCK *bsock) {
+#ifndef WIN32
+   int oflags;
+
+   /* Get current flags */
+   if((oflags = fcntl(bsock->fd, F_GETFL, 0)) < 0) {
+      berrno be;
+      Emsg1(M_ABORT, 0, "fcntl F_GETFL error. ERR=%s\n", be.strerror());
+   }
+
+   /* Set O_NONBLOCK flag */
+   if((fcntl(bsock->fd, F_SETFL, oflags|O_NONBLOCK)) < 0) {
+      berrno be;
+      Emsg1(M_ABORT, 0, "fcntl F_SETFL error. ERR=%s\n", be.strerror());
+   }
+
+   bsock->blocking = 0;
+   return oflags;
+#else
+   int flags;
+   u_long ioctlArg = 1;
+
+   flags = bsock->blocking;
+   ioctlsocket(bsock->fd, FIONBIO, &ioctlArg);
+   bsock->blocking = 0;
+
+   return (flags);
+#endif
+}
+
+/*
+ * Set socket blocking
+ * Returns previous socket flags
+ */
+int bnet_set_blocking (BSOCK *bsock) {
+#ifndef WIN32
+   int oflags;
+   /* Get current flags */
+   if((oflags = fcntl(bsock->fd, F_GETFL, 0)) < 0) {
+      berrno be;
+      Emsg1(M_ABORT, 0, "fcntl F_GETFL error. ERR=%s\n", be.strerror());
+   }
+
+   /* Set O_NONBLOCK flag */
+   if((fcntl(bsock->fd, F_SETFL, oflags & ~O_NONBLOCK)) < 0) {
+      berrno be;
+      Emsg1(M_ABORT, 0, "fcntl F_SETFL error. ERR=%s\n", be.strerror());
+   }
+
+   bsock->blocking = 1;
+   return (oflags);
+#else
+   int flags;
+   u_long ioctlArg = 0;
+
+   flags = bsock->blocking;
+   ioctlsocket(bsock->fd, FIONBIO, &ioctlArg);
+   bsock->blocking = 1;
+
+   return (flags);
+#endif
+}
+
+/*
+ * Restores socket flags
+ */
+void bnet_restore_blocking (BSOCK *bsock, int flags) {
+#ifndef WIN32
+   if((fcntl(bsock->fd, F_SETFL, flags)) < 0) {
+      berrno be;
+      Emsg1(M_ABORT, 0, "fcntl F_SETFL error. ERR=%s\n", be.strerror());
+   }
+
+   bsock->blocking = (flags & O_NONBLOCK);
+#else
+   u_long ioctlArg = flags;
+
+   ioctlsocket(bsock->fd, FIONBIO, &ioctlArg);
+   bsock->blocking = 1;
+#endif
+}
+
+
 /*
  * Send a network "signal" to the other end
  *  This consists of sending a negative packet length
@@ -908,6 +1058,9 @@ bool bnet_set_buffer_size(BSOCK * bs, uint32_t size, int rw)
 bool bnet_sig(BSOCK * bs, int sig)
 {
    bs->msglen = sig;
+   if (sig == BNET_TERMINATE) {
+      bs->suppress_error_msgs = true;
+   }
    return bnet_send(bs);
 }
 
@@ -936,7 +1089,7 @@ const char *bnet_sig_to_ascii(BSOCK * bs)
    case BNET_PROMPT:
       return "BNET_PROMPT";
    default:
-      sprintf(buf, "Unknown sig %d", bs->msglen);
+      sprintf(buf, "Unknown sig %d", (int)bs->msglen);
       return buf;
    }
 }
@@ -952,7 +1105,9 @@ BSOCK *init_bsock(JCR * jcr, int sockfd, const char *who, const char *host, int
    BSOCK *bsock = (BSOCK *)malloc(sizeof(BSOCK));
    memset(bsock, 0, sizeof(BSOCK));
    bsock->fd = sockfd;
+   bsock->tls = NULL;
    bsock->errors = 0;
+   bsock->blocking = 1;
    bsock->msg = get_pool_memory(PM_MESSAGE);
    bsock->errmsg = get_pool_memory(PM_MESSAGE);
    bsock->who = bstrdup(who);
@@ -992,6 +1147,14 @@ void bnet_close(BSOCK * bsock)
    for (; bsock != NULL; bsock = next) {
       next = bsock->next;
       if (!bsock->duped) {
+#ifdef HAVE_TLS
+        /* Shutdown tls cleanly. */
+        if (bsock->tls) {
+           tls_bsock_shutdown(bsock);
+           free_tls_connection(bsock->tls);
+           bsock->tls = NULL;
+        }
+#endif /* HAVE_TLS */
         if (bsock->timed_out) {
            shutdown(bsock->fd, 2);     /* discard any pending I/O */
         }