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