]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/console/authenticate.c
- Integrated TLS network encryption
[bacula/bacula] / bacula / src / console / authenticate.c
index fe242c256b332af2c63ed57bb76750407d618e6d..f981bebdac2b0fd19a3f3082480de740abed5049 100644 (file)
@@ -25,7 +25,7 @@
 
    You should have received a copy of the GNU General Public
    License along with this program; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 
+   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
    MA 02111-1307, USA.
  */
 
@@ -35,7 +35,7 @@
 
 
 void senditf(const char *fmt, ...);
-void sendit(const char *buf); 
+void sendit(const char *buf);
 
 /* Commands sent to Director */
 static char hello[]    = "Hello %s calling\n";
@@ -51,39 +51,116 @@ static char OKhello[]   = "1000 OK:";
 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons)
 {
    BSOCK *dir = jcr->dir_bsock;
-   int ssl_need = BNET_SSL_NONE;
+   int tls_local_need = BNET_TLS_NONE;
+   int tls_remote_need = BNET_TLS_NONE;
    char bashed_name[MAX_NAME_LENGTH];
+   bool auth_success = false;
    char *password;
+#ifdef HAVE_TLS
+   TLS_CONTEXT *tls_ctx = NULL;
+#endif /* HAVE_TLS */
 
-   /* 
+   /*
     * Send my name to the Director then do authentication
     */
    if (cons) {
       bstrncpy(bashed_name, cons->hdr.name, sizeof(bashed_name));
       bash_spaces(bashed_name);
       password = cons->password;
+#ifdef HAVE_TLS
+      /* TLS Requirement */
+      if (cons->tls_enable) {
+        if (cons->tls_require) {
+           tls_local_need = BNET_TLS_REQUIRED;
+        } else {
+           tls_local_need = BNET_TLS_OK;
+        }
+      }
+
+      tls_ctx = cons->tls_ctx;
+#endif /* HAVE_TLS */
    } else {
       bstrncpy(bashed_name, "*UserAgent*", sizeof(bashed_name));
       password = director->password;
+#ifdef HAVE_TLS
+      /* TLS Requirement */
+      if (director->tls_enable) {
+        if (director->tls_require) {
+           tls_local_need = BNET_TLS_REQUIRED;
+        } else {
+           tls_local_need = BNET_TLS_OK;
+        }
+      }
+
+      tls_ctx = director->tls_ctx;
+#endif /* HAVE_TLS */
    }
+
+   
    /* Timeout Hello after 5 mins */
    btimer_t *tid = start_bsock_timer(dir, 60 * 5);
    bnet_fsend(dir, hello, bashed_name);
 
-   if (!cram_md5_get_auth(dir, password, ssl_need) || 
-       !cram_md5_auth(dir, password, ssl_need)) {
+   if (!cram_md5_get_auth(dir, password, &tls_remote_need) ||
+       !cram_md5_auth(dir, password, tls_local_need)) {
+      auth_success = false;
+      goto auth_done;
+   } else {
+      auth_success = true;
+      /* Continue on, soldier ... */
+   }
+
+   /* Verify that the remote host is willing to meet our TLS requirements */
+   if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
+      sendit(_("Authorization problem:"
+             " Remote server did not advertise required TLS support.\n"));
+      auth_success = false;
+      goto auth_done;
+   }
+
+   /* Verify that we are willing to meet the remote host's requirements */
+   if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
+      sendit(_("Authorization problem:"
+             " Remote server requires TLS.\n"));
+      auth_success = false;
+      goto auth_done;
+   }
+
+#ifdef HAVE_TLS
+   /* Is TLS Enabled? */
+   if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
+      /* Engage TLS! Full Speed Ahead! */
+      if (!bnet_tls_client(tls_ctx, dir)) {
+        sendit(_("TLS negotiation failed\n"));
+        auth_success = false;
+        goto auth_done;
+      }
+      auth_success = true;
+   }
+#endif /* HAVE_TLS */
+
+/* Authorization Completed */
+auth_done:
+   if (!auth_success) {
       stop_bsock_timer(tid);
       sendit( _("Director authorization problem.\n"
-            "Most likely the passwords do not agree.\n"     
-       "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
+            "Most likely the passwords do not agree.\n"
+            "If you are using TLS, there may have been a certificate validation error during the TLS handshake.\n"
+            "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
       return 0;
    }
 
+   /*
+    * It's possible that the TLS connection will
+    * be dropped here if an invalid client certificate was presented
+    */
    Dmsg1(6, ">dird: %s", dir->msg);
    if (bnet_recv(dir) <= 0) {
       stop_bsock_timer(tid);
       senditf(_("Bad response to Hello command: ERR=%s\n"),
         bnet_strerror(dir));
+      senditf(_("If you are using TLS, it is possible that your client"
+              " certificate was not accepted. Check the server messages.\n"));
       return 0;
    }
    Dmsg1(10, "<dird: %s", dir->msg);