]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/main.cpp
Update
[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 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
71 #ifdef ENABLE_NLS
72    setlocale(LC_ALL, "");
73    bindtextdomain("bacula", LOCALEDIR);
74    textdomain("bacula");
75 #endif
76
77    init_stack_dump();
78    my_name_is(argc, argv, "gnome-console");
79    init_msg(NULL, NULL);
80    working_directory  = "/tmp";
81
82    struct sigaction sigignore;
83    sigignore.sa_flags = 0;
84    sigignore.sa_handler = SIG_IGN;
85    sigfillset(&sigignore.sa_mask);
86    sigaction(SIGPIPE, &sigignore, NULL);
87
88    while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
89       switch (ch) {
90       case 'c':                    /* configuration file */
91          if (configfile != NULL) {
92             free(configfile);
93          }
94          configfile = bstrdup(optarg);
95          break;
96
97       case 'd':
98          debug_level = atoi(optarg);
99          if (debug_level <= 0)
100             debug_level = 1;
101          break;
102
103       case 's':                    /* turn off signals */
104          no_signals = true;
105          break;
106
107       case 't':
108          test_config = true;
109          break;
110
111       case '?':
112       default:
113          usage();
114       }
115    }
116    argc -= optind;
117    argv += optind;
118
119
120    if (!no_signals) {
121       init_signals(terminate_console);
122    }
123
124    if (argc) {
125       usage();
126    }
127
128    if (configfile == NULL) {
129       configfile = bstrdup(CONFIG_FILE);
130    }
131
132    parse_config(configfile);
133
134    if (init_crypto() != 0) {
135       Emsg0(M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
136    }
137
138    if (!check_resources()) {
139       Emsg1(M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
140    }
141
142    mainWin = new MainWin;
143    mainWin->show();
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 #ifdef xxx
171 /*
172  * Call-back for reading a passphrase for an encrypted PEM file
173  * This function uses getpass(), which uses a static buffer and is NOT thread-safe.
174  */
175 static int tls_pem_callback(char *buf, int size, const void *userdata)
176 {
177 #ifdef HAVE_TLS
178    const char *prompt = (const char *) userdata;
179    char *passwd;
180
181    passwd = getpass(prompt);
182    bstrncpy(buf, passwd, size);
183    return (strlen(buf));
184 #else
185    buf[0] = 0;
186    return 0;
187 #endif
188 }
189 #endif
190
191
192 /*
193  * Make a quick check to see that we have all the
194  * resources needed.
195  */
196 static int check_resources()
197 {
198    bool ok = true;
199    DIRRES *director;
200    int numdir;
201
202    LockRes();
203
204    numdir = 0;
205    foreach_res(director, R_DIRECTOR) {
206       numdir++;
207       /* tls_require implies tls_enable */
208       if (director->tls_require) {
209          if (have_tls) {
210             director->tls_enable = true;
211          } else {
212             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
213             ok = false;
214             continue;
215          }
216       }
217
218       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && director->tls_enable) {
219          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
220                              " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
221                              " At least one CA certificate store is required.\n"),
222                              director->hdr.name, configfile);
223          ok = false;
224       }
225    }
226    
227    if (numdir == 0) {
228       Emsg1(M_FATAL, 0, _("No Director resource defined in %s\n"
229                           "Without that I don't how to speak to the Director :-(\n"), configfile);
230       ok = false;
231    }
232
233    CONRES *cons;
234    /* Loop over Consoles */
235    foreach_res(cons, R_CONSOLE) {
236       /* tls_require implies tls_enable */
237       if (cons->tls_require) {
238          if (have_tls) {
239             cons->tls_enable = true;
240          } else {
241             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
242             ok = false;
243             continue;
244          }
245       }
246
247       if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir) && cons->tls_enable) {
248          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
249                              " or \"TLS CA Certificate Dir\" are defined for Console \"%s\" in %s.\n"),
250                              cons->hdr.name, configfile);
251          ok = false;
252       }
253    }
254
255    UnlockRes();
256
257    return ok;
258 }