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