]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/main.cpp
Merge branch 'master' of ssh://bacula.git.sourceforge.net/gitroot/bacula/bacula
[bacula/bacula] / bacula / src / qt-console / main.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2009 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 two of the GNU 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 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  *   Version $Id$
30  *
31  *  Main program for bat (qt-console)
32  *
33  *   Kern Sibbald, January MMVII
34  *
35  */ 
36
37
38 #include "bat.h"
39 #include <QApplication>
40 #include <QTranslator>
41
42 MainWin *mainWin;
43 QApplication *app;
44
45 /* Forward referenced functions */
46 void terminate_console(int sig);                                
47 static void usage();
48 static int check_resources();
49
50 extern bool parse_bat_config(CONFIG *config, const char *configfile, int exit_code);
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    int rc;
62    bool no_signals = true;
63    bool test_config = false;
64
65
66    app = new QApplication(argc, argv);        
67    app->setQuitOnLastWindowClosed(true);
68    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
69      
70    QTranslator qtTranslator;
71    qtTranslator.load(QString("qt_") + QLocale::system().name());
72    app->installTranslator(&qtTranslator);
73
74    QTranslator batTranslator;
75    batTranslator.load(QString("bat_") + QLocale::system().name());
76    app->installTranslator(&batTranslator);
77
78
79
80 #ifdef xENABLE_NLS
81    setlocale(LC_ALL, "");
82    bindtextdomain("bacula", LOCALEDIR);
83    textdomain("bacula");
84 #endif
85
86    init_stack_dump();
87    my_name_is(argc, argv, "bat");
88    init_msg(NULL, NULL);
89    working_directory  = "/tmp";
90
91    struct sigaction sigignore;
92    sigignore.sa_flags = 0;
93    sigignore.sa_handler = SIG_IGN;
94    sigfillset(&sigignore.sa_mask);
95    sigaction(SIGPIPE, &sigignore, NULL);
96    sigaction(SIGUSR2, &sigignore, NULL);
97
98
99    while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
100       switch (ch) {
101       case 'c':                    /* configuration file */
102          if (configfile != NULL) {
103             free(configfile);
104          }
105          configfile = bstrdup(optarg);
106          break;
107
108       case 'd':
109          debug_level = atoi(optarg);
110          if (debug_level <= 0)
111             debug_level = 1;
112          break;
113
114       case 's':                    /* turn off signals */
115          no_signals = true;
116          break;
117
118       case 't':
119          test_config = true;
120          break;
121
122       case '?':
123       default:
124          usage();
125       }
126    }
127    argc -= optind;
128    argv += optind;
129
130
131    if (!no_signals) {
132       init_signals(terminate_console);
133    }
134
135    if (argc) {
136       usage();
137    }
138
139    OSDependentInit();
140 #ifdef HAVE_WIN32
141    WSA_Init();                        /* Initialize Windows sockets */
142 #endif
143
144    if (configfile == NULL) {
145       configfile = bstrdup(CONFIG_FILE);
146    }
147
148    config = new_config_parser();
149    parse_bat_config(config, configfile, M_ERROR_TERM);
150
151    if (init_crypto() != 0) {
152       Emsg0(M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
153    }
154
155    if (!check_resources()) {
156       Emsg1(M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
157    }
158
159    mainWin = new MainWin;
160    mainWin->show();
161
162    rc = app->exec();
163 // sm_dump(false);
164    return rc;
165 }
166
167 void terminate_console(int /*sig*/)
168 {
169 // WSA_Cleanup();                  /* TODO: check when we have to call it */
170    exit(0);
171 }
172
173 static void usage()
174 {
175    fprintf(stderr, _(
176 PROG_COPYRIGHT
177 "\nVersion: %s (%s) %s %s %s\n\n"
178 "Usage: bat [-s] [-c config_file] [-d debug_level] [config_file]\n"
179 "       -c <file>   set configuration file to file\n"
180 "       -dnn        set debug level to nn\n"
181 "       -s          no signals\n"
182 "       -t          test - read configuration and exit\n"
183 "       -?          print this message.\n"
184 "\n"), 2007, VERSION, BDATE, HOST_OS, DISTNAME, DISTVER);
185
186    exit(1);
187 }
188
189 /*
190  * Make a quick check to see that we have all the
191  * resources needed.
192  */
193 static int check_resources()
194 {
195    bool ok = true;
196    DIRRES *director;
197    int numdir;
198    bool tls_needed;
199
200    LockRes();
201
202    numdir = 0;
203    foreach_res(director, R_DIRECTOR) {
204       numdir++;
205       /* tls_require implies tls_enable */
206       if (director->tls_require) {
207          if (have_tls) {
208             director->tls_enable = true;
209          } else {
210             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
211             ok = false;
212             continue;
213          }
214       }
215       tls_needed = director->tls_enable || director->tls_authenticate;
216
217       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed) {
218          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
219                              " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
220                              " At least one CA certificate store is required.\n"),
221                              director->hdr.name, configfile);
222          ok = false;
223       }
224    }
225    
226    if (numdir == 0) {
227       Emsg1(M_FATAL, 0, _("No Director resource defined in %s\n"
228                           "Without that I don't how to speak to the Director :-(\n"), configfile);
229       ok = false;
230    }
231
232    CONRES *cons;
233    /* Loop over Consoles */
234    foreach_res(cons, R_CONSOLE) {
235       /* tls_require implies tls_enable */
236       if (cons->tls_require) {
237          if (have_tls) {
238             cons->tls_enable = true;
239          } else {
240             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
241             ok = false;
242             continue;
243          }
244       }
245       tls_needed = cons->tls_enable || cons->tls_authenticate;
246
247       if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir) && tls_needed) {
248          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
249                              " or \"TLS CA Certificate Dir\" are defined for Console \"%s\" in %s.\n"),
250                              cons->hdr.name, configfile);
251          ok = false;
252       }
253    }
254
255    UnlockRes();
256
257    return ok;
258 }