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