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