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 <selva.nair@gmail.com>
pull/530/head
Selva Nair 2022-10-16 16:30:11 -04:00
parent 67a8db7664
commit 577d982b51
6 changed files with 43 additions and 1 deletions

View File

@ -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

4
main.c
View File

@ -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;

View File

@ -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 = \

View File

@ -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;
}

View File

@ -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;
}

View File

@ -21,3 +21,5 @@
int CheckServiceStatus();
BOOL CheckIServiceStatus(BOOL warn);
/* Attempt to start OpenVPN Automatc Service */
void StartAutomaticService(void);