From: "stipa (Code Review)" Date: Tue, 16 Sep 2025 07:00:31 +0000 In-Reply-To: References: Reply-To: lstipakov@...277..., arne-openvpn@...1227..., selva.nair@...277..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <0a17015710f53f2761a368be518aeb8a54634873-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="BHIp0PDSrHg="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: openvpnserv: Fix writing messages to the event log List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel , selvanair --BHIp0PDSrHg= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos, stipa. Hello flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/1188?usp=3Demail to look at the new patch set (#2). Change subject: openvpnserv: Fix writing messages to the event log ...................................................................... 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 =C3=ADn 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 requi= res changes to the installer - we need to install the new DLL and add a registry entry. GitHub: https://github.com/OpenVPN/openvpn/issues/842 Change-Id: I423c9880def0eb479abb72bef2e8034a73cf5905 Signed-off-by: Lev Stipakov --- M src/openvpnserv/CMakeLists.txt M src/openvpnserv/common.c A src/openvpnserv/eventmsg.mc 3 files changed, 82 insertions(+), 14 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/88/1188/2 diff --git a/src/openvpnserv/CMakeLists.txt b/src/openvpnserv/CMakeLists.txt index 7e3efb2..82099f1 100644 --- a/src/openvpnserv/CMakeLists.txt +++ b/src/openvpnserv/CMakeLists.txt @@ -6,11 +6,14 @@ add_executable(openvpnserv) =20 +set(MC_GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/mc) + target_include_directories(openvpnserv PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../../ ../../include/ ../openvpn/ ../compat/ + ${MC_GEN_DIR} ) target_sources(openvpnserv PRIVATE common.c @@ -33,3 +36,34 @@ target_compile_options(openvpnserv PRIVATE -municode) target_link_options(openvpnserv PRIVATE -municode) endif () + +# below we generate a DLL which contains an event source for event log mes= sages from eventmsg.mc template +file(MAKE_DIRECTORY ${MC_GEN_DIR}) +set(MC_FILE ${CMAKE_CURRENT_SOURCE_DIR}/eventmsg.mc) + +find_program(MC_COMPILER NAMES mc mc.exe x86_64-w64-mingw32-windmc i686-w6= 4-mingw32-windmc windmc) + +if (NOT MC_COMPILER) + message(FATAL_ERROR "No message compiler found.") +endif() + +add_custom_command( + OUTPUT ${MC_GEN_DIR}/eventmsg.rc ${MC_GEN_DIR}/eventmsg.h + COMMAND ${MC_COMPILER} -U -h ${MC_GEN_DIR} -r ${MC_GEN_DIR} ${MC_FILE} + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/eventmsg.mc + VERBATIM + ) + +# generate rc file for DLL and header for the service binary +add_custom_target(msg_mc_gen ALL DEPENDS ${MC_GEN_DIR}/eventmsg.rc ${MC_GE= N_DIR}/eventmsg.h) + +add_library(openvpnservmsg SHARED ${MC_GEN_DIR}/eventmsg.rc) + +if (MSVC) + set_target_properties(openvpnservmsg PROPERTIES LINK_FLAGS "/NOENTRY") +else() + target_link_options(openvpnservmsg PRIVATE "-Wl,--no-entry") +endif() + +add_dependencies(openvpnservmsg msg_mc_gen) +add_dependencies(openvpnserv msg_mc_gen) diff --git a/src/openvpnserv/common.c b/src/openvpnserv/common.c index a42c65d..e975cc7 100644 --- a/src/openvpnserv/common.c +++ b/src/openvpnserv/common.c @@ -22,6 +22,7 @@ #include "service.h" #include "validate.h" +#include "eventmsg.h" LPCWSTR service_instance =3D L""; static wchar_t win_sys_path[MAX_PATH]; @@ -203,25 +204,29 @@ LPWSTR tmp =3D NULL; error =3D GetLastError(); - len =3D FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_= FROM_SYSTEM - | FORMAT_MESSAGE_ARGUMENT_ARRAY, - NULL, error, LANG_NEUTRAL, tmp, 0, NULL); + len =3D FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE= _FROM_SYSTEM + | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEF= AULT), (LPTSTR)&tmp, 0, NULL); - if (len =3D=3D 0 || (long)_countof(buf) < (long)len + 14) + if (!len || !tmp) { - buf[0] =3D L'\0'; - } - else - { - tmp[wcslen(tmp) - 2] =3D L'\0'; /* remove CR/LF characters */ - swprintf(buf, _countof(buf), L"%ls (0x%x)", tmp, error); + swprintf(buf, _countof(buf), L"Unknown error (0x%lx)", error); + if (tmp) + { + LocalFree(tmp); + } + return buf; } - if (tmp) + /* trim trailing CR / LF / spaces safely */ + while (len && (tmp[len - 1] =3D=3D L'\r' || tmp[len - 1] =3D=3D L'\n' = || tmp[len - 1] =3D=3D L' ')) { - LocalFree(tmp); + tmp[--len] =3D L'\0'; } + swprintf(buf, _countof(buf), L"%ls (0x%lx)", tmp, error); + + LocalFree(tmp); return buf; } @@ -253,8 +258,14 @@ const WCHAR *mesg[] =3D { msg[0], msg[1] }; ReportEvent(hEventSource, - flags & MSG_FLAGS_ERROR ? EVENTLOG_ERROR_TYPE : EVENTL= OG_INFORMATION_TYPE, 0, 0, - NULL, 2, 0, mesg, NULL); + flags & MSG_FLAGS_ERROR ? EVENTLOG_ERROR_TYPE : EVENTL= OG_INFORMATION_TYPE, + 0, + EVT_TEXT_2, + NULL, + 2, + 0, + mesg, + NULL); DeregisterEventSource(hEventSource); } diff --git a/src/openvpnserv/eventmsg.mc b/src/openvpnserv/eventmsg.mc new file mode 100644 index 0000000..722a30e --- /dev/null +++ b/src/openvpnserv/eventmsg.mc @@ -0,0 +1,23 @@ +MessageIdTypedef=3DDWORD + +SeverityNames=3D(Success=3D0x0:STATUS_SEVERITY_SUCCESS + Informational=3D0x1:STATUS_SEVERITY_INFORMATIONAL + Warning=3D0x2:STATUS_SEVERITY_WARNING + Error=3D0x3:STATUS_SEVERITY_ERROR + ) + +FacilityNames=3D(System=3D0x0:FACILITY_SYSTEM + Runtime=3D0x2:FACILITY_RUNTIME + Stubs=3D0x3:FACILITY_STUBS + Io=3D0x4:FACILITY_IO_ERROR_CODE +) + +LanguageNames=3D(English=3D0x409:MSG00409) + +MessageId=3D0x1 +Severity=3DError +Facility=3DRuntime +SymbolicName=3DEVT_TEXT_2 +Language=3DEnglish +%1%n%2 +. -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1188?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I423c9880def0eb479abb72bef2e8034a73cf5905 Gerrit-Change-Number: 1188 Gerrit-PatchSet: 2 Gerrit-Owner: stipa Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-CC: selvanair Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-Attention: stipa Gerrit-MessageType: newpatchset --BHIp0PDSrHg= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos, stipa. <= /p>

stipa uploaded patch set #2 to this change.

View Change

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 c= ode. We use
FORMAT_MESSAGE_ALLOCATE_BUFFER, in which case we must passa pointer to the LPTSTR, not the LPTSTR itself.

- The error is no= t displayed in the "General" tab, which is very confusing.
One= needs to go to the "Details" tab to see what is wrong.

Th= is 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 =C3=ADn a safer way.

To di= splay the message in "General" tab, we create a registered messag= e 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: https://github.com/OpenVPN/open= vpn/issues/842

Change-Id: I423c9880def0eb479abb72bef2e8034a73cf5905<= br>Signed-off-by: Lev Stipakov <lev@...515...>
---
M src/openvp= nserv/CMakeLists.txt
M src/openvpnserv/common.c
A src/openvpnserv/eve= ntmsg.mc
3 files changed, 82 insertions(+), 14 deletions(-)

gi=
t pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/88/1188/2
<= pre style=3D"font-family: monospace,monospace; white-space: pre-wrap;">diff --git a/src/openvpnserv/CMakeLists.txt b/src/openvpnserv/CMakeLists.= txt
index 7e3efb2..82099f1 100644
--- a/src= /openvpnserv/CMakeLists.txt
+++ b/src/openvpnserv/CMakeList= s.txt
@@ -6,11 +6,14 @@

= add_executable(openvpnserv)

+set(MC_GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/mc)
+
targ= et_include_directories(openvpnserv PRIVATE
${CMAKE_CUR= RENT_BINARY_DIR}/../../
../../include/
../openvpn/

../compat/
+ ${MC_GEN_DIR}
)

target_sources(openvpnserv PRIVATE
common= .c
@@ -33,3 +36,34 @@
target_compile_o= ptions(openvpnserv PRIVATE -municode)
target_link_opti= ons(openvpnserv PRIVATE -municode)
endif ()
+
+# below we generate a DLL which contains an event sou= rce for event log messages from eventmsg.mc template
+file(MAKE_DIRECTORY ${MC_GEN_DIR})=
+set(MC_FILE ${CMAKE_CURREN= T_SOURCE_DIR}/eventmsg.mc)
+
+find_program= (MC_COMPILER NAMES mc mc.exe x86_64-w64-mingw32-windmc i686-w64-mingw32-win= dmc windmc)
+<= br>+if (NOT MC_COMPILER)=
+ message(FATAL_ERROR &q= uot;No message compiler found.")
+endif()
+
+add_custom_co= mmand(
+ OUTPUT ${= MC_GEN_DIR}/eventmsg.rc ${MC_GEN_DIR}/eventmsg.h
+ COMMAND ${MC_COMPILER} -U -h ${MC_GEN_DIR}= -r ${MC_GEN_DIR} ${MC_FILE}
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/eventmsg.mc
+ VERBATIM
+ )
+
= +# generate rc file for DLL and header for the service binary
+add_custom_target(msg_mc_gen ALL = DEPENDS ${MC_GEN_DIR}/eventmsg.rc ${MC_GEN_DIR}/eventmsg.h)
+
+add_library(openvpnservmsg SHARED ${MC_GEN_DIR}/eventm= sg.rc)
+
+if (MSVC)
+ set_target_properties(openvpnservmsg = PROPERTIES LINK_FLAGS "/NOENTRY")
+else()
+ target_link_options(openvpnservmsg PRIVATE "-Wl,--no-en= try")
+endif()
+
+add_dependencies(openvpnservmsg msg_mc_g= en)
+add_dependencies= (openvpnserv msg_mc_gen)
diff --git a/src/openvpnserv/commo= n.c b/src/openvpnserv/common.c
index a42c65d..e975cc7 10064= 4
--- a/src/openvpnserv/common.c
+++ b/src/= openvpnserv/common.c
@@ -22,6 +22,7 @@

#include "service.h"
#include &qu= ot;validate.h"
+= #include "eventmsg.h"

LPCWSTR = service_instance =3D L"";
static wchar_t win_sys= _path[MAX_PATH];
@@ -203,25 +204,29 @@
= LPWSTR tmp =3D NULL;

error =3D GetL= astError();
- len = =3D FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYST= EM
- = | FORMAT_MESSAGE_ARGUMENT_ARRAY,
- NULL, error, LANG_NEUTRAL, tmp,= 0, NULL);
+ len = =3D FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYS= TEM
+ = | FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, error, MAKELANGID(= LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&tmp, 0, NULL);

- if (len =3D=3D = 0 || (long)_countof(buf) < (long)len + 14)
+ if (!len || !tmp)
{

- buf[0] =3D L'\= 0';
- }- else
- {
- tmp[wcslen(tmp) - 2] =3D L'\0'; /* remove C= R/LF characters */
- = swprintf(buf, _countof(buf), L"%ls (0x%x)", tmp, error);
+ swprintf(buf, _= countof(buf), L"Unknown error (0x%lx)", error);
+ if (tmp)
+ {
+ LocalFree(tmp);
+ }
+ return buf;
}
- if (tmp)=
+ /* trim trailin= g CR / LF / spaces safely */
+ while (len && (tmp[len - 1] =3D=3D L'\r' || tm= p[len - 1] =3D=3D L'\n' || tmp[len - 1] =3D=3D L' '))
{
- = LocalFree(tmp);
+= tmp[--len] =3D L'\0';
}

+ swprintf(buf= , _countof(buf), L"%ls (0x%lx)", tmp, error);
+
+ LocalFree(tmp);
return buf;
}

@@ -253,8 +258,14 @@=

const WCHAR *mesg[] =3D { msg[0], msg[= 1] };
ReportEvent(hEventSource,
- flags & MSG_FLAG= S_ERROR ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE, 0, 0,
= - NULL, 2, 0, = mesg, NULL);
+ = flags & MSG_FLAGS_ERROR ? EVENTLOG_ERROR_TYPE : EVENTLOG_I= NFORMATION_TYPE,
+ = 0,
+= EVT_TEXT_2,
+ NULL,
+ 2,
+ 0,
+ mesg,
+ NULL);
= DeregisterEventSource(hEventSource);
}
diff --git a/src/openvpnserv/eventmsg.mc b/src/op= envpnserv/eventmsg.mc
new file mode 100644
= index 0000000..722a30e
--- /dev/null
+++ b/= src/openvpnserv/eventmsg.mc
@@ -0,0 +1,23 @@
+MessageIdTypedef=3DDWORD
+
+SeverityNames=3D(Success=3D0x0:STATUS_SEVERITY_S= UCCESS
+ Informati= onal=3D0x1:STATUS_SEVERITY_INFORMATIONAL
+ Warning=3D0x2:STATUS_SEVERITY_WARNING
+ Error=3D0x3:STATUS_SEVERITY_= ERROR

+ )+
+FacilityNames=3D(System=3D0x0:FACILITY_SYSTEM
+ Runtime=3D0x2:FAC= ILITY_RUNTIME
+ St= ubs=3D0x3:FACILITY_STUBS
+ Io=3D0x4:FACILITY_IO_ERROR_CODE
+)
+LanguageNames=3D= (English=3D0x409:MSG00409)
+
+MessageId=3D= 0x1
+Severity=3DError=
+Facility=3DRuntime<= /span>
+SymbolicName=3DEVT_T= EXT_2
+Language=3DEng= lish
+%1%n%2+.

To view, visit change 1188. To unsubscribe, or for help writing mail = filters, visit settings.=

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I423c9880def0eb479abb72bef2e= 8034a73cf5905
Gerrit-Change-Number: 1188
Gerrit-PatchSet: 2
Gerrit-Owner: stipa <lstipakov@...277...>= ;
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-CC: selvanair <selva.nair@...277...&= gt;
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-Attention: stipa <lstipakov@...1663...= .>
Gerrit-MessageType: newpatchset
--BHIp0PDSrHg=--