]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/authenticate.c
- Move test for MaxStartDelay as suggested by Peter.
[bacula/bacula] / bacula / src / dird / authenticate.c
index 4ec64b41bdcc0ae3cd9ec20064735da6df7d9bdb..c778966fa166c668824dbc19c94190d1b7f42b00 100644 (file)
@@ -56,8 +56,9 @@ bool authenticate_storage_daemon(JCR *jcr, STORE *store)
 {
    BSOCK *sd = jcr->store_bsock;
    char dirname[MAX_NAME_LENGTH];
-   int ssl_need = BNET_SSL_NONE;
-   bool get_auth, auth = false;
+   int tls_local_need = BNET_TLS_NONE;
+   int tls_remote_need = BNET_TLS_NONE;
+   bool auth_success = false;
 
    /*
     * Send my name to the Storage daemon then do authentication
@@ -72,16 +73,29 @@ bool authenticate_storage_daemon(JCR *jcr, STORE *store)
       Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to Storage daemon. ERR=%s\n"), bnet_strerror(sd));
       return 0;
    }
-   get_auth = cram_md5_get_auth(sd, store->password, ssl_need);
-   if (get_auth) {
-      auth = cram_md5_auth(sd, store->password, ssl_need);
-      if (!auth) {
+
+#ifdef HAVE_TLS
+   /* TLS Requirement */
+   if (store->tls_enable) {
+     if (store->tls_require) {
+       tls_local_need = BNET_TLS_REQUIRED;
+     } else {
+       tls_local_need = BNET_TLS_OK;
+     }
+   }
+#endif
+
+   auth_success = cram_md5_get_auth(sd, store->password, &tls_remote_need);
+   if (auth_success) {
+      auth_success = cram_md5_auth(sd, store->password, tls_local_need);
+      if (!auth_success) {
         Dmsg1(50, "cram_auth failed for %s\n", sd->who);
       }
    } else {
       Dmsg1(50, "cram_get_auth failed for %s\n", sd->who);
    }
-   if (!get_auth || !auth) {
+
+   if (!auth_success) {
       stop_bsock_timer(tid);
       Dmsg0(50, _("Director and Storage daemon passwords or names not the same.\n"));
       Jmsg0(jcr, M_FATAL, 0,
@@ -92,6 +106,33 @@ bool authenticate_storage_daemon(JCR *jcr, STORE *store)
            "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
       return 0;
    }
+
+   /* 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) {
+      stop_bsock_timer(tid);
+      Jmsg(jcr, M_FATAL, 0, _("Authorization problem: Remote server did not advertise required TLS support.\n"));
+      return 0;
+   }
+
+   /* 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) {
+      stop_bsock_timer(tid);
+      Jmsg(jcr, M_FATAL, 0, _("Authorization problem: Remote server requires TLS.\n"));
+      return 0;
+   }
+
+#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(store->tls_ctx, sd)) {
+        stop_bsock_timer(tid);
+        Jmsg(jcr, M_FATAL, 0, _("TLS negotiation failed.\n"));
+        return 0;
+      }
+   }
+#endif
+
    Dmsg1(116, ">stored: %s", sd->msg);
    if (bnet_recv(sd) <= 0) {
       stop_bsock_timer(tid);
@@ -115,9 +156,11 @@ bool authenticate_storage_daemon(JCR *jcr, STORE *store)
 int authenticate_file_daemon(JCR *jcr)
 {
    BSOCK *fd = jcr->file_bsock;
+   CLIENT *client = jcr->client;
    char dirname[MAX_NAME_LENGTH];
-   int ssl_need = BNET_SSL_NONE;
-   bool get_auth, auth = false;
+   int tls_local_need = BNET_TLS_NONE;
+   int tls_remote_need = BNET_TLS_NONE;
+   bool auth_success = false;
 
    /*
     * Send my name to the File daemon then do authentication
@@ -131,16 +174,28 @@ int authenticate_file_daemon(JCR *jcr)
       Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to File daemon. ERR=%s\n"), bnet_strerror(fd));
       return 0;
    }
-   get_auth = cram_md5_get_auth(fd, jcr->client->password, ssl_need);
-   if (get_auth) {
-      auth = cram_md5_auth(fd, jcr->client->password, ssl_need);
-      if (!auth) {
+
+#ifdef HAVE_TLS
+   /* TLS Requirement */
+   if (client->tls_enable) {
+     if (client->tls_require) {
+       tls_local_need = BNET_TLS_REQUIRED;
+     } else {
+       tls_local_need = BNET_TLS_OK;
+     }
+   }
+#endif
+
+   auth_success = cram_md5_get_auth(fd, client->password, &tls_remote_need);
+   if (auth_success) {
+      auth_success = cram_md5_auth(fd, client->password, tls_local_need);
+      if (!auth_success) {
         Dmsg1(50, "cram_auth failed for %s\n", fd->who);
       }
    } else {
       Dmsg1(50, "cram_get_auth failed for %s\n", fd->who);
    }
-   if (!get_auth || !auth) {
+   if (!auth_success) {
       stop_bsock_timer(tid);
       Dmsg0(50, _("Director and File daemon passwords or names not the same.\n"));
       Jmsg(jcr, M_FATAL, 0,
@@ -151,6 +206,33 @@ int authenticate_file_daemon(JCR *jcr)
            "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
       return 0;
    }
+
+   /* 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) {
+      stop_bsock_timer(tid);
+      Jmsg(jcr, M_FATAL, 0, _("Authorization problem: Remote server did not advertise required TLS support.\n"));
+      return 0;
+   }
+
+   /* 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) {
+      stop_bsock_timer(tid);
+      Jmsg(jcr, M_FATAL, 0, _("Authorization problem: Remote server requires TLS.\n"));
+      return 0;
+   }
+
+#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(client->tls_ctx, fd)) {
+        stop_bsock_timer(tid);
+        Jmsg(jcr, M_FATAL, 0, _("TLS negotiation failed.\n"));
+        return 0;
+      }
+   }
+#endif
+
    Dmsg1(116, ">filed: %s", fd->msg);
    if (bnet_recv(fd) <= 0) {
       stop_bsock_timer(tid);
@@ -176,9 +258,16 @@ int authenticate_file_daemon(JCR *jcr)
 int authenticate_user_agent(UAContext *uac)
 {
    char name[MAX_NAME_LENGTH];
-   int ssl_need = BNET_SSL_NONE;
-   bool ok;
+   int tls_local_need = BNET_TLS_NONE;
+   int tls_remote_need = BNET_TLS_NONE;
+   CONRES *cons = NULL;
    BSOCK *ua = uac->UA_sock;
+   bool auth_success = false;
+#ifdef HAVE_TLS
+   TLS_CONTEXT *tls_ctx = NULL;
+   alist *verify_list = NULL;
+#endif /* HAVE_TLS */
 
 //  Emsg4(M_INFO, 0, _("UA Hello from %s:%s:%d is invalid. Len=%d\n"), ua->who,
 //         ua->host, ua->port, ua->msglen);
@@ -194,24 +283,94 @@ int authenticate_user_agent(UAContext *uac)
            ua->host, ua->port, ua->msg);
       return 0;
    }
+
    name[sizeof(name)-1] = 0;            /* terminate name */
    if (strcmp(name, "*UserAgent*") == 0) {  /* default console */
-      ok = cram_md5_auth(ua, director->password, ssl_need) &&
-          cram_md5_get_auth(ua, director->password, ssl_need);
+#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;
+         }
+      }
+
+      if (director->tls_verify_peer) {
+         verify_list = director->tls_allowed_cns;
+      }
+#endif /* HAVE_TLS */
+
+      auth_success = cram_md5_auth(ua, director->password, tls_local_need) &&
+          cram_md5_get_auth(ua, director->password, &tls_remote_need);
    } else {
       unbash_spaces(name);
-      CONRES *cons = (CONRES *)GetResWithName(R_CONSOLE, name);
+      cons = (CONRES *)GetResWithName(R_CONSOLE, name);
       if (cons) {
-        ok = cram_md5_auth(ua, cons->password, ssl_need) &&
-             cram_md5_get_auth(ua, cons->password, ssl_need);
-        if (ok) {
+#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;
+            }
+         }
+
+        if (cons->tls_verify_peer) {
+            verify_list = cons->tls_allowed_cns;
+        }
+#endif /* HAVE_TLS */
+
+        auth_success = cram_md5_auth(ua, cons->password, tls_local_need) &&
+             cram_md5_get_auth(ua, cons->password, &tls_remote_need);
+
+        if (auth_success) {
            uac->cons = cons;         /* save console resource pointer */
         }
       } else {
-        ok = false;
+        auth_success = false;
+        goto auth_done;
       }
    }
-   if (!ok) {
+
+   /* Verify that the remote peer 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) {
+      Emsg0(M_FATAL, 0, _("Authorization problem:"
+            " Remote client did not advertise required TLS support.\n"));
+      auth_success = false;
+      goto auth_done;
+   }
+
+   /* Verify that we are willing to meet the peer's requirements */
+   if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
+      Emsg0(M_FATAL, 0, _("Authorization problem:"
+            " Remote client requires TLS.\n"));
+      auth_success = false;
+      goto auth_done;
+   }
+
+#ifdef HAVE_TLS
+   if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
+      if (cons) {
+        tls_ctx = cons->tls_ctx;
+      } else {
+        tls_ctx = director->tls_ctx;
+      }
+
+      /* Engage TLS! Full Speed Ahead! */
+      if (!bnet_tls_server(tls_ctx, ua, verify_list)) {
+        Emsg0(M_ERROR, 0, "TLS negotiation failed.\n");
+        auth_success = false;
+        goto auth_done;
+      }
+   }
+#endif /* HAVE_TLS */
+
+
+/* Authorization Completed */
+auth_done:
+   if (!auth_success) {
       bnet_fsend(ua, "%s", _(Dir_sorry));
       Emsg4(M_ERROR, 0, _("Unable to authenticate console \"%s\" at %s:%s:%d.\n"),
            name, ua->who, ua->host, ua->port);