]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/cram-md5.c
04Jul05
[bacula/bacula] / bacula / src / lib / cram-md5.c
index 514c190755e55ee8ac0b51741dfdd11a9f4386f2..1eb58bb5525da22b7b267e6ee9c12c54ee62524c 100644 (file)
@@ -2,13 +2,13 @@
  *  Challenge Response Authentication Method using MD5 (CRAM-MD5)
  *
  * cram-md5 is based on RFC2104.
- * 
+ *
  * Written for Bacula by Kern E. Sibbald, May MMI.
  *
  *   Version $Id$
  */
 /*
-   Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
+   Copyright (C) 2000-2004 Kern Sibbald and John Walker
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
 
 #include "bacula.h"
 
-/* Authorize other end */
-int cram_md5_auth(BSOCK *bs, char *password, int ssl_need)
+/* Authorize other end
+ * Codes that tls_local_need and tls_remote_need can take:
+ *   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
+ */
+int cram_md5_auth(BSOCK *bs, char *password, int tls_local_need)
 {
    struct timeval t1;
    struct timeval t2;
@@ -49,27 +54,29 @@ int cram_md5_auth(BSOCK *bs, char *password, int ssl_need)
       bstrncpy(host, my_name, sizeof(host));
    }
    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)) {
+   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;
    }
 
-   Dmsg1(99, "%s", bs->msg);
    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;
    }
    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;
-   Dmsg3(99, "Authenticate %s: wanted %s, got %s\n", 
-              ok ? "OK" : "NOT OK", host, bs->msg);
+   if (ok) {
+      Dmsg1(50, "Authenticate OK %s\n", host);
+   } else {
+      Dmsg2(50, "Authenticate NOT OK: wanted %s, got %s\n", host, bs->msg);
+   }
    if (ok) {
       bnet_fsend(bs, "1000 OK auth\n");
    } else {
+      Dmsg1(50, "Auth failed PW: %s\n", password);
       bnet_fsend(bs, "1999 Authorization failed.\n");
       bmicrosleep(5, 0);
    }
@@ -77,45 +84,46 @@ int cram_md5_auth(BSOCK *bs, char *password, int ssl_need)
 }
 
 /* Get authorization from other end */
-int cram_md5_get_auth(BSOCK *bs, char *password, int ssl_need)
+int cram_md5_get_auth(BSOCK *bs, char *password, int *tls_remote_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) {
-      Dmsg1(99, "Wanted auth cram... Got: %s", bs->msg);
+      Dmsg1(50, "Msg too long wanted auth cram... Got: %s", bs->msg);
       bmicrosleep(5, 0);
       return 0;
    }
-   if (sscanf(bs->msg, "auth cram-md5 %s ssl=%d\n", chal, &ssl_has) != 2) {
-      ssl_has = BNET_SSL_NONE;
+   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-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;
       }
    }
-   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;
    if (!bnet_send(bs)) {
+      Dmsg1(50, "Send challenge failed. ERR=%s\n", bnet_strerror(bs));
       return 0;
    }
    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;
    }
    if (strcmp(bs->msg, "1000 OK auth\n") == 0) {
       return 1;
    }
+   Dmsg1(50, "Bad auth response: %s\n", bs->msg);
    bmicrosleep(5, 0);
    return 0;
 }