]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/main.cpp
Add qt-console configuration
[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 #include "mainwindow.h"
30 #include <QApplication>
31 #include "config.h"
32 #include "bacula.h"
33 #include "console_conf.h"
34 #include "jcr.h"
35
36
37 /* Imported functions */
38 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons);
39
40 /* Forward referenced functions */
41 void terminate_console(int sig);                                
42 static void usage();
43 static int check_resources();
44
45 #define CONFIG_FILE "./qt-console.conf"   /* default configuration file */
46
47 /* Static variables */
48 static char *configfile = NULL;
49
50 MainWindow *mainWin;
51
52 int main(int argc, char *argv[])
53 {
54
55    int ch;
56    bool no_signals = true;
57    bool test_config = false;
58
59
60    QApplication app(argc, argv);        
61    app.setQuitOnLastWindowClosed(true);
62
63    mainWin = new MainWindow;
64
65    mainWin->show();
66
67 #ifdef ENABLE_NLS
68    setlocale(LC_ALL, "");
69    bindtextdomain("bacula", LOCALEDIR);
70    textdomain("bacula");
71 #endif
72
73    init_stack_dump();
74    my_name_is(argc, argv, "gnome-console");
75    init_msg(NULL, NULL);
76    working_directory  = "/tmp";
77
78    struct sigaction sigignore;
79    sigignore.sa_flags = 0;
80    sigignore.sa_handler = SIG_IGN;
81    sigfillset(&sigignore.sa_mask);
82    sigaction(SIGPIPE, &sigignore, NULL);
83
84    while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
85       switch (ch) {
86       case 'c':                    /* configuration file */
87          if (configfile != NULL) {
88             free(configfile);
89          }
90          configfile = bstrdup(optarg);
91          break;
92
93       case 'd':
94          debug_level = atoi(optarg);
95          if (debug_level <= 0)
96             debug_level = 1;
97          break;
98
99       case 's':                    /* turn off signals */
100          no_signals = true;
101          break;
102
103       case 't':
104          test_config = true;
105          break;
106
107       case '?':
108       default:
109          usage();
110       }
111    }
112    argc -= optind;
113    argv += optind;
114
115
116    if (!no_signals) {
117       init_signals(terminate_console);
118    }
119
120    if (argc) {
121       usage();
122    }
123
124    if (configfile == NULL) {
125       configfile = bstrdup(CONFIG_FILE);
126    }
127
128    parse_config(configfile);
129    set_text("Configuration read.\n");
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    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: qt-console [-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 /*
165  * Call-back for reading a passphrase for an encrypted PEM file
166  * This function uses getpass(), which uses a static buffer and is NOT thread-safe.
167  */
168 static int tls_pem_callback(char *buf, int size, const void *userdata)
169 {
170 #ifdef HAVE_TLS
171    const char *prompt = (const char *) userdata;
172    char *passwd;
173
174    passwd = getpass(prompt);
175    bstrncpy(buf, passwd, size);
176    return (strlen(buf));
177 #else
178    buf[0] = 0;
179    return 0;
180 #endif
181 }
182
183
184 /*
185  * Make a quick check to see that we have all the
186  * resources needed.
187  */
188 static int check_resources()
189 {
190    bool ok = true;
191    DIRRES *director;
192    int numdir;
193
194    LockRes();
195
196    numdir = 0;
197    foreach_res(director, R_DIRECTOR) {
198       numdir++;
199       /* tls_require implies tls_enable */
200       if (director->tls_require) {
201          if (have_tls) {
202             director->tls_enable = true;
203          } else {
204             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
205             ok = false;
206             continue;
207          }
208       }
209
210       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && director->tls_enable) {
211          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
212                              " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
213                              " At least one CA certificate store is required.\n"),
214                              director->hdr.name, configfile);
215          ok = false;
216       }
217    }
218    
219    if (numdir == 0) {
220       Emsg1(M_FATAL, 0, _("No Director resource defined in %s\n"
221                           "Without that I don't how to speak to the Director :-(\n"), configfile);
222       ok = false;
223    }
224
225    CONRES *cons;
226    /* Loop over Consoles */
227    foreach_res(cons, R_CONSOLE) {
228       /* tls_require implies tls_enable */
229       if (cons->tls_require) {
230          if (have_tls) {
231             cons->tls_enable = true;
232          } else {
233             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
234             ok = false;
235             continue;
236          }
237       }
238
239       if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir) && cons->tls_enable) {
240          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
241                              " or \"TLS CA Certificate Dir\" are defined for Console \"%s\" in %s.\n"),
242                              cons->hdr.name, configfile);
243          ok = false;
244       }
245    }
246
247    UnlockRes();
248
249    return ok;
250 }