]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/cram-md5.c
Eliminate dependency on man2html.
[bacula/bacula] / bacula / src / lib / cram-md5.c
index 5026bc73aa307ea77003c581dbb5ca6344c480f7..fd0446e161506630e0080f58cbe335e8332c95e5 100644 (file)
  *   BNET_TLS_NONE     I cannot do tls
  *   BNET_TLS_OK       I can do tls, but it is not required on my end
  *   BNET_TLS_REQUIRED  tls is required on my end
+ *
+ *   Returns: false if authentication failed
+ *            true if OK
  */
-int cram_md5_auth(BSOCK *bs, char *password, int tls_local_need)
+bool cram_md5_challenge(BSOCK *bs, char *password, int tls_local_need, int compatible)
 {
    struct timeval t1;
    struct timeval t2;
    struct timezone tz;
-   int i, ok;
+   int i;
+   bool ok;
    char chal[MAXSTRING];
    char host[MAXSTRING];
    uint8_t hmac[20];
@@ -48,26 +52,42 @@ int cram_md5_auth(BSOCK *bs, char *password, int tls_local_need)
    if (!gethostname(host, sizeof(host))) {
       bstrncpy(host, my_name, sizeof(host));
    }
+   /* Send challenge -- no hashing yet */
    bsnprintf(chal, sizeof(chal), "<%u.%u@%s>", (uint32_t)random(), (uint32_t)time(NULL), host);
-   Dmsg2(50, "send: auth cram-md5 %s ssl=%d\n", chal, tls_local_need);
-   if (!bnet_fsend(bs, "auth cram-md5 %s ssl=%d\n", chal, tls_local_need)) {
-      Dmsg1(50, "Bnet send challenge error.\n", bnet_strerror(bs));
-      return 0;
+   if (compatible) {
+      Dmsg2(50, "send: auth cram-md5 %s ssl=%d\n", chal, tls_local_need);
+      if (!bnet_fsend(bs, "auth cram-md5 %s ssl=%d\n", chal, tls_local_need)) {
+         Dmsg1(50, "Bnet send challenge error.\n", bnet_strerror(bs));
+         return false;
+      }
+   } else {
+      /* Old non-compatible system */
+      Dmsg2(50, "send: auth cram-md5 %s ssl=%d\n", chal, tls_local_need);
+      if (!bnet_fsend(bs, "auth cram-md5 %s ssl=%d\n", chal, tls_local_need)) {
+         Dmsg1(50, "Bnet send challenge error.\n", bnet_strerror(bs));
+         return false;
+      }
    }
 
+   /* Read hashed response to challenge */
    if (bnet_wait_data(bs, 180) <= 0 || bnet_recv(bs) <= 0) {
       Dmsg1(50, "Bnet receive challenge response error.\n", bnet_strerror(bs));
       bmicrosleep(5, 0);
-      return 0;
+      return false;
    }
+
+   /* Attempt to duplicate hash with our password */
    hmac_md5((uint8_t *)chal, strlen(chal), (uint8_t *)password, strlen(password), hmac);
-   bin_to_base64(host, (char *)hmac, 16);
-// Dmsg3(100, "auth: chal=%s pw=%s hmac=%s\n", chal, password, host);
+   bin_to_base64(host, sizeof(host), (char *)hmac, 16, compatible);
    ok = strcmp(bs->msg, host) == 0;
    if (ok) {
       Dmsg1(50, "Authenticate OK %s\n", host);
    } else {
-      Dmsg2(50, "Authenticate NOT OK: wanted %s, got %s\n", host, bs->msg);
+      bin_to_base64(host, sizeof(host), (char *)hmac, 16, false);     
+      ok = strcmp(bs->msg, host) == 0;
+      if (!ok) {
+         Dmsg2(50, "Authenticate NOT OK: wanted %s, got %s\n", host, bs->msg);
+      }
    }
    if (ok) {
       bnet_fsend(bs, "1000 OK auth\n");
@@ -79,48 +99,51 @@ int cram_md5_auth(BSOCK *bs, char *password, int tls_local_need)
    return ok;
 }
 
-/* Get authorization from other end */
-int cram_md5_get_auth(BSOCK *bs, char *password, int *tls_remote_need)
+/* Respond to challenge from other end */
+bool cram_md5_respond(BSOCK *bs, char *password, int *tls_remote_need, int *compatible)
 {
    char chal[MAXSTRING];
    uint8_t hmac[20];
 
+   *compatible = false;
    if (bnet_recv(bs) <= 0) {
       bmicrosleep(5, 0);
-      return 0;
+      return false;
    }
    if (bs->msglen >= MAXSTRING) {
       Dmsg1(50, "Msg too long wanted auth cram... Got: %s", bs->msg);
       bmicrosleep(5, 0);
-      return 0;
+      return false;
    }
    Dmsg1(100, "cram-get: %s", bs->msg);
-   if (sscanf(bs->msg, "auth cram-md5 %s ssl=%d\n", chal, tls_remote_need) != 2) {
+   if (sscanf(bs->msg, "auth cram-md5c %s ssl=%d", chal, tls_remote_need) == 2) {
+      *compatible = true;
+   } else if (sscanf(bs->msg, "auth cram-md5 %s ssl=%d", chal, tls_remote_need) != 2) {
       if (sscanf(bs->msg, "auth cram-md5 %s\n", chal) != 1) {
          Dmsg1(50, "Cannot scan challenge: %s", bs->msg);
          bnet_fsend(bs, _("1999 Authorization failed.\n"));
          bmicrosleep(5, 0);
-         return 0;
+         return false;
       }
    }
 
    hmac_md5((uint8_t *)chal, strlen(chal), (uint8_t *)password, strlen(password), hmac);
-   bs->msglen = bin_to_base64(bs->msg, (char *)hmac, 16) + 1;
+   bs->msglen = bin_to_base64(bs->msg, 50, (char *)hmac, 16, *compatible) + 1;
 // Dmsg3(100, "get_auth: chal=%s pw=%s hmac=%s\n", chal, password, bs->msg);
    if (!bnet_send(bs)) {
       Dmsg1(50, "Send challenge failed. ERR=%s\n", bnet_strerror(bs));
-      return 0;
+      return false;
    }
    Dmsg1(99, "sending resp to challenge: %s\n", bs->msg);
    if (bnet_wait_data(bs, 180) <= 0 || bnet_recv(bs) <= 0) {
       Dmsg1(50, "Receive chanllenge response failed. ERR=%s\n", bnet_strerror(bs));
       bmicrosleep(5, 0);
-      return 0;
+      return false;
    }
    if (strcmp(bs->msg, "1000 OK auth\n") == 0) {
-      return 1;
+      return true;
    }
    Dmsg1(50, "Bad auth response: %s\n", bs->msg);
    bmicrosleep(5, 0);
-   return 0;
+   return false;
 }