]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/main.cpp
ebl Generated 193 translations (186 finished and 7 unfinished)
[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 #define CONFIG_FILE "./bat.conf"   /* default configuration file */
51
52 /* Static variables */
53 static char *configfile = NULL;
54
55 int main(int argc, char *argv[])
56 {
57    int ch;
58    bool no_signals = true;
59    bool test_config = false;
60
61
62    app = new QApplication(argc, argv);        
63    app->setQuitOnLastWindowClosed(true);
64    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
65      
66    QTranslator qtTranslator;
67    qtTranslator.load(QString("qt_") + QLocale::system().name());
68    app->installTranslator(&qtTranslator);
69
70    QTranslator batTranslator;
71    batTranslator.load(QString("bat_") + QLocale::system().name());
72    app->installTranslator(&batTranslator);
73
74
75
76 #ifdef xENABLE_NLS
77    setlocale(LC_ALL, "");
78    bindtextdomain("bacula", LOCALEDIR);
79    textdomain("bacula");
80 #endif
81
82    init_stack_dump();
83    my_name_is(argc, argv, "bat");
84    init_msg(NULL, NULL);
85    working_directory  = "/tmp";
86
87    struct sigaction sigignore;
88    sigignore.sa_flags = 0;
89    sigignore.sa_handler = SIG_IGN;
90    sigfillset(&sigignore.sa_mask);
91    sigaction(SIGPIPE, &sigignore, NULL);
92    sigaction(SIGUSR2, &sigignore, NULL);
93
94
95    while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
96       switch (ch) {
97       case 'c':                    /* configuration file */
98          if (configfile != NULL) {
99             free(configfile);
100          }
101          configfile = bstrdup(optarg);
102          break;
103
104       case 'd':
105          debug_level = atoi(optarg);
106          if (debug_level <= 0)
107             debug_level = 1;
108          break;
109
110       case 's':                    /* turn off signals */
111          no_signals = true;
112          break;
113
114       case 't':
115          test_config = true;
116          break;
117
118       case '?':
119       default:
120          usage();
121       }
122    }
123    argc -= optind;
124    argv += optind;
125
126
127    if (!no_signals) {
128       init_signals(terminate_console);
129    }
130
131    if (argc) {
132       usage();
133    }
134
135    if (configfile == NULL) {
136       configfile = bstrdup(CONFIG_FILE);
137    }
138
139    parse_config(configfile);
140
141    if (init_crypto() != 0) {
142       Emsg0(M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
143    }
144
145    if (!check_resources()) {
146       Emsg1(M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
147    }
148
149    mainWin = new MainWin;
150    mainWin->show();
151
152    return app->exec();
153 }
154
155 void terminate_console(int /*sig*/)
156 {
157    exit(0);
158 }
159
160 static void usage()
161 {
162    fprintf(stderr, _(
163 PROG_COPYRIGHT
164 "\nVersion: %s (%s) %s %s %s\n\n"
165 "Usage: bat [-s] [-c config_file] [-d debug_level] [config_file]\n"
166 "       -c <file>   set configuration file to file\n"
167 "       -dnn        set debug level to nn\n"
168 "       -s          no signals\n"
169 "       -t          test - read configuration and exit\n"
170 "       -?          print this message.\n"
171 "\n"), 2007, VERSION, BDATE, HOST_OS, DISTNAME, DISTVER);
172
173    exit(1);
174 }
175
176 #ifdef xxx
177 /*
178  * Call-back for reading a passphrase for an encrypted PEM file
179  * This function uses getpass(), which uses a static buffer and is NOT thread-safe.
180  */
181 static int tls_pem_callback(char *buf, int size, const void *userdata)
182 {
183 #ifdef HAVE_TLS
184    const char *prompt = (const char *) userdata;
185    char *passwd;
186
187    passwd = getpass(prompt);
188    bstrncpy(buf, passwd, size);
189    return (strlen(buf));
190 #else
191    buf[0] = 0;
192    return 0;
193 #endif
194 }
195 #endif
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 }