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