From patchwork Wed Sep 17 09:06:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 4421 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7000:fc89:b0:671:5a2c:6455 with SMTP id cv9csp2361mac; Wed, 17 Sep 2025 02:07:08 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUnp0mR+Eetk6eu8ZnAkeZq9yiKceOS+N1ea4Gs4Byd0vvlwqdwW7TMtFESksciNcoM6zu4qwcQsO8=@openvpn.net X-Google-Smtp-Source: AGHT+IHCHHN/vz9iKh1KAkGjgwIpn06cnYpudiwJpL6kj2CPbX0aWGbIMFp5ZxW44WxQuRml67xp X-Received: by 2002:a05:6830:8d2:b0:751:da0a:ee97 with SMTP id 46e09a7af769-7637000471amr690160a34.15.1758100028537; Wed, 17 Sep 2025 02:07:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1758100028; cv=none; d=google.com; s=arc-20240605; b=Tma8N+KqaOjZUU4IYWWL9Sk9GROHYiNazgYtQ+sUo6iGbAKg6MjAAon19NKAmrb9/x 55vAUlCSB3bBAuNn4xbXYQ4cPsTaxG7LUY4TFGV8xUXDHEsDfXEQdXJAYe1o20JGk4kB B5x3iaDl+HI18lcxIlnCUkUk6Odat/LC/fWBXshFESF8kzYERFbta15u18ZwNzD41VBL vMNXjA0OMvLPj8fUTcxJp9lN7lh4h3v7gU7w1CDds1bH5PgwrOi0y4q8AFVm6dIqO81J WjHpbKujKxtTwqgvC8SBXpK19RlYmARZcWYvnrNad7kNqLFp3AsmAV6u5TX4DWK4QveY 83uQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature:dkim-signature; bh=YpkadSzGTDSmUHvNT8f1AigZXrWs8YEz6DzG38TssCU=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=ZjBvYCJinstoIPcRjrRIHVNgVTkH8u/HFdR5EKEFG1m3Qq1pvZ6SScaC2Fyg+FcBxq kWDRrRxGtpatR0Y+OaEE12bpJ/qVsBQ8AyUgJe6grXYdCyUJDcZ16VsnlTaJ+7Sac5JH wFfbS22XgRD33g66nBYwTNv9RqE+/SAkLVjQSXK4Qac/94ZmY5oUlVISiZ6wALVTjfgM LBQhU0izoYtxwEW7pxqF3YwWisu+58uDzhZAaK1xCnNCQK7LzD1RFY3VR+GXLf6yEkXt UKD7Pj5srmcowhhCdNMnHKPvkbkV4nlgO9ji46jA5Z6C1Cxiz1+fMsdybv1Q/xIT2aaq jLPw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=IxsDTBsE; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=ANUjRkfh; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=iMBSY2yB; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id 46e09a7af769-761224751aesi532183a34.64.2025.09.17.02.07.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 17 Sep 2025 02:07:08 -0700 (PDT) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=IxsDTBsE; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=ANUjRkfh; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=iMBSY2yB; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Subject:MIME-Version:References:In-Reply-To:Message-ID:Date:To:From:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=YpkadSzGTDSmUHvNT8f1AigZXrWs8YEz6DzG38TssCU=; b=IxsDTBsExg+ZEYM9XSFr8jely+ bJ9i+5nVxSWZQlOnxr+zOL4vxxrNRjOVqrwEtUy8+wvqGiVB3pjwQ/dVtZvHudP2ItiXiiVu1zUN4 MSlguD7xBWHcAORDDZPeqb+xbemK5ZBlNII3B8+RklzznGtxwtTUAf93OhXUe1xibYfU=; Received: from [127.0.0.1] (helo=sfs-ml-3.v29.lw.sourceforge.com) by sfs-ml-3.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1uyo82-0007p9-6P; Wed, 17 Sep 2025 09:07:06 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-3.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1uyo7y-0007oy-Qs for openvpn-devel@lists.sourceforge.net; Wed, 17 Sep 2025 09:07:02 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:Content-Type:MIME-Version :References:In-Reply-To:Message-ID:Date:Subject:To:From:Sender:Reply-To:Cc: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=jQCa5ZbKOD+ntE4+wMhmtje44tmg3XDo2Xnu+7UMlZY=; b=ANUjRkfh64Gn+HDMEkGsV4LTjo Xv+Hsn6u+c2OVnF7zcCYT5aKCBHGSL+2mAFwG8OqG22u2dl7VhBb3SkAF/yBT3cy1AQkfRD0QCsnR Hu3Z2iGM3YwWENG5jEMOCYQsqoHIf5xhYsGfNLvwrONgYK1zzWlmmHWJRN8nZCMumFGo=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:To:From:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=jQCa5ZbKOD+ntE4+wMhmtje44tmg3XDo2Xnu+7UMlZY=; b=iMBSY2yBcWyF7g6bbn9qPxeebQ d/h45CYzj7silKx37iVx/haMuMWc2O85SXC9D/XI7d5lnrj0107aOWqLGQ4nheh9viItoOcP7ik/v 1YEYhCcTVtwiU0WELXuJ0U9dHizaRQTiBOzeXI4OyTpzJNJYVSV3Xn4MEa8rMQMGHuqM=; Received: from [193.149.48.134] (helo=blue.greenie.muc.de) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1uyo7w-0007JV-HR for openvpn-devel@lists.sourceforge.net; Wed, 17 Sep 2025 09:07:02 +0000 Received: from blue.greenie.muc.de (localhost [127.0.0.1]) by blue.greenie.muc.de (8.18.1/8.18.1) with ESMTP id 58H96sUX025530 for ; Wed, 17 Sep 2025 11:06:54 +0200 Received: (from gert@localhost) by blue.greenie.muc.de (8.18.1/8.18.1/Submit) id 58H96si1025529 for openvpn-devel@lists.sourceforge.net; Wed, 17 Sep 2025 11:06:54 +0200 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Wed, 17 Sep 2025 11:06:44 +0200 Message-ID: <20250917090653.25510-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.49.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Score: 1.3 (+) X-Spam-Report: Spam detection software, running on the system "sfi-spamd-1.hosts.colo.sdot.me", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Lev Stipakov 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 th [...] Content analysis details: (1.3 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 1.3 RDNS_NONE Delivered to internal network by a host with no rDNS X-Headers-End: 1uyo7w-0007JV-HR Subject: [Openvpn-devel] [PATCH v3] openvpnserv: Fix writing messages to the event log X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: =?utf-8?q?1843501495931950492?= X-GMAIL-MSGID: =?utf-8?q?1843501495931950492?= From: Lev Stipakov 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: https://github.com/OpenVPN/openvpn/issues/842 Change-Id: I9b9f38e11b06701142bdc1339d9bedf080de5f86 Signed-off-by: Lev Stipakov Acked-by: Gert Doering --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to release/2.6. Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1185 This mail reflects revision 3 of this Change. Acked-by according to Gerrit (reflected above): Gert Doering diff --git a/src/openvpnserv/CMakeLists.txt b/src/openvpnserv/CMakeLists.txt index 17cd90c..201350c 100644 --- a/src/openvpnserv/CMakeLists.txt +++ b/src/openvpnserv/CMakeLists.txt @@ -6,11 +6,14 @@ add_executable(openvpnserv) +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 @@ -32,3 +35,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 messages 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-w64-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_GEN_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 bd0a484..feaae19 100644 --- a/src/openvpnserv/common.c +++ b/src/openvpnserv/common.c @@ -23,6 +23,7 @@ #include "service.h" #include "validate.h" +#include "eventmsg.h" LPCTSTR service_instance = TEXT(""); static wchar_t win_sys_path[MAX_PATH]; @@ -219,24 +220,28 @@ LPTSTR tmp = NULL; error = GetLastError(); - len = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY, - NULL, error, LANG_NEUTRAL, tmp, 0, NULL); + len = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM| FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&tmp, 0, NULL); - if (len == 0 || (long) _countof(buf) < (long) len + 14) + if (!len || !tmp) { - buf[0] = TEXT('\0'); - } - else - { - tmp[wcslen(tmp) - 2] = TEXT('\0'); /* remove CR/LF characters */ - openvpn_swprintf(buf, _countof(buf), TEXT("%ls (0x%x)"), tmp, error); + openvpn_swprintf(buf, _countof(buf), TEXT("Unknown error (0x%lx)"), error); + if (tmp) + { + LocalFree(tmp); + } + return buf; } - if (tmp) + /* trim trailing CR / LF / spaces safely */ + while (len && (tmp[len - 1] == TEXT('\r') || tmp[len - 1] == TEXT('\n') || tmp[len - 1] == TEXT(' '))) { - LocalFree(tmp); + tmp[--len] = TEXT('\0'); } + openvpn_swprintf(buf, _countof(buf), TEXT("%ls (0x%lx)"), tmp, error); + + LocalFree(tmp); return buf; } @@ -268,9 +273,15 @@ va_end(arglist); const TCHAR *mesg[] = { msg[0], msg[1] }; - ReportEvent(hEventSource, flags & MSG_FLAGS_ERROR ? - EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE, - 0, 0, NULL, 2, 0, mesg, NULL); + ReportEvent(hEventSource, + flags & MSG_FLAGS_ERROR ? EVENTLOG_ERROR_TYPE : EVENTLOG_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=DWORD + +SeverityNames=(Success=0x0:STATUS_SEVERITY_SUCCESS + Informational=0x1:STATUS_SEVERITY_INFORMATIONAL + Warning=0x2:STATUS_SEVERITY_WARNING + Error=0x3:STATUS_SEVERITY_ERROR + ) + +FacilityNames=(System=0x0:FACILITY_SYSTEM + Runtime=0x2:FACILITY_RUNTIME + Stubs=0x3:FACILITY_STUBS + Io=0x4:FACILITY_IO_ERROR_CODE +) + +LanguageNames=(English=0x409:MSG00409) + +MessageId=0x1 +Severity=Error +Facility=Runtime +SymbolicName=EVT_TEXT_2 +Language=English +%1%n%2 +.