]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/cram-md5.c
Fix #2085 About director segfault in cram-md5 function
[bacula/bacula] / bacula / src / lib / cram-md5.c
index e880bb1118f846060007d4c93c96b20a6788fb73..facdbe0087fdd1a8e08a3ac878e03de319f02c32 100644 (file)
@@ -1,3 +1,21 @@
+/*
+   Bacula(R) - The Network Backup Solution
+
+   Copyright (C) 2000-2016 Kern Sibbald
+
+   The original author of Bacula is Kern Sibbald, with contributions
+   from many others, a complete list can be found in the file AUTHORS.
+
+   You may use this file and others of this release according to the
+   license defined in the LICENSE file, which includes the Affero General
+   Public License, v3.0 ("AGPLv3") and some additional permissions and
+   terms pursuant to its AGPLv3 Section 7.
+
+   This notice must be preserved when any source code is 
+   conveyed and/or propagated.
+
+   Bacula(R) is a registered trademark of Kern Sibbald.
+*/
 /*
  *  Challenge Response Authentication Method using MD5 (CRAM-MD5)
  *
  *
  * Written for Bacula by Kern E. Sibbald, May MMI.
  *
- *   Version $Id$
  */
-/*
-   Copyright (C) 2000-2006 Kern Sibbald
 
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License
-   version 2 as amended with additional clauses defined in the
-   file LICENSE in the main source directory.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
-   the file LICENSE for additional details.
+#include "bacula.h"
 
- */
+const int dbglvl = 50;
 
-#include "bacula.h"
 
 /* Authorize other end
  * Codes that tls_local_need and tls_remote_need can take:
@@ -33,7 +39,7 @@
  *   Returns: false if authentication failed
  *            true if OK
  */
-bool cram_md5_challenge(BSOCK *bs, char *password, int tls_local_need, int compatible)
+bool cram_md5_challenge(BSOCK *bs, const char *password, int tls_local_need, int compatible)
 {
    struct timeval t1;
    struct timeval t2;
@@ -44,6 +50,11 @@ bool cram_md5_challenge(BSOCK *bs, char *password, int tls_local_need, int compa
    char host[MAXSTRING];
    uint8_t hmac[20];
 
+   if (!bs) {
+      Dmsg0(dbglvl, "Invalid bsock\n");
+      return false;
+   }
+
    gettimeofday(&t1, &tz);
    for (i=0; i<4; i++) {
       gettimeofday(&t2, &tz);
@@ -54,23 +65,24 @@ bool cram_md5_challenge(BSOCK *bs, char *password, int tls_local_need, int compa
    }
    /* 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 (compatible) {
-      if (!bnet_fsend(bs, "auth cram-md5c %s ssl=%d\n", chal, tls_local_need)) {
-         Dmsg1(50, "Bnet send challenge error.\n", bnet_strerror(bs));
+      Dmsg2(dbglvl, "send: auth cram-md5 challenge %s ssl=%d\n", chal, tls_local_need);
+      if (!bs->fsend("auth cram-md5 %s ssl=%d\n", chal, tls_local_need)) {
+         Dmsg1(dbglvl, "Send challenge comm error. ERR=%s\n", bs->bstrerror());
          return false;
       }
    } else {
       /* Old non-compatible system */
-      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));
+      Dmsg2(dbglvl, "send: auth cram-md5 challenge %s ssl=%d\n", chal, tls_local_need);
+      if (!bs->fsend("auth cram-md5 %s ssl=%d\n", chal, tls_local_need)) {
+         Dmsg1(dbglvl, "Send challenge comm error. ERR=%s\n", bs->bstrerror());
          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));
+   if (bs->wait_data(180) <= 0 || bs->recv() <= 0) {
+      Dmsg1(dbglvl, "Receive cram-md5 response comm error. ERR=%s\n", bs->bstrerror());
       bmicrosleep(5, 0);
       return false;
    }
@@ -80,43 +92,55 @@ bool cram_md5_challenge(BSOCK *bs, char *password, int tls_local_need, int compa
    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);
+      Dmsg1(dbglvl, "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(dbglvl, "Authenticate NOT OK: wanted %s, got %s\n", host, bs->msg);
+      }
    }
    if (ok) {
-      bnet_fsend(bs, "1000 OK auth\n");
+      bs->fsend("1000 OK auth\n");
    } else {
-      Dmsg1(50, "Auth failed PW: %s\n", password);
-      bnet_fsend(bs, _("1999 Authorization failed.\n"));
+      bs->fsend(_("1999 Authorization failed.\n"));
       bmicrosleep(5, 0);
    }
    return ok;
 }
 
 /* Respond to challenge from other end */
-bool cram_md5_respond(BSOCK *bs, char *password, int *tls_remote_need, int *compatible)
+bool cram_md5_respond(BSOCK *bs, const char *password, int *tls_remote_need, int *compatible)
 {
    char chal[MAXSTRING];
    uint8_t hmac[20];
 
+   if (!bs) {
+      Dmsg0(dbglvl, "Invalid bsock\n");
+      return false;
+   }
+
    *compatible = false;
-   if (bnet_recv(bs) <= 0) {
+   if (bs->recv() <= 0) {
       bmicrosleep(5, 0);
       return false;
    }
    if (bs->msglen >= MAXSTRING) {
-      Dmsg1(50, "Msg too long wanted auth cram... Got: %s", bs->msg);
+      Dmsg1(dbglvl, "Msg too long wanted auth cram... Got: %s", bs->msg);
       bmicrosleep(5, 0);
       return false;
    }
-   Dmsg1(100, "cram-get: %s", bs->msg);
+   Dmsg1(100, "cram-get received: %s", bs->msg);
+   /*
+    * Note that the next call is only to keep compatibility with very
+    *  old versions of Bacula that used a non-compatible base64 algorithm.
+    */
    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"));
+         Dmsg1(dbglvl, "Cannot scan received response to challenge: %s", bs->msg);
+         bs->fsend(_("1999 Authorization failed.\n"));
          bmicrosleep(5, 0);
          return false;
       }
@@ -124,21 +148,22 @@ bool cram_md5_respond(BSOCK *bs, char *password, int *tls_remote_need, int *comp
 
    hmac_md5((uint8_t *)chal, strlen(chal), (uint8_t *)password, strlen(password), hmac);
    bs->msglen = bin_to_base64(bs->msg, 50, (char *)hmac, 16, *compatible) + 1;
+// Don't turn the following on except for local debugging -- security
 // 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));
+   if (!bs->send()) {
+      Dmsg1(dbglvl, "Send challenge failed. ERR=%s\n", bs->bstrerror());
       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));
+   if (bs->wait_data(180) <= 0 || bs->recv() <= 0) {
+      Dmsg1(dbglvl, "Receive cram-md5 response failed. ERR=%s\n", bs->bstrerror());
       bmicrosleep(5, 0);
       return false;
    }
    if (strcmp(bs->msg, "1000 OK auth\n") == 0) {
       return true;
    }
-   Dmsg1(50, "Bad auth response: %s\n", bs->msg);
+   Dmsg1(dbglvl, "Received bad response: %s\n", bs->msg);
    bmicrosleep(5, 0);
    return false;
 }