// Generate the Unicode string cache file name void GenerateUnicodeCacheFileName(wchar_t *name, UINT size, wchar_t *strfilename, UINT strfilesize, UCHAR *filehash) { wchar_t tmp[MAX_SIZE]; wchar_t hashstr[64]; wchar_t hashtemp[MAX_SIZE]; wchar_t exe[MAX_SIZE]; UCHAR hash[SHA1_SIZE]; // Validate arguments if (name == NULL || strfilename == NULL || filehash == NULL) { return; } GetExeDirW(exe, sizeof(exe)); UniStrCpy(hashtemp, sizeof(hashtemp), strfilename); BinToStrW(tmp, sizeof(tmp), filehash, MD5_SIZE); UniStrCat(hashtemp, sizeof(hashtemp), tmp); UniStrCat(hashtemp, sizeof(hashtemp), exe); UniStrLower(hashtemp); Hash(hash, hashtemp, UniStrLen(hashtemp) * sizeof(wchar_t), true); BinToStrW(hashstr, sizeof(hashstr), hash, 4); UniFormat(tmp, sizeof(tmp), UNICODE_CACHE_FILE, hashstr); UniStrLower(tmp); #ifndef OS_WIN32 UniStrCpy(exe, sizeof(exe), L"/tmp"); #else // OS_WIN32 StrToUni(exe, sizeof(exe), MsGetTempDir()); #endif // OS_WIN32 UniFormat(name, size, L"%s/%s", exe, tmp); NormalizePathW(name, size, name); }
// Copy a sys file bool SuCopySysFile(wchar_t *src, wchar_t *dst) { wchar_t dst_rename[MAX_PATH]; UINT i; if (src == NULL || dst == NULL) { return false; } if (FileCopyW(src, dst)) { for (i = 1;i <= 100;i++) { UniFormat(dst_rename, sizeof(dst_rename), L"%s.old%u", dst, i); FileDeleteW(dst_rename); } return true; } for (i = 1;;i++) { UniFormat(dst_rename, sizeof(dst_rename), L"%s.old%u", dst, i); if (IsFileExistsW(dst_rename) == false) { break; } if (i >= 100) { return false; } } if (MoveFileW(dst, dst_rename) == false) { return false; } if (FileCopyW(src, dst)) { for (i = 1;i <= 100;i++) { UniFormat(dst_rename, sizeof(dst_rename), L"%s.old%u", dst, i); FileDeleteW(dst_rename); } return true; } MoveFileW(dst_rename, dst); return false; }
// Create a backup of the configuration file void BackupCfgWEx(CFG_RW *rw, FOLDER *f, wchar_t *original, UINT revision_number) { wchar_t dirname[MAX_PATH]; wchar_t filename[MAX_PATH]; wchar_t fullpath[MAX_PATH]; wchar_t datestr[MAX_PATH]; SYSTEMTIME st; // Validate arguments if (f == NULL || filename == NULL || rw == NULL) { return; } // Determine the directory name UniFormat(dirname, sizeof(dirname), L"@backup.%s", original[0] == L'@' ? original + 1 : original); // Determine the file name LocalTime(&st); UniFormat(datestr, sizeof(datestr), L"%04u%02u%02u%02u_%s", st.wYear, st.wMonth, st.wDay, st.wHour, original[0] == L'@' ? original + 1 : original); if (revision_number == INFINITE) { UniStrCpy(filename, sizeof(filename), datestr); } else { UniFormat(filename, sizeof(filename), L"%08u_%s", revision_number, original[0] == L'@' ? original + 1 : original); } // Don't save if the date and time has not been changed if (UniStrCmpi(datestr, rw->LastSavedDateStr) == 0) { return; } UniStrCpy(rw->LastSavedDateStr, sizeof(rw->LastSavedDateStr), datestr); // Check the existence of file name if (IsFileExistsW(filename)) { return; } // Create the directory MakeDirW(dirname); // Save the file UniFormat(fullpath, sizeof(fullpath), L"%s/%s", dirname, filename); CfgSaveW(f, fullpath); }
// Update void NmMainDlgRefresh(HWND hWnd, RPC *r) { #if 0 RPC_NAT_STATUS t; wchar_t tmp[MAX_SIZE]; wchar_t tmp2[MAX_SIZE]; // Validate arguments if (r == NULL || hWnd == NULL) { return; } Zero(&t, sizeof(RPC_NAT_STATUS)); CALL(hWnd, NcGetStatus(r, &t)); if (t.Online == false) { UniStrCpy(tmp, sizeof(tmp), _UU("NM_OFFLINE")); Enable(hWnd, B_CONNECT); Disable(hWnd, B_DISCONNECT); } else { if (t.Connected) { UniFormat(tmp, sizeof(tmp), _UU("NM_CONNECTED"), t.Status.ServerName); } else { if (t.LastError == ERR_NO_ERROR) { UniStrCpy(tmp, sizeof(tmp), _UU("NM_CONNECTING")); } else { UniFormat(tmp, sizeof(tmp), _UU("NM_CONNECT_ERROR"), t.LastError, _E(t.LastError)); } } Disable(hWnd, B_CONNECT); Enable(hWnd, B_DISCONNECT); } UniFormat(tmp2, sizeof(tmp2), _UU("NM_STATUS_TAG"), tmp); SetText(hWnd, S_STATUS, tmp2); FreeRpcNatStatus(&t); #endif }
// Show the status of the router bool NmStatus(HWND hWnd, SM_SERVER *s, void *param) { LVB *b; RPC_NAT_STATUS t; wchar_t tmp[MAX_SIZE]; SM_HUB *h = (SM_HUB *)param; // Validate arguments if (hWnd == NULL || s == NULL) { return false; } Zero(&t, sizeof(t)); StrCpy(t.HubName, sizeof(t.HubName), h->HubName); if (CALL(hWnd, ScGetSecureNATStatus(s->Rpc, &t)) == false) { return false; } b = LvInsertStart(); StrToUni(tmp, sizeof(tmp), h->HubName); LvInsertAdd(b, ICO_HUB, NULL, 2, _UU("SM_HUB_COLUMN_1"), tmp); UniFormat(tmp, sizeof(tmp), _UU("SM_SNAT_NUM_SESSION"), t.NumTcpSessions); LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("NM_STATUS_TCP"), tmp); UniFormat(tmp, sizeof(tmp), _UU("SM_SNAT_NUM_SESSION"), t.NumUdpSessions); LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("NM_STATUS_UDP"), tmp); UniFormat(tmp, sizeof(tmp), _UU("SM_SNAT_NUM_SESSION"), t.NumIcmpSessions); LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("NM_STATUS_ICMP"), tmp); UniFormat(tmp, sizeof(tmp), _UU("SM_SNAT_NUM_SESSION"), t.NumDnsSessions); LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("NM_STATUS_DNS"), tmp); UniFormat(tmp, sizeof(tmp), _UU("SM_SNAT_NUM_CLIENT"), t.NumDhcpClients); LvInsertAdd(b, ICO_PROTOCOL_DHCP, NULL, 2, _UU("NM_STATUS_DHCP"), tmp); LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_SNAT_IS_KERNEL"), t.IsKernelMode ? _UU("SEC_YES") : _UU("SEC_NO")); LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_SNAT_IS_RAW"), t.IsRawIpMode ? _UU("SEC_YES") : _UU("SEC_NO")); LvInsertEnd(b, hWnd, L_STATUS); FreeRpcNatStatus(&t); return true; }
// Evaluation whether the specified file exists bool CmdEvalIsFile(CONSOLE *c, wchar_t *str, void *param) { wchar_t tmp[MAX_PATH]; // Validate arguments if (c == NULL || str == NULL) { return false; } UniStrCpy(tmp, sizeof(tmp), str); if (IsEmptyUniStr(tmp)) { c->Write(c, _UU("CMD_FILE_NAME_EMPTY")); return false; } if (IsFileExistsW(tmp) == false) { wchar_t tmp2[MAX_SIZE]; UniFormat(tmp2, sizeof(tmp2), _UU("CMD_FILE_NOT_FOUND"), tmp); c->Write(c, tmp2); return false; } return true; }
// Write to the lang.config file bool SaveLangConfig(wchar_t *filename, char *str) { BUF *b; LIST *o; UINT i; bool ret; // Validate arguments if (filename == NULL) { return false; } // Read the template b = ReadDump(LANG_CONFIG_TEMPLETE); if (b == NULL) { return false; } SeekBuf(b, b->Size, 0); o = LoadLangList(); if (o != NULL) { wchar_t tmp[MAX_SIZE]; AppendBufStr(b, "# Available Language IDs are:\r\n"); for (i = 0;i < LIST_NUM(o);i++) { LANGLIST *e = LIST_DATA(o, i); UniFormat(tmp, sizeof(tmp), L"# %S: %s (%s)\r\n", e->Name, e->TitleEnglish, e->TitleLocal); AppendBufUtf8(b, tmp); } AppendBufStr(b, "\r\n\r\n# Specify a Language ID here.\r\n"); AppendBufStr(b, str); AppendBufStr(b, "\r\n\r\n"); FreeLangList(o); } ret = DumpBufW(b, filename); FreeBuf(b); return ret; }
// Get description string for range of the policy value void GetPolicyValueRangeStr(wchar_t *str, UINT size, UINT id) { POLICY_ITEM *p; // Validate arguments if (str == NULL) { return; } p = GetPolicyItem(id); if (p->TypeInt == false) { // bool type UniStrCpy(str, size, _UU("CMD_PolicyList_Range_Bool")); } else { wchar_t *tag; wchar_t tmp1[256], tmp2[256]; // int type if (p->AllowZero) { tag = _UU("CMD_PolicyList_Range_Int_2"); } else { tag = _UU("CMD_PolicyList_Range_Int_1"); } UniFormat(tmp1, sizeof(tmp1), _UU(p->FormatStr), p->MinValue); UniFormat(tmp2, sizeof(tmp2), _UU(p->FormatStr), p->MaxValue); UniFormat(str, size, tag, tmp1, tmp2); } }
// Operation check of string library bool CheckStringLibrary() { wchar_t *compare_str = L"TEST_TEST_123_123456789012345"; char *teststr = "TEST"; wchar_t *testunistr = L"TEST"; wchar_t tmp[64]; UINT i1 = 123; UINT64 i2 = 123456789012345ULL; UniFormat(tmp, sizeof(tmp), L"%S_%s_%u_%I64u", teststr, testunistr, i1, i2); if (UniStrCmpi(tmp, compare_str) != 0) { return false; } return true; }
// Evaluation function for minimum / maximum value of the parameter bool CmdEvalMinMax(CONSOLE *c, wchar_t *str, void *param) { CMD_EVAL_MIN_MAX *e; wchar_t *tag; UINT v; // Validate arguments if (param == NULL) { return false; } e = (CMD_EVAL_MIN_MAX *)param; if (e->StrName == NULL) { tag = _UU("CMD_EVAL_MIN_MAX"); } else { tag = _UU(e->StrName); } v = UniToInt(str); if (v >= e->MinValue && v <= e->MaxValue) { return true; } else { wchar_t tmp[MAX_SIZE]; UniFormat(tmp, sizeof(tmp), tag, e->MinValue, e->MaxValue); c->Write(c, tmp); return false; } }
// Format policy value void FormatPolicyValue(wchar_t *str, UINT size, UINT id, UINT value) { POLICY_ITEM *p; // Validate arguments if (str == NULL) { return; } p = GetPolicyItem(id); if (p->TypeInt == false) { // bool type if (value == 0) { UniStrCpy(str, size, L"No"); } else { UniStrCpy(str, size, L"Yes"); } } else { // int type if (value == 0 && p->AllowZero) { UniStrCpy(str, size, _UU("CMD_NO_SETTINGS")); } else { UniFormat(str, size, _UU(p->FormatStr), value); } } }
bool CfgSaveExW3(CFG_RW *rw, FOLDER *f, wchar_t *name, UINT *written_size, bool write_binary) { wchar_t tmp[MAX_SIZE]; bool text = !write_binary; UCHAR hash[SHA1_SIZE]; BUF *b; IO *o; bool ret = true; UINT dummy_int = 0; // Validate arguments if (name == NULL || f == NULL) { return false; } if (written_size == NULL) { written_size = &dummy_int; } // Convert to buffer b = CfgFolderToBuf(f, text); if (b == NULL) { return false; } // Hash the contents Hash(hash, b->Buf, b->Size, true); // Compare the contents to be written with the content which was written last if (rw != NULL) { if (Cmp(hash, rw->LashHash, SHA1_SIZE) == 0) { // Contents are not changed ret = false; } else { Copy(rw->LashHash, hash, SHA1_SIZE); } } if (ret || OS_IS_UNIX(GetOsInfo()->OsType)) { // Generate a temporary file name UniFormat(tmp, sizeof(tmp), L"%s.log", name); // Copy the file that currently exist to a temporary file FileCopyW(name, tmp); // Save the new file o = FileCreateW(name); if (o != NULL) { if (FileWrite(o, b->Buf, b->Size) == false) { // File saving failure FileClose(o); FileDeleteW(name); FileRenameW(tmp, name); if (rw != NULL) { Zero(rw->LashHash, sizeof(rw->LashHash)); } } else { // Successful saving file FileClose(o); // Delete the temporary file FileDeleteW(tmp); } } else { // File saving failure FileRenameW(tmp, name); if (rw != NULL) { Zero(rw->LashHash, sizeof(rw->LashHash)); } } } *written_size = b->Size; // Release memory FreeBuf(b); return ret; }
// main function int main(int argc, char *argv[]) { wchar_t *s; UINT ret = 0; InitProcessCallOnce(); #ifdef OS_WIN32 SetConsoleTitleA(CEDAR_PRODUCT_STR " VPN Command Line Utility"); #else // For *nix, disable output buffering to allow for interactive use setbuf(stdout,NULL); #endif // OS_WIN32 #if defined(_DEBUG) || defined(DEBUG) // In VC++ compilers, the macro is "_DEBUG", not "DEBUG". // If set memcheck = true, the program will be vitally slow since it will log all malloc() / realloc() / free() calls to find the cause of memory leak. // For normal debug we set memcheck = false. // Please set memcheck = true if you want to test the cause of memory leaks. InitMayaqua(false, true, argc, argv); #else InitMayaqua(false, false, argc, argv); #endif InitCedar(); s = GetCommandLineUniStr(); if (s == NULL) { s = CopyUniStr(L""); } if (UniStrCmpi(s, L"exit") != 0) { UINT size = UniStrSize(s) + 64; wchar_t *tmp; tmp = Malloc(size); UniFormat(tmp, size, L"vpncmd %s", s); ret = CommandMain(tmp); Free(tmp); } #ifdef OS_WIN32 { UINT i; LIST *o = MsGetProcessList(); bool b = false; for (i = 0;i < LIST_NUM(o);i++) { MS_PROCESS *p = LIST_DATA(o, i); if (EndWith(p->ExeFilename, "\\cmd.exe") || EndWith(p->ExeFilename, "\\command.com")) { b = true; break; } } MsFreeProcessList(o); if (b == false) { if (ret != ERR_NO_ERROR) { SleepThread(1000); } } } #endif // OS_WIN32 Free(s); FreeCedar(); FreeMayaqua(); return ret; }
// License dialog update void EmLicenseDlgRefresh(HWND hWnd, RPC *s) { RPC_ENUM_LICENSE_KEY t; RPC_EL_LICENSE_STATUS st; UINT i; wchar_t tmp[MAX_SIZE]; LVB *b; // Validate arguments if (hWnd == NULL || s == NULL) { return; } Zero(&t, sizeof(t)); if (CALL(hWnd, EcEnumLicenseKey(s, &t)) == false) { Close(hWnd); return; } b = LvInsertStart(); for (i = 0;i < t.NumItem;i++) { wchar_t tmp1[32], tmp2[LICENSE_KEYSTR_LEN + 1], tmp3[LICENSE_MAX_PRODUCT_NAME_LEN + 1], *tmp4, tmp5[128], tmp6[LICENSE_LICENSEID_STR_LEN + 1], tmp7[64], tmp8[64], tmp9[64]; RPC_ENUM_LICENSE_KEY_ITEM *e = &t.Items[i]; UniToStru(tmp1, e->Id); StrToUni(tmp2, sizeof(tmp2), e->LicenseKey); StrToUni(tmp3, sizeof(tmp3), e->LicenseName); tmp4 = LiGetLicenseStatusStr(e->Status); if (e->Expires == 0) { UniStrCpy(tmp5, sizeof(tmp5), _UU("SM_LICENSE_NO_EXPIRES")); } else { GetDateStrEx64(tmp5, sizeof(tmp5), e->Expires, NULL); } StrToUni(tmp6, sizeof(tmp6), e->LicenseId); UniToStru(tmp7, e->ProductId); UniFormat(tmp8, sizeof(tmp8), L"%I64u", e->SystemId); UniToStru(tmp9, e->SerialId); LvInsertAdd(b, e->Status == LICENSE_STATUS_OK ? ICO_PASS : ICO_DISCARD, (void *)e->Id, 9, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9); } LvInsertEnd(b, hWnd, L_LIST); FreeRpcEnumLicenseKey(&t); Zero(&st, sizeof(st)); if (CALL(hWnd, EcGetLicenseStatus(s, &st)) == false) { Close(hWnd); return; } b = LvInsertStart(); if (st.Valid == false) { LvInsertAdd(b, 0, NULL, 2, _UU("EM_NO_LICENSE_COLUMN"), _UU("EM_NO_LICENSE")); } else { // Current system ID UniFormat(tmp, sizeof(tmp), L"%I64u", st.SystemId); LvInsertAdd(b, 0, NULL, 2, _UU("SM_LICENSE_STATUS_SYSTEM_ID"), tmp); // Expiration date of the current license product if (st.SystemExpires == 0) { UniStrCpy(tmp, sizeof(tmp), _UU("SM_LICENSE_NO_EXPIRES")); } else { GetDateStrEx64(tmp, sizeof(tmp), st.SystemExpires, NULL); } LvInsertAdd(b, 0, NULL, 2, _UU("SM_LICENSE_STATUS_EXPIRES"), tmp); } LvInsertEnd(b, hWnd, L_STATUS); if (LvNum(hWnd, L_STATUS) >= 1) { LvAutoSize(hWnd, L_STATUS); } EmLicenseDlgUpdate(hWnd, s); }
// Installation of WinPcap void EmInstallWinPcap(HWND hWnd, RPC *r) { wchar_t temp_name[MAX_SIZE]; HGLOBAL g; HINSTANCE h; HRSRC hr; UINT size; void *data; IO *io; // Ask whether the user want to start the installation if (MsgBox(hWnd, MB_ICONQUESTION | MB_YESNO, _UU("EM_WPCAP_INSTALL")) == IDNO) { return; } // Generate a temporary file name UniFormat(temp_name, sizeof(temp_name), L"%s\\winpcap_installer.exe", MsGetTempDirW()); // Read from the resource h = GetUiDll(); hr = FindResource(h, MAKEINTRESOURCE(BIN_WINPCAP), "BIN"); if (hr == NULL) { RES_ERROR: MsgBox(hWnd, MB_ICONSTOP, _UU("EM_RESOURCE")); return; } g = LoadResource(h, hr); if (g == NULL) { goto RES_ERROR; } size = SizeofResource(h, hr); data = LockResource(g); if (data == NULL) { goto RES_ERROR; } // Write to a temporary file io = FileCreateW(temp_name); if (io == NULL) { goto RES_ERROR; } FileWrite(io, data, size); FileClose(io); // Run if (RunW(temp_name, NULL, false, true) == false) { // Failure FileDeleteW(temp_name); goto RES_ERROR; } FileDeleteW(temp_name); if (r == NULL) { return; } // Message after the end if (OS_IS_WINDOWS_NT(GetOsInfo()->OsType) == false) { // Need to restart the computer MsgBox(hWnd, MB_ICONINFORMATION, _UU("EM_WPCAP_REBOOT1")); } else { // Need to restart the service if (MsgBox(hWnd, MB_ICONQUESTION | MB_YESNO, _UU("EM_WPCAP_REBOOT2")) == IDNO) { // Not restart } else { // Restart RPC_TEST t; RPC_BRIDGE_SUPPORT t2; Zero(&t, sizeof(t)); EcRebootServer(r, &t); SleepThread(500); Zero(&t2, sizeof(t2)); CALL(hWnd, EcGetBridgeSupport(r, &t2)); } } }
// Creating a new local console CONSOLE *NewLocalConsole(wchar_t *infile, wchar_t *outfile) { IO *in_io = NULL, *out_io = NULL; CONSOLE *c = ZeroMalloc(sizeof(CONSOLE)); LOCAL_CONSOLE_PARAM *p; UINT old_size = 0; #ifdef OS_WIN32 if (MsGetConsoleWidth() == 80) { //old_size = MsSetConsoleWidth(WIN32_DEFAULT_CONSOLE_WIDTH); } #endif // OS_WIN32 c->ConsoleType = CONSOLE_LOCAL; c->Free = ConsoleLocalFree; c->ReadLine = ConsoleLocalReadLine; c->ReadPassword = ConsoleLocalReadPassword; c->Write = ConsoleLocalWrite; c->GetWidth = ConsoleLocalGetWidth; c->OutputLock = NewLock(); if (UniIsEmptyStr(infile) == false) { // Input file is specified in_io = FileOpenW(infile, false); if (in_io == NULL) { wchar_t tmp[MAX_SIZE]; UniFormat(tmp, sizeof(tmp), _UU("CON_INFILE_ERROR"), infile); c->Write(c, tmp); Free(c); return NULL; } else { wchar_t tmp[MAX_SIZE]; UniFormat(tmp, sizeof(tmp), _UU("CON_INFILE_START"), infile); c->Write(c, tmp); } } if (UniIsEmptyStr(outfile) == false) { // Output file is specified out_io = FileCreateW(outfile); if (out_io == NULL) { wchar_t tmp[MAX_SIZE]; UniFormat(tmp, sizeof(tmp), _UU("CON_OUTFILE_ERROR"), outfile); c->Write(c, tmp); Free(c); if (in_io != NULL) { FileClose(in_io); } return NULL; } else { wchar_t tmp[MAX_SIZE]; UniFormat(tmp, sizeof(tmp), _UU("CON_OUTFILE_START"), outfile); c->Write(c, tmp); } } p = ZeroMalloc(sizeof(LOCAL_CONSOLE_PARAM)); c->Param = p; p->InFile = in_io; p->OutFile = out_io; p->Win32_OldConsoleWidth = old_size; if (in_io != NULL) { UINT size; void *buf; size = FileSize(in_io); buf = ZeroMalloc(size + 1); FileRead(in_io, buf, size); p->InBuf = NewBuf(); WriteBuf(p->InBuf, buf, size); Free(buf); p->InBuf->Current = 0; } return c; }
bool SuInstallDriverInner(bool force) { wchar_t sys_fullpath[MAX_PATH]; UINT current_sl_ver = 0; bool ret = false; wchar_t src_cat[MAX_PATH]; wchar_t src_inf[MAX_PATH]; wchar_t src_sys[MAX_PATH]; wchar_t dst_cat[MAX_PATH]; wchar_t dst_inf[MAX_PATH]; wchar_t dst_sys[MAX_PATH]; wchar_t tmp_dir[MAX_PATH]; char *cpu_type = MsIsX64() ? "x64" : "x86"; if (SuIsSupportedOs(true) == false) { // Unsupported OS return false; } CombinePathW(tmp_dir, sizeof(tmp_dir), MsGetWindowsDirW(), L"Temp"); MakeDirExW(tmp_dir); UniStrCat(tmp_dir, sizeof(tmp_dir), L"\\selowtmp"); MakeDirExW(tmp_dir); // Confirm whether the driver is currently installed CombinePathW(sys_fullpath, sizeof(sys_fullpath), MsGetSystem32DirW(), L"drivers\\SeLow_%S.sys"); UniFormat(sys_fullpath, sizeof(sys_fullpath), sys_fullpath, cpu_type); if (IsFileExistsW(sys_fullpath)) { char *path; // Read the current version from the registry current_sl_ver = MsRegReadIntEx2(REG_LOCAL_MACHINE, SL_REG_KEY_NAME, (MsIsWindows10() ? SL_REG_VER_VALUE_WIN10 : SL_REG_VER_VALUE), false, true); path = MsRegReadStrEx2(REG_LOCAL_MACHINE, SL_REG_KEY_NAME, "ImagePath", false, true); if (IsEmptyStr(path) || IsFileExists(path) == false || MsIsServiceInstalled(SL_PROTOCOL_NAME) == false) { current_sl_ver = 0; } Free(path); } if (force == false && current_sl_ver >= SL_VER) { // Newer version has already been installed Debug("Newer SeLow is Installed. %u >= %u\n", current_sl_ver, SL_VER); return true; } // Copy necessary files to a temporary directory UniFormat(src_sys, sizeof(src_sys), L"|DriverPackages\\%S\\%S\\SeLow_%S.sys", (MsIsWindows10() ? "SeLow_Win10" : "SeLow_Win8"), cpu_type, cpu_type); if (MsIsWindows8() == false) { // Windows Vista and Windows 7 uses SHA-1 catalog files UniFormat(src_cat, sizeof(src_cat), L"|DriverPackages\\SeLow_Win8\\%S\\inf.cat", cpu_type); } else { // Windows 8 or above uses SHA-256 catalog files UniFormat(src_cat, sizeof(src_cat), L"|DriverPackages\\SeLow_Win8\\%S\\inf2.cat", cpu_type); if (MsIsWindows10()) { // Windows 10 uses WHQL catalog files UniFormat(src_cat, sizeof(src_cat), L"|DriverPackages\\SeLow_Win10\\%S\\SeLow_Win10_%S.cat", cpu_type, cpu_type); } } UniFormat(src_inf, sizeof(src_inf), L"|DriverPackages\\%S\\%S\\SeLow_%S.inf", (MsIsWindows10() ? "SeLow_Win10" : "SeLow_Win8"), cpu_type, cpu_type); UniFormat(dst_sys, sizeof(dst_cat), L"%s\\SeLow_%S.sys", tmp_dir, cpu_type); UniFormat(dst_cat, sizeof(dst_cat), L"%s\\SeLow_%S_%S.cat", tmp_dir, (MsIsWindows10() ? "Win10" : "Win8"), cpu_type); UniFormat(dst_inf, sizeof(dst_inf), L"%s\\SeLow_%S.inf", tmp_dir, cpu_type); if (FileCopyW(src_sys, dst_sys) && FileCopyW(src_cat, dst_cat) && FileCopyW(src_inf, dst_inf)) { NO_WARNING *nw; nw = MsInitNoWarningEx(SL_USER_AUTO_PUSH_TIMER); if (MsIsWindows10()) { if (MsIsServiceInstalled(SL_PROTOCOL_NAME) == false && MsIsServiceRunning(SL_PROTOCOL_NAME) == false) { // On Windows 10, if there are no SwLow service installed, then uinstall the protocol driver first. // TODO: currently do nothing. On some versions of Windows 10 beta builds it is necessary to do something... } } if (MsIsWindows10()) { // Delete garbage INFs SuDeleteGarbageInfs(); } // Call the installer if (InstallNdisProtocolDriver(dst_inf, L"SeLow", SL_USER_INSTALL_LOCK_TIMEOUT) == false) { Debug("InstallNdisProtocolDriver Error.\n"); } else { Debug("InstallNdisProtocolDriver Ok.\n"); // Copy manually because there are cases where .sys file is not copied successfully for some reason FileCopyW(src_sys, sys_fullpath); ret = true; // Write the version number into the registry MsRegWriteIntEx2(REG_LOCAL_MACHINE, SL_REG_KEY_NAME, (MsIsWindows10() ? SL_REG_VER_VALUE_WIN10 : SL_REG_VER_VALUE), SL_VER, false, true); // Set to automatic startup MsRegWriteIntEx2(REG_LOCAL_MACHINE, SL_REG_KEY_NAME, "Start", SERVICE_SYSTEM_START, false, true); } MsFreeNoWarning(nw); } else { Debug("Fail Copying Files.\n"); } if (ret) { // If the service is installed this time, start and wait until the enumeration is completed SuFree(SuInitEx(180 * 1000)); } return ret; }
// Download thread void ViDownloadThread(THREAD *thread, void *param) { VI_INSTALL_DLG *d; VI_SETTING_ARCH *a; HWND hWnd; UINT num_files = 2; VI_DOWNLOAD_FILE files[2]; VI_DOWNLOAD_FILE *f; UINT i; // Validate arguments if (thread == NULL || param == NULL) { return; } d = (VI_INSTALL_DLG *)param; hWnd = d->hWnd; Zero(files, sizeof(files)); a = ViGetSuitableArchForCpu(); // File body f = &files[0]; StrCpy(f->SrcPath, sizeof(f->SrcPath), a->Path); // Configuration file if (IsEmptyStr(setting.SettingPath) == false) { f = &files[1]; StrCpy(f->SrcPath, sizeof(f->SrcPath), setting.SettingPath); } else { // No configuration file num_files = 1; } for (i = 0;i < num_files;i++) { bool b = true; if (i == 0 && setting.DownloadNotRequired) { b = false; } if (b) { wchar_t tmp[MAX_SIZE]; IO *dest = NULL; VI_FILE *down; UINT ret; UINT totalsize; UINT currentsize; wchar_t filename_w[MAX_PATH]; f = &files[i]; GetFileNameFromFilePath(f->FileName, sizeof(f->FileName), f->SrcPath); MakeSafeFileName(f->FileName, sizeof(f->FileName), f->FileName); StrToUni(filename_w, sizeof(filename_w), f->FileName); ConbinePathW(f->DestPathW, sizeof(f->DestPathW), MsGetMyTempDirW(), filename_w); ViInstallDlgSetPos(hWnd, 0); UniFormat(tmp, sizeof(tmp), _U(IDS_DOWNLOADSTART+skip), f->FileName); ViInstallDlgSetText(d, hWnd, S_STATUS, tmp); down = ViOpenFile(f->SrcPath); if (down == NULL) { MsgBoxEx(hWnd, MB_ICONSTOP, _U(IDS_DOWNLOAD_ERROR+skip), f->FileName); ViInstallDlgCancel(hWnd); return; } dest = FileCreateW(f->DestPathW); if (dest == NULL) { MsgBoxEx(hWnd, MB_ICONSTOP, _U(IDS_TEMP_ERROR+skip), f->DestPathW); ViCloseFile(down); ViInstallDlgCancel(hWnd); return; } totalsize = ViGetFileSize(down); currentsize = 0; UniFormat(tmp, sizeof(tmp), _U(IDS_DOWNLOADING3+skip), f->FileName); ViInstallDlgSetText(d, hWnd, S_STATUS, tmp); while (true) { UINT pos = 0; if (d->Halt) { // User cancel FileClose(dest); ViCloseFile(down); return; } UniFormat(tmp, sizeof(tmp), _U(IDS_DOWNLOADING3+skip), f->FileName); ViInstallDlgSetText(d, hWnd, IDS_DOWNLOADING3+skip, tmp); ret = ViReadFile(down, d->Buf, d->BufSize); if (ret == INFINITE) { // Communication error MsgBoxEx(hWnd, MB_ICONSTOP, _U(IDS_DOWNLOAD_ERROR+skip), f->FileName); FileClose(dest); ViCloseFile(down); ViInstallDlgCancel(hWnd); return; } // Draw progress currentsize += ret; if (totalsize != 0) { UniFormat(tmp, sizeof(tmp), _U(IDS_DOWNLOADING+skip), ((float)totalsize) / 1024.0f / 1024.0f, ((float)currentsize) / 1024.0f / 1024.0f); pos = (UINT)(((float)currentsize) * 100.0f / ((float)totalsize)); } else { UniFormat(tmp, sizeof(tmp), _U(IDS_DOWNLOADING2+skip), ((float)currentsize) / 1024.0f / 1024.0f); pos = (UINT)(((float)currentsize) * 100.0f / (1024.0f * 1024.0f * 10.0f)); } ViInstallDlgSetText(d, hWnd, S_SIZEINFO, tmp); ViInstallDlgSetPos(hWnd, pos); if (ret == 0) { // Download Complete break; } else { FileWrite(dest, d->Buf, ret); } } ViCloseFile(down); FileClose(dest); } } UniStrCpy(setting.DownloadedInstallerPathW, sizeof(setting.DownloadedInstallerPathW), files[0].DestPathW); if (num_files >= 2) { UniStrCpy(setting.DownloadedSettingPathW, sizeof(setting.DownloadedSettingPathW), files[1].DestPathW); } PostMessageA(hWnd, WM_VI_DOWNLOAD_FINISHED, 0, 0); }
// Update status void UtSpeedMeterDlgRefreshStatus(HWND hWnd) { char *title; MS_ADAPTER *a; UINT i; // Validate arguments if (hWnd == NULL) { return; } title = selected_adapter; a = MsGetAdapter(title); if (a == NULL) { LbReset(hWnd, L_STATUS); Disable(hWnd, L_STATUS); } else { LVB *b; wchar_t tmp[MAX_SIZE]; wchar_t tmp2[MAX_SIZE]; char str[MAX_SIZE]; b = LvInsertStart(); UniStrCpy(tmp, sizeof(tmp), a->TitleW); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_TITLE"), tmp); StrToUni(tmp, sizeof(tmp), a->Guid); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_GUID"), tmp); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_TYPE"), MsGetAdapterTypeStr(a->Type)); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_TYPE2"), (!a->IsNotEthernetLan ? _UU("SEC_YES") : _UU("SEC_NO"))); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_STATUS"), MsGetAdapterStatusStr(a->Status)); UniToStr3(tmp, sizeof(tmp), a->Mtu); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_MTU"), tmp); UniToStr3(tmp, sizeof(tmp), a->Speed); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_SPEED"), tmp); Zero(str, sizeof(str)); BinToStrEx2(str, sizeof(str), a->Address, a->AddressSize, '-'); StrToUni(tmp, sizeof(tmp), str); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_ADDRESS"), tmp); UniToStr3(tmp, sizeof(tmp), a->RecvBytes); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_RECV_BYTES"), tmp); UniToStr3(tmp, sizeof(tmp), a->RecvPacketsBroadcast); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_RECV_BCASTS"), tmp); UniToStr3(tmp, sizeof(tmp), a->RecvPacketsUnicast); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_RECV_UNICASTS"), tmp); UniToStr3(tmp, sizeof(tmp), a->SendBytes); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_SEND_BYTES"), tmp); UniToStr3(tmp, sizeof(tmp), a->SendPacketsBroadcast); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_SEND_BCASTS"), tmp); UniToStr3(tmp, sizeof(tmp), a->SendPacketsUnicast); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_SEND_UNICASTS"), tmp); for (i = 0;i < a->NumIpAddress;i++) { UniFormat(tmp2, sizeof(tmp2), _UU("UT_SM_ST_IP"), i + 1); IPToUniStr(tmp, sizeof(tmp), &a->IpAddresses[i]); LvInsertAdd(b, 0, NULL, 2, tmp2, tmp); UniFormat(tmp2, sizeof(tmp2), _UU("UT_SM_ST_SUBNET"), i + 1); IPToUniStr(tmp, sizeof(tmp), &a->SubnetMasks[i]); LvInsertAdd(b, 0, NULL, 2, tmp2, tmp); } for (i = 0;i < a->NumGateway;i++) { UniFormat(tmp2, sizeof(tmp2), _UU("UT_SM_ST_GATEWAY"), i + 1); IPToUniStr(tmp, sizeof(tmp), &a->Gateways[i]); LvInsertAdd(b, 0, NULL, 2, tmp2, tmp); } if (a->UseDhcp) { IPToUniStr(tmp, sizeof(tmp), &a->DhcpServer); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_DHCP"), tmp); GetDateTimeStrEx64(tmp, sizeof(tmp), a->DhcpLeaseStart, NULL); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_DHCP_1"), tmp); GetDateTimeStrEx64(tmp, sizeof(tmp), a->DhcpLeaseExpires, NULL); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_DHCP_2"), tmp); } if (a->UseWins) { IPToUniStr(tmp, sizeof(tmp), &a->PrimaryWinsServer); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_WINS_1"), tmp); IPToUniStr(tmp, sizeof(tmp), &a->SecondaryWinsServer); LvInsertAdd(b, 0, NULL, 2, _UU("UT_SM_ST_WINS_2"), tmp); } LvInsertEnd(b, hWnd, L_STATUS); Enable(hWnd, L_STATUS); MsFreeAdapter(a); } }
// Display the help for the command void PrintCmdHelp(CONSOLE *c, char *cmd_name, TOKEN_LIST *param_list) { wchar_t tmp[MAX_SIZE]; wchar_t *buf; UINT buf_size; wchar_t *description, *args, *help; UNI_TOKEN_LIST *t; UINT width; UINT i; char *space; // Validate arguments if (c == NULL || cmd_name == NULL || param_list == NULL) { return; } width = GetConsoleWidth(c) - 2; buf_size = sizeof(wchar_t) * (width + 32); buf = Malloc(buf_size); GetCommandHelpStr(cmd_name, &description, &args, &help); space = MakeCharArray(' ', 2); // Title UniFormat(tmp, sizeof(tmp), _UU("CMD_HELP_TITLE"), cmd_name); c->Write(c, tmp); c->Write(c, L""); // Purpose c->Write(c, _UU("CMD_HELP_DESCRIPTION")); t = SeparateStringByWidth(description, width - 2); for (i = 0;i < t->NumTokens;i++) { UniFormat(buf, buf_size, L"%S%s", space, t->Token[i]); c->Write(c, buf); } UniFreeToken(t); c->Write(c, L""); // Description c->Write(c, _UU("CMD_HELP_HELP")); t = SeparateStringByWidth(help, width - 2); for (i = 0;i < t->NumTokens;i++) { UniFormat(buf, buf_size, L"%S%s", space, t->Token[i]); c->Write(c, buf); } UniFreeToken(t); c->Write(c, L""); // Usage c->Write(c, _UU("CMD_HELP_USAGE")); t = SeparateStringByWidth(args, width - 2); for (i = 0;i < t->NumTokens;i++) { UniFormat(buf, buf_size, L"%S%s", space, t->Token[i]); c->Write(c, buf); } UniFreeToken(t); // Arguments if (param_list->NumTokens >= 1) { c->Write(c, L""); c->Write(c, _UU("CMD_HELP_ARGS")); PrintCandidateHelp(c, cmd_name, param_list, 2); } Free(space); Free(buf); }
bool DispatchNextCmdEx(CONSOLE *c, wchar_t *exec_command, char *prompt, CMD cmd[], UINT num_cmd, void *param) { wchar_t *str; wchar_t *tmp; char *cmd_name; bool b_exit = false; wchar_t *cmd_param; UINT ret = ERR_NO_ERROR; TOKEN_LIST *t; TOKEN_LIST *candidate; bool no_end_crlf = false; UINT i; // Validate arguments if (c == NULL || (num_cmd >= 1 && cmd == NULL)) { return false; } if (exec_command == NULL) { // Show the prompt RETRY: tmp = CopyStrToUni(prompt); if (c->ProgrammingMode) { wchar_t tmp2[MAX_PATH]; UniFormat(tmp2, sizeof(tmp2), L"[PROMPT:%u:%s]\r\n", c->RetCode, tmp); Free(tmp); tmp = CopyUniStr(tmp2); } str = c->ReadLine(c, tmp, false); Free(tmp); if (str != NULL && IsEmptyUniStr(str)) { Free(str); goto RETRY; } } else { wchar_t tmp[MAX_SIZE]; // Use exec_command if (UniStartWith(exec_command, L"vpncmd") == false) { if (prompt != NULL) { if (c->ConsoleType != CONSOLE_CSV) { UniFormat(tmp, sizeof(tmp), L"%S%s", prompt, exec_command); c->Write(c, tmp); } } } str = CopyUniStr(exec_command); } if (str == NULL) { // User canceled return false; } UniTrimCrlf(str); UniTrim(str); if (UniIsEmptyStr(str)) { // Do Nothing Free(str); return true; } // Divide into command name and parameter if (SeparateCommandAndParam(str, &cmd_name, &cmd_param) == false) { // Do Nothing Free(str); return true; } if (StrLen(cmd_name) >= 2 && cmd_name[0] == '?' && cmd_name[1] != '?') { char tmp[MAX_SIZE]; wchar_t *s; StrCpy(tmp, sizeof(tmp), cmd_name + 1); StrCpy(cmd_name, 0, tmp); s = UniCopyStr(L"/?"); Free(cmd_param); cmd_param = s; } if (StrLen(cmd_name) >= 2 && EndWith(cmd_name, "?") && cmd_name[StrLen(cmd_name) - 2] != '?') { wchar_t *s; cmd_name[StrLen(cmd_name) - 1] = 0; s = UniCopyStr(L"/?"); Free(cmd_param); cmd_param = s; } // Get the candidate of command t = ZeroMalloc(sizeof(TOKEN_LIST)); t->NumTokens = num_cmd; t->Token = ZeroMalloc(sizeof(char *) * t->NumTokens); for (i = 0;i < t->NumTokens;i++) { t->Token[i] = CopyStr(cmd[i].Name); } if (IsHelpStr(cmd_name)) { if (UniIsEmptyStr(cmd_param)) { wchar_t tmp[MAX_SIZE]; // Display the list of commands that can be used UniFormat(tmp, sizeof(tmp), _UU("CMD_HELP_1"), t->NumTokens); c->Write(c, tmp); PrintCandidateHelp(c, NULL, t, 1); c->Write(c, L""); c->Write(c, _UU("CMD_HELP_2")); } else { char *cmd_name; // Display the help for the specified command if (SeparateCommandAndParam(cmd_param, &cmd_name, NULL)) { bool b = true; if (IsHelpStr(cmd_name)) { b = false; } if (b) { wchar_t str[MAX_SIZE]; UniFormat(str, sizeof(str), L"%S /help", cmd_name); DispatchNextCmdEx(c, str, NULL, cmd, num_cmd, param); no_end_crlf = true; } Free(cmd_name); } } } else if (StrCmpi(cmd_name, "exit") == 0 || StrCmpi(cmd_name, "quit") == 0) { // Exit b_exit = true; } else { candidate = GetRealnameCandidate(cmd_name, t); if (candidate == NULL || candidate->NumTokens == 0) { wchar_t tmp[MAX_SIZE]; // No candidate UniFormat(tmp, sizeof(tmp), _UU("CON_UNKNOWN_CMD"), cmd_name); c->Write(c, tmp); c->RetCode = ERR_BAD_COMMAND_OR_PARAM; } else if (candidate->NumTokens >= 2) { wchar_t tmp[MAX_SIZE]; // There is more than one candidate UniFormat(tmp, sizeof(tmp), _UU("CON_AMBIGIOUS_CMD"), cmd_name); c->Write(c, tmp); c->Write(c, _UU("CON_AMBIGIOUS_CMD_1")); PrintCandidateHelp(c, NULL, candidate, 1); c->Write(c, _UU("CON_AMBIGIOUS_CMD_2")); c->RetCode = ERR_BAD_COMMAND_OR_PARAM; } else { char *real_cmd_name; UINT i; // The candidate was shortlisted to one real_cmd_name = candidate->Token[0]; for (i = 0;i < num_cmd;i++) { if (StrCmpi(cmd[i].Name, real_cmd_name) == 0) { if (cmd[i].Proc != NULL) { // Show the description of the command if it isn't in CSV mode if(c->ConsoleType != CONSOLE_CSV) { wchar_t tmp[256]; wchar_t *note; GetCommandHelpStr(cmd[i].Name, ¬e, NULL, NULL); UniFormat(tmp, sizeof(tmp), _UU("CMD_EXEC_MSG_NAME"), cmd[i].Name, note); c->Write(c, tmp); } // Call the procedure of the command ret = cmd[i].Proc(c, cmd[i].Name, cmd_param, param); if (ret == INFINITE) { // Exit command b_exit = true; } else { c->RetCode = ret; } } } } } FreeToken(candidate); } FreeToken(t); Free(str); Free(cmd_name); Free(cmd_param); if (no_end_crlf == false) { //c->Write(c, L""); } if (b_exit) { return false; } return true; }
// Display the help of the candidate list void PrintCandidateHelp(CONSOLE *c, char *cmd_name, TOKEN_LIST *candidate_list, UINT left_space) { UINT console_width; UINT max_keyword_width; LIST *o; UINT i; wchar_t *tmpbuf; UINT tmpbuf_size; char *left_space_array; char *max_space_array; // Validate arguments if (c == NULL || candidate_list == NULL) { return; } // Get the width of the screen console_width = GetConsoleWidth(c) - 1; tmpbuf_size = sizeof(wchar_t) * (console_width + 32); tmpbuf = Malloc(tmpbuf_size); left_space_array = MakeCharArray(' ', left_space); // Sort and enlist the command name // no need to sort the parameter name o = NewListFast(cmd_name == NULL ? CompareCandidateStr : NULL); max_keyword_width = 0; for (i = 0;i < candidate_list->NumTokens;i++) { UINT keyword_width; // Get the width of each keyword Insert(o, candidate_list->Token[i]); keyword_width = StrWidth(candidate_list->Token[i]); if (cmd_name != NULL) { if (candidate_list->Token[i][0] != '[') { keyword_width += 1; } else { keyword_width -= 2; } } max_keyword_width = MAX(max_keyword_width, keyword_width); } max_space_array = MakeCharArray(' ', max_keyword_width); // Display the candidate for (i = 0;i < LIST_NUM(o);i++) { char tmp[128]; char *name = LIST_DATA(o, i); UNI_TOKEN_LIST *t; wchar_t *help; UINT j; UINT keyword_start_width = left_space; UINT descript_start_width = left_space + max_keyword_width + 1; UINT descript_width; char *space; if (console_width >= (descript_start_width + 5)) { descript_width = console_width - descript_start_width - 3; } else { descript_width = 2; } // Generate the name if (cmd_name != NULL && name[0] != '[') { // Prepend a "/" in the case of a parameter Format(tmp, sizeof(tmp), "/%s", name); } else { // Use the characters as it is in the case of a command name if (cmd_name == NULL) { StrCpy(tmp, sizeof(tmp), name); } else { StrCpy(tmp, sizeof(tmp), name + 1); if (StrLen(tmp) >= 1) { tmp[StrLen(tmp) - 1] = 0; } } } // Get the help string if (cmd_name == NULL) { GetCommandHelpStr(name, &help, NULL, NULL); } else { GetCommandParamHelpStr(cmd_name, name, &help); } space = MakeCharArray(' ', max_keyword_width - StrWidth(name) - (cmd_name == NULL ? 0 : (name[0] != '[' ? 1 : -2))); t = SeparateStringByWidth(help, descript_width); for (j = 0;j < t->NumTokens;j++) { if (j == 0) { UniFormat(tmpbuf, tmpbuf_size, L"%S%S%S - %s", left_space_array, tmp, space, t->Token[j]); } else { UniFormat(tmpbuf, tmpbuf_size, L"%S%S %s", left_space_array, max_space_array, t->Token[j]); } c->Write(c, tmpbuf); } Free(space); UniFreeToken(t); } ReleaseList(o); Free(max_space_array); Free(tmpbuf); Free(left_space_array); }
FOLDER *CfgReadW(wchar_t *name) { wchar_t tmp[MAX_SIZE]; wchar_t newfile[MAX_SIZE]; BUF *b; IO *o; UINT size; void *buf; FOLDER *f; bool delete_new = false; bool binary_file = false; bool invalid_file = false; UCHAR header[8]; // Validate arguments if (name == NULL) { return NULL; } // Generate a new file name UniFormat(newfile, sizeof(newfile), L"%s.new", name); // Generate a temporary file name UniFormat(tmp, sizeof(tmp), L"%s.log", name); // Read the new file if it exists o = FileOpenW(newfile, false); if (o == NULL) { // Read the temporary file o = FileOpenW(tmp, false); } else { delete_new = true; } if (o == NULL) { // Read the original file if there is no temporary file o = FileOpenW(name, false); } else { // Read the original file too if the size of temporary file is 0 if (FileSize(o) == 0) { invalid_file = true; } if (invalid_file) { FileClose(o); o = FileOpenW(name, false); } } if (o == NULL) { // Failed to read return NULL; } // Read into the buffer size = FileSize(o); buf = Malloc(size); FileRead(o, buf, size); b = NewBuf(); WriteBuf(b, buf, size); SeekBuf(b, 0, 0); // Close the file FileClose(o); if (delete_new) { // Delete the new file FileDeleteW(newfile); } // If the beginning 8 character of the buffer is "SEVPN_DB", it is binary file ReadBuf(b, header, sizeof(header)); if (Cmp(header, TAG_BINARY, 8) == 0) { UCHAR hash1[SHA1_SIZE], hash2[SHA1_SIZE]; binary_file = true; // Check the hash ReadBuf(b, hash1, sizeof(hash1)); Hash(hash2, ((UCHAR *)b->Buf) + 8 + SHA1_SIZE, b->Size - 8 - SHA1_SIZE, true); if (Cmp(hash1, hash2, SHA1_SIZE) != 0) { // Corrupted file invalid_file = true; FreeBuf(b); return NULL; } } SeekBuf(b, 0, 0); if (binary_file) { SeekBuf(b, 8 + SHA1_SIZE, 0); } // Convert the buffer into a folder if (binary_file == false) { // Text mode f = CfgBufTextToFolder(b); } else { // Binary mode f = CfgBufBinToFolder(b); } // Memory release Free(buf); FreeBuf(b); FileDeleteW(newfile); return f; }
// Start the installation process void ViInstallProcessStart(HWND hWnd, VI_INSTALL_DLG *d) { wchar_t *exew; bool ok; char instdir[MAX_PATH]; char hamcore[MAX_PATH]; // Validate arguments if (hWnd == NULL || d == NULL) { return; } ViGenerateVpnSMgrTempDirName(instdir, sizeof(instdir), ViGetSuitableArchForCpu()->Build); ConbinePath(hamcore, sizeof(hamcore), instdir, "hamcore.se2"); exew = setting.DownloadedInstallerPathW; d->NoClose = true; Hide(hWnd, IDCANCEL); SetPos(hWnd, P_PROGRESS, 100); Hide(hWnd, P_PROGRESS); Hide(hWnd, S_SIZEINFO); SetText(hWnd, S_STATUS, _U(IDS_INSTALLSTART+skip)); ok = true; if (setting.DownloadNotRequired == false) { if (setting.WebMode && ViCheckExeSign(hWnd, exew) == false) { // The digital signature is not reliable ok = false; } else { // Installation HANDLE hProcess; SHELLEXECUTEINFOW info; // Run Zero(&info, sizeof(info)); info.cbSize = sizeof(info); info.lpVerb = L"open"; info.lpFile = exew; info.fMask = SEE_MASK_NOCLOSEPROCESS; info.lpParameters = L"/HIDESTARTCOMMAND:1 /DISABLEAUTOIMPORT:1 /ISWEBINSTALLER:1"; info.nShow = SW_SHOWNORMAL; if (ShellExecuteExW(&info) == false) { MsgBox(hWnd, MB_ICONSTOP, _U(IDS_INSTALLSTART_ERROR+skip)); ok = false; } else { hProcess = info.hProcess; // Wait for the install process to complete while (true) { if (WaitForSingleObject(hProcess, 50) != WAIT_TIMEOUT) { break; } DoEvents(hWnd); } CloseHandle(hProcess); } } } if (ok && d->WindowsShutdowning == false) { VI_SETTING_ARCH *a = ViGetSuitableArchForCpu(); wchar_t arg[MAX_PATH]; wchar_t exe[MAX_PATH]; char *arg1 = "/easy"; // Hide the screen Hide(hWnd, 0); if (setting.NormalMode) { arg1 = "/normal"; } // (Just in case) start the VPN Client service if (MsIsServiceRunning("vpnclient") == false) { MsStartService("vpnclient"); } // Wait for that the service becomes available SwWaitForVpnClientPortReady(0); if (UniIsEmptyStr(setting.DownloadedSettingPathW) == false) { // Start a connection by importing the configuration file into the VPN Client UniFormat(arg, sizeof(arg), L"%S \"%s\"", arg1, setting.DownloadedSettingPathW); } else { // Just start the Connection Manager UniFormat(arg, sizeof(arg), L"%S", arg1); } // Get the installation state ViLoadCurrentInstalledStatusForArch(a); if (a->CurrentInstalled) { HANDLE h; wchar_t filename[MAX_PATH]; StrToUni(filename, sizeof(filename), a->VpnCMgrExeFileName); ConbinePathW(exe, sizeof(exe), a->CurrentInstalledPathW, filename); // Start the Connection Manager h = MsRunAsUserExW(exe, arg, false); if (h != NULL) { if (UniIsEmptyStr(setting.DownloadedSettingPathW) == false) { sleep_before_exit = true; } CloseHandle(h); } } } d->NoClose = false; Close(hWnd); }
// NAT enumeration void NmNatRefresh(HWND hWnd, SM_HUB *r) { LVB *b; RPC_ENUM_NAT t; UINT i; // Validate arguments if (hWnd == NULL || r == NULL) { return; } Zero(&t, sizeof(t)); StrCpy(t.HubName, sizeof(t.HubName), r->HubName); if (CALL(hWnd, ScEnumNAT(r->Rpc, &t)) == false) { Close(hWnd); return; } b = LvInsertStart(); for (i = 0;i < t.NumItem;i++) { RPC_ENUM_NAT_ITEM *e = &t.Items[i]; wchar_t tmp0[MAX_SIZE]; wchar_t *tmp1 = L""; wchar_t tmp2[MAX_SIZE]; wchar_t tmp3[MAX_SIZE]; wchar_t tmp4[MAX_SIZE]; wchar_t tmp5[MAX_SIZE]; wchar_t tmp6[MAX_SIZE]; wchar_t tmp7[MAX_SIZE]; wchar_t tmp8[MAX_SIZE]; wchar_t *tmp9 = L""; char v1[128], v2[128]; // ID UniToStru(tmp0, e->Id); // Protocol switch (e->Protocol) { case NAT_TCP: tmp1 = _UU("NM_NAT_PROTO_TCP"); break; case NAT_UDP: tmp1 = _UU("NM_NAT_PROTO_UDP"); break; case NAT_DNS: tmp1 = _UU("NM_NAT_PROTO_DNS"); break; case NAT_ICMP: tmp1 = _UU("NM_NAT_PROTO_ICMP"); break; } // Source host StrToUni(tmp2, sizeof(tmp2), e->SrcHost); // Source port UniToStru(tmp3, e->SrcPort); // Destination host StrToUni(tmp4, sizeof(tmp4), e->DestHost); // Destination port UniToStru(tmp5, e->DestPort); // Creation date and time of the session GetDateTimeStrEx64(tmp6, sizeof(tmp6), SystemToLocal64(e->CreatedTime), NULL); // Last communication date and time GetDateTimeStrEx64(tmp7, sizeof(tmp7), SystemToLocal64(e->LastCommTime), NULL); // Communication amount ToStr3(v1, sizeof(v1), e->RecvSize); ToStr3(v2, sizeof(v2), e->SendSize); UniFormat(tmp8, sizeof(tmp8), L"%S / %S", v1, v2); // TCP state if (e->Protocol == NAT_TCP) { switch (e->TcpStatus) { case NAT_TCP_CONNECTING: tmp9 = _UU("NAT_TCP_CONNECTING"); break; case NAT_TCP_SEND_RESET: tmp9 = _UU("NAT_TCP_SEND_RESET"); break; case NAT_TCP_CONNECTED: tmp9 = _UU("NAT_TCP_CONNECTED"); break; case NAT_TCP_ESTABLISHED: tmp9 = _UU("NAT_TCP_ESTABLISHED"); break; case NAT_TCP_WAIT_DISCONNECT: tmp9 = _UU("NAT_TCP_WAIT_DISCONNECT"); break; } } LvInsertAdd(b, ICO_PROTOCOL, NULL, 10, tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9); } LvInsertEnd(b, hWnd, L_TABLE); FreeRpcEnumNat(&t); }
// main function int main(int argc, char *argv[]) { wchar_t *s; UINT ret = 0; #ifdef OS_WIN32 SetConsoleTitleA(CEDAR_PRODUCT_STR " VPN Command Line Utility"); #endif // OS_WIN32 InitMayaqua(false, false, argc, argv); InitCedar(); s = GetCommandLineUniStr(); if (s == NULL) { s = CopyUniStr(L""); } if (UniStrCmpi(s, L"exit") != 0) { UINT size = UniStrSize(s) + 64; wchar_t *tmp; tmp = Malloc(size); UniFormat(tmp, size, L"vpncmd %s", s); ret = CommandMain(tmp); Free(tmp); } #ifdef OS_WIN32 { UINT i; LIST *o = MsGetProcessList(); bool b = false; for (i = 0;i < LIST_NUM(o);i++) { MS_PROCESS *p = LIST_DATA(o, i); if (EndWith(p->ExeFilename, "\\cmd.exe") || EndWith(p->ExeFilename, "\\command.com")) { b = true; break; } } MsFreeProcessList(o); if (b == false) { if (ret != ERR_NO_ERROR) { SleepThread(1000); } } } #endif // OS_WIN32 Free(s); FreeCedar(); FreeMayaqua(); return ret; }
// Show the information of the router bool NmInfo(HWND hWnd, SM_SERVER *s, void *param) { LVB *b; RPC_NAT_INFO t; wchar_t tmp[MAX_SIZE]; // Validate arguments if (hWnd == NULL || s == NULL) { return false; } Zero(&t, sizeof(t)); if (CALL(hWnd, NcGetInfo(s->Rpc, &t)) == false) { return false; } b = LvInsertStart(); StrToUni(tmp, sizeof(tmp), t.NatProductName); LvInsertAdd(b, ICO_ROUTER, NULL, 2, _UU("NM_INFO_PRODUCT_NAME"), tmp); StrToUni(tmp, sizeof(tmp), t.NatVersionString); LvInsertAdd(b, ICO_INFORMATION, NULL, 2, _UU("NM_INFO_VERSION_STR"), tmp); StrToUni(tmp, sizeof(tmp), t.NatBuildInfoString); LvInsertAdd(b, ICO_INFORMATION, NULL, 2, _UU("NM_INFO_BUILD_INFO"), tmp); StrToUni(tmp, sizeof(tmp), t.NatHostName); LvInsertAdd(b, ICO_TOWER, NULL, 2, _UU("NM_INFO_HOSTNAME"), tmp); // OS StrToUni(tmp, sizeof(tmp), t.OsInfo.OsSystemName); LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_OS_SYSTEM_NAME"), tmp); StrToUni(tmp, sizeof(tmp), t.OsInfo.OsProductName); LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_OS_PRODUCT_NAME"), tmp); if (t.OsInfo.OsServicePack != 0) { UniFormat(tmp, sizeof(tmp), _UU("SM_OS_SP_TAG"), t.OsInfo.OsServicePack); LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_OS_SERVICE_PACK"), tmp); } StrToUni(tmp, sizeof(tmp), t.OsInfo.OsVendorName); LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_OS_VENDER_NAME"), tmp); StrToUni(tmp, sizeof(tmp), t.OsInfo.OsVersion); LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_OS_VERSION"), tmp); StrToUni(tmp, sizeof(tmp), t.OsInfo.KernelName); LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_OS_KERNEL_NAME"), tmp); StrToUni(tmp, sizeof(tmp), t.OsInfo.KernelVersion); LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_OS_KERNEL_VERSION"), tmp); // Memory information if (t.MemInfo.TotalMemory != 0) { char vv[128]; ToStr3(vv, sizeof(vv), t.MemInfo.TotalMemory); UniFormat(tmp, sizeof(tmp), _UU("SM_ST_RAM_SIZE_KB"), vv); LvInsertAdd(b, ICO_MEMORY, NULL, 2, _UU("SM_ST_TOTAL_MEMORY"), tmp); ToStr3(vv, sizeof(vv), t.MemInfo.UsedMemory); UniFormat(tmp, sizeof(tmp), _UU("SM_ST_RAM_SIZE_KB"), vv); LvInsertAdd(b, ICO_MEMORY, NULL, 2, _UU("SM_ST_USED_MEMORY"), tmp); ToStr3(vv, sizeof(vv), t.MemInfo.FreeMemory); UniFormat(tmp, sizeof(tmp), _UU("SM_ST_RAM_SIZE_KB"), vv); LvInsertAdd(b, ICO_MEMORY, NULL, 2, _UU("SM_ST_FREE_MEMORY"), tmp); ToStr3(vv, sizeof(vv), t.MemInfo.TotalPhys); UniFormat(tmp, sizeof(tmp), _UU("SM_ST_RAM_SIZE_KB"), vv); LvInsertAdd(b, ICO_MEMORY, NULL, 2, _UU("SM_ST_TOTAL_PHYS"), tmp); ToStr3(vv, sizeof(vv), t.MemInfo.UsedPhys); UniFormat(tmp, sizeof(tmp), _UU("SM_ST_RAM_SIZE_KB"), vv); LvInsertAdd(b, ICO_MEMORY, NULL, 2, _UU("SM_ST_USED_PHYS"), tmp); ToStr3(vv, sizeof(vv), t.MemInfo.FreePhys); UniFormat(tmp, sizeof(tmp), _UU("SM_ST_RAM_SIZE_KB"), vv); LvInsertAdd(b, ICO_MEMORY, NULL, 2, _UU("SM_ST_FREE_PHYS"), tmp); } LvInsertEnd(b, hWnd, L_STATUS); FreeRpcNatInfo(&t); return true; }
// Main dialog procedure UINT EmMainDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param) { NMHDR *n; RPC *r = (RPC *)param; UINT i; char *name; // Validate arguments if (hWnd == NULL) { return 0; } switch (msg) { case WM_INITDIALOG: EmMainInit(hWnd, r); break; case WM_COMMAND: switch (wParam) { case IDOK: // Edit i = LvGetSelected(hWnd, L_LIST); if (i != INFINITE) { wchar_t *tmp; tmp = LvGetStr(hWnd, L_LIST, i, 0); if (tmp != NULL) { name = CopyUniToStr(tmp); EmAdd(hWnd, r, name); Free(tmp); Free(name); } } break; case B_PASSWORD: // Admin password Dialog(hWnd, D_EM_PASSWORD, EmPasswordDlg, r); break; case B_LICENSE: // Admin password Dialog(hWnd, D_EM_LICENSE, EmLicenseDlg, r); break; case B_ADD: // Add EmAdd(hWnd, r, NULL); EmMainRefresh(hWnd, r); break; case B_DELETE: // Delete i = LvGetSelected(hWnd, L_LIST); if (i != INFINITE) { wchar_t *tmp; tmp = LvGetStr(hWnd, L_LIST, i, 0); if (tmp != NULL) { RPC_DELETE_DEVICE t; wchar_t msg[MAX_SIZE]; name = CopyUniToStr(tmp); UniFormat(msg, sizeof(msg), _UU("EM_DELETE_CONFIRM"), name); if (MsgBox(hWnd, MB_YESNO | MB_ICONEXCLAMATION | MB_DEFBUTTON2, msg) == IDYES) { Zero(&t, sizeof(t)); StrCpy(t.DeviceName, sizeof(t.DeviceName), name); if (CALL(hWnd, EcDelDevice(r, &t))) { EmMainRefresh(hWnd, r); } } Free(tmp); Free(name); } } break; case IDCANCEL: Close(hWnd); break; } break; case WM_TIMER: switch (wParam) { case 1: KillTimer(hWnd, 1); EmMainRefresh(hWnd, r); SetTimer(hWnd, 1, 1000, NULL); break; } break; case WM_NOTIFY: n = (NMHDR *)lParam; switch (n->code) { case NM_DBLCLK: switch (n->idFrom) { case L_LIST: if (IsEnable(hWnd, IDOK)) { Command(hWnd, IDOK); } break; } break; case LVN_ITEMCHANGED: switch (n->idFrom) { case L_LIST: EmMainUpdate(hWnd, r); break; } break; } break; case WM_CLOSE: EndDialog(hWnd, 0); break; } return 0; }
// Parse the command list LIST *ParseCommandList(CONSOLE *c, char *cmd_name, wchar_t *command, PARAM param[], UINT num_param) { UINT i; LIST *o; bool ok = true; TOKEN_LIST *param_list; TOKEN_LIST *real_name_list; bool help_mode = false; wchar_t *tmp; // Validate arguments if (c == NULL || command == NULL || (num_param >= 1 && param == NULL) || cmd_name == NULL) { return NULL; } // Initialization for (i = 0;i < num_param;i++) { if (IsEmptyStr(param[i].Name) == false) { if (param[i].Name[0] == '[') { param[i].Tmp = ""; } else { param[i].Tmp = NULL; } } else { param[i].Tmp = ""; } } real_name_list = ZeroMalloc(sizeof(TOKEN_LIST)); real_name_list->NumTokens = num_param; real_name_list->Token = ZeroMalloc(sizeof(char *) * real_name_list->NumTokens); for (i = 0;i < real_name_list->NumTokens;i++) { real_name_list->Token[i] = CopyStr(param[i].Name); } // Generate a list of parameter name specified by the user param_list = GetCommandNameList(command); for (i = 0;i < param_list->NumTokens;i++) { char *s = param_list->Token[i]; if (StrCmpi(s, "help") == 0 || StrCmpi(s, "?") == 0) { help_mode = true; break; } } tmp = ParseCommand(command, L""); if (tmp != NULL) { if (UniStrCmpi(tmp, L"?") == 0) { help_mode = true; } Free(tmp); } if (help_mode) { // Show the help PrintCmdHelp(c, cmd_name, real_name_list); FreeToken(param_list); FreeToken(real_name_list); return NULL; } for (i = 0;i < param_list->NumTokens;i++) { // Get the corresponding commands for all parameter names which is specified by the user TOKEN_LIST *candidate = GetRealnameCandidate(param_list->Token[i], real_name_list); if (candidate != NULL && candidate->NumTokens >= 1) { if (candidate->NumTokens >= 2) { wchar_t tmp[MAX_SIZE]; // There is more than one candidate UniFormat(tmp, sizeof(tmp), _UU("CON_AMBIGIOUS_PARAM"), param_list->Token[i]); c->Write(c, tmp); UniFormat(tmp, sizeof(tmp), _UU("CON_AMBIGIOUS_PARAM_1"), cmd_name); c->Write(c, tmp); PrintCandidateHelp(c, cmd_name, candidate, 1); c->Write(c, _UU("CON_AMBIGIOUS_PARAM_2")); ok = false; } else { UINT j; char *real_name = candidate->Token[0]; // There is only one candidate for (j = 0;j < num_param;j++) { if (StrCmpi(param[j].Name, real_name) == 0) { param[j].Tmp = param_list->Token[i]; } } } } else { wchar_t tmp[MAX_SIZE]; // No candidate UniFormat(tmp, sizeof(tmp), _UU("CON_INVALID_PARAM"), param_list->Token[i], cmd_name, cmd_name); c->Write(c, tmp); ok = false; } FreeToken(candidate); } if (ok == false) { FreeToken(param_list); FreeToken(real_name_list); return NULL; } // Creating a list o = NewParamValueList(); // Read all the parameters of the specified name in the parameter list for (i = 0;i < num_param;i++) { bool prompt_input_value = false; PARAM *p = ¶m[i]; if (p->Tmp != NULL || p->PromptProc != NULL) { wchar_t *name = CopyStrToUni(p->Name); wchar_t *tmp; wchar_t *str; if (p->Tmp != NULL) { tmp = CopyStrToUni(p->Tmp); } else { tmp = CopyStrToUni(p->Name); } str = ParseCommand(command, tmp); Free(tmp); if (str != NULL) { wchar_t *unistr; bool ret; EVAL_VALUE: // Reading succeeded unistr = str; if (p->EvalProc != NULL) { // Evaluate the value if EvalProc is specified ret = p->EvalProc(c, unistr, p->EvalProcParam); } else { // Accept any value if EvalProc is not specified ret = true; } if (ret == false) { // The specified value is invalid if (p->PromptProc == NULL) { // Cancel ok = false; Free(name); Free(str); break; } else if (c->ProgrammingMode) { // In the programming mode, return the error immediately. ok = false; Free(name); Free(str); break; } else { // Request to re-enter Free(str); str = NULL; goto SHOW_PROMPT; } } else { PARAM_VALUE *v; // Finished loading, add it to the list v = ZeroMalloc(sizeof(PARAM_VALUE)); v->Name = CopyStr(p->Name); v->StrValue = CopyUniToStr(str); v->UniStrValue = CopyUniStr(str); v->IntValue = ToInt(v->StrValue); Insert(o, v); } } else { // Failed to read. The parameter is not specified if (p->PromptProc != NULL) { wchar_t *tmp; SHOW_PROMPT: // Prompt because it is a mandatory parameter tmp = NULL; if (c->ProgrammingMode == false) { tmp = p->PromptProc(c, p->PromptProcParam); } if (tmp == NULL) { // User canceled ok = false; Free(str); Free(name); break; } else { // Entered by the user c->Write(c, L""); str = tmp; prompt_input_value = true; goto EVAL_VALUE; } } } Free(str); Free(name); } } FreeToken(param_list); FreeToken(real_name_list); if (ok) { return o; } else { FreeParamValueList(o); return NULL; } }