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