// Read an INI file LIST *ReadIni(BUF *b) { LIST *o; // Validate arguments if (b == NULL) { return NULL; } o = NewListFast(NULL); SeekBuf(b, 0, 0); while (true) { char *line = CfgReadNextLine(b); if (line == NULL) { break; } Trim(line); if (IsEmptyStr(line) == false) { if (StartWith(line, "#") == false && StartWith(line, "//") == false && StartWith(line, ";") == false) { char *key, *value; UINT size = StrLen(line) + 1; key = ZeroMalloc(size); value = ZeroMalloc(size); if (GetKeyAndValue(line, key, size, value, size, NULL)) { UINT uni_size; INI_ENTRY *e = ZeroMalloc(sizeof(INI_ENTRY)); e->Key = CopyStr(key); e->Value = CopyStr(value); uni_size = CalcUtf8ToUni((BYTE *)value, StrLen(value)); e->UnicodeValue = ZeroMalloc(uni_size); Utf8ToUni(e->UnicodeValue, uni_size, (BYTE *)value, StrLen(value)); Add(o, e); } Free(key); Free(value); } } Free(line); } return o; }
// inComment -- global state of multiline comment static QString SimplifyLine(QString s, bool &inComment) { s = s.trimmed(); QString res; bool inLocalComment = false; int inString = 0; int pos = 0; while(pos < s.size()) { if (s.at(pos) == CStringChar) inString++; // in string detection if ((inString & 1) == 0) // outside string -- process global and local comments { if (inComment) { if ( StartWith(s, CGlobalCommentEnd, pos) ) { inComment = false; pos += CGlobalCommentEnd.length(); continue; } } else { if ( StartWith(s, CGlobalCommentBegin, pos) ) { inComment = true; pos += CGlobalCommentBegin.length(); continue; } if ( !inLocalComment && StartWith(s, CComment, pos) ) { inLocalComment = true; pos += CComment.length(); continue; } } } if (!(inComment || inLocalComment)) // add to result current char if not in comment { res.append( s.at(pos) ); } ++pos; // next } return res; }
// Determine whether the specified file name is the file on the Internet bool ViIsInternetFile(char *path) { // Validate arguments if (path == NULL) { return false; } if (StartWith(path, "http://") || StartWith(path, "https://") || StartWith(path, "ftp://")) { return true; } return false; }
// Get Ethernet device list on Solaris TOKEN_LIST *GetEthListSolaris() { TOKEN_LIST *t; int i, s; LIST *o; o = NewListFast(CompareStr); s = socket(AF_INET, SOCK_DGRAM, 0); if (s != INVALID_SOCKET) { struct lifnum lifn; lifn.lifn_family = AF_INET; lifn.lifn_flags = 0; if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) >= 0) { struct lifconf lifc; struct lifreq *buf; UINT numifs; UINT bufsize; numifs = lifn.lifn_count; Debug("NumIFs:%d\n",numifs); bufsize = numifs * sizeof(struct lifreq); buf = Malloc(bufsize); lifc.lifc_family = AF_INET; lifc.lifc_flags = 0; lifc.lifc_len = bufsize; lifc.lifc_buf = (char*) buf; if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) >= 0) { for (i = 0; i<numifs; i++) { if(StartWith(buf[i].lifr_name, "lo") == false){ Add(o, CopyStr(buf[i].lifr_name)); } } } Free(buf); } closesocket(s); } Sort(o); t = ZeroMalloc(sizeof(TOKEN_LIST)); t->NumTokens = LIST_NUM(o); t->Token = ZeroMalloc(sizeof(char *) * t->NumTokens); for (i = 0;i < LIST_NUM(o);i++) { char *name = LIST_DATA(o, i); t->Token[i] = name; } ReleaseList(o); return t; }
// Create an adapter list item SU_ADAPTER_LIST *SuAdapterInfoToAdapterList(SL_ADAPTER_INFO *info) { SU_ADAPTER_LIST t; char tmp[MAX_SIZE]; // Validate arguments if (info == NULL) { return NULL; } Zero(&t, sizeof(t)); Copy(&t.Info, info, sizeof(SL_ADAPTER_INFO)); UniToStr(tmp, sizeof(tmp), info->AdapterId); if (IsEmptyStr(tmp) || IsEmptyStr(info->FriendlyName) || StartWith(tmp, SL_ADAPTER_ID_PREFIX) == false) { // Name is invalid return NULL; } // GUID (Part after "SELOW_A_" prefix) StrCpy(t.Guid, sizeof(t.Guid), tmp + StrLen(SL_ADAPTER_ID_PREFIX)); // Name StrCpy(t.Name, sizeof(t.Name), tmp); // Key for sort if (GetClassRegKeyWin32(t.SortKey, sizeof(t.SortKey), tmp, sizeof(tmp), t.Guid) == false) { // Can not be found return NULL; } return Clone(&t, sizeof(t)); }
// Search for the best language from LANG string of UNIX LANGLIST *GetBestLangByLangStr(LIST *o, char *str) { UINT i; LANGLIST *ret; // Validate arguments if (o == NULL) { return NULL; } for (i = 0;i < LIST_NUM(o);i++) { LANGLIST *e = LIST_DATA(o, i); UINT j; for (j = 0;j < LIST_NUM(e->LangList);j++) { char *v = LIST_DATA(e->LangList, j); if (StrCmpi(v, str) == 0) { return e; } } } for (i = 0;i < LIST_NUM(o);i++) { LANGLIST *e = LIST_DATA(o, i); UINT j; for (j = 0;j < LIST_NUM(e->LangList);j++) { char *v = LIST_DATA(e->LangList, j); if (StartWith(str, v) || StartWith(v, str)) { return e; } } } ret = GetBestLangByName(o, "en"); return ret; }
// Search for the best language from the name LANGLIST *GetBestLangByName(LIST *o, char *name) { UINT i; LANGLIST *ret = NULL; // Validate arguments if (o == NULL) { return NULL; } for (i = 0;i < LIST_NUM(o);i++) { LANGLIST *e = LIST_DATA(o, i); if (StrCmpi(e->Name, name) == 0) { ret = e; break; } } if (ret != NULL) { return ret; } for (i = 0;i < LIST_NUM(o);i++) { LANGLIST *e = LIST_DATA(o, i); if (StartWith(e->Name, name) || StartWith(name, e->Name)) { ret = e; break; } } if (ret != NULL) { return ret; } return ret; }
// Convert a string to a Boolean value bool ToBool(char *str) { char tmp[MAX_SIZE]; // Validate arguments if (str == NULL) { return false; } StrCpy(tmp, sizeof(tmp), str); Trim(tmp); if (IsEmptyStr(tmp)) { return false; } if (ToInt(tmp) != 0) { return true; } if (StartWith("true", tmp)) { return true; } if (StartWith("yes", tmp)) { return true; } if (StartWith(tmp, "true")) { return true; } if (StartWith(tmp, "yes")) { return true; } return false; }
// Read the lang.config file bool LoadLangConfig(wchar_t *filename, char *str, UINT str_size) { BUF *b; bool ret = false; // Validate arguments if (filename == NULL || str == NULL) { return false; } b = ReadDumpW(filename); if (b == NULL) { return false; } while (true) { char *line = CfgReadNextLine(b); if (line == NULL) { break; } Trim(line); if (IsEmptyStr(line) == false) { if (StartWith(line, "#") == false && StartWith(line, "//") == false && StartWith(line, ";") == false && InStr(line, "#") == false) { StrCpy(str, str_size, line); ret = true; } } Free(line); } FreeBuf(b); return ret; }
// Check whether the command specified by the user is a abbreviation of existing commands bool IsOmissionName(char *input_name, char *real_name) { char oname[128]; // Validate arguments if (input_name == NULL || real_name == NULL) { return false; } if (IsAllUpperStr(real_name)) { // Command of all capital letters do not take abbreviations return false; } GetOmissionName(oname, sizeof(oname), real_name); if (IsEmptyStr(oname)) { return false; } if (StartWith(oname, input_name)) { // Example: The oname of AccountSecureCertSet is "ascs". // But if the user enters "asc", returns true return true; } if (StartWith(input_name, oname)) { // Example: When two commands AccountCreate and AccountConnect exist, // if the user enter "aconnect" , only AccountConnect is true if (EndWith(real_name, &input_name[StrLen(oname)])) { return true; } } return false; }
// Check whether the command specified by the user matches the existing commands bool IsNameInRealName(char *input_name, char *real_name) { // Validate arguments if (input_name == NULL || real_name == NULL) { return false; } if (StartWith(real_name, input_name)) { return true; } return false; }
// Acquisition of [Yes] or [No] bool GetParamYes(LIST *o, char *name) { char *s; char tmp[64]; // Validate arguments if (o == NULL) { return false; } s = GetParamStr(o, name); if (s == NULL) { return false; } StrCpy(tmp, sizeof(tmp), s); Trim(tmp); if (StartWith(tmp, "y")) { return true; } if (StartWith(tmp, "t")) { return true; } if (ToInt(tmp) != 0) { return true; } return false; }
// Get interface description string bool EthGetInterfaceDescriptionUnix(char *name, char *str, UINT size) { char tmp[MAX_SIZE]; bool ret = false; BUF *b; // Validate arguments if (name == NULL || str == NULL) { return false; } StrCpy(str, size, name); Format(tmp, sizeof(tmp), "/etc/sysconfig/networking/devices/ifcfg-%s", name); b = ReadDump(tmp); if (b != NULL) { char *line = CfgReadNextLine(b); if (IsEmptyStr(line) == false) { if (StartWith(line, "#")) { char tmp[MAX_SIZE]; StrCpy(tmp, sizeof(tmp), line + 1); Trim(tmp); tmp[60] = 0; StrCpy(str, size, tmp); ret = true; } } Free(line); FreeBuf(b); } return ret; }
// Get policy id for name UINT PolicyStrToId(char *name) { UINT i; // Validate arguments if (name == NULL) { return INFINITE; } for (i = 0;i < NUM_POLICY_ITEM;i++) { if (StartWith(PolicyIdToStr(i), name)) { return i; } } return INFINITE; }
// Decompose combined name UINT Win32EthGetNameAndIdFromCombinedName(char *name, UINT name_size, char *str) { UINT ret = 0; char id_str[MAX_SIZE]; UINT len; // Validate arguments ClearStr(name, name_size); StrCpy(name, name_size, str); if (name == NULL || str == NULL) { return 0; } len = StrLen(str); if (len >= 16) { StrCpy(id_str, sizeof(id_str), str + len - 16); if (StartWith(id_str, " (ID=")) { if (EndWith(id_str, ")")) { char num[MAX_SIZE]; Zero(num, sizeof(num)); StrCpy(num, sizeof(num), id_str + 5); num[StrLen(num) - 1] = 0; ret = ToInt(num); if (ret != 0) { name[len - 16] = 0; } } } } return ret; }
// Parse the URL bool ParseUrl(URL_DATA *data, char *str, bool is_post, char *referrer) { char tmp[MAX_SIZE * 3]; char server_port[MAX_HOST_NAME_LEN + 16]; char *s = NULL; char *host; UINT port; UINT i; // Validate arguments if (data == NULL || str == NULL) { return false; } Zero(data, sizeof(URL_DATA)); if (is_post) { StrCpy(data->Method, sizeof(data->Method), WPC_HTTP_POST_NAME); } else { StrCpy(data->Method, sizeof(data->Method), WPC_HTTP_GET_NAME); } if (referrer != NULL) { StrCpy(data->Referer, sizeof(data->Referer), referrer); } StrCpy(tmp, sizeof(tmp), str); Trim(tmp); // Determine the protocol if (StartWith(tmp, "http://")) { data->Secure = false; s = &tmp[7]; } else if (StartWith(tmp, "https://")) { data->Secure = true; s = &tmp[8]; } else { if (SearchStrEx(tmp, "://", 0, false) != INFINITE) { return false; } data->Secure = false; s = &tmp[0]; } // Get the "server name:port number" StrCpy(server_port, sizeof(server_port), s); i = SearchStrEx(server_port, "/", 0, false); if (i != INFINITE) { server_port[i] = 0; s += StrLen(server_port); StrCpy(data->Target, sizeof(data->Target), s); } else { StrCpy(data->Target, sizeof(data->Target), "/"); } if (ParseHostPort(server_port, &host, &port, data->Secure ? 443 : 80) == false) { return false; } StrCpy(data->HostName, sizeof(data->HostName), host); data->Port = port; Free(host); if ((data->Secure && data->Port == 443) || (data->Secure == false && data->Port == 80)) { StrCpy(data->HeaderHostName, sizeof(data->HeaderHostName), data->HostName); } else { Format(data->HeaderHostName, sizeof(data->HeaderHostName), "%s:%u", data->HostName, data->Port); } return true; }
// Open the file VI_FILE *ViOpenFile(char *path) { VI_FILE *f; // Validate arguments if (path == NULL) { return NULL; } if (ViIsInternetFile(path)) { HINTERNET hHttpFile; HINTERNET hInternet = InternetOpenA(DEFAULT_USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); UINT size; UINT sizesize; if (hInternet == NULL) { return NULL; } hHttpFile = InternetOpenUrlA(hInternet, path, NULL, 0, INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD, 0); if (hHttpFile == NULL) { InternetCloseHandle(hInternet); return NULL; } size = 0; sizesize = sizeof(size); if (StartWith(path, "ftp://")) { // ftp DWORD high = 0; size = FtpGetFileSize(hHttpFile, &high); } else { UINT errorcode = 0; UINT errorcode_size = sizeof(errorcode); // http if (HttpQueryInfo(hHttpFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &size, &sizesize, NULL) == false) { size = 0; } if (HttpQueryInfo(hHttpFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &errorcode, &errorcode_size, NULL) == false || (errorcode / 100) != 2) { // HTTP getting error InternetCloseHandle(hInternet); InternetCloseHandle(hHttpFile); return NULL; } } f = ZeroMalloc(sizeof(VI_FILE)); f->InternetFile = true; f->hInternet = hInternet; f->hHttpFile = hHttpFile; f->FileSize = size; return f; } else { IO *io; char fullpath[MAX_PATH]; char exedir[MAX_PATH]; GetExeDir(exedir, sizeof(exedir)); ConbinePath(fullpath, sizeof(fullpath), exedir, path); io = FileOpen(fullpath, false); if (io == NULL) { return NULL; } f = ZeroMalloc(sizeof(VI_FILE)); f->InternetFile = false; f->FileSize = FileSize(io); f->io = io; return f; } }
// Main process void ViMain() { char tmp[MAX_PATH]; UINT ostype = GetOsInfo()->OsType; VI_SETTING_ARCH *suitable; TOKEN_LIST *t; UINT i; if (OS_IS_WINDOWS_NT(ostype) == false || GET_KETA(ostype, 100) <= 1) { // The OS is too old MsgBox(NULL, MB_ICONEXCLAMATION, _U(IDS_BAD_OS+skip)); return; } Zero(&setting, sizeof(setting)); // Read the inf file Format(tmp, sizeof(tmp), "%s\\%s", MsGetExeDirName(), VI_INF_FILENAME); if (ViLoadInf(&setting, tmp) == false) { // Failure MsgBoxEx(NULL, MB_ICONSTOP, _U(IDS_INF_LOAD_FAILED+skip), VI_INF_FILENAME); return; } ViSetSkip(); // Parse the command line options t = GetCommandLineToken(); for (i = 0;i < t->NumTokens;i++) { char *s = t->Token[i]; if (IsEmptyStr(s) == false) { if (StartWith(s, "/") || StartWith(s, "-")) { if (StrCmpi(&s[1], "web") == 0) { setting.WebMode = true; } } else { StrCpy(setting.SettingPath, sizeof(setting.SettingPath), s); } } } FreeToken(t); suitable = ViGetSuitableArchForCpu(); // Security check if (setting.WebMode) { bool ok = true; if (ViIsInternetFile(suitable->Path) == false) { ok = false; } if (IsEmptyStr(setting.SettingPath) == false) { if (ViIsInternetFile(setting.SettingPath) == false) { ok = false; } } if (ok == false) { // Security breach MsgBox(NULL, MB_ICONEXCLAMATION, _U(IDS_SECURITY_ERROR+skip)); return; } } // Get the current installation state ViLoadCurrentInstalledStates(); if (suitable->Supported == false) { // This CPU isn't supported MsgBox(NULL, MB_ICONEXCLAMATION, _U(IDS_CPU_NOT_SUPPORTED+skip)); return; } if (suitable->CurrentInstalled && suitable->Build <= suitable->CurrentInstalledBuild) { // Do not download client software since it has already been installed setting.DownloadNotRequired = true; } // Show the dialog ViInstallDlg(); }
// Read the inf file from the buffer bool ViLoadInfFromBuf(VI_SETTING *set, BUF *buf) { bool ret; if (set == NULL || buf == NULL) { return false; } Zero(set, sizeof(VI_SETTING)); SeekBuf(buf, 0, 0); while (true) { char *tmp = CfgReadNextLine(buf); TOKEN_LIST *tokens; if (tmp == NULL) { break; } tokens = ParseToken(tmp, " \t"); if (tokens != NULL) { if (tokens->NumTokens >= 2) { if (StartWith(tokens->Token[0], "#") == false || StartWith(tokens->Token[0], "//") == false) { char *name, *value; name = tokens->Token[0]; value = tokens->Token[1]; if (StrCmpi(name, "VpnInstallBuild") == 0) { set->VpnInstallBuild = ToInt(value); } else if (StrCmpi(name, "NormalMode") == 0) { set->NormalMode = ToBool(value); } else if (StrCmpi(name, "VpnSettingPath") == 0) { StrCpy(set->SettingPath, sizeof(set->SettingPath), value); } else if (StrCmpi(name, "VpnClientBuild") == 0) { set->x86.Build = ToInt(value); } else if (StrCmpi(name, "VpnClientPath") == 0) { StrCpy(set->x86.Path, sizeof(set->x86.Path), value); } } } FreeToken(tokens); } Free(tmp); } ret = false; StrCpy(set->x86.VpnCMgrExeFileName, sizeof(set->x86.VpnCMgrExeFileName), (MsIsX64() ? "vpncmgr_x64.exe" : "vpncmgr.exe")); if (set->VpnInstallBuild != 0) { if (set->x86.Build != 0 && IsEmptyStr(set->x86.Path) == false) { set->x86.Supported = true; ret = true; } } return ret; }
UINT DCGetMyIpMain(DDNS_CLIENT *c, bool ipv6, char *dst, UINT dst_size, bool use_ssl, char *replace_v6) { char *url; char url2[MAX_SIZE]; UINT ret = ERR_INTERNAL_ERROR; URL_DATA data; BUF *recv; BUF *cert_hash; // Validate arguments if (dst == NULL || c == NULL) { return ERR_INTERNAL_ERROR; } if (ipv6 == false) { url = DDNS_URL2_V4_GLOBAL; if (IsUseAlternativeHostname()) { url = DDNS_URL2_V4_ALT; } } else { url = DDNS_URL2_V6_GLOBAL; if (IsUseAlternativeHostname()) { url = DDNS_URL2_V6_ALT; } if (replace_v6) { url = replace_v6; } } Format(url2, sizeof(url2), "%s?v=%I64u", url, Rand64()); if (use_ssl) { ReplaceStr(url2, sizeof(url2), url2, "http://", "https://"); } if (ParseUrl(&data, url2, false, NULL) == false) { return ERR_INTERNAL_ERROR; } cert_hash = StrToBin(DDNS_CERT_HASH); recv = HttpRequest(&data, (ipv6 ? NULL : &c->InternetSetting), DDNS_CONNECT_TIMEOUT, DDNS_COMM_TIMEOUT, &ret, false, NULL, NULL, NULL, ((cert_hash != NULL && cert_hash->Size == SHA1_SIZE) ? cert_hash->Buf : NULL)); FreeBuf(cert_hash); if (recv != NULL) { char *str = ZeroMalloc(recv->Size + 1); Copy(str, recv->Buf, recv->Size); if (StartWith(str, "IP=") == false) { ret = ERR_PROTOCOL_ERROR; } else { StrCpy(dst, dst_size, str + 3); ret = ERR_NO_ERROR; } Free(str); FreeBuf(recv); } if (IsUseAlternativeHostname() == false) { if (ret == ERR_CONNECT_FAILED) { if (ipv6 && replace_v6 == NULL && use_ssl == false) { UINT type = DetectFletsType(); if (type & FLETS_DETECT_TYPE_EAST_BFLETS_PRIVATE && ret != ERR_NO_ERROR) { ret = DCGetMyIpMain(c, ipv6, dst, dst_size, use_ssl, DDNS_REPLACE_URL2_FOR_EAST_BFLETS); } if (type & FLETS_DETECT_TYPE_EAST_NGN_PRIVATE && ret != ERR_NO_ERROR) { ret = DCGetMyIpMain(c, ipv6, dst, dst_size, use_ssl, DDNS_REPLACE_URL2_FOR_EAST_NGN); } if (type & FLETS_DETECT_TYPE_WEST_NGN_PRIVATE && ret != ERR_NO_ERROR) { ret = DCGetMyIpMain(c, ipv6, dst, dst_size, use_ssl, DDNS_REPLACE_URL2_FOR_WEST_NGN); } } } } return ret; }
void SuDeleteGarbageInfsInner() { char *base_key_name = "DRIVERS\\DriverDatabase\\DriverPackages"; TOKEN_LIST *keys; HINSTANCE hSetupApiDll = NULL; BOOL (WINAPI *_SetupUninstallOEMInfA)(PCSTR, DWORD, PVOID) = NULL; if (MsIsWindows10() == false) { return; } hSetupApiDll = LoadLibraryA("setupapi.dll"); if (hSetupApiDll == NULL) { return; } _SetupUninstallOEMInfA = (UINT (__stdcall *)(PCSTR,DWORD,PVOID)) GetProcAddress(hSetupApiDll, "SetupUninstallOEMInfA"); if (_SetupUninstallOEMInfA != NULL) { keys = MsRegEnumKeyEx2(REG_LOCAL_MACHINE, base_key_name, false, true); if (keys != NULL) { char full_key[MAX_PATH]; UINT i; for (i = 0; i < keys->NumTokens; i++) { char *oem_name, *inf_name, *provider; Format(full_key, sizeof(full_key), "%s\\%s", base_key_name, keys->Token[i]); oem_name = MsRegReadStrEx2(REG_LOCAL_MACHINE, full_key, "", false, true); inf_name = MsRegReadStrEx2(REG_LOCAL_MACHINE, full_key, "InfName", false, true); provider = MsRegReadStrEx2(REG_LOCAL_MACHINE, full_key, "Provider", false, true); if (IsEmptyStr(oem_name) == false && IsEmptyStr(inf_name) == false) { if (StartWith(oem_name, "oem")) { if (StartWith(inf_name, "selow")) { if (InStr(provider, "softether")) { Debug("Delete OEM INF %s (%s): %u\n", oem_name, inf_name, _SetupUninstallOEMInfA(oem_name, 0x00000001, NULL)); } } } } Free(oem_name); Free(inf_name); Free(provider); } FreeToken(keys); } } if (hSetupApiDll != NULL) { FreeLibrary(hSetupApiDll); } }
// Check the name is valid for account name bool IsUserName(char *name) { char tmp[MAX_SIZE]; // Validate arguments if (name == NULL) { return false; } StrCpy(tmp, sizeof(tmp), name); name = tmp; Trim(name); if (StrLen(name) == 0) { return false; } if (StrCmpi(name, "*") == 0) { return true; } if (IsSafeStr(name) == false) { return false; } if (StrCmpi(name, LINK_USER_NAME) == 0) { return false; } if (StartWith(name, L3_USERNAME)) { return false; } if (StrCmpi(name, LINK_USER_NAME_PRINT) == 0) { return false; } if (StrCmpi(name, SNAT_USER_NAME) == 0) { return false; } if (StrCmpi(name, SNAT_USER_NAME_PRINT) == 0) { return false; } if (StrCmpi(name, BRIDGE_USER_NAME) == 0) { return false; } if (StrCmpi(name, BRIDGE_USER_NAME_PRINT) == 0) { return false; } if (StrCmpi(name, ADMINISTRATOR_USERNAME) == 0) { return false; } return true; }
// Read the language list LIST *LoadLangList() { LIST *o = NewListFast(NULL); char *filename = LANGLIST_FILENAME; BUF *b; b = ReadDump(filename); if (b == NULL) { return NULL; } while (true) { char *line = CfgReadNextLine(b); if (line == NULL) { break; } Trim(line); if (IsEmptyStr(line) == false && StartWith(line, "#") == false) { TOKEN_LIST *t = ParseToken(line, "\t "); if (t != NULL) { if (t->NumTokens == 6) { LANGLIST *e = ZeroMalloc(sizeof(LANGLIST)); TOKEN_LIST *t2; e->Id = ToInt(t->Token[0]); StrCpy(e->Name, sizeof(e->Name), t->Token[1]); Utf8ToUni(e->TitleEnglish, sizeof(e->TitleEnglish), t->Token[2], StrLen(t->Token[2])); Utf8ToUni(e->TitleLocal, sizeof(e->TitleLocal), t->Token[3], StrLen(t->Token[3])); UniReplaceStrEx(e->TitleEnglish, sizeof(e->TitleEnglish), e->TitleEnglish, L"_", L" ", true); UniReplaceStrEx(e->TitleLocal, sizeof(e->TitleLocal), e->TitleLocal, L"_", L" ", true); e->LcidList = NewIntList(false); t2 = ParseToken(t->Token[4], ","); if (t2 != NULL) { UINT i; for (i = 0;i < t2->NumTokens;i++) { UINT id = ToInt(t2->Token[i]); AddIntDistinct(e->LcidList, id); } FreeToken(t2); } e->LangList = NewListFast(NULL); t2 = ParseToken(t->Token[5], ","); if (t2 != NULL) { UINT i; for (i = 0;i < t2->NumTokens;i++) { Add(e->LangList, CopyStr(t2->Token[i])); } FreeToken(t2); } Add(o, e); } FreeToken(t); } } Free(line); } FreeBuf(b); return o; }
// Get Ethernet device list on Linux TOKEN_LIST *GetEthListLinux() { struct ifreq ifr; TOKEN_LIST *t; UINT i, n; int s; LIST *o; char name[MAX_SIZE]; o = NewListFast(CompareStr); s = UnixEthOpenRawSocket(); if (s != INVALID_SOCKET) { n = 0; for (i = 0;;i++) { Zero(&ifr, sizeof(ifr)); ifr.ifr_ifindex = i; if (ioctl(s, SIOCGIFNAME, &ifr) >= 0) { n = 0; StrCpy(name, sizeof(name), ifr.ifr_name); Zero(&ifr, sizeof(ifr)); StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), name); if (ioctl(s, SIOCGIFHWADDR, &ifr) >= 0) { UINT type = ifr.ifr_hwaddr.sa_family; if (type == 1 || type == 2 || type == 6 || type == 800 || type == 801) { if (IsInListStr(o, name) == false) { if (StartWith(name, "tap_") == false) { Add(o, CopyStr(name)); } } } } } else { n++; if (n >= 64) { break; } } } closesocket(s); } Sort(o); t = ZeroMalloc(sizeof(TOKEN_LIST)); t->NumTokens = LIST_NUM(o); t->Token = ZeroMalloc(sizeof(char *) * t->NumTokens); for (i = 0;i < LIST_NUM(o);i++) { char *name = LIST_DATA(o, i); t->Token[i] = name; } ReleaseList(o); return t; }
ETH *OpenEthInternal(char *name, bool local, bool tapmode, char *tapaddr) { WP_ADAPTER *t; ETH *e; ADAPTER *a = NULL; HANDLE h; CANCEL *c; MS_ADAPTER *ms; char name_with_id[MAX_SIZE]; SU *su = NULL; SU_ADAPTER *su_adapter = NULL; // Validate arguments if (name == NULL || IsEthSupported() == false) { return NULL; } if (tapmode) { // Tap is not supported in Windows return NULL; } Lock(eth_list_lock); InitEthAdaptersList(); t = Win32EthSearch(name); if (t == NULL) { Unlock(eth_list_lock); return NULL; } Debug("OpenEthInternal: %s\n", t->Name); if (StartWith(t->Name, SL_ADAPTER_ID_PREFIX)) { // Open with SU su = SuInit(); if (su == NULL) { // Fail to initialize SU Unlock(eth_list_lock); return NULL; } su_adapter = SuOpenAdapter(su, t->Name); if (su_adapter == NULL) { // Fail to get adapter SuFree(su); Unlock(eth_list_lock); return NULL; } is_using_selow = true; } else { // Open with SEE a = wp->PacketOpenAdapter(t->Name); if (a == NULL) { Unlock(eth_list_lock); return NULL; } if (IsWin32BridgeWithSee() == false) { MsSetThreadSingleCpu(); } is_using_selow = false; } e = ZeroMalloc(sizeof(ETH)); e->Name = CopyStr(t->Name); Win32EthMakeCombinedName(name_with_id, sizeof(name_with_id), t->Title, t->Guid); e->Title = CopyStr(name_with_id); if (su_adapter != NULL) { // SU e->SuAdapter = su_adapter; e->Su = su; // Get event object h = e->SuAdapter->hEvent; c = NewCancelSpecial(h); e->Cancel = c; } else { // SEE e->Adapter = a; wp->PacketSetBuff(e->Adapter, BRIDGE_WIN32_ETH_BUFFER); wp->PacketSetHwFilter(e->Adapter, local ? 0x0080 : 0x0020); wp->PacketSetMode(e->Adapter, PACKET_MODE_CAPT); wp->PacketSetReadTimeout(e->Adapter, -1); wp->PacketSetNumWrites(e->Adapter, 1); if (wp->PacketSetLoopbackBehavior != NULL) { // Filter loopback packet in kernel if (GET_KETA(GetOsType(), 100) >= 3) { if (MsIsWindows8() == false) { // Enable for Windows XP, Server 2003 or later // But disable for Windows 8 or later bool ret = wp->PacketSetLoopbackBehavior(e->Adapter, 1); Debug("*** PacketSetLoopbackBehavior: %u\n", ret); e->LoopbackBlock = ret; } } } // Get event object h = wp->PacketGetReadEvent(e->Adapter); c = NewCancelSpecial(h); e->Cancel = c; e->Packet = wp->PacketAllocatePacket(); e->PutPacket = wp->PacketAllocatePacket(); } e->Buffer = Malloc(BRIDGE_WIN32_ETH_BUFFER); e->BufferSize = BRIDGE_WIN32_ETH_BUFFER; e->PacketQueue = NewQueue(); // Get MAC address by GUID ms = MsGetAdapterByGuid(t->Guid); if (ms != NULL) { if (ms->AddressSize == 6) { Copy(e->MacAddress, ms->Address, 6); } MsFreeAdapter(ms); } Unlock(eth_list_lock); return e; }
// Get the current iptables state IPTABLES_STATE *GetCurrentIpTables() { IPTABLES_STATE *ret = NULL; TOKEN_LIST *t = NULL; #ifdef OS_UNIX t = UnixExec("iptables -L -x -n --line-numbers"); #endif // OS_UNIX if (t != NULL) { UINT i; UINT tmp_num = 0; for (i = 0;i < t->NumTokens;i++) { char *line = t->Token[i]; if (StartWith(line, "Chain INPUT") || StartWith(line, "Chain FORWARD") || StartWith(line, "Chain OUTPUT")) { tmp_num++; } } if (tmp_num >= 3) { char current_chain[64]; UINT mode = 0; Zero(current_chain, sizeof(current_chain)); for (i = 0;i < t->NumTokens;i++) { char *line = t->Token[i]; if (StartWith(line, "Chain")) { TOKEN_LIST *t2 = ParseToken(line, " \t"); if (t2 != NULL) { if (t2->NumTokens >= 4) { StrCpy(current_chain, sizeof(current_chain), t2->Token[1]); mode = 1; if (ret == NULL) { ret = ZeroMalloc(sizeof(IPTABLES_STATE)); ret->EntryList = NewListFast(NULL); } } FreeToken(t2); } } if (mode == 1) { if (StartWith(line, "num")) { mode = 2; } } else if (mode == 2) { TOKEN_LIST *t2 = ParseToken(line, " \t"); if (t2 != NULL) { if (t2->NumTokens >= 6 && ToInt(t2->Token[0]) != 0) { IPTABLES_ENTRY *e = ZeroMalloc(sizeof(IPTABLES_ENTRY)); StrCpy(e->Chain, sizeof(e->Chain), current_chain); e->LineNumber = ToInt(t2->Token[0]); StrCpy(e->ConditionAndArgs, sizeof(e->ConditionAndArgs), line); Add(ret->EntryList, e); } FreeToken(t2); } } } } FreeToken(t); } return ret; }
// Read a string table from the buffer bool LoadTableFromBuf(BUF *b) { char *tmp; char prefix[MAX_SIZE]; LIST *replace_list = NULL; UINT i; // Validate arguments if (b == NULL) { return false; } // If the table already exists, delete it FreeTable(); // Create a list TableList = NewList(CmpTableName); Zero(prefix, sizeof(prefix)); replace_list = NewListFast(NULL); // Read the contents of the buffer line by line while (true) { TABLE *t; bool ok = true; tmp = CfgReadNextLine(b); if (tmp == NULL) { break; } if (tmp[0] == '$') { char key[128]; char value[MAX_SIZE]; if (GetKeyAndValue(tmp, key, sizeof(key), value, sizeof(value), " \t")) { if (StartWith(key, "$") && EndWith(key, "$") && StrLen(key) >= 3) { TABLE *t; wchar_t univalue[MAX_SIZE]; wchar_t uniname[MAX_SIZE]; t = ZeroMalloc(sizeof(TABLE)); Zero(univalue, sizeof(univalue)); Utf8ToUni(univalue, sizeof(univalue), value, StrLen(value)); StrToUni(uniname, sizeof(uniname), key); t->name = (char *)CopyUniStr(uniname); t->unistr = CopyUniStr(univalue); Add(replace_list, t); // Found a replacement definition ok = false; } } } if (ok) { t = ParseTableLine(tmp, prefix, sizeof(prefix), replace_list); if (t != NULL) { // Register Insert(TableList, t); } } Free(tmp); } for (i = 0;i < LIST_NUM(replace_list);i++) { TABLE *t = LIST_DATA(replace_list, i); Free(t->name); Free(t->str); Free(t->unistr); Free(t); } ReleaseList(replace_list); return true; }
/*--------------------------------------------------------------------------*/ bool IniFile::IsCommentLine(const string &str) { return StartWith(str, commentHead); }
// Check the name is valid for account name bool IsUserName(char *name) { UINT i, len; char tmp[MAX_SIZE]; // Validate arguments if (name == NULL) { return false; } StrCpy(tmp, sizeof(tmp), name); name = tmp; Trim(name); len = StrLen(name); if (len == 0) { return false; } if (StrCmpi(name, "*") == 0) { return true; } for (i = 0; i < len; i++) { if (IsSafeChar(name[i]) == false && name[i] != '@') { return false; } } if (StrCmpi(name, LINK_USER_NAME) == 0) { return false; } if (StartWith(name, L3_USERNAME)) { return false; } if (StrCmpi(name, LINK_USER_NAME_PRINT) == 0) { return false; } if (StrCmpi(name, SNAT_USER_NAME) == 0) { return false; } if (StrCmpi(name, SNAT_USER_NAME_PRINT) == 0) { return false; } if (StrCmpi(name, BRIDGE_USER_NAME) == 0) { return false; } if (StrCmpi(name, BRIDGE_USER_NAME_PRINT) == 0) { return false; } if (StrCmpi(name, ADMINISTRATOR_USERNAME) == 0) { return false; } return true; }