Skip to content

Commit 06919a6

Browse files
lstipakovcron2
authored andcommitted
openvpnserv: Fix writing messages to the event log
There are two problems with the current implementation: - due to the code bug, we never display actual error message corresponding to the Windows error code. We use FORMAT_MESSAGE_ALLOCATE_BUFFER, in which case we must pass a pointer to the LPTSTR, not the LPTSTR itself. - The error is not displayed in the "General" tab, which is very confusing. One needs to go to the "Details" tab to see what is wrong. This commit solves both problems. We now display a proper error message in addition to the text provided by the service ("what went wrong"). While on it, remove trailing symbols ín a safer way. To display the message in "General" tab, we create a registered message file (openvpnservmsg.dll), which contains message template. Note that this requires changes to the installer - we need to install the new DLL and add a registry entry. GitHub: #842 Change-Id: I423c9880def0eb479abb72bef2e8034a73cf5905 Signed-off-by: Lev Stipakov <[email protected]> Acked-by: Selva Nair <[email protected]> Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1188 Message-Id: <[email protected]> URL: https://sourceforge.net/p/openvpn/mailman/message/59234559/ Signed-off-by: Gert Doering <[email protected]>
1 parent 1687927 commit 06919a6

File tree

3 files changed

+82
-14
lines changed

3 files changed

+82
-14
lines changed

src/openvpnserv/CMakeLists.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ project(openvpnserv)
66

77
add_executable(openvpnserv)
88

9+
set(MC_GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/mc)
10+
911
target_include_directories(openvpnserv PRIVATE
1012
${CMAKE_CURRENT_BINARY_DIR}/../../
1113
../../include/
1214
../openvpn/
1315
../compat/
16+
${MC_GEN_DIR}
1417
)
1518
target_sources(openvpnserv PRIVATE
1619
common.c
@@ -33,3 +36,34 @@ if (MINGW)
3336
target_compile_options(openvpnserv PRIVATE -municode)
3437
target_link_options(openvpnserv PRIVATE -municode)
3538
endif ()
39+
40+
# below we generate a DLL which contains an event source for event log messages from eventmsg.mc template
41+
file(MAKE_DIRECTORY ${MC_GEN_DIR})
42+
set(MC_FILE ${CMAKE_CURRENT_SOURCE_DIR}/eventmsg.mc)
43+
44+
find_program(MC_COMPILER NAMES mc mc.exe x86_64-w64-mingw32-windmc i686-w64-mingw32-windmc windmc)
45+
46+
if (NOT MC_COMPILER)
47+
message(FATAL_ERROR "No message compiler found.")
48+
endif()
49+
50+
add_custom_command(
51+
OUTPUT ${MC_GEN_DIR}/eventmsg.rc ${MC_GEN_DIR}/eventmsg.h
52+
COMMAND ${MC_COMPILER} -U -h ${MC_GEN_DIR} -r ${MC_GEN_DIR} ${MC_FILE}
53+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/eventmsg.mc
54+
VERBATIM
55+
)
56+
57+
# generate rc file for DLL and header for the service binary
58+
add_custom_target(msg_mc_gen ALL DEPENDS ${MC_GEN_DIR}/eventmsg.rc ${MC_GEN_DIR}/eventmsg.h)
59+
60+
add_library(openvpnservmsg SHARED ${MC_GEN_DIR}/eventmsg.rc)
61+
62+
if (MSVC)
63+
set_target_properties(openvpnservmsg PROPERTIES LINK_FLAGS "/NOENTRY")
64+
else()
65+
target_link_options(openvpnservmsg PRIVATE "-Wl,--no-entry")
66+
endif()
67+
68+
add_dependencies(openvpnservmsg msg_mc_gen)
69+
add_dependencies(openvpnserv msg_mc_gen)

src/openvpnserv/common.c

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "service.h"
2424
#include "validate.h"
25+
#include "eventmsg.h"
2526

2627
LPCWSTR service_instance = L"";
2728
static wchar_t win_sys_path[MAX_PATH];
@@ -203,25 +204,29 @@ GetLastErrorText(void)
203204
LPWSTR tmp = NULL;
204205

205206
error = GetLastError();
206-
len = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
207-
| FORMAT_MESSAGE_ARGUMENT_ARRAY,
208-
NULL, error, LANG_NEUTRAL, tmp, 0, NULL);
207+
len = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
208+
| FORMAT_MESSAGE_IGNORE_INSERTS,
209+
NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&tmp, 0, NULL);
209210

210-
if (len == 0 || (long)_countof(buf) < (long)len + 14)
211+
if (!len || !tmp)
211212
{
212-
buf[0] = L'\0';
213-
}
214-
else
215-
{
216-
tmp[wcslen(tmp) - 2] = L'\0'; /* remove CR/LF characters */
217-
swprintf(buf, _countof(buf), L"%ls (0x%x)", tmp, error);
213+
swprintf(buf, _countof(buf), L"Unknown error (0x%lx)", error);
214+
if (tmp)
215+
{
216+
LocalFree(tmp);
217+
}
218+
return buf;
218219
}
219220

220-
if (tmp)
221+
/* trim trailing CR / LF / spaces safely */
222+
while (len && (tmp[len - 1] == L'\r' || tmp[len - 1] == L'\n' || tmp[len - 1] == L' '))
221223
{
222-
LocalFree(tmp);
224+
tmp[--len] = L'\0';
223225
}
224226

227+
swprintf(buf, _countof(buf), L"%ls (0x%lx)", tmp, error);
228+
229+
LocalFree(tmp);
225230
return buf;
226231
}
227232

@@ -253,8 +258,14 @@ MsgToEventLog(DWORD flags, LPCWSTR format, ...)
253258

254259
const WCHAR *mesg[] = { msg[0], msg[1] };
255260
ReportEvent(hEventSource,
256-
flags & MSG_FLAGS_ERROR ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE, 0, 0,
257-
NULL, 2, 0, mesg, NULL);
261+
flags & MSG_FLAGS_ERROR ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE,
262+
0,
263+
EVT_TEXT_2,
264+
NULL,
265+
2,
266+
0,
267+
mesg,
268+
NULL);
258269
DeregisterEventSource(hEventSource);
259270
}
260271

src/openvpnserv/eventmsg.mc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
MessageIdTypedef=DWORD
2+
3+
SeverityNames=(Success=0x0:STATUS_SEVERITY_SUCCESS
4+
Informational=0x1:STATUS_SEVERITY_INFORMATIONAL
5+
Warning=0x2:STATUS_SEVERITY_WARNING
6+
Error=0x3:STATUS_SEVERITY_ERROR
7+
)
8+
9+
FacilityNames=(System=0x0:FACILITY_SYSTEM
10+
Runtime=0x2:FACILITY_RUNTIME
11+
Stubs=0x3:FACILITY_STUBS
12+
Io=0x4:FACILITY_IO_ERROR_CODE
13+
)
14+
15+
LanguageNames=(English=0x409:MSG00409)
16+
17+
MessageId=0x1
18+
Severity=Error
19+
Facility=Runtime
20+
SymbolicName=EVT_TEXT_2
21+
Language=English
22+
%1%n%2
23+
.

0 commit comments

Comments
 (0)