]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/authenticate.c
Fix #2085 About director segfault in cram-md5 function
[bacula/bacula] / bacula / src / dird / authenticate.c
index bb73cd3de23e1d647f495372941f8630274eeea2..0dce7ef5db62f139bf0c325ddf15d336c8cfb704 100644 (file)
@@ -1,43 +1,54 @@
+/*
+   Bacula(R) - The Network Backup Solution
+
+   Copyright (C) 2000-2015 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.
+*/
 /*
  *
- *   Bacula Director -- authorize.c -- handles authorization of
+ *   Bacula Director -- authenticate.c -- handles authorization of
  *     Storage and File daemons.
  *
- *     Kern Sibbald, May MMI
+ *    Written by: Kern Sibbald, May MMI
  *
  *    This routine runs as a thread and must be thread reentrant.
  *
- *   Version $Id$
- *
- */
-/*
-   Copyright (C) 2001-2005 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 ammended 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"
 #include "dird.h"
 
+static const int dbglvl = 50;
+
 extern DIRRES *director;
-extern char my_name[];
 
-/* Commands sent to Storage daemon and File daemon and received
- *  from the User Agent */
-static char hello[]    = "Hello Director %s calling\n";
+/* Version at end of Hello
+ *   prior to 06Aug13 no version
+ *      102 04Jun15 - added jobmedia change
+ */
+#define DIR_VERSION 102
+
 
-/* Response from Storage daemon */
-static char OKhello[]   = "3000 OK Hello\n";
-static char FDOKhello[] = "2000 OK Hello\n";
+/* Command sent to SD */
+static char hello[]    = "Hello %sDirector %s calling %d\n";
+
+/* Responses from Storage and File daemons */
+static char OKhello[]      = "3000 OK Hello";
+static char SDOKnewHello[] = "3000 OK Hello %d";
+static char FDOKhello[]    = "2000 OK Hello";
+static char FDOKnewHello[] = "2000 OK Hello %d";
 
 /* Sent to User Agent */
 static char Dir_sorry[]  = "1999 You are not authorized.\n";
@@ -53,8 +64,14 @@ bool authenticate_storage_daemon(JCR *jcr, STORE *store)
    char dirname[MAX_NAME_LENGTH];
    int tls_local_need = BNET_TLS_NONE;
    int tls_remote_need = BNET_TLS_NONE;
+   int compatible = true;
    bool auth_success = false;
 
+   if (!sd) {
+      Dmsg0(dbglvl, "Invalid bsock\n");
+      return false;
+   }
+
    /*
     * Send my name to the Storage daemon then do authentication
     */
@@ -62,10 +79,11 @@ bool authenticate_storage_daemon(JCR *jcr, STORE *store)
    bash_spaces(dirname);
    /* Timeout Hello after 1 min */
    btimer_t *tid = start_bsock_timer(sd, AUTH_TIMEOUT);
-   if (!bnet_fsend(sd, hello, dirname)) {
+   /* Sent Hello SD: Bacula Director <dirname> calling <version> */
+   if (!sd->fsend(hello, "SD: Bacula ", dirname, DIR_VERSION)) {
       stop_bsock_timer(tid);
-      Dmsg1(50, _("Error sending Hello to Storage daemon. ERR=%s\n"), bnet_strerror(sd));
-      Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to Storage daemon. ERR=%s\n"), bnet_strerror(sd));
+      Dmsg1(dbglvl, _("Error sending Hello to Storage daemon. ERR=%s\n"), sd->bstrerror());
+      Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to Storage daemon. ERR=%s\n"), sd->bstrerror());
       return 0;
    }
 
@@ -78,25 +96,30 @@ bool authenticate_storage_daemon(JCR *jcr, STORE *store)
      }
    }
 
-   auth_success = cram_md5_get_auth(sd, store->password, &tls_remote_need);
+   if (store->tls_authenticate) {
+      tls_local_need = BNET_TLS_REQUIRED;
+   }
+
+   auth_success = cram_md5_respond(sd, store->password, &tls_remote_need, &compatible);
    if (auth_success) {
-      auth_success = cram_md5_auth(sd, store->password, tls_local_need);
+      auth_success = cram_md5_challenge(sd, store->password, tls_local_need, compatible);
       if (!auth_success) {
-         Dmsg1(50, "cram_auth failed for %s\n", sd->who);
+         Dmsg1(dbglvl, "cram_challenge failed for %s\n", sd->who());
       }
    } else {
-      Dmsg1(50, "cram_get_auth failed for %s\n", sd->who);
+      Dmsg1(dbglvl, "cram_respond failed for %s\n", sd->who());
    }
 
    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,
-            _("Unable to authenticate with Storage daemon. Possible causes:\n"
+      Dmsg0(dbglvl, _("Director and Storage daemon passwords or names not the same.\n"));
+      Jmsg2(jcr, M_FATAL, 0,
+            _("Director unable to authenticate with Storage daemon at \"%s:%d\". Possible causes:\n"
             "Passwords or names not the same or\n"
             "Maximum Concurrent Jobs exceeded on the SD or\n"
             "SD networking messed up (restart daemon).\n"
-            "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"));
+            "For help, please see: " MANUAL_AUTH_URL "\n"),
+            sd->host(), sd->port());
       return 0;
    }
 
@@ -114,30 +137,40 @@ bool authenticate_storage_daemon(JCR *jcr, STORE *store)
       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)) {
+      if (!bnet_tls_client(store->tls_ctx, sd, NULL)) {
          stop_bsock_timer(tid);
-         Jmsg(jcr, M_FATAL, 0, _("TLS negotiation failed.\n"));
+         Jmsg(jcr, M_FATAL, 0, _("TLS negotiation failed with SD at \"%s:%d\"\n"),
+            sd->host(), sd->port());
          return 0;
       }
+      if (store->tls_authenticate) {       /* authentication only? */
+         sd->free_tls();                   /* yes, stop tls */
+      }
    }
-#endif
 
    Dmsg1(116, ">stored: %s", sd->msg);
-   if (bnet_recv(sd) <= 0) {
+   if (sd->recv() <= 0) {
       stop_bsock_timer(tid);
-      Jmsg1(jcr, M_FATAL, 0, _("bdird<stored: bad response to Hello command: ERR=%s\n"),
-         bnet_strerror(sd));
+      Jmsg3(jcr, M_FATAL, 0, _("bdird<stored: \"%s:%s\" bad response to Hello command: ERR=%s\n"),
+         sd->who(), sd->host(), sd->bstrerror());
       return 0;
    }
    Dmsg1(110, "<stored: %s", sd->msg);
    stop_bsock_timer(tid);
-   if (strncmp(sd->msg, OKhello, sizeof(OKhello)) != 0) {
-      Dmsg0(50, _("Storage daemon rejected Hello command\n"));
-      Jmsg0(jcr, M_FATAL, 0, _("Storage daemon rejected Hello command\n"));
+   jcr->SDVersion = 0;
+   if (sscanf(sd->msg, SDOKnewHello, &jcr->SDVersion) != 1 &&
+       strncmp(sd->msg, OKhello, sizeof(OKhello)) != 0) {
+      Dmsg0(dbglvl, _("Storage daemon rejected Hello command\n"));
+      Jmsg2(jcr, M_FATAL, 0, _("Storage daemon at \"%s:%d\" rejected Hello command\n"),
+         sd->host(), sd->port());
+      return 0;
+   }
+   if (jcr->SDVersion < 305) {
+      Jmsg2(jcr, M_FATAL, 0, _("Older Storage daemon at \"%s:%d\" incompatible with this Director.\n"),
+         sd->host(), sd->port());
       return 0;
    }
    return 1;
@@ -153,22 +186,26 @@ int authenticate_file_daemon(JCR *jcr)
    char dirname[MAX_NAME_LENGTH];
    int tls_local_need = BNET_TLS_NONE;
    int tls_remote_need = BNET_TLS_NONE;
+   int compatible = true;
    bool auth_success = false;
 
    /*
     * Send my name to the File daemon then do authentication
     */
-   bstrncpy(dirname, director->hdr.name, sizeof(dirname));
+   bstrncpy(dirname, director->name(), sizeof(dirname));
    bash_spaces(dirname);
-   /* Timeout Hello after 10 mins */
+   /* Timeout Hello after 1 min */
    btimer_t *tid = start_bsock_timer(fd, AUTH_TIMEOUT);
-   if (!bnet_fsend(fd, hello, dirname)) {
+   if (!fd->fsend(hello, "", dirname, DIR_VERSION)) {
       stop_bsock_timer(tid);
-      Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to File daemon. ERR=%s\n"), bnet_strerror(fd));
+      Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to File daemon at \"%s:%d\". ERR=%s\n"),
+           fd->host(), fd->port(), fd->bstrerror());
+      Dmsg3(50, _("Error sending Hello to File daemon at \"%s:%d\". ERR=%s\n"),
+           fd->host(), fd->port(), fd->bstrerror());
       return 0;
    }
+   Dmsg1(dbglvl, "Sent: %s", fd->msg);
 
-#ifdef HAVE_TLS
    /* TLS Requirement */
    if (client->tls_enable) {
      if (client->tls_require) {
@@ -177,69 +214,80 @@ int authenticate_file_daemon(JCR *jcr)
         tls_local_need = BNET_TLS_OK;
      }
    }
-#endif
 
-   auth_success = cram_md5_get_auth(fd, client->password, &tls_remote_need);
+   if (client->tls_authenticate) {
+      tls_local_need = BNET_TLS_REQUIRED;
+   }
+
+   auth_success = cram_md5_respond(fd, client->password, &tls_remote_need, &compatible);
    if (auth_success) {
-      auth_success = cram_md5_auth(fd, client->password, tls_local_need);
+      auth_success = cram_md5_challenge(fd, client->password, tls_local_need, compatible);
       if (!auth_success) {
-         Dmsg1(50, "cram_auth failed for %s\n", fd->who);
+         Dmsg1(dbglvl, "cram_auth failed for %s\n", fd->who());
       }
    } else {
-      Dmsg1(50, "cram_get_auth failed for %s\n", fd->who);
+      Dmsg1(dbglvl, "cram_get_auth failed for %s\n", fd->who());
    }
    if (!auth_success) {
       stop_bsock_timer(tid);
-      Dmsg0(50, _("Director and File daemon passwords or names not the same.\n"));
+      Dmsg0(dbglvl, _("Director and File daemon passwords or names not the same.\n"));
       Jmsg(jcr, M_FATAL, 0,
-            _("Unable to authenticate with File daemon. Possible causes:\n"
+            _("Unable to authenticate with File daemon at \"%s:%d\". Possible causes:\n"
             "Passwords or names not the same or\n"
             "Maximum Concurrent Jobs exceeded on the FD or\n"
             "FD networking messed up (restart daemon).\n"
-            "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"));
+            "For help, please see: " MANUAL_AUTH_URL "\n"),
+            fd->host(), fd->port());
       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"));
+      Jmsg(jcr, M_FATAL, 0, _("Authorization problem: FD \"%s:%s\" did not advertise required TLS support.\n"),
+           fd->who(), fd->host());
       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"));
+      Jmsg(jcr, M_FATAL, 0, _("Authorization problem: FD at \"%s:%d\" requires TLS.\n"),
+           fd->host(), fd->port());
       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)) {
+      if (!bnet_tls_client(client->tls_ctx, fd, client->tls_allowed_cns)) {
          stop_bsock_timer(tid);
-         Jmsg(jcr, M_FATAL, 0, _("TLS negotiation failed.\n"));
+         Jmsg(jcr, M_FATAL, 0, _("TLS negotiation failed with FD at \"%s:%d\".\n"),
+              fd->host(), fd->port());
          return 0;
       }
+      if (client->tls_authenticate) {        /* tls authentication only? */
+         fd->free_tls();                     /* yes, shutdown tls */
+      }
    }
-#endif
 
    Dmsg1(116, ">filed: %s", fd->msg);
-   if (bnet_recv(fd) <= 0) {
+   if (fd->recv() <= 0) {
       stop_bsock_timer(tid);
-      Dmsg1(50, _("Bad response from File daemon to Hello command: ERR=%s\n"),
-         bnet_strerror(fd));
-      Jmsg(jcr, M_FATAL, 0, _("Bad response from File daemon to Hello command: ERR=%s\n"),
-         bnet_strerror(fd));
+      Dmsg1(dbglvl, _("Bad response from File daemon to Hello command: ERR=%s\n"),
+         fd->bstrerror());
+      Jmsg(jcr, M_FATAL, 0, _("Bad response from File daemon at \"%s:%d\" to Hello command: ERR=%s\n"),
+         fd->host(), fd->port(), fd->bstrerror());
       return 0;
    }
-   Dmsg1(110, "<stored: %s", fd->msg);
+   Dmsg1(110, "<filed: %s", fd->msg);
    stop_bsock_timer(tid);
-   if (strncmp(fd->msg, FDOKhello, sizeof(FDOKhello)) != 0) {
-      Dmsg0(50, _("File daemon rejected Hello command\n"));
-      Jmsg(jcr, M_FATAL, 0, _("File daemon rejected Hello command\n"));
+   jcr->FDVersion = 0;
+   if (strncmp(fd->msg, FDOKhello, sizeof(FDOKhello)) != 0 &&
+       sscanf(fd->msg, FDOKnewHello, &jcr->FDVersion) != 1) {
+      Dmsg0(dbglvl, _("File daemon rejected Hello command\n"));
+      Jmsg(jcr, M_FATAL, 0, _("File daemon at \"%s:%d\" rejected Hello command\n"),
+           fd->host(), fd->port());
       return 0;
    }
    return 1;
@@ -253,33 +301,31 @@ int authenticate_user_agent(UAContext *uac)
    char name[MAX_NAME_LENGTH];
    int tls_local_need = BNET_TLS_NONE;
    int tls_remote_need = BNET_TLS_NONE;
+   bool tls_authenticate;
+   int compatible = true;
    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 */
+   int ua_version = 0;
 
-//  Emsg4(M_INFO, 0, _("UA Hello from %s:%s:%d is invalid. Len=%d\n"), ua->who,
-//          ua->host, ua->port, ua->msglen);
    if (ua->msglen < 16 || ua->msglen >= MAX_NAME_LENGTH + 15) {
-      Emsg4(M_ERROR, 0, _("UA Hello from %s:%s:%d is invalid. Len=%d\n"), ua->who,
-            ua->host, ua->port, ua->msglen);
+      Emsg4(M_ERROR, 0, _("UA Hello from %s:%s:%d is invalid. Len=%d\n"), ua->who(),
+            ua->host(), ua->port(), ua->msglen);
       return 0;
    }
 
-   if (sscanf(ua->msg, "Hello %127s calling\n", name) != 1) {
+   if (sscanf(ua->msg, "Hello %127s calling %d", name, &ua_version) != 2 &&
+       sscanf(ua->msg, "Hello %127s calling", name) != 1) {
       ua->msg[100] = 0;               /* terminate string */
-      Emsg4(M_ERROR, 0, _("UA Hello from %s:%s:%d is invalid. Got: %s\n"), ua->who,
-            ua->host, ua->port, ua->msg);
+      Emsg4(M_ERROR, 0, _("UA Hello from %s:%s:%d is invalid. Got: %s\n"), ua->who(),
+            ua->host(), ua->port(), ua->msg);
       return 0;
    }
 
    name[sizeof(name)-1] = 0;             /* terminate name */
    if (strcmp(name, "*UserAgent*") == 0) {  /* default console */
-#ifdef HAVE_TLS
       /* TLS Requirement */
       if (director->tls_enable) {
          if (director->tls_require) {
@@ -289,18 +335,23 @@ int authenticate_user_agent(UAContext *uac)
          }
       }
 
+      tls_authenticate = director->tls_authenticate;
+
+      if (tls_authenticate) {
+         tls_local_need = BNET_TLS_REQUIRED;
+      }
+
       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);
+      auth_success = cram_md5_challenge(ua, director->password, tls_local_need,
+                                        compatible) &&
+                     cram_md5_respond(ua, director->password, &tls_remote_need, &compatible);
    } else {
       unbash_spaces(name);
       cons = (CONRES *)GetResWithName(R_CONSOLE, name);
       if (cons) {
-#ifdef HAVE_TLS
          /* TLS Requirement */
          if (cons->tls_enable) {
             if (cons->tls_require) {
@@ -310,13 +361,19 @@ int authenticate_user_agent(UAContext *uac)
             }
          }
 
+         tls_authenticate = cons->tls_authenticate;
+
+         if (tls_authenticate) {
+            tls_local_need = BNET_TLS_REQUIRED;
+         }
+
          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);
+         auth_success = cram_md5_challenge(ua, cons->password, tls_local_need,
+                                           compatible) &&
+                     cram_md5_respond(ua, cons->password, &tls_remote_need, &compatible);
 
          if (auth_success) {
             uac->cons = cons;         /* save console resource pointer */
@@ -327,6 +384,7 @@ int authenticate_user_agent(UAContext *uac)
       }
    }
 
+
    /* 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:"
@@ -343,7 +401,6 @@ int authenticate_user_agent(UAContext *uac)
       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;
@@ -353,23 +410,26 @@ int authenticate_user_agent(UAContext *uac)
 
       /* Engage TLS! Full Speed Ahead! */
       if (!bnet_tls_server(tls_ctx, ua, verify_list)) {
-         Emsg0(M_ERROR, 0, "TLS negotiation failed.\n");
+         Emsg0(M_ERROR, 0, _("TLS negotiation failed.\n"));
          auth_success = false;
          goto auth_done;
       }
+      if (tls_authenticate) {            /* authentication only? */
+         ua->free_tls();                 /* stop tls */
+      }
    }
-#endif /* HAVE_TLS */
 
 
 /* Authorization Completed */
 auth_done:
    if (!auth_success) {
-      bnet_fsend(ua, "%s", _(Dir_sorry));
+      ua->fsend("%s", _(Dir_sorry));
       Emsg4(M_ERROR, 0, _("Unable to authenticate console \"%s\" at %s:%s:%d.\n"),
-            name, ua->who, ua->host, ua->port);
+            name, ua->who(), ua->host(), ua->port());
       sleep(5);
       return 0;
    }
-   bnet_fsend(ua, "1000 OK: %s Version: " VERSION " (" BDATE ")\n", my_name);
+   ua->fsend(_("1000 OK: %d %s %sVersion: %s (%s)\n"),
+      DIR_VERSION, my_name, "", VERSION, BDATE);
    return 1;
 }