]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/main.cpp
Get new api working + mark/unmark restore files
[bacula/bacula] / bacula / src / qt-console / main.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-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  *   Version $Id$
30  *
31  *  Main program for bat (qt-console)
32  *
33  *   Kern Sibbald, January MMVII
34  *
35  */ 
36
37
38 #include <QApplication>
39 #include "bat.h"
40
41
42 /* Forward referenced functions */
43 void terminate_console(int sig);                                
44 static void usage();
45 static int check_resources();
46
47 #define CONFIG_FILE "./bat.conf"   /* default configuration file */
48
49 /* Static variables */
50 static char *configfile = NULL;
51
52 MainWin *mainWin;
53 QApplication *app;
54 bool commDebug = false;
55
56
57 int main(int argc, char *argv[])
58 {
59
60    int ch;
61    bool no_signals = true;
62    bool test_config = false;
63
64
65    app = new QApplication(argc, argv);        
66    app->setQuitOnLastWindowClosed(true);
67
68
69 #ifdef ENABLE_NLS
70    setlocale(LC_ALL, "");
71    bindtextdomain("bacula", LOCALEDIR);
72    textdomain("bacula");
73 #endif
74
75    init_stack_dump();
76    my_name_is(argc, argv, "gnome-console");
77    init_msg(NULL, NULL);
78    working_directory  = "/tmp";
79
80    struct sigaction sigignore;
81    sigignore.sa_flags = 0;
82    sigignore.sa_handler = SIG_IGN;
83    sigfillset(&sigignore.sa_mask);
84    sigaction(SIGPIPE, &sigignore, NULL);
85
86    while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
87       switch (ch) {
88       case 'c':                    /* configuration file */
89          if (configfile != NULL) {
90             free(configfile);
91          }
92          configfile = bstrdup(optarg);
93          break;
94
95       case 'd':
96          debug_level = atoi(optarg);
97          if (debug_level <= 0)
98             debug_level = 1;
99          break;
100
101       case 's':                    /* turn off signals */
102          no_signals = true;
103          break;
104
105       case 't':
106          test_config = true;
107          break;
108
109       case '?':
110       default:
111          usage();
112       }
113    }
114    argc -= optind;
115    argv += optind;
116
117
118    if (!no_signals) {
119       init_signals(terminate_console);
120    }
121
122    if (argc) {
123       usage();
124    }
125
126    if (configfile == NULL) {
127       configfile = bstrdup(CONFIG_FILE);
128    }
129
130    parse_config(configfile);
131
132    if (init_crypto() != 0) {
133       Emsg0(M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
134    }
135
136    if (!check_resources()) {
137       Emsg1(M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
138    }
139
140    mainWin = new MainWin;
141    mainWin->show();
142
143    return app->exec();
144 }
145
146 void terminate_console(int sig)
147 {
148    (void)sig;                         /* avoid compiler complaints */
149    exit(0);
150 }
151
152 static void usage()
153 {
154    fprintf(stderr, _(
155 PROG_COPYRIGHT
156 "\nVersion: %s (%s) %s %s %s\n\n"
157 "Usage: bat [-s] [-c config_file] [-d debug_level] [config_file]\n"
158 "       -c <file>   set configuration file to file\n"
159 "       -dnn        set debug level to nn\n"
160 "       -s          no signals\n"
161 "       -t          test - read configuration and exit\n"
162 "       -?          print this message.\n"
163 "\n"), 2007, VERSION, BDATE, HOST_OS, DISTNAME, DISTVER);
164
165    exit(1);
166 }
167
168 #ifdef xxx
169 /*
170  * Call-back for reading a passphrase for an encrypted PEM file
171  * This function uses getpass(), which uses a static buffer and is NOT thread-safe.
172  */
173 static int tls_pem_callback(char *buf, int size, const void *userdata)
174 {
175 #ifdef HAVE_TLS
176    const char *prompt = (const char *) userdata;
177    char *passwd;
178
179    passwd = getpass(prompt);
180    bstrncpy(buf, passwd, size);
181    return (strlen(buf));
182 #else
183    buf[0] = 0;
184    return 0;
185 #endif
186 }
187 #endif
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 }