]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/filed/plugins/api.c
fix crash on exchange restore fail
[bacula/bacula] / bacula / src / win32 / filed / plugins / api.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2008-2009 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, which is 
11    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 Kern Sibbald.
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  *  Written by James Harper, October 2008
30  */
31
32 #include "exchange-fd.h"
33
34 HrESEBackupRestoreGetNodes_t HrESEBackupRestoreGetNodes;
35 HrESEBackupPrepare_t HrESEBackupPrepare;
36 HrESEBackupGetLogAndPatchFiles_t HrESEBackupGetLogAndPatchFiles;
37 HrESEBackupTruncateLogs_t HrESEBackupTruncateLogs;
38 HrESEBackupEnd_t HrESEBackupEnd;
39 HrESEBackupSetup_t HrESEBackupSetup;
40 HrESEBackupInstanceEnd_t HrESEBackupInstanceEnd;
41 HrESEBackupOpenFile_t HrESEBackupOpenFile;
42 HrESEBackupReadFile_t HrESEBackupReadFile;
43 HrESEBackupCloseFile_t HrESEBackupCloseFile;
44
45 HrESERestoreOpen_t HrESERestoreOpen;
46 HrESERestoreReopen_t HrESERestoreReopen;
47 HrESERestoreComplete_t HrESERestoreComplete;
48 HrESERestoreClose_t HrESERestoreClose;
49 HrESERestoreGetEnvironment_t HrESERestoreGetEnvironment;
50 HrESERestoreSaveEnvironment_t HrESERestoreSaveEnvironment;
51 HrESERestoreAddDatabase_t HrESERestoreAddDatabase;
52 HrESERestoreOpenFile_t HrESERestoreOpenFile;
53
54 bRC
55 loadExchangeApi()
56 {
57    HMODULE h;
58    LONG status;
59    HKEY key_handle;
60    WCHAR *buf;
61    DWORD buf_len;
62    DWORD type;
63
64    status = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\BackupRestore\\DLLPaths", &key_handle);
65    if (status != ERROR_SUCCESS)
66    {
67       _JobMessageNull(M_ERROR, "Cannot get key for Exchange DLL path, result = %08x\n", status);
68       return bRC_Error;
69    }
70    
71    type = REG_EXPAND_SZ;
72    status = RegQueryValueExW(key_handle, L"esebcli2", NULL, &type, NULL, &buf_len);
73    if (status != ERROR_SUCCESS)
74    {
75       _JobMessageNull(M_ERROR, "Cannot get key for Exchange DLL path, result = %08x\n", status);
76       return bRC_Error;
77    }
78    buf_len += 2;
79    buf = new WCHAR[buf_len];
80
81    type = REG_EXPAND_SZ;
82    status = RegQueryValueExW(key_handle, L"esebcli2", NULL, &type, (LPBYTE)buf, &buf_len);
83    if (status != ERROR_SUCCESS)
84    {
85       _JobMessageNull(M_ERROR, "Cannot get key for Exchange DLL path, result = %08x\n", status);
86       delete buf;
87       return bRC_Error;
88    }
89
90 printf("Got value %S\n", buf);
91
92    // strictly speaking, a REG_EXPAND_SZ should be run through ExpandEnvironmentStrings
93
94    h = LoadLibraryW(buf);
95    delete buf;
96    if (!h) {
97       return bRC_Error;
98    }
99    HrESEBackupRestoreGetNodes = (HrESEBackupRestoreGetNodes_t)GetProcAddress(h, "HrESEBackupRestoreGetNodes");
100    HrESEBackupPrepare = (HrESEBackupPrepare_t)GetProcAddress(h, "HrESEBackupPrepare");
101    HrESEBackupEnd = (HrESEBackupEnd_t)GetProcAddress(h, "HrESEBackupEnd");
102    HrESEBackupSetup = (HrESEBackupSetup_t)GetProcAddress(h, "HrESEBackupSetup");
103    HrESEBackupGetLogAndPatchFiles = (HrESEBackupGetLogAndPatchFiles_t)GetProcAddress(h, "HrESEBackupGetLogAndPatchFiles");
104    HrESEBackupTruncateLogs = (HrESEBackupTruncateLogs_t)GetProcAddress(h, "HrESEBackupTruncateLogs");
105    HrESEBackupInstanceEnd = (HrESEBackupInstanceEnd_t)GetProcAddress(h, "HrESEBackupInstanceEnd");
106    HrESEBackupOpenFile = (HrESEBackupOpenFile_t)GetProcAddress(h, "HrESEBackupOpenFile");
107    HrESEBackupReadFile = (HrESEBackupReadFile_t)GetProcAddress(h, "HrESEBackupReadFile");
108    HrESEBackupCloseFile = (HrESEBackupCloseFile_t)GetProcAddress(h, "HrESEBackupCloseFile");
109    HrESERestoreOpen = (HrESERestoreOpen_t)GetProcAddress(h, "HrESERestoreOpen");
110    HrESERestoreReopen = (HrESERestoreReopen_t)GetProcAddress(h, "HrESERestoreReopen");
111    HrESERestoreComplete = (HrESERestoreComplete_t)GetProcAddress(h, "HrESERestoreComplete");
112    HrESERestoreClose = (HrESERestoreClose_t)GetProcAddress(h, "HrESERestoreClose");
113    HrESERestoreSaveEnvironment = (HrESERestoreSaveEnvironment_t)GetProcAddress(h, "HrESERestoreSaveEnvironment");
114    HrESERestoreGetEnvironment = (HrESERestoreGetEnvironment_t)GetProcAddress(h, "HrESERestoreGetEnvironment");
115    HrESERestoreAddDatabase = (HrESERestoreAddDatabase_t)GetProcAddress(h, "HrESERestoreAddDatabase");
116    HrESERestoreOpenFile = (HrESERestoreOpenFile_t)GetProcAddress(h, "HrESERestoreOpenFile");
117    return bRC_OK;
118 }
119
120 const char *
121 ESEErrorMessage(HRESULT result)
122 {
123    switch (result) {
124    case 0:
125       return "No error.";
126    case hrLogfileHasBadSignature:
127       return "Log file has bad signature. Check that no stale files are left in the Exchange data/log directories.";
128    case hrCBDatabaseInUse:
129       return "Database in use. Make sure database is dismounted.";
130    case hrRestoreAtFileLevel:
131       return "File must be restored using Windows file I/O calls.";
132    case hrMissingFullBackup:
133       return "Exchange reports that no previous full backup has been done.";
134    case hrBackupInProgress:
135       return "Exchange backup already in progress.";
136    case hrLogfileNotContiguous:
137       return "Existing log file is not contiguous. Check that no stale files are left in the Exchange data/log directories.";
138    case hrErrorFromESECall:
139       return "Error returned from ESE function call. Check the Windows Event Logs for more information.";
140    case hrCBDatabaseNotFound:
141       return "Database not found. Check that the Database you are trying to restore actually exists in the Storage Group you are restoring to.";
142    default:
143       return "Unknown error.";
144    }
145 }