From 7c4bea3f7e1fb794b2886a68d089d9f565b8cfae Mon Sep 17 00:00:00 2001 From: Heiko Hund Date: Tue, 29 Jun 2010 16:12:17 +0200 Subject: [PATCH] remove support for openvpn version < 2.0 --- openvpn-gui-res-de.rc | 1 - openvpn-gui-res-en.rc | 1 - openvpn-gui-res-es.rc | 1 - openvpn-gui-res-fi.rc | 1 - openvpn-gui-res-fr.rc | 1 - openvpn-gui-res-it.rc | 1 - openvpn-gui-res-nl.rc | 1 - openvpn-gui-res-no.rc | 1 - openvpn-gui-res-pt.rc | 1 - openvpn-gui-res-se.rc | 1 - openvpn-gui-res.h | 1 - openvpn.c | 503 ++++++++++---------------------------- openvpn.h | 2 +- openvpn_monitor_process.c | 6 +- options.h | 2 - 15 files changed, 129 insertions(+), 395 deletions(-) diff --git a/openvpn-gui-res-de.rc b/openvpn-gui-res-de.rc index 8c0a27f..619420a 100644 --- a/openvpn-gui-res-de.rc +++ b/openvpn-gui-res-de.rc @@ -226,7 +226,6 @@ BEGIN IDS_ERR_CREATE_EDIT_LOGWINDOW "Erstellen des RichEdit LogWindow fehlgeschlagen!!" IDS_ERR_SET_SIZE "Setzen der Grösse ist fehlgeschlagen!" IDS_ERR_AUTOSTART_CONF "Kann gewünschte Konfigurationdatei für Autostart nicht finden: %s" - IDS_ERR_VERSION_CREATE_EVENT "CreateEvent() beim überprüfen der OpenVPN version fehlgeschlagen." IDS_ERR_CREATE_PIPE_IN_READ "CreatePipe an hInputRead fehlgeschlagen." IDS_NFO_STATE_CONNECTING "Aktueller Status: Verbinden" IDS_NFO_CONNECTION_XXX "OpenVPN Verbindung (%s)" diff --git a/openvpn-gui-res-en.rc b/openvpn-gui-res-en.rc index 9543229..a49f90f 100644 --- a/openvpn-gui-res-en.rc +++ b/openvpn-gui-res-en.rc @@ -221,7 +221,6 @@ BEGIN IDS_ERR_CREATE_EDIT_LOGWINDOW "Creating RichEdit LogWindow Failed!!" IDS_ERR_SET_SIZE "Set Size failed!" IDS_ERR_AUTOSTART_CONF "Cannot find requested config to autostart: %s" - IDS_ERR_VERSION_CREATE_EVENT "CreateEvent() failed when checking openvpn version." IDS_ERR_CREATE_PIPE_IN_READ "CreatePipe on hInputRead failed." IDS_NFO_STATE_CONNECTING "Current State: Connecting" IDS_NFO_CONNECTION_XXX "OpenVPN Connection (%s)" diff --git a/openvpn-gui-res-es.rc b/openvpn-gui-res-es.rc index bb4bff0..3fbf31c 100644 --- a/openvpn-gui-res-es.rc +++ b/openvpn-gui-res-es.rc @@ -208,7 +208,6 @@ BEGIN IDS_ERR_CREATE_EDIT_LOGWINDOW "La creación de RichEdit LogWindow falló!!" IDS_ERR_SET_SIZE "Set Size falló!" IDS_ERR_AUTOSTART_CONF "No se encuentra la configuración requerida para el autoinicio: %s" - IDS_ERR_VERSION_CREATE_EVENT "CreateEvent() falló al chequear la versión de OpenVPN." IDS_ERR_CREATE_PIPE_IN_READ "CreatePipe on hInputRead falló." IDS_NFO_STATE_CONNECTING "Estado actual: Conectando" IDS_NFO_CONNECTION_XXX "Conexión OpenVPN (%s)" diff --git a/openvpn-gui-res-fi.rc b/openvpn-gui-res-fi.rc index 2fbf82d..0638d85 100644 --- a/openvpn-gui-res-fi.rc +++ b/openvpn-gui-res-fi.rc @@ -221,7 +221,6 @@ BEGIN IDS_ERR_CREATE_EDIT_LOGWINDOW "RichEdit LogWindowin luonti epäonnistui!" IDS_ERR_SET_SIZE "Koon määrittäminen epäonnistui!" IDS_ERR_AUTOSTART_CONF "Ei löydetty automaattisesti käynnistettävää asetustiedostoa %s" - IDS_ERR_VERSION_CREATE_EVENT "CreateEvent() failed when checking openvpn version." IDS_ERR_CREATE_PIPE_IN_READ "CreatePipe on hInputRead failed." IDS_NFO_STATE_CONNECTING "Tila: Yhdistetään" IDS_NFO_CONNECTION_XXX "OpenVPN-yhteys (%s)" diff --git a/openvpn-gui-res-fr.rc b/openvpn-gui-res-fr.rc index 8a5a367..79d55de 100644 --- a/openvpn-gui-res-fr.rc +++ b/openvpn-gui-res-fr.rc @@ -219,7 +219,6 @@ BEGIN IDS_ERR_CREATE_EDIT_LOGWINDOW "Creating RichEdit LogWindow échoué !" IDS_ERR_SET_SIZE "Set Size échoué !" IDS_ERR_AUTOSTART_CONF "Impossible de trouver la configuration pour démarrer automatiquement: %s" - IDS_ERR_VERSION_CREATE_EVENT "CreateEvent() échoué après vérification de la version OpenVPN." IDS_ERR_CREATE_PIPE_IN_READ "CreatePipe sur hInputRead échoué." IDS_NFO_STATE_CONNECTING "Etat actuel: En cours de connexion" IDS_NFO_CONNECTION_XXX "Connexion OpenVPN (%s)" diff --git a/openvpn-gui-res-it.rc b/openvpn-gui-res-it.rc index e6b4e29..3bba596 100644 --- a/openvpn-gui-res-it.rc +++ b/openvpn-gui-res-it.rc @@ -207,7 +207,6 @@ BEGIN IDS_ERR_CREATE_EDIT_LOGWINDOW "Creazione RichEdit LogWindow Fallita!!" IDS_ERR_SET_SIZE "Set Size fallita!" IDS_ERR_AUTOSTART_CONF "Non riesco a trovare una configurazione per partire in automatico: %s" - IDS_ERR_VERSION_CREATE_EVENT "CreateEvent() fallito quando ha cercato la versione di openvpn." IDS_ERR_CREATE_PIPE_IN_READ "CreatePipe on hInputRead falito." IDS_NFO_STATE_CONNECTING "Stato corrente: Connessione in corso" IDS_NFO_CONNECTION_XXX "Connessione OpenVPN (%s)" diff --git a/openvpn-gui-res-nl.rc b/openvpn-gui-res-nl.rc index 33a0565..c11c99b 100644 --- a/openvpn-gui-res-nl.rc +++ b/openvpn-gui-res-nl.rc @@ -207,7 +207,6 @@ BEGIN IDS_ERR_CREATE_EDIT_LOGWINDOW "Creatie van RichEdit LogWindow mislukt!" IDS_ERR_SET_SIZE "Instellen afmetingen mislukt!" IDS_ERR_AUTOSTART_CONF "Kan opgegeven configuratie voor automatische verbinding niet vinden: %s" - IDS_ERR_VERSION_CREATE_EVENT "CreateEvent() mislukt tijdens verificatie OpenVPN versie." IDS_ERR_CREATE_PIPE_IN_READ "CreatePipe tijdens hInputRead mislukt." IDS_NFO_STATE_CONNECTING "Huidige Status: Verbinden" IDS_NFO_CONNECTION_XXX "OpenVPN Verbinding (%s)" diff --git a/openvpn-gui-res-no.rc b/openvpn-gui-res-no.rc index 574ffa5..bb8e4ba 100644 --- a/openvpn-gui-res-no.rc +++ b/openvpn-gui-res-no.rc @@ -212,7 +212,6 @@ BEGIN IDS_ERR_CREATE_EDIT_LOGWINDOW "Opprettelse av RichEdit LogWindow feilet!!" IDS_ERR_SET_SIZE "Set Size feilet!" IDS_ERR_AUTOSTART_CONF "Følgende konfig kunne ikke starte automatisk: %s" - IDS_ERR_VERSION_CREATE_EVENT "CreateEvent() feilet under sjekk av openvpn version." IDS_ERR_CREATE_PIPE_IN_READ "CreatePipe på hInputRead feilet." IDS_NFO_STATE_CONNECTING "Status: Kobler til" IDS_NFO_CONNECTION_XXX "OpenVPN Tilkoblet (%s)" diff --git a/openvpn-gui-res-pt.rc b/openvpn-gui-res-pt.rc index 3504387..cef7e47 100644 --- a/openvpn-gui-res-pt.rc +++ b/openvpn-gui-res-pt.rc @@ -208,7 +208,6 @@ BEGIN IDS_ERR_CREATE_EDIT_LOGWINDOW "Creating RichEdit LogWindow falhou!!" IDS_ERR_SET_SIZE "Set Size falhou!" IDS_ERR_AUTOSTART_CONF "Impossível encontrar configurações para autostart: %s" - IDS_ERR_VERSION_CREATE_EVENT "CreateEvent() falhou ao verificar a versão do openvpn." IDS_ERR_CREATE_PIPE_IN_READ "CreatePipe em hInputRead falhou." IDS_NFO_STATE_CONNECTING "Estado atual: Conectando" IDS_NFO_CONNECTION_XXX "Conexão OpenVPN (%s)" diff --git a/openvpn-gui-res-se.rc b/openvpn-gui-res-se.rc index 5ada70a..5da0654 100644 --- a/openvpn-gui-res-se.rc +++ b/openvpn-gui-res-se.rc @@ -213,7 +213,6 @@ BEGIN IDS_ERR_CREATE_EDIT_LOGWINDOW "Skapande av RichEdit LogWindow misslyckades!!" IDS_ERR_SET_SIZE "Set Size misslyckades!" IDS_ERR_AUTOSTART_CONF "Följande konfig gick inte att automatiskt starta: %s" - IDS_ERR_VERSION_CREATE_EVENT "CreateEvent() misslyckades vid kontroll av openvpn version." IDS_ERR_CREATE_PIPE_IN_READ "CreatePipe på hInputRead misslyckades." IDS_NFO_STATE_CONNECTING "Status: Ansluter" IDS_NFO_CONNECTION_XXX "OpenVPN Anslutning (%s)" diff --git a/openvpn-gui-res.h b/openvpn-gui-res.h index 07340e1..707e0c0 100644 --- a/openvpn-gui-res.h +++ b/openvpn-gui-res.h @@ -152,7 +152,6 @@ #define IDS_ERR_CREATE_EDIT_LOGWINDOW 1236 #define IDS_ERR_SET_SIZE 1237 #define IDS_ERR_AUTOSTART_CONF 1238 -#define IDS_ERR_VERSION_CREATE_EVENT 1239 #define IDS_ERR_CREATE_PIPE_IN_READ 1240 #define IDS_NFO_STATE_CONNECTING 1241 #define IDS_NFO_CONNECTION_XXX 1242 diff --git a/openvpn.c b/openvpn.c index 46e6daf..097014e 100644 --- a/openvpn.c +++ b/openvpn.c @@ -2,6 +2,7 @@ * OpenVPN-GUI -- A Windows GUI for OpenVPN. * * Copyright (C) 2004 Mathias Sundman + * 2010 Heiko Hund * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,7 +24,6 @@ * . */ - #include #include #include @@ -48,50 +48,20 @@ extern options_t o; /* - * Creates a unique exit_event name based on the + * Creates a unique exit_event name based on the * config file number. */ -int CreateExitEvent(int config) +static BOOL +CreateExitEvent(int config) { - o.conn[config].exit_event = NULL; - if (o.oldversion == 1) - { - _sntprintf_0(o.conn[config].exit_event_name, _T("openvpn_exit")); - o.conn[config].exit_event = CreateEvent (NULL, - TRUE, - FALSE, - o.conn[config].exit_event_name); - if (o.conn[config].exit_event == NULL) - { - if (GetLastError() == ERROR_ACCESS_DENIED) - { - /* service mustn't be running, while using old version */ - ShowLocalizedMsg(IDS_ERR_STOP_SERV_OLD_VER); - } - else - { - /* error creating exit event */ - ShowLocalizedMsg(IDS_ERR_CREATE_EVENT, o.conn[config].exit_event_name); - } - return(false); - } + _sntprintf_0(o.conn[config].exit_event_name, _T("openvpngui_exit_event_%d"), config); + o.conn[config].exit_event = CreateEvent(NULL, TRUE, FALSE, o.conn[config].exit_event_name); + if (o.conn[config].exit_event == NULL) { + /* error creating exit event */ + ShowLocalizedMsg(IDS_ERR_CREATE_EVENT, o.conn[config].exit_event_name); + return FALSE; } - else - { - _sntprintf_0(o.conn[config].exit_event_name, _T("openvpngui_exit_event_%d"), config); - o.conn[config].exit_event = CreateEvent (NULL, - TRUE, - FALSE, - o.conn[config].exit_event_name); - if (o.conn[config].exit_event == NULL) - { - /* error creating exit event */ - ShowLocalizedMsg(IDS_ERR_CREATE_EVENT, o.conn[config].exit_event_name); - return(false); - } - } - - return(true); + return TRUE; } @@ -124,16 +94,69 @@ int SetProcessPriority(DWORD *priority) } +static BOOL +GetPipeHandles(PHANDLE phInputRead, PHANDLE phInputWrite, + PHANDLE phOutputRead, PHANDLE phOutputWrite) +{ + HANDLE hProc = GetCurrentProcess(); + HANDLE hOutputReadTmp, hInputWriteTmp; + SECURITY_DESCRIPTOR sd; + SECURITY_ATTRIBUTES sa; + + CLEAR(sa); + CLEAR(sd); + + /* Make security attributes for the pipes so they can be inherited */ + sa.nLength = sizeof(sa); + sa.lpSecurityDescriptor = &sd; + sa.bInheritHandle = TRUE; + if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) { + ShowLocalizedMsg(IDS_ERR_INIT_SEC_DESC); + return FALSE; + } + if (!SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE)) { + ShowLocalizedMsg(IDS_ERR_SET_SEC_DESC_ACL); + return FALSE; + } + + /* Create the stdin pipe with uninheritable write end */ + if (!CreatePipe(phInputRead, &hInputWriteTmp, &sa, 0)) { + ShowLocalizedMsg(IDS_ERR_CREATE_PIPE_IN_READ); + return FALSE; + } + if (!DuplicateHandle(hProc, hInputWriteTmp, hProc, phInputWrite, 0, FALSE, + DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) { + ShowLocalizedMsg(IDS_ERR_DUP_HANDLE_IN_WRITE); + CloseHandle(*phInputRead); + return FALSE; + } + + /* Create the stdout pipe with uninheritable read end */ + if (!CreatePipe(&hOutputReadTmp, phOutputWrite, &sa, 0)) { + ShowLocalizedMsg(IDS_ERR_CREATE_PIPE_OUTPUT); + CloseHandle(*phInputRead); + CloseHandle(*phInputWrite); + return FALSE; + } + if (!DuplicateHandle(hProc, hOutputReadTmp, hProc, phOutputRead, 0, FALSE, + DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) { + ShowLocalizedMsg(IDS_ERR_DUP_HANDLE_OUT_READ); + CloseHandle(*phInputRead); + CloseHandle(*phInputWrite); + CloseHandle(*phOutputWrite); + return FALSE; + } + return TRUE; +} + + /* * Launch an OpenVPN process */ int StartOpenVPN(int config) { - - HANDLE hOutputReadTmp = NULL; HANDLE hOutputRead = NULL; HANDLE hOutputWrite = NULL; - HANDLE hInputWriteTmp = NULL; HANDLE hInputRead = NULL; HANDLE hInputWrite = NULL; HANDLE hErrorWrite = NULL; @@ -143,37 +166,11 @@ int StartOpenVPN(int config) DWORD priority; STARTUPINFO start_info; PROCESS_INFORMATION proc_info; - SECURITY_ATTRIBUTES sa; - SECURITY_DESCRIPTOR sd; TCHAR command_line[256]; TCHAR proxy_string[100]; - int i, is_connected=0; CLEAR (start_info); CLEAR (proc_info); - CLEAR (sa); - CLEAR (sd); - - - /* If oldversion, allow only ONE connection */ - if (o.oldversion == 1) - { - for (i=0; i < o.num_configs; i++) - { - if ((o.conn[i].state != disconnected) && - (o.conn[i].state != disconnecting)) - { - is_connected=1; - break; - } - } - if (is_connected) - { - /* only one simultanious connection on old version */ - ShowLocalizedMsg(IDS_ERR_ONE_CONN_OLD_VER); - return(false); - } - } /* Warn if "log" or "log-append" option is found in config file */ if ((ConfigFileOptionExist(config, "log ")) || @@ -207,46 +204,13 @@ int StartOpenVPN(int config) ConstructProxyCmdLine(proxy_string, _tsizeof(proxy_string)); /* construct command line */ - if (o.oldversion == 1) - { - _sntprintf_0(command_line, _T("openvpn --config \"%s\" %s"), - o.conn[config].config_file, proxy_string); - } - else - { - _sntprintf_0(command_line, _T("openvpn --service %s 0 --config \"%s\" %s"), - o.conn[config].exit_event_name, - o.conn[config].config_file, - proxy_string); - } + _sntprintf_0(command_line, _T("openvpn --service %s 0 --config \"%s\" %s"), + o.conn[config].exit_event_name, + o.conn[config].config_file, + proxy_string); - - /* Make security attributes struct for logfile handle so it can - be inherited. */ - sa.nLength = sizeof (sa); - sa.lpSecurityDescriptor = &sd; - sa.bInheritHandle = TRUE; - if (!InitializeSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION)) - { - /* Init Sec. Desc. failed */ - ShowLocalizedMsg(IDS_ERR_INIT_SEC_DESC); - goto failed; - } - if (!SetSecurityDescriptorDacl (&sd, TRUE, NULL, FALSE)) - { - /* set Dacl failed */ - ShowLocalizedMsg(IDS_ERR_SET_SEC_DESC_ACL); - goto failed; - } - - - /* Create the child output pipe. */ - if (!CreatePipe(&hOutputReadTmp,&hOutputWrite,&sa,0)) - { - /* CreatePipe failed. */ - ShowLocalizedMsg(IDS_ERR_CREATE_PIPE_OUTPUT); - goto failed; - } + if (!GetPipeHandles(&hInputRead, &hInputWrite, &hOutputRead, &hOutputWrite)) + return false; // Create a duplicate of the output write handle for the std error // write handle. This is necessary in case the child application @@ -260,50 +224,6 @@ int StartOpenVPN(int config) goto failed; } - // Create the child input pipe. - if (!CreatePipe(&hInputRead,&hInputWriteTmp,&sa,0)) - { - /* CreatePipe failed. */ - ShowLocalizedMsg(IDS_ERR_CREATE_PIPE_INPUT); - goto failed; - } - - // Create new output read handle and the input write handles. Set - // the Properties to FALSE. Otherwise, the child inherits the - // properties and, as a result, non-closeable handles to the pipes - // are created. - if (!DuplicateHandle(GetCurrentProcess(),hOutputReadTmp, - GetCurrentProcess(), - &hOutputRead, // Address of new handle. - 0,FALSE, // Make it uninheritable. - DUPLICATE_SAME_ACCESS)) - { - /* Duplicate Handle failed. */ - ShowLocalizedMsg(IDS_ERR_DUP_HANDLE_OUT_READ); - goto failed; - } - - if (!DuplicateHandle(GetCurrentProcess(),hInputWriteTmp, - GetCurrentProcess(), - &hInputWrite, // Address of new handle. - 0,FALSE, // Make it uninheritable. - DUPLICATE_SAME_ACCESS)) - { - /* DuplicateHandle failed */ - ShowLocalizedMsg(IDS_ERR_DUP_HANDLE_IN_WRITE); - goto failed; - } - - /* Close inheritable copies of the handles */ - if (!CloseHandle(hOutputReadTmp) || !CloseHandle(hInputWriteTmp)) - { - /* Close Handle failed */ - ShowLocalizedMsg(IDS_ERR_CLOSE_HANDLE_TMP); - CloseHandle (o.conn[config].exit_event); - return(0); - } - hOutputReadTmp=NULL; - hInputWriteTmp=NULL; /* fill in STARTUPINFO struct */ GetStartupInfo(&start_info); @@ -623,242 +543,67 @@ int VerifyAutoConnections() } -int CheckVersion() +BOOL +CheckVersion() { - HANDLE hOutputReadTmp = NULL; - HANDLE hOutputRead = NULL; - HANDLE hOutputWrite = NULL; - HANDLE hInputWriteTmp = NULL; - HANDLE hInputRead = NULL; - HANDLE hInputWrite = NULL; - HANDLE exit_event; + HANDLE hOutputRead; + HANDLE hOutputWrite; + HANDLE hInputRead; + HANDLE hInputWrite; - STARTUPINFO start_info; - PROCESS_INFORMATION proc_info; - SECURITY_ATTRIBUTES sa; - SECURITY_DESCRIPTOR sd; - TCHAR command_line[256]; - char line[1024]; - TCHAR bin_path[MAX_PATH]; - char *p; - int oldversion, i; + BOOL retval = FALSE; + STARTUPINFO si; + PROCESS_INFORMATION pi; + TCHAR cmdline[] = _T("openvpn --version"); + char match_version[] = "OpenVPN 2."; + TCHAR pwd[MAX_PATH]; + char line[1024]; + TCHAR *p; - CLEAR (start_info); - CLEAR (proc_info); - CLEAR (sa); - CLEAR (sd); - - exit_event = CreateEvent (NULL, TRUE, FALSE, _T("openvpn_exit")); - if (exit_event == NULL) - { + CLEAR(si); + CLEAR(pi); + + if (!GetPipeHandles(&hInputRead, &hInputWrite, &hOutputRead, &hOutputWrite)) + return FALSE; + + /* Construct the process' working directory */ + _tcsncpy(pwd, o.exe_path, _tsizeof(pwd)); + p = _tcsrchr(pwd, _T('\\')); + if (p != NULL) + *p = _T('\0'); + + /* Fill in STARTUPINFO struct */ + si.cb = sizeof(si); + si.dwFlags = STARTF_USESTDHANDLES; + si.hStdInput = hInputRead; + si.hStdOutput = hOutputWrite; + si.hStdError = hOutputWrite; + + /* Start OpenVPN to check version */ + if (!CreateProcess(o.exe_path, cmdline, NULL, NULL, TRUE, + CREATE_NO_WINDOW, NULL, pwd, &si, &pi)) { + ShowLocalizedMsg(IDS_ERR_CREATE_PROCESS, o.exe_path, cmdline, pwd); + } + else if (ReadLineFromStdOut(hOutputRead, 0, line)) { #ifdef DEBUG - PrintErrorDebug("CreateEvent(openvpn_exit) failed."); + PrintDebug("VersionString: %s", line); #endif - if (GetLastError() == ERROR_ACCESS_DENIED) - { - /* Assume we're running OpenVPN 1.5/1.6 and the service is started. */ - o.oldversion=1; - strncpy(o.connect_string, "Successful ARP Flush", sizeof(o.connect_string)); - return(true); - } - else - { - /* CreateEvent failed */ - ShowLocalizedMsg(IDS_ERR_VERSION_CREATE_EVENT); - return(false); - } + CloseHandle(pi.hThread); + CloseHandle(pi.hProcess); + + /* OpenVPN version 2.x */ + if (strstr(line, match_version)) + retval = TRUE; } -#ifdef DEBUG - PrintErrorDebug("CreateEvent(openvpn_exit) succeded."); -#endif + if (!CloseHandle(hInputRead) || !CloseHandle(hInputWrite) + || !CloseHandle(hOutputRead) || !CloseHandle(hOutputWrite)) + ShowLocalizedMsg(IDS_ERR_CLOSE_HANDLE); - /* construct command line */ - _sntprintf_0(command_line, _T("openvpn --version")); - - /* construct bin path */ - _tcsncpy(bin_path, o.exe_path, _tsizeof(bin_path)); - for (i=_tcslen(bin_path) - 1; i > 0; i--) - if (bin_path[i] == '\\') break; - bin_path[i] = '\0'; - - /* Make security attributes struct for logfile handle so it can - be inherited. */ - sa.nLength = sizeof (sa); - sa.lpSecurityDescriptor = &sd; - sa.bInheritHandle = TRUE; - if (!InitializeSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION)) - { - /* Init Sec. Desc. failed */ - ShowLocalizedMsg(IDS_ERR_INIT_SEC_DESC); - return(0); - } - if (!SetSecurityDescriptorDacl (&sd, TRUE, NULL, FALSE)) - { - /* Set Dacl failed */ - ShowLocalizedMsg(IDS_ERR_SET_SEC_DESC_ACL); - return(0); - } - - /* Create the child input pipe. */ - if (!CreatePipe(&hInputRead,&hInputWriteTmp,&sa,0)) - { - /* create pipe failed */ - ShowLocalizedMsg(IDS_ERR_CREATE_PIPE_IN_READ); - return(0); - } - - /* Create the child output pipe. */ - if (!CreatePipe(&hOutputReadTmp,&hOutputWrite,&sa,0)) - { - /* CreatePipe failed */ - ShowLocalizedMsg(IDS_ERR_CREATE_PIPE_OUTPUT); - return(0); - } - - if (!DuplicateHandle(GetCurrentProcess(),hOutputReadTmp, - GetCurrentProcess(), - &hOutputRead, // Address of new handle. - 0,FALSE, // Make it uninheritable. - DUPLICATE_SAME_ACCESS)) - { - /* DuplicateHandle failed */ - ShowLocalizedMsg(IDS_ERR_DUP_HANDLE_OUT_READ); - return(0); - } - - if (!DuplicateHandle(GetCurrentProcess(),hInputWriteTmp, - GetCurrentProcess(), - &hInputWrite, // Address of new handle. - 0,FALSE, // Make it uninheritable. - DUPLICATE_SAME_ACCESS)) - { - /* DuplicateHandle failed */ - ShowLocalizedMsg(IDS_ERR_DUP_HANDLE_IN_WRITE); - return(0); - } - - - /* Close inheritable copies of the handles */ - if (!CloseHandle(hOutputReadTmp) || !CloseHandle(hInputWriteTmp)) - { - /* CloseHandle failed */ - ShowLocalizedMsg(IDS_ERR_CLOSE_HANDLE_TMP); - return(0); - } - - /* fill in STARTUPINFO struct */ - GetStartupInfo(&start_info); - start_info.cb = sizeof(start_info); - start_info.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; - start_info.wShowWindow = SW_HIDE; - start_info.hStdInput = hInputRead; - start_info.hStdOutput = hOutputWrite; - start_info.hStdError = hOutputWrite; - - /* Start OpenVPN to check version */ - if (!CreateProcess(o.exe_path, - command_line, - NULL, - NULL, - TRUE, - CREATE_NEW_CONSOLE, - NULL, - bin_path, - &start_info, - &proc_info)) - { - /* CreateProcess failed */ - ShowLocalizedMsg(IDS_ERR_CREATE_PROCESS, - o.exe_path, - command_line, - bin_path); - return(0); - } - - /* Default value for oldversion */ - oldversion=0; - - /* Default string to look for to report "Connected". */ - strncpy(o.connect_string, "Successful ARP Flush", sizeof(o.connect_string)); - - if (ReadLineFromStdOut(hOutputRead, 0, line) == 1) - { -#ifdef DEBUG - PrintDebug("VersionString: %s", line); -#endif - if (line[8] == '2') /* Majorversion = 2 */ - { - if (line[10] == '0') /* Minorversion = 0 */ - { - p=strstr(line, "beta"); - if (p != NULL) - { - if (p[5] == ' ') /* 2.0-beta1 - 2.0-beta9 */ - { - if (p[4] >= '6') /* 2.0-beta6 - 2.0-beta9 */ - { - oldversion=0; - } - else /* < 2.0-beta6 */ - { - oldversion=1; - } - } - else /* >= 2.0-beta10 */ - { - if (strncmp(&p[6], "ms", 2) == 0) /* 2.0-betaXXms */ - strncpy(o.connect_string, "Initialization Sequence Completed", - sizeof(o.connect_string)); - if ( !((p[4] == 1) && (p[5] == 0)) ) /* >= 2.0-beta11 */ - strncpy(o.connect_string, "Initialization Sequence Completed", - sizeof(o.connect_string)); - - oldversion=0; - } - } - else /* 2.0 non-beta */ - { - strncpy(o.connect_string, "Initialization Sequence Completed", - sizeof(o.connect_string)); - oldversion=0; - } - } - else /* > 2.0 */ - { - strncpy(o.connect_string, "Initialization Sequence Completed", - sizeof(o.connect_string)); - oldversion=0; - } - } - else - { - if (line[8] == '1') /* Majorversion = 1 */ - { - oldversion=1; - } - else /* Majorversion != (1 || 2) */ - { - oldversion=0; - } - } - } - else return(0); - - o.oldversion = oldversion; - - - if(!CloseHandle (proc_info.hThread) || !CloseHandle (hOutputWrite) - || !CloseHandle (hInputRead) || !CloseHandle(exit_event)) - { - /* CloseHandle failed */ - ShowLocalizedMsg(IDS_ERR_CLOSE_HANDLE); - return(0); - } - - return(1); + return retval; } + void CheckAndSetTrayIcon() { diff --git a/openvpn.h b/openvpn.h index e197a9b..36a3474 100644 --- a/openvpn.h +++ b/openvpn.h @@ -32,7 +32,7 @@ int ReadLineFromStdOut(HANDLE hStdOut, int config, char line[1024]); BOOL CALLBACK StatusDialogFunc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam); int AutoStartConnections(); int VerifyAutoConnections(); -int CheckVersion(); +BOOL CheckVersion(); void CheckAndSetTrayIcon(); void SetStatusWinIcon(HWND hwndDlg, int IconID); void ThreadOpenVPNStatus(int status) __attribute__ ((noreturn)); diff --git a/openvpn_monitor_process.c b/openvpn_monitor_process.c index 6594edf..7606e4f 100644 --- a/openvpn_monitor_process.c +++ b/openvpn_monitor_process.c @@ -40,6 +40,8 @@ extern options_t o; +static const char connect_string[] = "Initialization Sequence Completed"; + /* Wait for a complete line (CR/LF) and return it. * Return values: * 1 - Successful. Line is available in *line. @@ -197,7 +199,7 @@ void monitor_openvpnlog_while_connecting(int config, char *line) char *linepos; /* Check for Connected message */ - if (strstr(line, o.connect_string) != NULL) + if (strstr(line, connect_string) != NULL) { /* Run Connect Script */ RunConnectScript(config, false); @@ -317,7 +319,7 @@ void monitor_openvpnlog_while_reconnecting(int config, char *line) size_t i; /* Check for Connected message */ - if (strstr(line, o.connect_string) != NULL) + if (strstr(line, connect_string) != NULL) { o.conn[config].state = connected; SetTrayIcon(connected); diff --git a/options.h b/options.h index 93b57dd..b9f674c 100644 --- a/options.h +++ b/options.h @@ -97,9 +97,7 @@ typedef struct { connection_t conn[MAX_CONFIGS]; /* Connection structure */ int num_configs; /* Number of configs */ - BOOL oldversion; /* OpenVPN version below 2.0-beta6 if TRUE */ service_state_t service_state; /* State of the OpenVPN Service */ - char connect_string[100]; /* String to look for to report connected */ int psw_attempts; /* Number of psw attemps to allow */ int connectscript_timeout; /* Connect Script execution timeout (sec) */ int disconnectscript_timeout; /* Disconnect Script execution timeout (sec) */