]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/cram-md5.c
Fix reporting jobs from state file + misc
[bacula/bacula] / bacula / src / lib / cram-md5.c
index 7d788381a68ff83026ffd02627bb2f17ea54d4c1..be1ed8db4c529b3446719b58932b7a502ef53b8a 100644 (file)
@@ -30,7 +30,7 @@
 #include "bacula.h"
 
 /* Authorize other end */
-int cram_md5_auth(BSOCK *bs, char *password)
+int cram_md5_auth(BSOCK *bs, char *password, int ssl_need)
 {
    struct timeval t1;
    struct timeval t2;
@@ -41,34 +41,36 @@ int cram_md5_auth(BSOCK *bs, char *password)
    uint8_t hmac[20];
 
    gettimeofday(&t1, &tz);
-   for (i=0; i<4; i++)
+   for (i=0; i<4; i++) {
       gettimeofday(&t2, &tz);
+   }
    srandom((t1.tv_sec&0xffff) * (t2.tv_usec&0xff));
    if (!gethostname(host, sizeof(host))) {
       bstrncpy(host, my_name, sizeof(host));
    }
-   sprintf((char *)chal, "<%u.%u@%s>", (uint32_t)random(), (uint32_t)time(NULL), host);
-   if (!bnet_fsend(bs, "auth cram-md5 %s\n", chal)) {
+   bsnprintf(chal, sizeof(chal), "<%u.%u@%s>", (uint32_t)random(), (uint32_t)time(NULL), host);
+   if (!bnet_fsend(bs, "auth cram-md5 %s ssl=%d\n", chal, ssl_need)) {
+      return 0;
+   }
+
+   if (!bnet_ssl_client(bs, password, ssl_need)) {
       return 0;
    }
-   Dmsg1(99, "%s", bs->msg);
+
+   Dmsg1(99, "sent challenge: %s", bs->msg);
    if (bnet_wait_data(bs, 180) <= 0 || bnet_recv(bs) <= 0) {
       bmicrosleep(5, 0);
       return 0;
    }
    hmac_md5((uint8_t *)chal, strlen(chal), (uint8_t *)password, strlen(password), hmac);
    bin_to_base64(host, (char *)hmac, 16);
-   ok = strcmp(bs->msg, host) == 0;
-   if (ok) {
-      Dmsg3(99, "Authenticate %s: wanted %s, got %s\n", 
-            ok ? "OK" : "NOT OK", host, bs->msg);
-   } else {
-      Dmsg3(99, "Authenticate %s: wanted %s, got %s\n", 
-            ok ? "OK" : "NOT OK", host, bs->msg);
-   }
+   ok = strcmp(mp_chr(bs->msg), host) == 0;
+   Dmsg3(99, "Authenticate %s: wanted %s, got %s\n", 
+              ok ? "OK" : "NOT OK", host, bs->msg);
    if (ok) {
       bnet_fsend(bs, "1000 OK auth\n");
    } else {
+      Dmsg1(100, "PW: %s\n", password);
       bnet_fsend(bs, "1999 Authorization failed.\n");
       bmicrosleep(5, 0);
    }
@@ -76,23 +78,38 @@ int cram_md5_auth(BSOCK *bs, char *password)
 }
 
 /* Get authorization from other end */
-int cram_md5_get_auth(BSOCK *bs, char *password)
+int cram_md5_get_auth(BSOCK *bs, char *password, int ssl_need)
 {
    char chal[MAXSTRING];
    uint8_t hmac[20];
+   int ssl_has;                      /* This is what the other end has */
 
    if (bnet_recv(bs) <= 0) {
       bmicrosleep(5, 0);
       return 0;
    }
-   if (bs->msglen >= MAXSTRING || sscanf(bs->msg, "auth cram-md5 %s\n", chal) != 1) {
-     Dmsg1(99, "Wanted auth cram... Got: %s", bs->msg);
-     bmicrosleep(5, 0);
-     return 0;
+   if (bs->msglen >= MAXSTRING) {
+      Dmsg1(99, "Wanted auth cram... Got: %s", bs->msg);
+      bmicrosleep(5, 0);
+      return 0;
+   }
+   if (sscanf(mp_chr(bs->msg), "auth cram-md5 %s ssl=%d\n", chal, &ssl_has) != 2) {
+      ssl_has = BNET_SSL_NONE;
+      if (sscanf(mp_chr(bs->msg), "auth cram-md5 %s\n", chal) != 1) {
+         bnet_fsend(bs, "1999 Authorization failed.\n");
+         Dmsg1(100, "Cannot scan challenge: %s\n", bs->msg);
+        bmicrosleep(5, 0);
+        return 0;
+      }
    }
+   if (!bnet_ssl_server(bs, password, ssl_need, ssl_has)) {
+      return 0;
+   }
+
    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(mp_chr(bs->msg), (char *)hmac, 16) + 1;
    if (!bnet_send(bs)) {
+      Dmsg0(100, "Send response failed.\n");
       return 0;
    }
    Dmsg1(99, "sending resp to challenge: %s\n", bs->msg);
@@ -100,9 +117,10 @@ int cram_md5_get_auth(BSOCK *bs, char *password)
       bmicrosleep(5, 0);
       return 0;
    }
-   if (strcmp(bs->msg, "1000 OK auth\n") == 0) {
+   if (strcmp(mp_chr(bs->msg), "1000 OK auth\n") == 0) {
       return 1;
    }
+   Dmsg1(100, "PW: %s\n", password);
    bmicrosleep(5, 0);
    return 0;
 }