void CheckAuthUsernamePrompt (char *line, int config) { DWORD nCharsWritten; struct user_auth user_auth; /* Check for Passphrase prompt */ if (strncmp(line, "Enter Auth Username:"******""); } } else { if (!WriteFile(o.cnn[config].hStdIn, "\n", 1, &nCharsWritten, NULL)) { ShowLocalizedMsg(GUI_NAME, ERR_CR2STDIN, ""); } } if (strlen(user_auth.password) > 0) { if (!WriteFile(o.cnn[config].hStdIn, user_auth.password, strlen(user_auth.password), &nCharsWritten, NULL)) { ShowLocalizedMsg(GUI_NAME, ERR_AUTH_PASSWORD2STDIN, ""); } } else { if (!WriteFile(o.cnn[config].hStdIn, "\n", 1, &nCharsWritten, NULL)) { ShowLocalizedMsg(GUI_NAME, ERR_CR2STDIN, ""); } } /* Remove Username prompt from lastline buffer */ line[0]='\0'; /* Clear user_auth buffer */ CLEAR(user_auth); } }
/* * DialogProc for OpenVPN username/password auth dialog windows */ INT_PTR CALLBACK UserAuthDialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { connection_t *c; switch (msg) { case WM_INITDIALOG: /* Set connection for this dialog and show it */ c = (connection_t *) lParam; SetProp(hwndDlg, cfgProp, (HANDLE) c); if (c->state == resuming) ForceForegroundWindow(hwndDlg); else SetForegroundWindow(hwndDlg); break; case WM_COMMAND: c = (connection_t *) GetProp(hwndDlg, cfgProp); switch (LOWORD(wParam)) { case ID_EDT_AUTH_USER: if (HIWORD(wParam) == EN_UPDATE) { int len = Edit_GetTextLength((HWND) lParam); EnableWindow(GetDlgItem(hwndDlg, IDOK), (len ? TRUE : FALSE)); } break; case IDOK: ManagementCommandFromInput(c, "username \"Auth\" \"%s\"", hwndDlg, ID_EDT_AUTH_USER); ManagementCommandFromInput(c, "password \"Auth\" \"%s\"", hwndDlg, ID_EDT_AUTH_PASS); EndDialog(hwndDlg, LOWORD(wParam)); return TRUE; case IDCANCEL: EndDialog(hwndDlg, LOWORD(wParam)); StopOpenVPN(c); return TRUE; } break; case WM_CLOSE: EndDialog(hwndDlg, LOWORD(wParam)); return TRUE; case WM_NCDESTROY: RemoveProp(hwndDlg, cfgProp); break; } return FALSE; }
static void ResumeConnections() { int i; for (i = 0; i < o.num_configs; i++) { /* Restart suspend connections */ if (o.conn[i].state == suspended) StartOpenVPN(&o.conn[i]); /* If some connection never reached SUSPENDED state */ if (o.conn[i].state == suspending) StopOpenVPN(&o.conn[i]); } }
static void StopAllOpenVPN() { int i; for (i = 0; i < o.num_configs; i++) { if (o.conn[i].state != disconnected) StopOpenVPN(&o.conn[i]); } /* Wait for all connections to terminate (Max 5 sec) */ for (i = 0; i < 20; i++, Sleep(250)) { if (CountConnState(disconnected) == o.num_configs) break; } }
void CheckPrivateKeyPassphrasePrompt (char *line, int config) { DWORD nCharsWritten; char passphrase_ascii[256]; /* Check for Passphrase prompt */ if (strncmp(line, "Enter PEM pass phrase:", 22) == 0) { CLEAR(passphrase); if (DialogBox(o.hInstance, (LPCTSTR)IDD_PASSPHRASE, NULL, (DLGPROC)PassphraseDialogFunc) == IDCANCEL) { StopOpenVPN(config); } if (wcslen(passphrase) > 0) { CLEAR(passphrase_ascii); ConvertUnicode2Ascii(passphrase, passphrase_ascii, sizeof(passphrase_ascii)); if (!WriteFile(o.cnn[config].hStdIn, passphrase_ascii, strlen(passphrase_ascii), &nCharsWritten, NULL)) { /* PassPhrase -> stdin failed */ ShowLocalizedMsg(GUI_NAME, ERR_PASSPHRASE2STDIN, ""); } } if (!WriteFile(o.cnn[config].hStdIn, "\r\n", 2, &nCharsWritten, NULL)) { /* CR -> stdin failed */ ShowLocalizedMsg(GUI_NAME, ERR_CR2STDIN, ""); } /* Remove Passphrase prompt from lastline buffer */ line[0]='\0'; /* Clear passphrase buffer */ CLEAR(passphrase); CLEAR(passphrase_ascii); } /* Check for new passphrase prompt introduced with OpenVPN 2.0-beta12. */ if (strncmp(line, "Enter Private Key Password:"******""); } } else { if (!WriteFile(o.cnn[config].hStdIn, "\n", 1, &nCharsWritten, NULL)) { /* CR -> stdin failed */ ShowLocalizedMsg(GUI_NAME, ERR_CR2STDIN, ""); } } /* Remove Passphrase prompt from lastline buffer */ line[0]='\0'; /* Clear passphrase buffer */ CLEAR(passphrase); CLEAR(passphrase_ascii); } }
/* * DialogProc for OpenVPN status dialog windows */ INT_PTR CALLBACK StatusDialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { connection_t *c; switch (msg) { case WM_MANAGEMENT: /* Management interface related event */ OnManagement(wParam, lParam); return TRUE; case WM_INITDIALOG: c = (connection_t *) lParam; /* Set window icon "disconnected" */ SetStatusWinIcon(hwndDlg, ID_ICO_CONNECTING); /* Set connection for this dialog */ SetProp(hwndDlg, cfgProp, (HANDLE) c); /* Create log window */ HWND hLogWnd = CreateWindowEx(0, RICHEDIT_CLASS, NULL, WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|ES_SUNKEN|ES_LEFT| ES_MULTILINE|ES_READONLY|ES_AUTOHSCROLL|ES_AUTOVSCROLL, 20, 25, 350, 160, hwndDlg, (HMENU) ID_EDT_LOG, o.hInstance, NULL); if (!hLogWnd) { ShowLocalizedMsg(IDS_ERR_CREATE_EDIT_LOGWINDOW); return FALSE; } /* Set font and fontsize of the log window */ CHARFORMAT cfm = { .cbSize = sizeof(CHARFORMAT), .dwMask = CFM_SIZE|CFM_FACE|CFM_BOLD, .szFaceName = _T("Microsoft Sans Serif"), .dwEffects = 0, .yHeight = 160 }; if (SendMessage(hLogWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM) &cfm) == 0) ShowLocalizedMsg(IDS_ERR_SET_SIZE); /* Set size and position of controls */ RECT rect; GetClientRect(hwndDlg, &rect); MoveWindow(hLogWnd, 20, 25, rect.right - 40, rect.bottom - 70, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_TXT_STATUS), 20, 5, rect.right - 25, 15, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_DISCONNECT), 20, rect.bottom - 30, 110, 23, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_RESTART), 145, rect.bottom - 30, 110, 23, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_HIDE), rect.right - 130, rect.bottom - 30, 110, 23, TRUE); /* Set focus on the LogWindow so it scrolls automatically */ SetFocus(hLogWnd); return FALSE; case WM_SIZE: MoveWindow(GetDlgItem(hwndDlg, ID_EDT_LOG), 20, 25, LOWORD(lParam) - 40, HIWORD(lParam) - 70, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_DISCONNECT), 20, HIWORD(lParam) - 30, 110, 23, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_RESTART), 145, HIWORD(lParam) - 30, 110, 23, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_HIDE), LOWORD(lParam) - 130, HIWORD(lParam) - 30, 110, 23, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_TXT_STATUS), 20, 5, LOWORD(lParam) - 25, 15, TRUE); InvalidateRect(hwndDlg, NULL, TRUE); return TRUE; case WM_COMMAND: c = (connection_t *) GetProp(hwndDlg, cfgProp); switch (LOWORD(wParam)) { case ID_DISCONNECT: SetFocus(GetDlgItem(c->hwndStatus, ID_EDT_LOG)); StopOpenVPN(c); return TRUE; case ID_HIDE: if (c->state != disconnected) ShowWindow(hwndDlg, SW_HIDE); else DestroyWindow(hwndDlg); return TRUE; case ID_RESTART: c->state = reconnecting; SetFocus(GetDlgItem(c->hwndStatus, ID_EDT_LOG)); ManagementCommand(c, "signal SIGHUP", NULL, regular); return TRUE; } break; case WM_SHOWWINDOW: if (wParam == TRUE) { c = (connection_t *) GetProp(hwndDlg, cfgProp); if (c->hwndStatus) SetFocus(GetDlgItem(c->hwndStatus, ID_EDT_LOG)); } return FALSE; case WM_CLOSE: c = (connection_t *) GetProp(hwndDlg, cfgProp); if (c->state != disconnected) ShowWindow(hwndDlg, SW_HIDE); else DestroyWindow(hwndDlg); return TRUE; case WM_NCDESTROY: RemoveProp(hwndDlg, cfgProp); break; case WM_DESTROY: PostQuitMessage(0); break; case WM_OVPN_STOP: c = (connection_t *) GetProp(hwndDlg, cfgProp); c->state = disconnecting; RunDisconnectScript(c, false); EnableWindow(GetDlgItem(c->hwndStatus, ID_DISCONNECT), FALSE); EnableWindow(GetDlgItem(c->hwndStatus, ID_RESTART), FALSE); SetMenuStatus(c, disconnecting); SetDlgItemText(c->hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_WAIT_TERM)); SetEvent(c->exit_event); SetTimer(hwndDlg, IDT_STOP_TIMER, 3000, NULL); break; case WM_OVPN_SUSPEND: c = (connection_t *) GetProp(hwndDlg, cfgProp); c->state = suspending; EnableWindow(GetDlgItem(c->hwndStatus, ID_DISCONNECT), FALSE); EnableWindow(GetDlgItem(c->hwndStatus, ID_RESTART), FALSE); SetMenuStatus(c, disconnecting); SetDlgItemText(c->hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_WAIT_TERM)); SetEvent(c->exit_event); SetTimer(hwndDlg, IDT_STOP_TIMER, 3000, NULL); break; case WM_TIMER: PrintDebug(L"WM_TIMER message with wParam = %lu", wParam); c = (connection_t *) GetProp(hwndDlg, cfgProp); if (wParam == IDT_STOP_TIMER) { /* openvpn failed to respond to stop signal -- terminate */ TerminateOpenVPN(c); KillTimer (hwndDlg, IDT_STOP_TIMER); } break; } return FALSE; } /* * ThreadProc for OpenVPN status dialog windows */ static DWORD WINAPI ThreadOpenVPNStatus(void *p) { connection_t *c = p; TCHAR conn_name[200]; MSG msg; HANDLE wait_event; CLEAR (msg); /* Cut of extention from config filename. */ _tcsncpy(conn_name, c->config_file, _countof(conn_name)); conn_name[_tcslen(conn_name) - _tcslen(o.ext_string) - 1] = _T('\0'); c->state = (c->state == suspended ? resuming : connecting); /* Create and Show Status Dialog */ c->hwndStatus = CreateLocalizedDialogParam(ID_DLG_STATUS, StatusDialogFunc, (LPARAM) c); if (!c->hwndStatus) return 1; CheckAndSetTrayIcon(); SetMenuStatus(c, connecting); SetDlgItemText(c->hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_CONNECTING)); SetWindowText(c->hwndStatus, LoadLocalizedString(IDS_NFO_CONNECTION_XXX, conn_name)); if (!OpenManagement(c)) PostMessage(c->hwndStatus, WM_CLOSE, 0, 0); /* Start the async read loop for service and set it as the wait event */ if (c->iserv.hEvent) { HandleServiceIO (0, 0, (LPOVERLAPPED) &c->iserv); wait_event = c->iserv.hEvent; } else wait_event = c->hProcess; if (o.silent_connection == 0) ShowWindow(c->hwndStatus, SW_SHOW); /* Run the message loop for the status window */ while (WM_QUIT != msg.message) { DWORD res; if (!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if ((res = MsgWaitForMultipleObjectsEx (1, &wait_event, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE)) == WAIT_OBJECT_0) { if (wait_event == c->hProcess) OnProcess (c, NULL); else if (wait_event == c->iserv.hEvent) OnService (c, NULL); } continue; } if (IsDialogMessage(c->hwndStatus, &msg) == 0) { TranslateMessage(&msg); DispatchMessage(&msg); } } /* release handles etc.*/ Cleanup (c); c->hwndStatus = NULL; return 0; }
/* * DialogProc for OpenVPN private key password dialog windows */ INT_PTR CALLBACK PrivKeyPassDialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { connection_t *c; WCHAR passphrase[KEY_PASS_LEN]; switch (msg) { case WM_INITDIALOG: /* Set connection for this dialog and show it */ c = (connection_t *) lParam; SetProp(hwndDlg, cfgProp, (HANDLE) c); if (RecallKeyPass(c->config_name, passphrase) && wcslen(passphrase)) { /* Use the saved password and skip the dialog */ SetDlgItemTextW(hwndDlg, ID_EDT_PASSPHRASE, passphrase); SecureZeroMemory(passphrase, sizeof(passphrase)); ManagementCommandFromInput(c, "password \"Private Key\" \"%s\"", hwndDlg, ID_EDT_PASSPHRASE); EndDialog(hwndDlg, IDOK); return TRUE; } if (c->flags & FLAG_SAVE_KEY_PASS) Button_SetCheck (GetDlgItem (hwndDlg, ID_CHK_SAVE_PASS), BST_CHECKED); if (c->state == resuming) ForceForegroundWindow(hwndDlg); else SetForegroundWindow(hwndDlg); break; case WM_COMMAND: c = (connection_t *) GetProp(hwndDlg, cfgProp); switch (LOWORD(wParam)) { case ID_CHK_SAVE_PASS: c->flags ^= FLAG_SAVE_KEY_PASS; if (c->flags & FLAG_SAVE_KEY_PASS) Button_SetCheck (GetDlgItem (hwndDlg, ID_CHK_SAVE_PASS), BST_CHECKED); else { Button_SetCheck (GetDlgItem (hwndDlg, ID_CHK_SAVE_PASS), BST_UNCHECKED); DeleteSavedKeyPass(c->config_name); } break; case IDOK: if ((c->flags & FLAG_SAVE_KEY_PASS) && GetDlgItemTextW(hwndDlg, ID_EDT_PASSPHRASE, passphrase, _countof(passphrase)) && wcslen(passphrase) > 0) { SaveKeyPass(c->config_name, passphrase); SecureZeroMemory(passphrase, sizeof(passphrase)); } ManagementCommandFromInput(c, "password \"Private Key\" \"%s\"", hwndDlg, ID_EDT_PASSPHRASE); EndDialog(hwndDlg, LOWORD(wParam)); return TRUE; case IDCANCEL: EndDialog(hwndDlg, LOWORD(wParam)); StopOpenVPN (c); return TRUE; } break; case WM_CLOSE: EndDialog(hwndDlg, LOWORD(wParam)); return TRUE; case WM_NCDESTROY: RemoveProp(hwndDlg, cfgProp); break; } return FALSE; }
/* * DialogProc for OpenVPN username/password/challenge auth dialog windows */ INT_PTR CALLBACK UserAuthDialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { auth_param_t *param; WCHAR username[USER_PASS_LEN]; WCHAR password[USER_PASS_LEN]; switch (msg) { case WM_INITDIALOG: /* Set connection for this dialog and show it */ param = (auth_param_t *) lParam; SetProp(hwndDlg, cfgProp, (HANDLE) param); if (param->challenge_str) { int wchars_num = MultiByteToWideChar(CP_UTF8, 0, param->challenge_str, -1, NULL, 0); LPWSTR wstr = (LPWSTR)malloc(sizeof(WCHAR) * wchars_num); HWND wnd_challenge = GetDlgItem(hwndDlg, ID_EDT_AUTH_CHALLENGE); MultiByteToWideChar(CP_UTF8, 0, param->challenge_str, -1, wstr, wchars_num); SetDlgItemTextW(hwndDlg, ID_TXT_AUTH_CHALLENGE, wstr); free(wstr); /* Set/Remove style ES_PASSWORD by SetWindowLong(GWL_STYLE) does nothing, send EM_SETPASSWORDCHAR just works. */ if(param->challenge_echo) SendMessage(wnd_challenge, EM_SETPASSWORDCHAR, 0, 0); } if (RecallUsername(param->c->config_name, username)) SetDlgItemTextW(hwndDlg, ID_EDT_AUTH_USER, username); if (RecallAuthPass(param->c->config_name, password)) { SetDlgItemTextW(hwndDlg, ID_EDT_AUTH_PASS, password); SecureZeroMemory(password, sizeof(password)); } if (param->c->flags & FLAG_SAVE_AUTH_PASS) Button_SetCheck(GetDlgItem (hwndDlg, ID_CHK_SAVE_PASS), BST_CHECKED); if (param->c->state == resuming) ForceForegroundWindow(hwndDlg); else SetForegroundWindow(hwndDlg); break; case WM_COMMAND: param = (auth_param_t *) GetProp(hwndDlg, cfgProp); switch (LOWORD(wParam)) { case ID_EDT_AUTH_USER: if (HIWORD(wParam) == EN_UPDATE) { int len = Edit_GetTextLength((HWND) lParam); EnableWindow(GetDlgItem(hwndDlg, IDOK), (len ? TRUE : FALSE)); } break; case ID_CHK_SAVE_PASS: param->c->flags ^= FLAG_SAVE_AUTH_PASS; if (param->c->flags & FLAG_SAVE_AUTH_PASS) Button_SetCheck(GetDlgItem (hwndDlg, ID_CHK_SAVE_PASS), BST_CHECKED); else { DeleteSavedAuthPass(param->c->config_name); Button_SetCheck(GetDlgItem (hwndDlg, ID_CHK_SAVE_PASS), BST_UNCHECKED); } break; case IDOK: if (GetDlgItemTextW(hwndDlg, ID_EDT_AUTH_USER, username, _countof(username))) { SaveUsername(param->c->config_name, username); } if ( param->c->flags & FLAG_SAVE_AUTH_PASS && GetDlgItemTextW(hwndDlg, ID_EDT_AUTH_PASS, password, _countof(password)) && wcslen(password) ) { SaveAuthPass(param->c->config_name, password); SecureZeroMemory(password, sizeof(password)); } ManagementCommandFromInput(param->c, "username \"Auth\" \"%s\"", hwndDlg, ID_EDT_AUTH_USER); if (param->challenge_str) ManagementCommandFromInputBase64(param->c, "password \"Auth\" \"SCRV1:%s:%s\"", hwndDlg, ID_EDT_AUTH_PASS, ID_EDT_AUTH_CHALLENGE); else ManagementCommandFromInput(param->c, "password \"Auth\" \"%s\"", hwndDlg, ID_EDT_AUTH_PASS); EndDialog(hwndDlg, LOWORD(wParam)); return TRUE; case IDCANCEL: EndDialog(hwndDlg, LOWORD(wParam)); StopOpenVPN(param->c); return TRUE; } break; case WM_CLOSE: EndDialog(hwndDlg, LOWORD(wParam)); return TRUE; case WM_NCDESTROY: param = (auth_param_t *) GetProp(hwndDlg, cfgProp); if (param->challenge_str) free(param->challenge_str); free(param); RemoveProp(hwndDlg, cfgProp); break; } return FALSE; }
/* * DialogProc for OpenVPN status dialog windows */ INT_PTR CALLBACK StatusDialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { connection_t *c; switch (msg) { case WM_MANAGEMENT: /* Management interface related event */ OnManagement(wParam, lParam); return TRUE; case WM_INITDIALOG: c = (connection_t *) lParam; /* Set window icon "disconnected" */ SetStatusWinIcon(hwndDlg, ID_ICO_CONNECTING); /* Set connection for this dialog */ SetProp(hwndDlg, cfgProp, (HANDLE) c); /* Create log window */ HWND hLogWnd = CreateWindowEx(0, RICHEDIT_CLASS, NULL, WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|ES_SUNKEN|ES_LEFT| ES_MULTILINE|ES_READONLY|ES_AUTOHSCROLL|ES_AUTOVSCROLL, 20, 25, 350, 160, hwndDlg, (HMENU) ID_EDT_LOG, o.hInstance, NULL); if (!hLogWnd) { ShowLocalizedMsg(IDS_ERR_CREATE_EDIT_LOGWINDOW); return FALSE; } /* Set font and fontsize of the log window */ CHARFORMAT cfm = { .cbSize = sizeof(CHARFORMAT), .dwMask = CFM_SIZE|CFM_FACE|CFM_BOLD, .szFaceName = _T("Microsoft Sans Serif"), .dwEffects = 0, .yHeight = 160 }; if (SendMessage(hLogWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM) &cfm) == 0) ShowLocalizedMsg(IDS_ERR_SET_SIZE); /* Set size and position of controls */ RECT rect; GetClientRect(hwndDlg, &rect); MoveWindow(hLogWnd, 20, 25, rect.right - 40, rect.bottom - 70, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_TXT_STATUS), 20, 5, rect.right - 25, 15, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_DISCONNECT), 20, rect.bottom - 30, 110, 23, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_RESTART), 145, rect.bottom - 30, 110, 23, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_HIDE), rect.right - 130, rect.bottom - 30, 110, 23, TRUE); /* Set focus on the LogWindow so it scrolls automatically */ SetFocus(hLogWnd); return FALSE; case WM_SIZE: MoveWindow(GetDlgItem(hwndDlg, ID_EDT_LOG), 20, 25, LOWORD(lParam) - 40, HIWORD(lParam) - 70, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_DISCONNECT), 20, HIWORD(lParam) - 30, 110, 23, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_RESTART), 145, HIWORD(lParam) - 30, 110, 23, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_HIDE), LOWORD(lParam) - 130, HIWORD(lParam) - 30, 110, 23, TRUE); MoveWindow(GetDlgItem(hwndDlg, ID_TXT_STATUS), 20, 5, LOWORD(lParam) - 25, 15, TRUE); InvalidateRect(hwndDlg, NULL, TRUE); return TRUE; case WM_COMMAND: c = (connection_t *) GetProp(hwndDlg, cfgProp); switch (LOWORD(wParam)) { case ID_DISCONNECT: SetFocus(GetDlgItem(c->hwndStatus, ID_EDT_LOG)); StopOpenVPN(c); return TRUE; case ID_HIDE: if (c->state != disconnected) ShowWindow(hwndDlg, SW_HIDE); else DestroyWindow(hwndDlg); return TRUE; case ID_RESTART: c->state = reconnecting; SetFocus(GetDlgItem(c->hwndStatus, ID_EDT_LOG)); ManagementCommand(c, "signal SIGHUP", NULL, regular); return TRUE; } break; case WM_SHOWWINDOW: if (wParam == TRUE) { c = (connection_t *) GetProp(hwndDlg, cfgProp); if (c->hwndStatus) SetFocus(GetDlgItem(c->hwndStatus, ID_EDT_LOG)); } return FALSE; case WM_CLOSE: c = (connection_t *) GetProp(hwndDlg, cfgProp); if (c->state != disconnected) ShowWindow(hwndDlg, SW_HIDE); else DestroyWindow(hwndDlg); return TRUE; case WM_NCDESTROY: RemoveProp(hwndDlg, cfgProp); break; case WM_DESTROY: PostQuitMessage(0); break; } return FALSE; } /* * ThreadProc for OpenVPN status dialog windows */ static DWORD WINAPI ThreadOpenVPNStatus(void *p) { connection_t *c = p; TCHAR conn_name[200]; MSG msg; /* Cut of extention from config filename. */ _tcsncpy(conn_name, c->config_file, _countof(conn_name)); conn_name[_tcslen(conn_name) - _tcslen(o.ext_string) - 1] = _T('\0'); c->state = (c->state == suspended ? resuming : connecting); /* Create and Show Status Dialog */ c->hwndStatus = CreateLocalizedDialogParam(ID_DLG_STATUS, StatusDialogFunc, (LPARAM) c); if (!c->hwndStatus) return 1; CheckAndSetTrayIcon(); SetMenuStatus(c, connecting); SetDlgItemText(c->hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_CONNECTING)); SetWindowText(c->hwndStatus, LoadLocalizedString(IDS_NFO_CONNECTION_XXX, conn_name)); if (!OpenManagement(c)) PostMessage(c->hwndStatus, WM_CLOSE, 0, 0); if (o.silent_connection[0] == '0') ShowWindow(c->hwndStatus, SW_SHOW); /* Run the message loop for the status window */ while (GetMessage(&msg, NULL, 0, 0)) { if (msg.hwnd == NULL) { switch (msg.message) { case WM_OVPN_STOP: c->state = disconnecting; RunDisconnectScript(c, false); EnableWindow(GetDlgItem(c->hwndStatus, ID_DISCONNECT), FALSE); EnableWindow(GetDlgItem(c->hwndStatus, ID_RESTART), FALSE); SetMenuStatus(c, disconnecting); SetDlgItemText(c->hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_WAIT_TERM)); SetEvent(c->exit_event); break; case WM_OVPN_SUSPEND: c->state = suspending; EnableWindow(GetDlgItem(c->hwndStatus, ID_DISCONNECT), FALSE); EnableWindow(GetDlgItem(c->hwndStatus, ID_RESTART), FALSE); SetMenuStatus(&o.conn[config], disconnecting); SetDlgItemText(c->hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_WAIT_TERM)); SetEvent(c->exit_event); break; } } else if (IsDialogMessage(c->hwndStatus, &msg) == 0) { TranslateMessage(&msg); DispatchMessage(&msg); } } return 0; }
/* This function is called by the Windows function DispatchMessage() */ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static UINT s_uTaskbarRestart; int i; switch (message) { case WM_CREATE: /* Save Window Handle */ o.hWnd = hwnd; s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated")); WTSRegisterSessionNotification(hwnd, NOTIFY_FOR_THIS_SESSION); /* Load application icon */ HICON hIcon = LoadLocalizedIcon(ID_ICO_APP); if (hIcon) { SendMessage(hwnd, WM_SETICON, (WPARAM) (ICON_SMALL), (LPARAM) (hIcon)); SendMessage(hwnd, WM_SETICON, (WPARAM) (ICON_BIG), (LPARAM) (hIcon)); } CreatePopupMenus(); /* Create popup menus */ ShowTrayIcon(); if (o.allow_service[0]=='1' || o.service_only[0]=='1') CheckServiceStatus(); // Check if service is running or not if (!AutoStartConnections()) { SendMessage(hwnd, WM_CLOSE, 0, 0); break; } break; case WM_NOTIFYICONTRAY: OnNotifyTray(lParam); // Manages message from tray break; case WM_COMMAND: if ( (LOWORD(wParam) >= IDM_CONNECTMENU) && (LOWORD(wParam) < IDM_CONNECTMENU + MAX_CONFIGS) ) { StartOpenVPN(&o.conn[LOWORD(wParam) - IDM_CONNECTMENU]); } if ( (LOWORD(wParam) >= IDM_DISCONNECTMENU) && (LOWORD(wParam) < IDM_DISCONNECTMENU + MAX_CONFIGS) ) { StopOpenVPN(&o.conn[LOWORD(wParam) - IDM_DISCONNECTMENU]); } if ( (LOWORD(wParam) >= IDM_STATUSMENU) && (LOWORD(wParam) < IDM_STATUSMENU + MAX_CONFIGS) ) { ShowWindow(o.conn[LOWORD(wParam) - IDM_STATUSMENU].hwndStatus, SW_SHOW); } if ( (LOWORD(wParam) >= IDM_VIEWLOGMENU) && (LOWORD(wParam) < IDM_VIEWLOGMENU + MAX_CONFIGS) ) { ViewLog(LOWORD(wParam) - IDM_VIEWLOGMENU); } if ( (LOWORD(wParam) >= IDM_EDITMENU) && (LOWORD(wParam) < IDM_EDITMENU + MAX_CONFIGS) ) { EditConfig(LOWORD(wParam) - IDM_EDITMENU); } #ifndef DISABLE_CHANGE_PASSWORD if ( (LOWORD(wParam) >= IDM_PASSPHRASEMENU) && (LOWORD(wParam) < IDM_PASSPHRASEMENU + MAX_CONFIGS) ) { ShowChangePassphraseDialog(&o.conn[LOWORD(wParam) - IDM_PASSPHRASEMENU]); } #endif if (LOWORD(wParam) == IDM_SETTINGS) { ShowSettingsDialog(); } if (LOWORD(wParam) == IDM_CLOSE) { CloseApplication(hwnd); } if (LOWORD(wParam) == IDM_SERVICE_START) { MyStartService(); } if (LOWORD(wParam) == IDM_SERVICE_STOP) { MyStopService(); } if (LOWORD(wParam) == IDM_SERVICE_RESTART) MyReStartService(); break; case WM_CLOSE: CloseApplication(hwnd); break; case WM_DESTROY: WTSUnRegisterSessionNotification(hwnd); StopAllOpenVPN(); OnDestroyTray(); /* Remove Tray Icon and destroy menus */ PostQuitMessage (0); /* Send a WM_QUIT to the message queue */ break; case WM_QUERYENDSESSION: return(TRUE); case WM_ENDSESSION: StopAllOpenVPN(); OnDestroyTray(); break; case WM_WTSSESSION_CHANGE: switch (wParam) { case WTS_SESSION_LOCK: o.session_locked = TRUE; break; case WTS_SESSION_UNLOCK: o.session_locked = FALSE; if (CountConnState(suspended) != 0) ResumeConnections(); break; } break; case WM_POWERBROADCAST: switch (wParam) { case PBT_APMSUSPEND: if (o.disconnect_on_suspend[0] == '1') { /* Suspend running connections */ for (i=0; i<o.num_configs; i++) { if (o.conn[i].state == connected) SuspendOpenVPN(i); } /* Wait for all connections to suspend */ for (i=0; i<10; i++, Sleep(500)) if (CountConnState(suspending) == 0) break; } return FALSE; case PBT_APMRESUMESUSPEND: case PBT_APMRESUMECRITICAL: if (CountConnState(suspended) != 0 && !o.session_locked) ResumeConnections(); return FALSE; } default: /* for messages that we don't deal with */ if (message == s_uTaskbarRestart) { /* Explorer has restarted, re-register the tray icon. */ ShowTrayIcon(); CheckAndSetTrayIcon(); break; } return DefWindowProc (hwnd, message, wParam, lParam); } return 0; }
/* This function is called by the Windows function DispatchMessage() */ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static UINT s_uTaskbarRestart; int i; switch (message) { case WM_CREATE: /* Save Window Handle */ o.hWnd = hwnd; s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated")); CreatePopupMenus(); /* Create popup menus */ LoadAppIcon(); /* Load App Icon */ ShowTrayIcon(); if (o.allow_service[0]=='1' || o.service_only[0]=='1') CheckServiceStatus(); // Check if service is running or not if (!AutoStartConnections()) { SendMessage(hwnd, WM_CLOSE, 0, 0); break; } break; case WM_NOTIFYICONTRAY: OnNotifyTray(lParam); // Manages message from tray return TRUE; case WM_COMMAND: if ( (LOWORD(wParam) >= IDM_CONNECTMENU) && (LOWORD(wParam) < IDM_CONNECTMENU + MAX_CONFIGS) ) { StartOpenVPN(LOWORD(wParam) - IDM_CONNECTMENU); } if ( (LOWORD(wParam) >= IDM_DISCONNECTMENU) && (LOWORD(wParam) < IDM_DISCONNECTMENU + MAX_CONFIGS) ) { StopOpenVPN(LOWORD(wParam) - IDM_DISCONNECTMENU); } if ( (LOWORD(wParam) >= IDM_STATUSMENU) && (LOWORD(wParam) < IDM_STATUSMENU + MAX_CONFIGS) ) { ShowWindow(o.cnn[LOWORD(wParam) - IDM_STATUSMENU].hwndStatus, SW_SHOW); } if ( (LOWORD(wParam) >= IDM_VIEWLOGMENU) && (LOWORD(wParam) < IDM_VIEWLOGMENU + MAX_CONFIGS) ) { ViewLog(LOWORD(wParam) - IDM_VIEWLOGMENU); } if ( (LOWORD(wParam) >= IDM_EDITMENU) && (LOWORD(wParam) < IDM_EDITMENU + MAX_CONFIGS) ) { EditConfig(LOWORD(wParam) - IDM_EDITMENU); } #ifndef DISABLE_CHANGE_PASSWORD if ( (LOWORD(wParam) >= IDM_PASSPHRASEMENU) && (LOWORD(wParam) < IDM_PASSPHRASEMENU + MAX_CONFIGS) ) { ShowChangePassphraseDialog(LOWORD(wParam) - IDM_PASSPHRASEMENU); } #endif if (LOWORD(wParam) == IDM_PROXY) { ShowProxySettingsDialog(); } if (LOWORD(wParam) == IDM_ABOUT) { DialogBox(o.hInstance, (LPCTSTR)IDD_ABOUTDIALOG, NULL, (DLGPROC)AboutDialogFunc); } if (LOWORD(wParam) == IDM_CLOSE) { CloseApplication(hwnd); } if (LOWORD(wParam) == IDM_SERVICE_START) { MyStartService(); } if (LOWORD(wParam) == IDM_SERVICE_STOP) { MyStopService(); } if (LOWORD(wParam) == IDM_SERVICE_RESTART) MyReStartService(); break; case WM_CLOSE: CloseApplication(hwnd); break; case WM_DESTROY: StopAllOpenVPN(); OnDestroyTray(); /* Remove Tray Icon and destroy menus */ PostQuitMessage (0); /* Send a WM_QUIT to the message queue */ break; case WM_QUERYENDSESSION: return(TRUE); case WM_ENDSESSION: StopAllOpenVPN(); OnDestroyTray(); break; case WM_POWERBROADCAST: switch (wParam) { case PBT_APMSUSPEND: if (o.disconnect_on_suspend[0] == '1') { /* Suspend running connections */ for (i=0; i<o.num_configs; i++) { if (o.cnn[i].connect_status == CONNECTED) SuspendOpenVPN(i); } /* Wait for all connections to suspend */ for (i=0; i<10; i++, Sleep(500)) if (CountConnectedState(SUSPENDING) == 0) break; } return FALSE; case PBT_APMRESUMESUSPEND: case PBT_APMRESUMECRITICAL: for (i=0; i<o.num_configs; i++) { /* Restart suspend connections */ if (o.cnn[i].connect_status == SUSPENDED) StartOpenVPN(i); /* If some connection never reached SUSPENDED state */ if (o.cnn[i].connect_status == SUSPENDING) StopOpenVPN(i); } return FALSE; } default: /* for messages that we don't deal with */ if (message == s_uTaskbarRestart) { /* Explorer has restarted, re-register the tray icon. */ ShowTrayIcon(); CheckAndSetTrayIcon(); break; } return DefWindowProc (hwnd, message, wParam, lParam); } return 0; }