]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/main.cpp
Require Qt 4.8.1 to build bat
[bacula/bacula] / bacula / src / qt-console / main.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2012 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *  Main program for bat (qt-console)
30  *
31  *   Kern Sibbald, January MMVII
32  *
33  */ 
34
35
36 #include "bat.h"
37 #include <QApplication>
38 #include <QTranslator>
39
40 /*
41  * We need Qt version 4.8.1 or later to be able to comple correctly
42  */
43 #if QT_VERSION < 0x040801
44 #error "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
45 #error "You need Qt version 4.8.1 or later to build Bat"
46 #error "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
47 #endif
48
49 MainWin *mainWin;
50 QApplication *app;
51
52 /* Forward referenced functions */
53 void terminate_console(int sig);                                
54 static void usage();
55 static int check_resources();
56
57 extern bool parse_bat_config(CONFIG *config, const char *configfile, int exit_code);
58 extern void message_callback(int /* type */, char *msg);
59
60
61 #define CONFIG_FILE "bat.conf"     /* default configuration file */
62
63 /* Static variables */
64 static CONFIG *config;
65 static char *configfile = NULL;
66
67 int main(int argc, char *argv[])
68 {
69    int ch;
70    bool no_signals = true;
71    bool test_config = false;
72
73
74    app = new QApplication(argc, argv);        
75    app->setQuitOnLastWindowClosed(true);
76    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
77      
78    QTranslator qtTranslator;
79    qtTranslator.load(QString("qt_") + QLocale::system().name());
80    app->installTranslator(&qtTranslator);
81
82    QTranslator batTranslator;
83    batTranslator.load(QString("bat_") + QLocale::system().name());
84    app->installTranslator(&batTranslator);
85
86    register_message_callback(message_callback);
87
88 #ifdef xENABLE_NLS
89    setlocale(LC_ALL, "");
90    bindtextdomain("bacula", LOCALEDIR);
91    textdomain("bacula");
92 #endif
93
94 #ifdef HAVE_WIN32
95    set_trace(true);          /* output to trace file */
96 #endif
97
98    init_stack_dump();
99    my_name_is(argc, argv, "bat");
100    lmgr_init_thread();
101    init_msg(NULL, NULL);
102    working_directory  = "/tmp";
103
104    struct sigaction sigignore;
105    sigignore.sa_flags = 0;
106    sigignore.sa_handler = SIG_IGN;
107    sigfillset(&sigignore.sa_mask);
108    sigaction(SIGPIPE, &sigignore, NULL);
109    sigaction(SIGUSR2, &sigignore, NULL);
110
111
112    while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
113       switch (ch) {
114       case 'c':                    /* configuration file */
115          if (configfile != NULL) {
116             free(configfile);
117          }
118          configfile = bstrdup(optarg);
119          break;
120
121       case 'd':
122          debug_level = atoi(optarg);
123          if (debug_level <= 0)
124             debug_level = 1;
125          break;
126
127       case 's':                    /* turn off signals */
128          no_signals = true;
129          break;
130
131       case 't':
132          test_config = true;
133          break;
134
135       case '?':
136       default:
137          usage();
138       }
139    }
140    argc -= optind;
141    argv += optind;
142
143
144    if (!no_signals) {
145       init_signals(terminate_console);
146    }
147
148    if (argc) {
149       usage();
150    }
151
152    OSDependentInit();
153 #ifdef HAVE_WIN32
154    WSA_Init();                        /* Initialize Windows sockets */
155 #endif
156
157    if (configfile == NULL) {
158       configfile = bstrdup(CONFIG_FILE);
159    }
160
161    config = new_config_parser();
162    parse_bat_config(config, configfile, M_ERROR_TERM);
163
164    if (init_crypto() != 0) {
165       Emsg0(M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
166    }
167
168    if (!check_resources()) {
169       Emsg1(M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
170    }
171    if (test_config) {
172       exit(0);
173    }
174
175    mainWin = new MainWin;
176    mainWin->show();
177
178    return app->exec();
179 }
180
181 void terminate_console(int /*sig*/)
182 {
183 // WSA_Cleanup();                  /* TODO: check when we have to call it */
184    exit(0);
185 }
186
187 static void usage()
188 {
189    fprintf(stderr, _(
190 PROG_COPYRIGHT
191 "\nVersion: %s (%s) %s %s %s\n\n"
192 "Usage: bat [-s] [-c config_file] [-d debug_level] [config_file]\n"
193 "       -c <file>   set configuration file to file\n"
194 "       -dnn        set debug level to nn\n"
195 "       -s          no signals\n"
196 "       -t          test - read configuration and exit\n"
197 "       -?          print this message.\n"
198 "\n"), 2007, VERSION, BDATE, HOST_OS, DISTNAME, DISTVER);
199
200    exit(1);
201 }
202
203 /*
204  * Make a quick check to see that we have all the
205  * resources needed.
206  */
207 static int check_resources()
208 {
209    bool ok = true;
210    DIRRES *director;
211    int numdir;
212    bool tls_needed;
213
214    LockRes();
215
216    numdir = 0;
217    foreach_res(director, R_DIRECTOR) {
218       numdir++;
219       /* tls_require implies tls_enable */
220       if (director->tls_require) {
221          if (have_tls) {
222             director->tls_enable = true;
223          } else {
224             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
225             ok = false;
226             continue;
227          }
228       }
229       tls_needed = director->tls_enable || director->tls_authenticate;
230
231       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed) {
232          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
233                              " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
234                              " At least one CA certificate store is required.\n"),
235                              director->hdr.name, configfile);
236          ok = false;
237       }
238    }
239    
240    if (numdir == 0) {
241       Emsg1(M_FATAL, 0, _("No Director resource defined in %s\n"
242                           "Without that I don't how to speak to the Director :-(\n"), configfile);
243       ok = false;
244    }
245
246    CONRES *cons;
247    /* Loop over Consoles */
248    foreach_res(cons, R_CONSOLE) {
249       /* tls_require implies tls_enable */
250       if (cons->tls_require) {
251          if (have_tls) {
252             cons->tls_enable = true;
253          } else {
254             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
255             ok = false;
256             continue;
257          }
258       }
259       tls_needed = cons->tls_enable || cons->tls_authenticate;
260
261       if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir) && tls_needed) {
262          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
263                              " or \"TLS CA Certificate Dir\" are defined for Console \"%s\" in %s.\n"),
264                              cons->hdr.name, configfile);
265          ok = false;
266       }
267    }
268
269    UnlockRes();
270
271    return ok;
272 }