/* * Handle mouse clicks on tray icon */ void OnNotifyTray(LPARAM lParam) { POINT pt; switch (lParam) { case WM_RBUTTONUP: /* Recreate popup menus */ DestroyPopupMenus(); BuildFileList(); CreatePopupMenus(); GetCursorPos(&pt); SetForegroundWindow(o.hWnd); TrackPopupMenu(hMenu, TPM_RIGHTALIGN, pt.x, pt.y, 0, o.hWnd, NULL); PostMessage(o.hWnd, WM_NULL, 0, 0); break; case WM_LBUTTONDBLCLK: if (o.service_only) { /* Start or stop OpenVPN service */ if (o.service_state == service_disconnected) { MyStartService(); } else if (o.service_state == service_connected && ShowLocalizedMsgEx(MB_YESNO, _T(PACKAGE_NAME), IDS_MENU_ASK_STOP_SERVICE) == IDYES) { MyStopService(); } } else { int disconnected_conns = CountConnState(disconnected); DestroyPopupMenus(); BuildFileList(); CreatePopupMenus(); /* Start connection if only one config exist */ if (o.num_configs == 1 && o.conn[0].state == disconnected) StartOpenVPN(&o.conn[0]); else if (disconnected_conns == o.num_configs - 1) { /* Show status window if only one connection is running */ int i; for (i = 0; i < o.num_configs; i++) { if (o.conn[i].state != disconnected) { ShowWindow(o.conn[i].hwndStatus, SW_SHOW); SetForegroundWindow(o.conn[i].hwndStatus); break; } } } } break; } }
void CloseApplication(HWND hwnd) { int i; if (o.service_state == service_connected && ShowLocalizedMsgEx(MB_YESNO, _T("Exit OpenVPN"), IDS_NFO_SERVICE_ACTIVE_EXIT) == IDNO) return; for (i = 0; i < o.num_configs; i++) { if (o.conn[i].state == disconnected) continue; /* Ask for confirmation if still connected */ if (ShowLocalizedMsgEx(MB_YESNO, _T("Exit OpenVPN"), IDS_NFO_ACTIVE_CONN_EXIT) == IDNO) return; } DestroyWindow(hwnd); }
static int add_option(options_t *options, int i, TCHAR **p) { if (streq(p[0], _T("help"))) { TCHAR caption[200]; LoadLocalizedStringBuf(caption, _countof(caption), IDS_NFO_USAGECAPTION); ShowLocalizedMsgEx(MB_OK, caption, IDS_NFO_USAGE); exit(0); } else if (streq(p[0], _T("connect")) && p[1]) { ++i; static int auto_connect_nr = 0; if (auto_connect_nr == MAX_CONFIGS) { /* Too many configs */ ShowLocalizedMsg(IDS_ERR_MANY_CONFIGS, MAX_CONFIGS); exit(1); } options->auto_connect[auto_connect_nr++] = p[1]; } else if (streq(p[0], _T("exe_path")) && p[1]) { ++i; _tcsncpy(options->exe_path, p[1], _countof(options->exe_path) - 1); } else if (streq(p[0], _T("config_dir")) && p[1]) { ++i; _tcsncpy(options->config_dir, p[1], _countof(options->config_dir) - 1); } else if (streq(p[0], _T("ext_string")) && p[1]) { ++i; _tcsncpy(options->ext_string, p[1], _countof(options->ext_string) - 1); } else if (streq(p[0], _T("log_dir")) && p[1]) { ++i; _tcsncpy(options->log_dir, p[1], _countof(options->log_dir) - 1); } else if (streq(p[0], _T("priority_string")) && p[1]) { ++i; _tcsncpy(options->priority_string, p[1], _countof(options->priority_string) - 1); } else if (streq(p[0], _T("append_string")) && p[1]) { ++i; _tcsncpy(options->append_string, p[1], _countof(options->append_string) - 1); } else if (streq(p[0], _T("log_viewer")) && p[1]) { ++i; _tcsncpy(options->log_viewer, p[1], _countof(options->log_viewer) - 1); } else if (streq(p[0], _T("editor")) && p[1]) { ++i; _tcsncpy(options->editor, p[1], _countof(options->editor) - 1); } else if (streq(p[0], _T("allow_edit")) && p[1]) { ++i; _tcsncpy(options->allow_edit, p[1], _countof(options->allow_edit) - 1); } else if (streq(p[0], _T("allow_service")) && p[1]) { ++i; _tcsncpy(options->allow_service, p[1], _countof(options->allow_service) - 1); } else if (streq(p[0], _T("allow_password")) && p[1]) { ++i; _tcsncpy(options->allow_password, p[1], _countof(options->allow_password) - 1); } else if (streq(p[0], _T("allow_proxy")) && p[1]) { ++i; _tcsncpy(options->allow_proxy, p[1], _countof(options->allow_proxy) - 1); } else if (streq(p[0], _T("show_balloon")) && p[1]) { ++i; _tcsncpy(options->show_balloon, p[1], _countof(options->show_balloon) - 1); } else if (streq(p[0], _T("service_only")) && p[1]) { ++i; _tcsncpy(options->service_only, p[1], _countof(options->service_only) - 1); } else if (streq(p[0], _T("show_script_window")) && p[1]) { ++i; _tcsncpy(options->show_script_window, p[1], _countof(options->show_script_window) - 1); } else if (streq(p[0], _T("silent_connection")) && p[1]) { ++i; _tcsncpy(options->silent_connection, p[1], _countof(options->silent_connection) - 1); } else if (streq(p[0], _T("passphrase_attempts")) && p[1]) { ++i; _tcsncpy(options->psw_attempts_string, p[1], _countof(options->psw_attempts_string) - 1); } else if (streq(p[0], _T("connectscript_timeout")) && p[1]) { ++i; _tcsncpy(options->connectscript_timeout_string, p[1], _countof(options->connectscript_timeout_string) - 1); } else if (streq(p[0], _T("disconnectscript_timeout")) && p[1]) { ++i; _tcsncpy(options->disconnectscript_timeout_string, p[1], _countof(options->disconnectscript_timeout_string) - 1); } else if (streq(p[0], _T("preconnectscript_timeout")) && p[1]) { ++i; _tcsncpy(options->preconnectscript_timeout_string, p[1], _countof(options->preconnectscript_timeout_string) - 1); } else { /* Unrecognized option or missing parameter */ ShowLocalizedMsg(IDS_ERR_BAD_OPTION, p[0]); exit(1); } return i; }
INT_PTR CALLBACK ChangePassphraseDialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, UNUSED LPARAM lParam) { HICON hIcon; TCHAR keyfile[MAX_PATH]; int keyfile_format; BOOL Translated; switch (msg) { case WM_INITDIALOG: hIcon = LoadLocalizedIcon(ID_ICO_APP); if (hIcon) { SendMessage(hwndDlg, WM_SETICON, (WPARAM) (ICON_SMALL), (LPARAM) (hIcon)); SendMessage(hwndDlg, WM_SETICON, (WPARAM) (ICON_BIG), (LPARAM) (hIcon)); } return FALSE; case WM_COMMAND: switch (LOWORD(wParam)) { case IDOK: /* Check if the type new passwords match. */ if (!ConfirmNewPassword (hwndDlg)) { /* passwords don't match */ ShowLocalizedMsg(IDS_ERR_PWD_DONT_MATCH); break; } /* Check minimum length of password */ if (NewPasswordLengh(hwndDlg) < MIN_PASSWORD_LEN) { ShowLocalizedMsg(IDS_ERR_PWD_TO_SHORT, MIN_PASSWORD_LEN); break; } /* Confirm if the new password is empty. */ if (NewPasswordLengh(hwndDlg) == 0 && ShowLocalizedMsgEx(MB_YESNO, _T(PACKAGE_NAME), IDS_NFO_EMPTY_PWD) == IDNO) break; GetDlgItemText(hwndDlg, ID_TXT_KEYFILE, keyfile, _countof(keyfile) - 1); keyfile_format=GetDlgItemInt(hwndDlg, ID_TXT_KEYFORMAT, &Translated, FALSE); if (keyfile_format == KEYFILE_FORMAT_PEM) { /* Change password of a PEM file */ if (ChangePasswordPEM(hwndDlg) == -1) /* Wrong password */ break; } else if (keyfile_format == KEYFILE_FORMAT_PKCS12) { /* Change password of a .P12 file */ if (ChangePasswordPKCS12(hwndDlg) == -1) /* Wrong password */ break; } else { /* Unknown key format */ ShowLocalizedMsg(IDS_ERR_UNKNOWN_KEYFILE_FORMAT); } DestroyWindow(hwndDlg); break; case IDCANCEL: DestroyWindow(hwndDlg); break; } break; case WM_DESTROY: PostQuitMessage(0); break; case WM_CLOSE: DestroyWindow(hwndDlg); return FALSE; } return FALSE; }
BOOL CheckVersion() { HANDLE hStdOutRead; HANDLE hStdOutWrite; 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(si); CLEAR(pi); /* Make handles inheritable and accessible by all */ SECURITY_DESCRIPTOR sd; SECURITY_ATTRIBUTES sa = { .nLength = sizeof(sa), .lpSecurityDescriptor = &sd, .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 pipe for STDOUT with inheritable write end */ if (!CreatePipe(&hStdOutRead, &hStdOutWrite, &sa, 0)) { ShowLocalizedMsg(IDS_ERR_CREATE_PIPE_IN_READ); return FALSE; } if (!SetHandleInformation(hStdOutRead, HANDLE_FLAG_INHERIT, 0)) { ShowLocalizedMsg(IDS_ERR_DUP_HANDLE_IN_WRITE); goto out; } /* Construct the process' working directory */ _tcsncpy(pwd, o.exe_path, _countof(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 = GetStdHandle(STD_INPUT_HANDLE); si.hStdOutput = hStdOutWrite; si.hStdError = hStdOutWrite; /* 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(hStdOutRead, line, sizeof(line))) { #ifdef DEBUG PrintDebug(_T("VersionString: %S"), line); #endif CloseHandle(pi.hThread); CloseHandle(pi.hProcess); /* OpenVPN version 2.x */ char *p = strstr(line, match_version); if (p) { retval = TRUE; p = strtok(p+8, " "); strncpy(o.ovpn_version, p, _countof(o.ovpn_version)-1); o.ovpn_version[_countof(o.ovpn_version)-1] = '\0'; } } out: CloseHandle(hStdOutRead); CloseHandle(hStdOutWrite); return retval; } void DisablePasswordSave(connection_t *c) { if (ShowLocalizedMsgEx(MB_OKCANCEL, TEXT(PACKAGE_NAME), IDS_NFO_DELETE_PASS, c->config_name) == IDCANCEL) return; DeleteSavedPasswords(c->config_name); c->flags &= ~(FLAG_SAVE_KEY_PASS | FLAG_SAVE_AUTH_PASS); SetMenuStatus(c, c->state); }