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