From c03e8afd4d6edbcf9c11f35482ff57d6d4957d38 Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Sun, 16 Oct 2022 16:30:11 -0400 Subject: [PATCH] PLAP: try to start automatic service if not running Connection profiles shown on the login screen using PLAP requires automatic service that starts openvpn.exe processes for these profiles. This commit adds an attempt to start the service from PLAP dll. The service is started only if any PLAP enabled profiles are found. As starting the service can spawn up OpenVPN.exe processes and the GUI may attach to them, auto-connect in the GUI is suspended during session lock to leave the connections free to be controlled from PLAP screen. Signed-off-by: Selva Nair --- CMakeLists.txt | 1 + main.c | 4 +++- plap/Makefile.am | 1 + plap/ui_glue.c | 15 +++++++++++++++ service.c | 21 +++++++++++++++++++++ service.h | 2 ++ 6 files changed, 43 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e225651..1be82ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,6 +110,7 @@ add_library(${PROJECT_NAME_PLAP} SHARED pkcs11.c registry.c config_parser.c + service.c plap/ui_glue.c plap/stub.c plap/plap_common.c diff --git a/main.c b/main.c index 273819d..29913b3 100644 --- a/main.c +++ b/main.c @@ -590,7 +590,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM break; } /* A timer to periodically tend to persistent connections */ - SetTimer(hwnd, 0, 100, ManagePersistent); + SetTimer(hwnd, 1, 100, ManagePersistent); break; @@ -686,12 +686,14 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM o.session_locked = TRUE; /* Detach persistent connections so that other users can connect to it */ HandleSessionLock(); + KillTimer(hwnd, 1); /* This ensure ManagePersistent is not called when session is locked */ break; case WTS_SESSION_UNLOCK: PrintDebug(L"Session unlock triggered"); o.session_locked = FALSE; HandleSessionUnlock(); + SetTimer(hwnd, 1, 100, ManagePersistent); if (CountConnState(suspended) != 0) ResumeConnections(); break; diff --git a/plap/Makefile.am b/plap/Makefile.am index 311b6ac..5cd9d24 100644 --- a/plap/Makefile.am +++ b/plap/Makefile.am @@ -89,6 +89,7 @@ libopenvpn_plap_la_SOURCES = \ $(top_srcdir)/openvpn_config.c \ $(top_srcdir)/config_parser.c \ $(top_srcdir)/pkcs11.c \ + $(top_srcdir)/service.c \ openvpn-plap-res.rc libopenvpn_plap_la_LIBADD = \ diff --git a/plap/ui_glue.c b/plap/ui_glue.c index 9d9c777..3ac0274 100644 --- a/plap/ui_glue.c +++ b/plap/ui_glue.c @@ -40,6 +40,7 @@ #include "localization.h" #include "misc.h" #include "tray.h" +#include "service.h" /* Global options structure */ options_t o; @@ -260,6 +261,20 @@ InitializeUI(HINSTANCE hinstance) dpi_initialize(&o); + /* If openvpn service is not running but, available, attempt to start it */ + CheckServiceStatus(); + int num_persistent = 0; + + for (int i = 0; i < o.num_configs; i++) { + if (o.conn[i].flags & FLAG_DAEMON_PERSISTENT) num_persistent++; + } + + if (o.service_state == service_disconnected && num_persistent > 0) { + dmsg(L"Attempting to start automatic service"); + StartAutomaticService(); + CheckServiceStatus(); + } + return 0; } diff --git a/service.c b/service.c index 6610fc7..18cf870 100644 --- a/service.c +++ b/service.c @@ -147,3 +147,24 @@ out: CloseServiceHandle(schSCManager); return ret; } + +/* Attempt to start OpenVPN Automatc Service */ +void StartAutomaticService(void) +{ + SC_HANDLE schSCManager = NULL; + SC_HANDLE schService = NULL; + + schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT); + + if (schSCManager) { + schService = OpenService(schSCManager, L"OpenVPNService", SERVICE_START); + + if (schService) { + StartService(schService, 0, NULL); + CloseServiceHandle(schService); + } + + CloseServiceHandle(schSCManager); + } + return; +} diff --git a/service.h b/service.h index 2f5fb21..70b8ed3 100644 --- a/service.h +++ b/service.h @@ -21,3 +21,5 @@ int CheckServiceStatus(); BOOL CheckIServiceStatus(BOOL warn); +/* Attempt to start OpenVPN Automatc Service */ +void StartAutomaticService(void);