Merge pull request #16 from selvanair/error-messages

Better error reporting when connection fails to come up
pull/19/head
Samuli Seppänen 2016-02-22 10:13:01 +02:00
commit 49746f2995
8 changed files with 92 additions and 6 deletions

3
main.c
View File

@ -178,6 +178,9 @@ int WINAPI _tWinMain (HINSTANCE hThisInstance,
exit(1); exit(1);
} }
if (!IsUserAdmin())
CheckIServiceStatus();
BuildFileList(); BuildFileList();
if (!VerifyAutoConnections()) { if (!VerifyAutoConnections()) {
exit(1); exit(1);

View File

@ -54,7 +54,6 @@ InitManagement(const mgmt_rtmsg_handler *handler)
} }
} }
/* /*
* Connect to the OpenVPN management interface and register * Connect to the OpenVPN management interface and register
* asynchronous socket event notification for it * asynchronous socket event notification for it
@ -205,7 +204,11 @@ OnManagement(SOCKET sk, LPARAM lParam)
if (time(NULL) < c->manage.timeout) if (time(NULL) < c->manage.timeout)
connect(c->manage.sk, (SOCKADDR *)&c->manage.skaddr, sizeof(c->manage.skaddr)); connect(c->manage.sk, (SOCKADDR *)&c->manage.skaddr, sizeof(c->manage.skaddr));
else else
{
/* Connection to MI timed out. */
c->state = timedout;
rtmsg_handler[stop](c, ""); rtmsg_handler[stop](c, "");
}
} }
break; break;

View File

@ -156,6 +156,8 @@
#define IDS_ERR_CONN_SCRIPT_FAILED 1248 #define IDS_ERR_CONN_SCRIPT_FAILED 1248
#define IDS_ERR_RUN_CONN_SCRIPT_TIMEOUT 1249 #define IDS_ERR_RUN_CONN_SCRIPT_TIMEOUT 1249
#define IDS_ERR_CONFIG_EXIST 1251 #define IDS_ERR_CONFIG_EXIST 1251
#define IDS_NFO_CONN_TIMEOUT 1252
#define IDS_NFO_NO_CONFIGS 1253
/* Program Startup Related */ /* Program Startup Related */
#define IDS_ERR_OPEN_DEBUG_FILE 1301 #define IDS_ERR_OPEN_DEBUG_FILE 1301
@ -225,6 +227,10 @@
#define IDS_ERR_OPEN_SCMGR 1706 #define IDS_ERR_OPEN_SCMGR 1706
#define IDS_ERR_STOP_SERVICE 1707 #define IDS_ERR_STOP_SERVICE 1707
#define IDS_NFO_RESTARTED 1708 #define IDS_NFO_RESTARTED 1708
#define IDS_ERR_ACCESS_SERVICE_PIPE 1709
#define IDS_ERR_WRITE_SERVICE_PIPE 1710
#define IDS_ERR_NOTSTARTED_ISERVICE 1711
#define IDS_ERR_INSTALL_ISERVICE 1712
/* Registry Related */ /* Registry Related */
#define IDS_ERR_GET_WINDOWS_DIR 1801 #define IDS_ERR_GET_WINDOWS_DIR 1801

View File

@ -341,6 +341,7 @@ void
OnStop(connection_t *c, UNUSED char *msg) OnStop(connection_t *c, UNUSED char *msg)
{ {
UINT txt_id, msg_id; UINT txt_id, msg_id;
TCHAR *msg_xtra;
SetMenuStatus(c, disconnected); SetMenuStatus(c, disconnected);
switch (c->state) switch (c->state)
@ -366,9 +367,13 @@ OnStop(connection_t *c, UNUSED char *msg)
case resuming: case resuming:
case connecting: case connecting:
case reconnecting: case reconnecting:
case timedout:
/* We have failed to (re)connect */ /* We have failed to (re)connect */
txt_id = c->state == reconnecting ? IDS_NFO_STATE_FAILED_RECONN : IDS_NFO_STATE_FAILED; txt_id = c->state == reconnecting ? IDS_NFO_STATE_FAILED_RECONN : IDS_NFO_STATE_FAILED;
msg_id = c->state == reconnecting ? IDS_NFO_RECONN_FAILED : IDS_NFO_CONN_FAILED; msg_id = c->state == reconnecting ? IDS_NFO_RECONN_FAILED : IDS_NFO_CONN_FAILED;
msg_xtra = c->state == timedout ? c->log_path : c->config_name;
if (c->state == timedout)
msg_id = IDS_NFO_CONN_TIMEOUT;
c->state = disconnecting; c->state = disconnecting;
CheckAndSetTrayIcon(); CheckAndSetTrayIcon();
@ -382,7 +387,7 @@ OnStop(connection_t *c, UNUSED char *msg)
SetForegroundWindow(c->hwndStatus); SetForegroundWindow(c->hwndStatus);
ShowWindow(c->hwndStatus, SW_SHOW); ShowWindow(c->hwndStatus, SW_SHOW);
} }
ShowLocalizedMsg(msg_id, c->config_name); ShowLocalizedMsg(msg_id, msg_xtra);
SendMessage(c->hwndStatus, WM_CLOSE, 0, 0); SendMessage(c->hwndStatus, WM_CLOSE, 0, 0);
break; break;
@ -704,7 +709,7 @@ StartOpenVPN(connection_t *c)
DWORD dwMode = PIPE_READMODE_MESSAGE; DWORD dwMode = PIPE_READMODE_MESSAGE;
if (!SetNamedPipeHandleState(service, &dwMode, NULL, NULL)) if (!SetNamedPipeHandleState(service, &dwMode, NULL, NULL))
{ {
// TODO: add error message ShowLocalizedMsg (IDS_ERR_ACCESS_SERVICE_PIPE);
CloseHandle(c->exit_event); CloseHandle(c->exit_event);
goto out; goto out;
} }
@ -716,7 +721,7 @@ StartOpenVPN(connection_t *c)
if (!WriteFile(service, startup_info, size * sizeof (TCHAR), &written, NULL)) if (!WriteFile(service, startup_info, size * sizeof (TCHAR), &written, NULL))
{ {
// TODO: add error message ShowLocalizedMsg (IDS_ERR_WRITE_SERVICE_PIPE);
CloseHandle(c->exit_event); CloseHandle(c->exit_event);
goto out; goto out;
} }

View File

@ -61,6 +61,22 @@ match(const WIN32_FIND_DATA *find, const TCHAR *ext)
return match_false; return match_false;
} }
static bool
CheckReadAccess (const TCHAR *path)
{
HANDLE h;
bool ret = FALSE;
h = CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if ( h != INVALID_HANDLE_VALUE )
{
ret = TRUE;
CloseHandle (h);
}
return ret;
}
static int static int
ConfigAlreadyExists(TCHAR *newconfig) ConfigAlreadyExists(TCHAR *newconfig)
@ -111,6 +127,8 @@ BuildFileList()
HANDLE find_handle; HANDLE find_handle;
TCHAR find_string[MAX_PATH]; TCHAR find_string[MAX_PATH];
TCHAR subdir_table[MAX_CONFIG_SUBDIRS][MAX_PATH]; TCHAR subdir_table[MAX_CONFIG_SUBDIRS][MAX_PATH];
TCHAR fullpath[MAX_PATH];
static int warn_no_configs = 1;
int subdirs = 0; int subdirs = 0;
int i; int i;
@ -134,7 +152,9 @@ BuildFileList()
match_t match_type = match(&find_obj, o.ext_string); match_t match_type = match(&find_obj, o.ext_string);
if (match_type == match_file) if (match_type == match_file)
{ {
AddConfigFileToList(o.num_configs++, find_obj.cFileName, o.config_dir); _sntprintf_0(fullpath, _T("%s\\%s"), o.config_dir, find_obj.cFileName);
if (CheckReadAccess (fullpath))
AddConfigFileToList(o.num_configs++, find_obj.cFileName, o.config_dir);
} }
else if (match_type == match_dir) else if (match_type == match_dir)
{ {
@ -184,4 +204,9 @@ BuildFileList()
FindClose(find_handle); FindClose(find_handle);
} }
if (o.num_configs == 0 && warn_no_configs)
{
ShowLocalizedMsg(IDS_NFO_NO_CONFIGS, o.config_dir);
warn_no_configs = 0;
}
} }

View File

@ -67,7 +67,8 @@ typedef enum {
disconnecting, disconnecting,
suspending, suspending,
suspended, suspended,
resuming resuming,
timedout
} conn_state_t; } conn_state_t;
/* Connections parameters */ /* Connections parameters */

View File

@ -182,6 +182,7 @@ BEGIN
/* OpenVPN */ /* OpenVPN */
IDS_ERR_MANY_CONFIGS "OpenVPN GUI does not support more than %d configs. Please contact the author if you have the need for more." IDS_ERR_MANY_CONFIGS "OpenVPN GUI does not support more than %d configs. Please contact the author if you have the need for more."
IDS_NFO_NO_CONFIGS "No readable connection profiles (config files) found at %s"
IDS_ERR_ONE_CONN_OLD_VER "You can only have one connection running at the same time when using an older version on OpenVPN than 2.0-beta6." IDS_ERR_ONE_CONN_OLD_VER "You can only have one connection running at the same time when using an older version on OpenVPN than 2.0-beta6."
IDS_ERR_STOP_SERV_OLD_VER "You cannot use OpenVPN GUI to start a connection while the OpenVPN Service is running (with OpenVPN 1.5/1.6). Stop OpenVPN Service first if you want to use OpenVPN GUI." IDS_ERR_STOP_SERV_OLD_VER "You cannot use OpenVPN GUI to start a connection while the OpenVPN Service is running (with OpenVPN 1.5/1.6). Stop OpenVPN Service first if you want to use OpenVPN GUI."
IDS_ERR_CREATE_EVENT "CreateEvent failed on exit event: %s" IDS_ERR_CREATE_EVENT "CreateEvent failed on exit event: %s"
@ -226,6 +227,8 @@ BEGIN
IDS_ERR_CONFIG_EXIST "There already exist a config file named '%s'. You cannot " \ IDS_ERR_CONFIG_EXIST "There already exist a config file named '%s'. You cannot " \
"have multiple config files with the same name, even if " \ "have multiple config files with the same name, even if " \
"they reside in diffrent folders." "they reside in diffrent folders."
IDS_NFO_CONN_TIMEOUT "Connecting to management interface failed.\n" \
"View log file (%s) for more details."
/* main - Resources */ /* main - Resources */
IDS_ERR_OPEN_DEBUG_FILE "Error opening debug file (%s) for output." IDS_ERR_OPEN_DEBUG_FILE "Error opening debug file (%s) for output."
IDS_ERR_CREATE_PATH "Could not create %s path:\n%s" IDS_ERR_CREATE_PATH "Could not create %s path:\n%s"
@ -322,6 +325,12 @@ BEGIN
IDS_ERR_OPEN_SCMGR "OpenSCManager failed (%d)" IDS_ERR_OPEN_SCMGR "OpenSCManager failed (%d)"
IDS_ERR_STOP_SERVICE "Failed to stop OpenVPN Service" IDS_ERR_STOP_SERVICE "Failed to stop OpenVPN Service"
IDS_NFO_RESTARTED "OpenVPN Service Restarted." IDS_NFO_RESTARTED "OpenVPN Service Restarted."
IDS_ERR_ACCESS_SERVICE_PIPE "Access to service pipe failed."
IDS_ERR_WRITE_SERVICE_PIPE "Writing to service pipe failed."
IDS_ERR_INSTALL_ISERVICE """OpenVPNServiceInteractive"" is not installed.\n"
"Tasks requiring administrative access may not work."
IDS_ERR_NOTSTARTED_ISERVICE """OpenVPNServiceInteractive"" is not started.\n"
"Tasks requiring administrative access may not work."
/* registry */ /* registry */
IDS_ERR_GET_WINDOWS_DIR "Error getting Windows Directory." IDS_ERR_GET_WINDOWS_DIR "Error getting Windows Directory."

View File

@ -241,6 +241,40 @@ int MyReStartService()
return(false); return(false);
} }
bool
CheckIServiceStatus()
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
SERVICE_STATUS ssStatus;
// Open a handle to the SC Manager database.
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (NULL == schSCManager)
return(false);
schService = OpenService(schSCManager, _T("OpenVPNServiceInteractive"),
SERVICE_QUERY_STATUS);
if (schService == NULL &&
GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
{
/* warn that iservice is not installed */
ShowLocalizedMsg(IDS_ERR_INSTALL_ISERVICE);
return(false);
}
if (!QueryServiceStatus(schService, &ssStatus))
return(false);
if (ssStatus.dwCurrentState != SERVICE_RUNNING)
{
/* warn that iservice is not started */
ShowLocalizedMsg(IDS_ERR_NOTSTARTED_ISERVICE);
return(false);
}
return true;
}
int CheckServiceStatus() int CheckServiceStatus()
{ {