]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/main.cpp
ebl Fix socket problem on win32
[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 and included
11    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 MainWin *mainWin;
42 QApplication *app;
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 int main(int argc, char *argv[])
55 {
56    int ch;
57    bool no_signals = true;
58    bool test_config = false;
59
60
61    app = new QApplication(argc, argv);        
62    app->setQuitOnLastWindowClosed(true);
63    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
64
65
66 #ifdef ENABLE_NLS
67    setlocale(LC_ALL, "");
68    bindtextdomain("bacula", LOCALEDIR);
69    textdomain("bacula");
70 #endif
71
72    init_stack_dump();
73    my_name_is(argc, argv, "bat");
74    init_msg(NULL, NULL);
75    working_directory  = "/tmp";
76
77    struct sigaction sigignore;
78    sigignore.sa_flags = 0;
79    sigignore.sa_handler = SIG_IGN;
80    sigfillset(&sigignore.sa_mask);
81    sigaction(SIGPIPE, &sigignore, NULL);
82    sigaction(SIGUSR2, &sigignore, NULL);
83
84
85    while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
86       switch (ch) {
87       case 'c':                    /* configuration file */
88          if (configfile != NULL) {
89             free(configfile);
90          }
91          configfile = bstrdup(optarg);
92          break;
93
94       case 'd':
95          debug_level = atoi(optarg);
96          if (debug_level <= 0)
97             debug_level = 1;
98          break;
99
100       case 's':                    /* turn off signals */
101          no_signals = true;
102          break;
103
104       case 't':
105          test_config = true;
106          break;
107
108       case '?':
109       default:
110          usage();
111       }
112    }
113    argc -= optind;
114    argv += optind;
115
116
117    if (!no_signals) {
118       init_signals(terminate_console);
119    }
120
121    if (argc) {
122       usage();
123    }
124
125    OSDependentInit();
126    WSA_Init();                        /* Initialize Windows sockets */
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    // WSA_Cleanup();                  /* TODO: check when we have to call it */
152    exit(0);
153 }
154
155 static void usage()
156 {
157    fprintf(stderr, _(
158 PROG_COPYRIGHT
159 "\nVersion: %s (%s) %s %s %s\n\n"
160 "Usage: bat [-s] [-c config_file] [-d debug_level] [config_file]\n"
161 "       -c <file>   set configuration file to file\n"
162 "       -dnn        set debug level to nn\n"
163 "       -s          no signals\n"
164 "       -t          test - read configuration and exit\n"
165 "       -?          print this message.\n"
166 "\n"), 2007, VERSION, BDATE, HOST_OS, DISTNAME, DISTVER);
167
168    exit(1);
169 }
170
171 #ifdef xxx
172 /*
173  * Call-back for reading a passphrase for an encrypted PEM file
174  * This function uses getpass(), which uses a static buffer and is NOT thread-safe.
175  */
176 static int tls_pem_callback(char *buf, int size, const void *userdata)
177 {
178 #ifdef HAVE_TLS
179    const char *prompt = (const char *) userdata;
180    char *passwd;
181
182    passwd = getpass(prompt);
183    bstrncpy(buf, passwd, size);
184    return (strlen(buf));
185 #else
186    buf[0] = 0;
187    return 0;
188 #endif
189 }
190 #endif
191
192
193 /*
194  * Make a quick check to see that we have all the
195  * resources needed.
196  */
197 static int check_resources()
198 {
199    bool ok = true;
200    DIRRES *director;
201    int numdir;
202
203    LockRes();
204
205    numdir = 0;
206    foreach_res(director, R_DIRECTOR) {
207       numdir++;
208       /* tls_require implies tls_enable */
209       if (director->tls_require) {
210          if (have_tls) {
211             director->tls_enable = true;
212          } else {
213             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
214             ok = false;
215             continue;
216          }
217       }
218
219       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && director->tls_enable) {
220          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
221                              " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
222                              " At least one CA certificate store is required.\n"),
223                              director->hdr.name, configfile);
224          ok = false;
225       }
226    }
227    
228    if (numdir == 0) {
229       Emsg1(M_FATAL, 0, _("No Director resource defined in %s\n"
230                           "Without that I don't how to speak to the Director :-(\n"), configfile);
231       ok = false;
232    }
233
234    CONRES *cons;
235    /* Loop over Consoles */
236    foreach_res(cons, R_CONSOLE) {
237       /* tls_require implies tls_enable */
238       if (cons->tls_require) {
239          if (have_tls) {
240             cons->tls_enable = true;
241          } else {
242             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
243             ok = false;
244             continue;
245          }
246       }
247
248       if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir) && cons->tls_enable) {
249          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
250                              " or \"TLS CA Certificate Dir\" are defined for Console \"%s\" in %s.\n"),
251                              cons->hdr.name, configfile);
252          ok = false;
253       }
254    }
255
256    UnlockRes();
257
258    return ok;
259 }