+bool console_thread::inited = false;
+bool console_thread::configloaded = false;
+wxString console_thread::working_dir = wxT(".");
+
+int numdir = 0;
+
+/*
+ * Call-back for reading a passphrase for an encrypted PEM file
+ * This function uses getpass(), which uses a static buffer and is NOT thread-safe.
+ */
+static int tls_pem_callback(char *buf, int size, const void *userdata)
+{
+#if defined(HAVE_TLS) && !defined(HAVE_WIN32)
+ const char *prompt = (const char *) userdata;
+ char *passwd;
+
+ passwd = getpass(prompt);
+ bstrncpy(buf, passwd, size);
+ return (strlen(buf));
+#else
+ buf[0] = 0;
+ return 0;
+#endif
+}
+
+
+/*
+ * Make a quick check to see that we have all the
+ * resources needed.
+ */
+static int check_resources()
+{
+ int xOK = true;
+ DIRRES *director;
+
+ LockRes();
+
+ numdir = 0;
+ foreach_res(director, R_DIRECTOR) {
+ numdir++;
+ /* tls_require implies tls_enable */
+ if (director->tls_require) {
+ if (have_tls) {
+ director->tls_enable = true;
+ } else {
+ Jmsg(NULL, M_FATAL, 0, wxString(_("TLS required but not configured in Bacula.\n")).mb_str(*wxConvCurrent));
+ xOK = false;
+ continue;
+ }
+ }
+
+ if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && director->tls_enable) {
+ Jmsg(NULL, M_FATAL, 0, wxString(_("Neither \"TLS CA Certificate\" or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in config file.\nAt least one CA certificate store is required.\n")).mb_str(*wxConvCurrent),
+ director->hdr.name);
+ xOK = false;
+ }
+ }
+
+ if (numdir == 0) {
+ Jmsg(NULL, M_FATAL, 0, wxString(_("No Director resource defined in config file.\nWithout that I don't how to speak to the Director :-(\n")).mb_str(*wxConvCurrent));
+ xOK = false;
+ }
+
+ CONRES *cons;
+ /* Loop over Consoles */
+ foreach_res(cons, R_CONSOLE) {
+ /* tls_require implies tls_enable */
+ if (cons->tls_require) {
+ if (have_tls) {
+ cons->tls_enable = true;
+ } else {
+ Jmsg(NULL, M_FATAL, 0, wxString(_("TLS required but not configured in Bacula.\n")).mb_str(*wxConvCurrent));
+ xOK = false;
+ continue;
+ }
+ }
+
+ if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir) && cons->tls_enable) {
+ Jmsg(NULL, M_FATAL, 0, wxString(_("Neither \"TLS CA Certificate\" or \"TLS CA Certificate Dir\" are defined for Console \"%s\" in config file.\n")).mb_str(*wxConvCurrent),
+ cons->hdr.name);
+ xOK = false;
+ }
+ }
+ UnlockRes();
+ return xOK;
+}
+
+
+void console_thread::SetWorkingDirectory(wxString w_dir) {
+ if ((w_dir.Last() == '/') || (w_dir.Last() == '\\')) {
+ console_thread::working_dir = w_dir.Mid(0, w_dir.Length()-1);
+ }
+ else {
+ console_thread::working_dir = w_dir;
+ }
+}
+
+void console_thread::InitLib()
+{
+ if (WSA_Init() != 0) {
+ csprint(_("Error while initializing windows sockets...\n"));
+ inited = false;
+ return;
+ }
+
+ init_stack_dump();
+ my_name_is(0, NULL, "wx-console");
+ working_directory = (const char*) console_thread::working_dir.GetData();
+
+ inited = true;
+}
+
+void console_thread::FreeLib()
+{
+ if (inited) {
+ if (WSACleanup() != 0) {
+ csprint(_("Error while cleaning up windows sockets...\n"));
+ }
+ }
+}
+
+wxString errmsg;
+
+/*
+ * Format a scanner error message
+ */
+static void scan_err(const char *file, int line, LEX *lc, const char *msg, ...)
+{
+ va_list arg_ptr;
+ char buf[MAXSTRING];
+ char more[MAXSTRING];
+ wxString err;
+
+ va_start(arg_ptr, msg);
+ bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
+ va_end(arg_ptr);
+
+ if (lc->line_no > lc->begin_line_no) {
+ bsnprintf(more, sizeof(more),
+ wxString(_("Problem probably begins at line %d.\n")).mb_str(*wxConvCurrent), lc->begin_line_no);
+ } else {
+ more[0] = 0;
+ }
+
+ err.Format(_("Config error: %s\n : line %d, col %d of file %s\n%s\n%s"),
+ buf, lc->line_no, lc->col_no, lc->fname, lc->line, more);
+
+ errmsg << err;
+}
+
+wxString console_thread::LoadConfig(wxString configfile)
+{
+ if (!inited) {
+ InitLib();
+ if (!inited)
+ return _("Error while initializing library.");
+ }
+
+ free_config_resources();
+
+ MSGS* msgs = (MSGS *)bmalloc(sizeof(MSGS));
+ memset(msgs, 0, sizeof(MSGS));
+ for (int i=1; i<=M_MAX; i++) {
+#ifndef HAVE_WIN32
+ add_msg_dest(msgs, MD_STDOUT, i, NULL, NULL);
+#endif
+// add_msg_dest(msgs, MD_SYSLOG, i, NULL, NULL);
+ add_msg_dest(msgs, MD_CONSOLE, i, NULL, NULL);
+ }
+
+ init_msg(NULL, msgs);
+ //init_console_msg(console_thread::working_dir.mb_str(*wxConvCurrent));
+
+ errmsg = wxT("");
+ if (!parse_config(configfile.mb_str(*wxConvCurrent), &scan_err)) {
+ configloaded = false;
+ term_msg();
+ return errmsg;
+ }
+
+ if (init_crypto() != 0) {
+ Jmsg(NULL, M_ERROR_TERM, 0, wxString(_("Cryptographic library initialization failed.\n")).mb_str(*wxConvCurrent));
+ }
+
+ if (!check_resources()) {
+ Jmsg(NULL, M_ERROR_TERM, 0, wxString(_("Please correct configuration file.\n")).mb_str(*wxConvCurrent));
+ }
+
+ term_msg();
+ wxRemoveFile(console_thread::working_dir + wxT("/wx-console.conmsg"));
+ init_msg(NULL, NULL);
+
+ configloaded = true;
+
+ return wxT("");
+}
+