]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/main.cpp
ebl fix segfault on win32
[bacula/bacula] / bacula / src / qt-console / main.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2008 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 John Walker.
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 <QApplication>
39 #include <QTranslator>
40 #include "bat.h"
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 #ifdef HAVE_WIN32
59 void dummy_free_jcr(JCR *jcr)
60 {}
61 #endif
62
63 int main(int argc, char *argv[])
64 {
65    int ch;
66    bool no_signals = true;
67    bool test_config = false;
68
69 #ifdef HAVE_WIN32
70    JCR *jcr = new_jcr(sizeof(JCR), dummy_free_jcr);
71 #endif
72
73    app = new QApplication(argc, argv);        
74    app->setQuitOnLastWindowClosed(true);
75    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
76      
77    QTranslator qtTranslator;
78    qtTranslator.load(QString("qt_") + QLocale::system().name());
79    app->installTranslator(&qtTranslator);
80
81    QTranslator batTranslator;
82    batTranslator.load(QString("bat_") + QLocale::system().name());
83    app->installTranslator(&batTranslator);
84
85
86
87 #ifdef xENABLE_NLS
88    setlocale(LC_ALL, "");
89    bindtextdomain("bacula", LOCALEDIR);
90    textdomain("bacula");
91 #endif
92
93    init_stack_dump();
94    my_name_is(argc, argv, "bat");
95    init_msg(NULL, NULL);
96    working_directory  = "/tmp";
97
98    struct sigaction sigignore;
99    sigignore.sa_flags = 0;
100    sigignore.sa_handler = SIG_IGN;
101    sigfillset(&sigignore.sa_mask);
102    sigaction(SIGPIPE, &sigignore, NULL);
103    sigaction(SIGUSR2, &sigignore, NULL);
104
105
106    while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
107       switch (ch) {
108       case 'c':                    /* configuration file */
109          if (configfile != NULL) {
110             free(configfile);
111          }
112          configfile = bstrdup(optarg);
113          break;
114
115       case 'd':
116          debug_level = atoi(optarg);
117          if (debug_level <= 0)
118             debug_level = 1;
119          break;
120
121       case 's':                    /* turn off signals */
122          no_signals = true;
123          break;
124
125       case 't':
126          test_config = true;
127          break;
128
129       case '?':
130       default:
131          usage();
132       }
133    }
134    argc -= optind;
135    argv += optind;
136
137
138    if (!no_signals) {
139       init_signals(terminate_console);
140    }
141
142    if (argc) {
143       usage();
144    }
145
146    if (configfile == NULL) {
147       configfile = bstrdup(CONFIG_FILE);
148    }
149
150    config = new_config_parser();
151    parse_bat_config(config, configfile, M_ERROR_TERM);
152
153    if (init_crypto() != 0) {
154       Emsg0(M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
155    }
156
157    if (!check_resources()) {
158       Emsg1(M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
159    }
160
161    mainWin = new MainWin;
162    mainWin->show();
163
164    return app->exec();
165 }
166
167 void terminate_console(int /*sig*/)
168 {
169    exit(0);
170 }
171
172 static void usage()
173 {
174    fprintf(stderr, _(
175 PROG_COPYRIGHT
176 "\nVersion: %s (%s) %s %s %s\n\n"
177 "Usage: bat [-s] [-c config_file] [-d debug_level] [config_file]\n"
178 "       -c <file>   set configuration file to file\n"
179 "       -dnn        set debug level to nn\n"
180 "       -s          no signals\n"
181 "       -t          test - read configuration and exit\n"
182 "       -?          print this message.\n"
183 "\n"), 2007, VERSION, BDATE, HOST_OS, DISTNAME, DISTVER);
184
185    exit(1);
186 }
187
188 #ifdef xxx
189 /*
190  * Call-back for reading a passphrase for an encrypted PEM file
191  * This function uses getpass(), which uses a static buffer and is NOT thread-safe.
192  */
193 static int tls_pem_callback(char *buf, int size, const void *userdata)
194 {
195 #ifdef HAVE_TLS
196    const char *prompt = (const char *) userdata;
197    char *passwd;
198
199    passwd = getpass(prompt);
200    bstrncpy(buf, passwd, size);
201    return (strlen(buf));
202 #else
203    buf[0] = 0;
204    return 0;
205 #endif
206 }
207 #endif
208
209
210 /*
211  * Make a quick check to see that we have all the
212  * resources needed.
213  */
214 static int check_resources()
215 {
216    bool ok = true;
217    DIRRES *director;
218    int numdir;
219    bool tls_needed;
220
221    LockRes();
222
223    numdir = 0;
224    foreach_res(director, R_DIRECTOR) {
225       numdir++;
226       /* tls_require implies tls_enable */
227       if (director->tls_require) {
228          if (have_tls) {
229             director->tls_enable = true;
230          } else {
231             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
232             ok = false;
233             continue;
234          }
235       }
236       tls_needed = director->tls_enable || director->tls_authenticate;
237
238       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && tls_needed) {
239          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
240                              " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
241                              " At least one CA certificate store is required.\n"),
242                              director->hdr.name, configfile);
243          ok = false;
244       }
245    }
246    
247    if (numdir == 0) {
248       Emsg1(M_FATAL, 0, _("No Director resource defined in %s\n"
249                           "Without that I don't how to speak to the Director :-(\n"), configfile);
250       ok = false;
251    }
252
253    CONRES *cons;
254    /* Loop over Consoles */
255    foreach_res(cons, R_CONSOLE) {
256       /* tls_require implies tls_enable */
257       if (cons->tls_require) {
258          if (have_tls) {
259             cons->tls_enable = true;
260          } else {
261             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
262             ok = false;
263             continue;
264          }
265       }
266       tls_needed = cons->tls_enable || cons->tls_authenticate;
267
268       if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir) && tls_needed) {
269          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
270                              " or \"TLS CA Certificate Dir\" are defined for Console \"%s\" in %s.\n"),
271                              cons->hdr.name, configfile);
272          ok = false;
273       }
274    }
275
276    UnlockRes();
277
278    return ok;
279 }