]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/qt-console/bcomm/dircomm.cpp
Fix invalid .mod command in BAT during restore
[bacula/bacula] / bacula / src / qt-console / bcomm / dircomm.cpp
index 94ddd6d105b48669cabe8d90f22e6351c3181012..33500b00c81e9e9005cd4711d5c749af5bc0ed81 100644 (file)
@@ -3,34 +3,22 @@
 
    Copyright (C) 2007-2011 Free Software Foundation Europe e.V.
 
-   The main author of Bacula is Kern Sibbald, with contributions from
-   many others, a complete list can be found in the file AUTHORS.
-   This program is Free Software; you can redistribute it and/or
-   modify it under the terms of version three of the GNU Affero General Public
-   License as published by the Free Software Foundation and included
-   in the file LICENSE.
-
-   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 GNU
-   General Public License for more details.
-
-   You should have received a copy of the GNU Affero General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA.
+   The main 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.
 
    Bacula® is a registered trademark of Kern Sibbald.
-   The licensor of Bacula is the Free Software Foundation Europe
-   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
-   Switzerland, email:ftf@fsfeurope.org.
 */
 /*
  *  DirComm, Director communications,class
  *
  *   Kern Sibbald, January MMVII
  *
- */ 
+ */
 
 #include "bat.h"
 #include "console.h"
@@ -70,13 +58,12 @@ void DirComm::terminate()
       }
       if (mainWin->m_connDebug)
          Pmsg2(000, "DirComm %i terminating connections %s\n", m_conn, m_console->m_dir->name());
-      m_sock->close();
-      m_sock = NULL;
+      free_bsock(m_sock);
    }
 }
 
 /*
- * Connect to Director. 
+ * Connect to Director.
  */
 bool DirComm::connect_dir()
 {
@@ -84,10 +71,16 @@ bool DirComm::connect_dir()
    utime_t heart_beat;
    char buf[1024];
    CONRES *cons;
-      
+   int numcon = 0;
+   int i = 0;
+
    buf[0] = 0;
 
-   if (m_sock) {
+   foreach_res(cons, R_CONSOLE) {
+      numcon++;
+   }
+
+   if (m_sock && !is_bsock_open(m_sock)) {
       mainWin->set_status( tr("Already connected."));
       m_console->display_textf(_("Already connected\"%s\".\n"),
             m_console->m_dir->name());
@@ -107,21 +100,47 @@ bool DirComm::connect_dir()
 
    /* Give GUI a chance */
    app->processEvents();
-   
+
    LockRes();
    /* If cons==NULL, default console will be used */
-   cons = (CONRES *)GetNextRes(R_CONSOLE, NULL);
+   for (i=0; i<numcon; i++) {
+      cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)cons);
+      if (cons->director && strcasecmp(cons->director, m_console->m_dir->name()) == 0) {
+         break;
+      }
+      if (i == (numcon - 1)) {
+         cons = NULL;
+      }
+   }
+
+   /* Look for the first non-linked console */
+   if (cons == NULL) {
+      for (i=0; i<numcon; i++) {
+         cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)cons);
+         if (cons->director == NULL) {
+            break;
+         }
+         if (i == (numcon - 1)) {
+            cons = NULL;
+         }
+      }
+   }
+
+   /* If no console, take first one */
+   if (!cons) {
+      cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
+   }
    UnlockRes();
 
    /* Initialize Console TLS context once */
    if (cons && !cons->tls_ctx && (cons->tls_enable || cons->tls_require)) {
       /* Generate passphrase prompt */
-      bsnprintf(buf, sizeof(buf), "Passphrase for Console \"%s\" TLS private key: ", 
+      bsnprintf(buf, sizeof(buf), "Passphrase for Console \"%s\" TLS private key: ",
                 cons->name());
 
       /* Initialize TLS context:
        * Args: CA certfile, CA certdir, Certfile, Keyfile,
-       * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer   
+       * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer
        */
       cons->tls_ctx = new_tls_context(cons->tls_ca_certfile,
          cons->tls_ca_certdir, cons->tls_certfile,
@@ -140,7 +159,7 @@ bool DirComm::connect_dir()
    /* Initialize Director TLS context once */
    if (!m_console->m_dir->tls_ctx && (m_console->m_dir->tls_enable || m_console->m_dir->tls_require)) {
       /* Generate passphrase prompt */
-      bsnprintf(buf, sizeof(buf), "Passphrase for Director \"%s\" TLS private key: ", 
+      bsnprintf(buf, sizeof(buf), "Passphrase for Director \"%s\" TLS private key: ",
                 m_console->m_dir->name());
 
       /* Initialize TLS context:
@@ -167,11 +186,17 @@ bool DirComm::connect_dir()
       heart_beat = cons->heartbeat_interval;
    } else {
       heart_beat = 0;
-   }        
+   }
 
-   m_sock = bnet_connect(NULL, 5, 15, heart_beat,
+   if (!m_sock) {
+      m_sock = new_bsock();
+   }
+   if (!m_sock->connect(NULL, 5, 15, heart_beat,
                           _("Director daemon"), m_console->m_dir->address,
-                          NULL, m_console->m_dir->DIRport, 0);
+                          NULL, m_console->m_dir->DIRport, 0)) {
+      m_sock->destroy();
+      m_sock = NULL;
+   }
    if (m_sock == NULL) {
       mainWin->set_status("Connection failed");
       if (mainWin->m_connDebug) {
@@ -207,7 +232,7 @@ bool DirComm::connect_dir()
 
    mainWin->set_status(_("Initializing ..."));
 
-   /* 
+   /*
     * Set up input notifier
     */
    m_notifier = new QSocketNotifier(m_sock->m_fd, QSocketNotifier::Read, 0);
@@ -236,8 +261,8 @@ bail_out:
    return false;
 }
 
-/* 
- * This should be moved into a bSocket class 
+/*
+ * This should be moved into a bSocket class
  */
 char *DirComm::msg()
 {
@@ -290,7 +315,7 @@ int DirComm::sock_read()
    return stat;
 }
 
-/* 
+/*
  * Blocking read from director
  */
 int DirComm::read()
@@ -306,7 +331,7 @@ int DirComm::read()
          stat = m_sock->wait_data_intr(0, 50000);
          if (stat > 0) {
             break;
-         } 
+         }
          app->processEvents();
          if (m_api_set && m_console->is_messagesPending() && is_notify_enabled() && m_console->hasFocus()) {
             if (mainWin->m_commDebug) Pmsg1(000, "conn %i process_events\n", m_conn);
@@ -369,12 +394,10 @@ int DirComm::read()
          mainWin->set_status(_("At prompt waiting for input ..."));
          break;
       case BNET_TEXT_INPUT:
-         if (mainWin->m_commDebug) Pmsg4(000, "conn %i TEXT_INPUT at_prompt=%d  m_in_select=%d notify=%d\n", 
+         if (mainWin->m_commDebug) Pmsg4(000, "conn %i TEXT_INPUT at_prompt=%d  m_in_select=%d notify=%d\n",
                m_conn, m_at_prompt, m_in_select, is_notify_enabled());
          if (!m_in_select && is_notify_enabled()) {
-            mainWin->waitExit();
             new textInputDialog(m_console, m_conn);
-         } else {
             if (mainWin->m_commDebug) Pmsg0(000, "!m_in_select && is_notify_enabled\n");
             m_at_prompt = true;
             m_at_main_prompt = false;
@@ -444,11 +467,10 @@ int DirComm::read()
          stat = BNET_HARDEOF;
          return stat;
       }
-      if (is_bnet_stop(m_sock)) {         /* error or term request */
+      if (m_sock->is_stop()) {         /* error or term request */
          if (mainWin->m_commDebug) Pmsg1(000, "conn %i BNET STOP\n", m_conn);
          m_console->stopTimer();
-         m_sock->close();
-         m_sock = NULL;
+         free_bsock(m_sock);
          mainWin->actionConnect->setIcon(QIcon(":images/disconnected.png"));
          QBrush redBrush(Qt::red);
          QTreeWidgetItem *item = mainWin->getFromHash(m_console);
@@ -463,7 +485,7 @@ int DirComm::read()
          stat = BNET_HARDEOF;
       }
       break;
-   } 
+   }
    return stat;
 }
 
@@ -487,7 +509,7 @@ void DirComm::notify_read_dir(int /* fd */)
 
 /*
  * When the notifier is enabled, read_dir() will automatically be
- * called by the Qt event loop when ever there is any output 
+ * called by the Qt event loop when ever there is any output
  * from the Director, and read_dir() will then display it on
  * the console.
  *
@@ -495,13 +517,13 @@ void DirComm::notify_read_dir(int /* fd */)
  * from the Directory, so we set notify to off.
  *    m_console->notify(false);
  */
-bool DirComm::notify(bool enable) 
-{ 
+bool DirComm::notify(bool enable)
+{
    bool prev_enabled = false;
    /* Set global flag */
    mainWin->m_notify = enable;
    if (m_notifier) {
-      prev_enabled = m_notifier->isEnabled();   
+      prev_enabled = m_notifier->isEnabled();
       m_notifier->setEnabled(enable);
       m_notify = enable;
       if (mainWin->m_connDebug) Pmsg3(000, "conn=%i set_notify=%d prev=%d\n", m_conn, enable, prev_enabled);
@@ -518,7 +540,7 @@ bool DirComm::is_notify_enabled() const
 
 /*
  * Call-back for reading a passphrase for an encrypted PEM file
- * This function uses getpass(), 
+ * This function uses getpass(),
  *  which uses a static buffer and is NOT thread-safe.
  */
 static int tls_pem_callback(char *buf, int size, const void *userdata)