]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/main.cpp
kes Implement auto display of messages in bat.
[bacula/bacula] / bacula / src / qt-console / main.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2007 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 plus additions
11    that are listed 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 "bat.h"
40
41 MainWin *mainWin;
42 QApplication *app;
43
44 /*
45  * ***FIXME*** move the following two into the MainWin class or
46  *   the Console class.
47  */
48 bool g_commDebug = false;
49 bool g_displayAll = false;
50
51
52 /* Forward referenced functions */
53 void terminate_console(int sig);                                
54 static void usage();
55 static int check_resources();
56
57 #define CONFIG_FILE "./bat.conf"   /* default configuration file */
58
59 /* Static variables */
60 static char *configfile = NULL;
61
62 int main(int argc, char *argv[])
63 {
64    int ch;
65    bool no_signals = true;
66    bool test_config = false;
67
68
69    app = new QApplication(argc, argv);        
70    app->setQuitOnLastWindowClosed(true);
71
72
73 #ifdef ENABLE_NLS
74    setlocale(LC_ALL, "");
75    bindtextdomain("bacula", LOCALEDIR);
76    textdomain("bacula");
77 #endif
78
79    init_stack_dump();
80    my_name_is(argc, argv, "bat");
81    init_msg(NULL, NULL);
82    working_directory  = "/tmp";
83
84    struct sigaction sigignore;
85    sigignore.sa_flags = 0;
86    sigignore.sa_handler = SIG_IGN;
87    sigfillset(&sigignore.sa_mask);
88    sigaction(SIGPIPE, &sigignore, NULL);
89
90    while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
91       switch (ch) {
92       case 'c':                    /* configuration file */
93          if (configfile != NULL) {
94             free(configfile);
95          }
96          configfile = bstrdup(optarg);
97          break;
98
99       case 'd':
100          debug_level = atoi(optarg);
101          if (debug_level <= 0)
102             debug_level = 1;
103          break;
104
105       case 's':                    /* turn off signals */
106          no_signals = true;
107          break;
108
109       case 't':
110          test_config = true;
111          break;
112
113       case '?':
114       default:
115          usage();
116       }
117    }
118    argc -= optind;
119    argv += optind;
120
121
122    if (!no_signals) {
123       init_signals(terminate_console);
124    }
125
126    if (argc) {
127       usage();
128    }
129
130    if (configfile == NULL) {
131       configfile = bstrdup(CONFIG_FILE);
132    }
133
134    parse_config(configfile);
135
136    if (init_crypto() != 0) {
137       Emsg0(M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
138    }
139
140    if (!check_resources()) {
141       Emsg1(M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
142    }
143
144    mainWin = new MainWin;
145    mainWin->show();
146
147    return app->exec();
148 }
149
150 void terminate_console(int sig)
151 {
152    (void)sig;                         /* avoid compiler complaints */
153    exit(0);
154 }
155
156 static void usage()
157 {
158    fprintf(stderr, _(
159 PROG_COPYRIGHT
160 "\nVersion: %s (%s) %s %s %s\n\n"
161 "Usage: bat [-s] [-c config_file] [-d debug_level] [config_file]\n"
162 "       -c <file>   set configuration file to file\n"
163 "       -dnn        set debug level to nn\n"
164 "       -s          no signals\n"
165 "       -t          test - read configuration and exit\n"
166 "       -?          print this message.\n"
167 "\n"), 2007, VERSION, BDATE, HOST_OS, DISTNAME, DISTVER);
168
169    exit(1);
170 }
171
172 #ifdef xxx
173 /*
174  * Call-back for reading a passphrase for an encrypted PEM file
175  * This function uses getpass(), which uses a static buffer and is NOT thread-safe.
176  */
177 static int tls_pem_callback(char *buf, int size, const void *userdata)
178 {
179 #ifdef HAVE_TLS
180    const char *prompt = (const char *) userdata;
181    char *passwd;
182
183    passwd = getpass(prompt);
184    bstrncpy(buf, passwd, size);
185    return (strlen(buf));
186 #else
187    buf[0] = 0;
188    return 0;
189 #endif
190 }
191 #endif
192
193
194 /*
195  * Make a quick check to see that we have all the
196  * resources needed.
197  */
198 static int check_resources()
199 {
200    bool ok = true;
201    DIRRES *director;
202    int numdir;
203
204    LockRes();
205
206    numdir = 0;
207    foreach_res(director, R_DIRECTOR) {
208       numdir++;
209       /* tls_require implies tls_enable */
210       if (director->tls_require) {
211          if (have_tls) {
212             director->tls_enable = true;
213          } else {
214             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
215             ok = false;
216             continue;
217          }
218       }
219
220       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && director->tls_enable) {
221          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
222                              " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
223                              " At least one CA certificate store is required.\n"),
224                              director->hdr.name, configfile);
225          ok = false;
226       }
227    }
228    
229    if (numdir == 0) {
230       Emsg1(M_FATAL, 0, _("No Director resource defined in %s\n"
231                           "Without that I don't how to speak to the Director :-(\n"), configfile);
232       ok = false;
233    }
234
235    CONRES *cons;
236    /* Loop over Consoles */
237    foreach_res(cons, R_CONSOLE) {
238       /* tls_require implies tls_enable */
239       if (cons->tls_require) {
240          if (have_tls) {
241             cons->tls_enable = true;
242          } else {
243             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
244             ok = false;
245             continue;
246          }
247       }
248
249       if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir) && cons->tls_enable) {
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 }