]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/main.cpp
Require correct Qt version to build bat
[bacula/bacula] / bacula / src / qt-console / main.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2011 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.6.2 or later to be able to comple correctly
42  */
43 #if QT_VERSION < 0x040602
44 #error "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
45 #error "You need Qt version 4.6.2 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
172    mainWin = new MainWin;
173    mainWin->show();
174
175    return app->exec();
176 }
177
178 void terminate_console(int /*sig*/)
179 {
180 // WSA_Cleanup();                  /* TODO: check when we have to call it */
181    exit(0);
182 }
183
184 static void usage()
185 {
186    fprintf(stderr, _(
187 PROG_COPYRIGHT
188 "\nVersion: %s (%s) %s %s %s\n\n"
189 "Usage: bat [-s] [-c config_file] [-d debug_level] [config_file]\n"
190 "       -c <file>   set configuration file to file\n"
191 "       -dnn        set debug level to nn\n"
192 "       -s          no signals\n"
193 "       -t          test - read configuration and exit\n"
194 "       -?          print this message.\n"
195 "\n"), 2007, VERSION, BDATE, HOST_OS, DISTNAME, DISTVER);
196
197    exit(1);
198 }
199
200 /*
201  * Make a quick check to see that we have all the
202  * resources needed.
203  */
204 static int check_resources()
205 {
206    bool ok = true;
207    DIRRES *director;
208    int numdir;
209    bool tls_needed;
210
211    LockRes();
212
213    numdir = 0;
214    foreach_res(director, R_DIRECTOR) {
215       numdir++;
216       /* tls_require implies tls_enable */
217       if (director->tls_require) {
218          if (have_tls) {
219             director->tls_enable = true;
220          } else {
221             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
222             ok = false;
223             continue;
224          }
225       }
226       tls_needed = director->tls_enable || director->tls_authenticate;
227
228       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed) {
229          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
230                              " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
231                              " At least one CA certificate store is required.\n"),
232                              director->hdr.name, configfile);
233          ok = false;
234       }
235    }
236    
237    if (numdir == 0) {
238       Emsg1(M_FATAL, 0, _("No Director resource defined in %s\n"
239                           "Without that I don't how to speak to the Director :-(\n"), configfile);
240       ok = false;
241    }
242
243    CONRES *cons;
244    /* Loop over Consoles */
245    foreach_res(cons, R_CONSOLE) {
246       /* tls_require implies tls_enable */
247       if (cons->tls_require) {
248          if (have_tls) {
249             cons->tls_enable = true;
250          } else {
251             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
252             ok = false;
253             continue;
254          }
255       }
256       tls_needed = cons->tls_enable || cons->tls_authenticate;
257
258       if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir) && tls_needed) {
259          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
260                              " or \"TLS CA Certificate Dir\" are defined for Console \"%s\" in %s.\n"),
261                              cons->hdr.name, configfile);
262          ok = false;
263       }
264    }
265
266    UnlockRes();
267
268    return ok;
269 }