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