OpenVPN
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
interactive.c File Reference
#include "service.h"
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <userenv.h>
#include <accctrl.h>
#include <aclapi.h>
#include <stdio.h>
#include <sddl.h>
#include <shellapi.h>
#include <mstcpip.h>
#include "compat-versionhelpers.h"
#include "openvpn-msg.h"
#include "validate.h"
#include "block_dns.h"
Include dependency graph for interactive.c:

Go to the source code of this file.

Data Structures

struct  STARTUP_DATA
 
struct  _list_item
 
struct  block_dns_data_t
 

Macros

#define IO_TIMEOUT   2000 /*ms*/
 
#define ERROR_OPENVPN_STARTUP   0x20000000
 
#define ERROR_STARTUP_DATA   0x20000001
 
#define ERROR_MESSAGE_DATA   0x20000002
 
#define ERROR_MESSAGE_TYPE   0x20000003
 
#define RDNS_TIMEOUT   600 /* seconds to wait for the semaphore */
 

Typedefs

typedef struct _list_item list_item_t
 
typedef list_item_tundo_lists_t[_undo_type_max]
 
typedef BOOL(* match_fn_t) (LPVOID item, LPVOID ctx)
 

Enumerations

enum  undo_type_t {
  address, route, block_dns, undo_dns4,
  undo_dns6, _undo_type_max
}
 
enum  async_op_t { peek, read, write }
 

Functions

static DWORD AddListItem (list_item_t **pfirst, LPVOID data)
 
static LPVOID RemoveListItem (list_item_t **pfirst, match_fn_t match, LPVOID ctx)
 
static HANDLE CloseHandleEx (LPHANDLE handle)
 
static HANDLE InitOverlapped (LPOVERLAPPED overlapped)
 
static BOOL ResetOverlapped (LPOVERLAPPED overlapped)
 
static DWORD AsyncPipeOp (async_op_t op, HANDLE pipe, LPVOID buffer, DWORD size, DWORD count, LPHANDLE events)
 
static DWORD PeekNamedPipeAsync (HANDLE pipe, DWORD count, LPHANDLE events)
 
static DWORD ReadPipeAsync (HANDLE pipe, LPVOID buffer, DWORD size, DWORD count, LPHANDLE events)
 
static DWORD WritePipeAsync (HANDLE pipe, LPVOID data, DWORD size, DWORD count, LPHANDLE events)
 
static VOID ReturnProcessId (HANDLE pipe, DWORD pid, DWORD count, LPHANDLE events)
 
static VOID ReturnError (HANDLE pipe, DWORD error, LPCWSTR func, DWORD count, LPHANDLE events)
 
static VOID ReturnLastError (HANDLE pipe, LPCWSTR func)
 
static VOID ReturnOpenvpnOutput (HANDLE pipe, HANDLE ovpn_output, DWORD count, LPHANDLE events)
 
static BOOL ValidateOptions (HANDLE pipe, const WCHAR *workdir, const WCHAR *options)
 
static BOOL GetStartupData (HANDLE pipe, STARTUP_DATA *sud)
 
static VOID FreeStartupData (STARTUP_DATA *sud)
 
static SOCKADDR_INET sockaddr_inet (short family, inet_address_t *addr)
 
static DWORD InterfaceLuid (const char *iface_name, PNET_LUID luid)
 
static BOOL CmpAddress (LPVOID item, LPVOID address)
 
static DWORD DeleteAddress (PMIB_UNICASTIPADDRESS_ROW addr_row)
 
static DWORD HandleAddressMessage (address_message_t *msg, undo_lists_t *lists)
 
static BOOL CmpRoute (LPVOID item, LPVOID route)
 
static DWORD DeleteRoute (PMIB_IPFORWARD_ROW2 fwd_row)
 
static DWORD HandleRouteMessage (route_message_t *msg, undo_lists_t *lists)
 
static DWORD HandleFlushNeighborsMessage (flush_neighbors_message_t *msg)
 
static void BlockDNSErrHandler (DWORD err, const char *msg)
 
static BOOL CmpEngine (LPVOID item, LPVOID any)
 
static DWORD HandleBlockDNSMessage (const block_dns_message_t *msg, undo_lists_t *lists)
 
static DWORD ExecCommand (const WCHAR *argv0, const WCHAR *cmdline, DWORD timeout)
 
static DWORD WINAPI RegisterDNS (LPVOID unused)
 
static DWORD HandleRegisterDNSMessage (void)
 
static DWORD netsh_dns_cmd (const wchar_t *action, const wchar_t *proto, const wchar_t *if_name, const wchar_t *addr)
 Run the command: netsh interface $proto $action dns $if_name $addr [validate=no]. More...
 
static DWORD DeleteDNS (short family, wchar_t *if_name)
 
static DWORD AddDNS (short family, wchar_t *if_name, wchar_t *addr)
 
static BOOL CmpWString (LPVOID item, LPVOID str)
 
static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg, undo_lists_t *lists)
 
static DWORD HandleEnableDHCPMessage (const enable_dhcp_message_t *dhcp)
 
static VOID HandleMessage (HANDLE pipe, DWORD bytes, DWORD count, LPHANDLE events, undo_lists_t *lists)
 
static VOID Undo (undo_lists_t *lists)
 
static DWORD WINAPI RunOpenvpn (LPVOID p)
 
static DWORD WINAPI ServiceCtrlInteractive (DWORD ctrl_code, DWORD event, LPVOID data, LPVOID ctx)
 
static HANDLE CreateClientPipeInstance (VOID)
 
static DWORD UpdateWaitHandles (LPHANDLE *handles_ptr, LPDWORD count, HANDLE io_event, HANDLE exit_event, list_item_t *threads)
 
static VOID FreeWaitHandles (LPHANDLE h)
 
static BOOL CmpHandle (LPVOID item, LPVOID hnd)
 
VOID WINAPI ServiceStartInteractiveOwn (DWORD dwArgc, LPTSTR *lpszArgv)
 
VOID WINAPI ServiceStartInteractive (DWORD dwArgc, LPTSTR *lpszArgv)
 

Variables

static SERVICE_STATUS_HANDLE service
 
static SERVICE_STATUS status = { .dwServiceType = SERVICE_WIN32_SHARE_PROCESS }
 
static HANDLE exit_event = NULL
 
static settings_t settings
 
static HANDLE rdns_semaphore = NULL
 
openvpn_service_t interactive_service
 

Macro Definition Documentation

◆ ERROR_MESSAGE_DATA

#define ERROR_MESSAGE_DATA   0x20000002

Definition at line 51 of file interactive.c.

Referenced by HandleDNSConfigMessage(), and HandleMessage().

◆ ERROR_MESSAGE_TYPE

#define ERROR_MESSAGE_TYPE   0x20000003

Definition at line 52 of file interactive.c.

Referenced by HandleMessage().

◆ ERROR_OPENVPN_STARTUP

#define ERROR_OPENVPN_STARTUP   0x20000000

Definition at line 49 of file interactive.c.

Referenced by ReturnError(), ReturnOpenvpnOutput(), and RunOpenvpn().

◆ ERROR_STARTUP_DATA

#define ERROR_STARTUP_DATA   0x20000001

Definition at line 50 of file interactive.c.

Referenced by GetStartupData(), and ValidateOptions().

◆ IO_TIMEOUT

#define IO_TIMEOUT   2000 /*ms*/

Definition at line 47 of file interactive.c.

Referenced by AsyncPipeOp(), and RunOpenvpn().

◆ RDNS_TIMEOUT

#define RDNS_TIMEOUT   600 /* seconds to wait for the semaphore */

Definition at line 59 of file interactive.c.

Referenced by RegisterDNS().

Typedef Documentation

◆ list_item_t

typedef struct _list_item list_item_t

◆ match_fn_t

typedef BOOL(* match_fn_t) (LPVOID item, LPVOID ctx)

Definition at line 120 of file interactive.c.

◆ undo_lists_t

typedef list_item_t* undo_lists_t[_undo_type_max]

Definition at line 94 of file interactive.c.

Enumeration Type Documentation

◆ async_op_t

enum async_op_t
Enumerator
peek 
read 
write 

Definition at line 181 of file interactive.c.

◆ undo_type_t

Enumerator
address 
route 
block_dns 
undo_dns4 
undo_dns6 
_undo_type_max 

Definition at line 86 of file interactive.c.

Function Documentation

◆ AddDNS()

static DWORD AddDNS ( short  family,
wchar_t *  if_name,
wchar_t *  addr 
)
static

Definition at line 1073 of file interactive.c.

References netsh_dns_cmd().

Referenced by HandleDNSConfigMessage().

◆ AddListItem()

static DWORD AddListItem ( list_item_t **  pfirst,
LPVOID  data 
)
static

◆ AsyncPipeOp()

static DWORD AsyncPipeOp ( async_op_t  op,
HANDLE  pipe,
LPVOID  buffer,
DWORD  size,
DWORD  count,
LPHANDLE  events 
)
static

◆ BlockDNSErrHandler()

static void BlockDNSErrHandler ( DWORD  err,
const char *  msg 
)
static

Definition at line 743 of file interactive.c.

References M_ERR, and MsgToEventLog().

Referenced by HandleBlockDNSMessage().

◆ CloseHandleEx()

static HANDLE CloseHandleEx ( LPHANDLE  handle)
static

◆ CmpAddress()

static BOOL CmpAddress ( LPVOID  item,
LPVOID  address 
)
static

Definition at line 570 of file interactive.c.

Referenced by HandleAddressMessage().

◆ CmpEngine()

static BOOL CmpEngine ( LPVOID  item,
LPVOID  any 
)
static

Definition at line 772 of file interactive.c.

Referenced by HandleBlockDNSMessage().

◆ CmpHandle()

static BOOL CmpHandle ( LPVOID  item,
LPVOID  hnd 
)
static

Definition at line 1818 of file interactive.c.

Referenced by ServiceStartInteractive().

◆ CmpRoute()

static BOOL CmpRoute ( LPVOID  item,
LPVOID  route 
)
static

Definition at line 648 of file interactive.c.

Referenced by HandleRouteMessage().

◆ CmpWString()

static BOOL CmpWString ( LPVOID  item,
LPVOID  str 
)
static

Definition at line 1080 of file interactive.c.

Referenced by HandleDNSConfigMessage().

◆ CreateClientPipeInstance()

static HANDLE CreateClientPipeInstance ( VOID  )
static

◆ DeleteAddress()

static DWORD DeleteAddress ( PMIB_UNICASTIPADDRESS_ROW  addr_row)
static

Definition at line 576 of file interactive.c.

Referenced by HandleAddressMessage(), and Undo().

◆ DeleteDNS()

static DWORD DeleteDNS ( short  family,
wchar_t *  if_name 
)
static

Definition at line 1065 of file interactive.c.

References netsh_dns_cmd().

Referenced by HandleDNSConfigMessage(), and Undo().

◆ DeleteRoute()

static DWORD DeleteRoute ( PMIB_IPFORWARD_ROW2  fwd_row)
static

Definition at line 654 of file interactive.c.

Referenced by HandleRouteMessage(), and Undo().

◆ ExecCommand()

static DWORD ExecCommand ( const WCHAR *  argv0,
const WCHAR *  cmdline,
DWORD  timeout 
)
static

Definition at line 871 of file interactive.c.

References free, M_ERR, M_INFO, M_SYSERR, and MsgToEventLog().

Referenced by HandleEnableDHCPMessage(), netsh_dns_cmd(), and RegisterDNS().

◆ FreeStartupData()

static VOID FreeStartupData ( STARTUP_DATA sud)
static

Definition at line 528 of file interactive.c.

References STARTUP_DATA::directory, and free.

Referenced by RunOpenvpn().

◆ FreeWaitHandles()

static VOID FreeWaitHandles ( LPHANDLE  h)
static

Definition at line 1812 of file interactive.c.

References free.

Referenced by ServiceStartInteractive().

◆ GetStartupData()

static BOOL GetStartupData ( HANDLE  pipe,
STARTUP_DATA sud 
)
static

◆ HandleAddressMessage()

static DWORD HandleAddressMessage ( address_message_t msg,
undo_lists_t lists 
)
static

◆ HandleBlockDNSMessage()

static DWORD HandleBlockDNSMessage ( const block_dns_message_t msg,
undo_lists_t lists 
)
static

◆ HandleDNSConfigMessage()

static DWORD HandleDNSConfigMessage ( const dns_cfg_message_t msg,
undo_lists_t lists 
)
static

◆ HandleEnableDHCPMessage()

static DWORD HandleEnableDHCPMessage ( const enable_dhcp_message_t dhcp)
static

◆ HandleFlushNeighborsMessage()

static DWORD HandleFlushNeighborsMessage ( flush_neighbors_message_t msg)
static

◆ HandleMessage()

static VOID HandleMessage ( HANDLE  pipe,
DWORD  bytes,
DWORD  count,
LPHANDLE  events,
undo_lists_t lists 
)
static

◆ HandleRegisterDNSMessage()

static DWORD HandleRegisterDNSMessage ( void  )
static

Definition at line 978 of file interactive.c.

References RegisterDNS().

Referenced by HandleMessage().

◆ HandleRouteMessage()

static DWORD HandleRouteMessage ( route_message_t msg,
undo_lists_t lists 
)
static

◆ InitOverlapped()

static HANDLE InitOverlapped ( LPOVERLAPPED  overlapped)
static

Definition at line 159 of file interactive.c.

Referenced by AsyncPipeOp(), and ServiceStartInteractive().

◆ InterfaceLuid()

static DWORD InterfaceLuid ( const char *  iface_name,
PNET_LUID  luid 
)
static

Definition at line 552 of file interactive.c.

References free, status, and utf8to16().

Referenced by get_default_gateway_ipv6(), HandleAddressMessage(), and HandleRouteMessage().

◆ netsh_dns_cmd()

static DWORD netsh_dns_cmd ( const wchar_t *  action,
const wchar_t *  proto,
const wchar_t *  if_name,
const wchar_t *  addr 
)
static

Run the command: netsh interface $proto $action dns $if_name $addr [validate=no].

Parameters
action"delete" or "add"
proto"ipv6" or "ip"
if_name"name_of_interface"
addrIPv4 (for proto = ip) or IPv6 address as a string

If addr is null and action = "delete" all addresses are deleted.

Definition at line 1013 of file interactive.c.

References ExecCommand(), free, get_win_sys_path(), malloc, and openvpn_sntprintf().

Referenced by AddDNS(), and DeleteDNS().

◆ PeekNamedPipeAsync()

static DWORD PeekNamedPipeAsync ( HANDLE  pipe,
DWORD  count,
LPHANDLE  events 
)
static

Definition at line 252 of file interactive.c.

References AsyncPipeOp(), and peek.

Referenced by GetStartupData(), and RunOpenvpn().

◆ ReadPipeAsync()

static DWORD ReadPipeAsync ( HANDLE  pipe,
LPVOID  buffer,
DWORD  size,
DWORD  count,
LPHANDLE  events 
)
static

Definition at line 258 of file interactive.c.

References AsyncPipeOp(), and read.

Referenced by GetStartupData(), and HandleMessage().

◆ RegisterDNS()

static DWORD WINAPI RegisterDNS ( LPVOID  unused)
static

◆ RemoveListItem()

static LPVOID RemoveListItem ( list_item_t **  pfirst,
match_fn_t  match,
LPVOID  ctx 
)
static

◆ ResetOverlapped()

static BOOL ResetOverlapped ( LPOVERLAPPED  overlapped)
static

Definition at line 168 of file interactive.c.

Referenced by ServiceStartInteractive().

◆ ReturnError()

static VOID ReturnError ( HANDLE  pipe,
DWORD  error,
LPCWSTR  func,
DWORD  count,
LPHANDLE  events 
)
static

◆ ReturnLastError()

static VOID ReturnLastError ( HANDLE  pipe,
LPCWSTR  func 
)
static

Definition at line 329 of file interactive.c.

References exit_event, and ReturnError().

Referenced by GetStartupData(), RunOpenvpn(), and ValidateOptions().

◆ ReturnOpenvpnOutput()

static VOID ReturnOpenvpnOutput ( HANDLE  pipe,
HANDLE  ovpn_output,
DWORD  count,
LPHANDLE  events 
)
static

Definition at line 336 of file interactive.c.

References ERROR_OPENVPN_STARTUP, free, malloc, and ReturnError().

◆ ReturnProcessId()

static VOID ReturnProcessId ( HANDLE  pipe,
DWORD  pid,
DWORD  count,
LPHANDLE  events 
)
static

Definition at line 270 of file interactive.c.

References msg, and WritePipeAsync().

Referenced by RunOpenvpn().

◆ RunOpenvpn()

static DWORD WINAPI RunOpenvpn ( LPVOID  p)
static

◆ ServiceCtrlInteractive()

static DWORD WINAPI ServiceCtrlInteractive ( DWORD  ctrl_code,
DWORD  event,
LPVOID  data,
LPVOID  ctx 
)
static

Definition at line 1664 of file interactive.c.

References exit_event, ReportStatusToSCMgr(), service, and status.

Referenced by ServiceStartInteractive().

◆ ServiceStartInteractive()

VOID WINAPI ServiceStartInteractive ( DWORD  dwArgc,
LPTSTR *  lpszArgv 
)

◆ ServiceStartInteractiveOwn()

VOID WINAPI ServiceStartInteractiveOwn ( DWORD  dwArgc,
LPTSTR *  lpszArgv 
)

Definition at line 1825 of file interactive.c.

References ServiceStartInteractive(), and status.

Referenced by _tmain().

◆ sockaddr_inet()

static SOCKADDR_INET sockaddr_inet ( short  family,
inet_address_t addr 
)
static

Definition at line 535 of file interactive.c.

References inet_address_t::ipv4, and inet_address_t::ipv6.

Referenced by HandleAddressMessage(), and HandleRouteMessage().

◆ Undo()

static VOID Undo ( undo_lists_t lists)
static

◆ UpdateWaitHandles()

static DWORD UpdateWaitHandles ( LPHANDLE *  handles_ptr,
LPDWORD  count,
HANDLE  io_event,
HANDLE  exit_event,
list_item_t threads 
)
static

Definition at line 1762 of file interactive.c.

References _list_item::data, exit_event, malloc, _list_item::next, pos(), and realloc.

Referenced by ServiceStartInteractive().

◆ ValidateOptions()

static BOOL ValidateOptions ( HANDLE  pipe,
const WCHAR *  workdir,
const WCHAR *  options 
)
static

◆ WritePipeAsync()

static DWORD WritePipeAsync ( HANDLE  pipe,
LPVOID  data,
DWORD  size,
DWORD  count,
LPHANDLE  events 
)
static

Definition at line 264 of file interactive.c.

References AsyncPipeOp(), and write.

Referenced by HandleMessage(), ReturnError(), and ReturnProcessId().

Variable Documentation

◆ exit_event

HANDLE exit_event = NULL
static

◆ interactive_service

openvpn_service_t interactive_service
Initial value:
= {
TEXT(PACKAGE_NAME "ServiceInteractive"),
TEXT(PACKAGE_NAME " Interactive Service"),
SERVICE_AUTO_START
}
#define SERVICE_DEPENDENCIES
Definition: service.h:39
#define PACKAGE_NAME
Definition: config.h:730

Definition at line 62 of file interactive.c.

Referenced by _tmain().

◆ rdns_semaphore

HANDLE rdns_semaphore = NULL
static

Definition at line 58 of file interactive.c.

Referenced by RegisterDNS(), and ServiceStartInteractive().

◆ service

SERVICE_STATUS_HANDLE service
static

Definition at line 54 of file interactive.c.

Referenced by ServiceCtrlInteractive(), and ServiceStartInteractive().

◆ settings

settings_t settings
static

Definition at line 57 of file interactive.c.

Referenced by ServiceStartAutomatic().

◆ status

SERVICE_STATUS status = { .dwServiceType = SERVICE_WIN32_SHARE_PROCESS }
static