// Get the help string of command void GetCommandHelpStr(char *command_name, wchar_t **description, wchar_t **args, wchar_t **help) { char tmp1[128], tmp2[128], tmp3[128]; Format(tmp1, sizeof(tmp1), "CMD_%s", command_name); Format(tmp2, sizeof(tmp2), "CMD_%s_ARGS", command_name); Format(tmp3, sizeof(tmp3), "CMD_%s_HELP", command_name); if (description != NULL) { *description = _UU(tmp1); if (UniIsEmptyStr(*description)) { *description = _UU("CMD_UNKNOWM"); } } if (args != NULL) { *args = _UU(tmp2); if (UniIsEmptyStr(*args)) { *args = _UU("CMD_UNKNOWN_ARGS"); } } if (help != NULL) { *help = _UU(tmp3); if (UniIsEmptyStr(*help)) { *help = _UU("CMD_UNKNOWN_HELP"); } } }
wchar_t *FolderDlgInnerW(HWND hWnd, wchar_t *title, wchar_t *default_dir) { BROWSEINFOW info; wchar_t display_name[MAX_PATH]; FOLDER_DLG_INNER_DATA data; LPMALLOC pMalloc; wchar_t *ret = NULL; if (UniIsEmptyStr(title)) { title = NULL; } if (UniIsEmptyStr(default_dir)) { default_dir = NULL; } Zero(&data, sizeof(data)); data.default_dir = default_dir; Zero(display_name, sizeof(display_name)); Zero(&info, sizeof(info)); info.hwndOwner = hWnd; info.pidlRoot = NULL; info.pszDisplayName = display_name; info.lpszTitle = title; info.ulFlags = BIF_NEWDIALOGSTYLE | BIF_RETURNONLYFSDIRS | BIF_VALIDATE | BIF_SHAREABLE; info.lpfn = FolderDlgInnerCallbackW; info.lParam = (LPARAM)&data; if (SUCCEEDED(SHGetMalloc(&pMalloc))) { LPITEMIDLIST pidl; pidl = SHBrowseForFolderW(&info); if (pidl) { wchar_t tmp[MAX_PATH]; if (SHGetPathFromIDListW(pidl, tmp)) { ret = CopyUniStr(tmp); } pMalloc->Free(pidl); } pMalloc->Release(); } return ret; }
CFG_RW *NewCfgRwEx2W(FOLDER **root, wchar_t *cfg_name, bool dont_backup, wchar_t *template_name) { CFG_RW *rw; FOLDER *f; bool loaded_from_template = false; // Validate arguments if (cfg_name == NULL || root == NULL) { return NULL; } f = CfgReadW(cfg_name); if (f == NULL) { // Load from template if (UniIsEmptyStr(template_name) == false) { f = CfgReadW(template_name); if (f != NULL) { loaded_from_template = true; goto LABEL_CONTIUNE; } } rw = ZeroMalloc(sizeof(CFG_RW)); rw->lock = NewLock(); rw->FileNameW = CopyUniStr(cfg_name); rw->FileName = CopyUniToStr(cfg_name); rw->Io = FileCreateW(cfg_name); *root = NULL; rw->DontBackup = dont_backup; return rw; } LABEL_CONTIUNE: rw = ZeroMalloc(sizeof(CFG_RW)); rw->FileNameW = CopyUniStr(cfg_name); rw->FileName = CopyUniToStr(cfg_name); if (loaded_from_template == false) { rw->Io = FileOpenW(cfg_name, false); } else { rw->Io = FileCreateW(cfg_name); } rw->lock = NewLock(); *root = f; rw->DontBackup = dont_backup; return rw; }
// Evaluation of the parameters that a blank cannot be specified to bool CmdEvalNotEmpty(CONSOLE *c, wchar_t *str, void *param) { wchar_t *p = (param == NULL) ? _UU("CMD_EVAL_NOT_EMPTY") : (wchar_t *)param; if (UniIsEmptyStr(str) == false) { return true; } c->Write(c, p); return false; }
// Create a root certification authentication data void *NewRootCertAuthData(X_SERIAL *serial, wchar_t *common_name) { AUTHROOTCERT *a; a = ZeroMallocEx(sizeof(AUTHROOTCERT), true); if (common_name != NULL && UniIsEmptyStr(common_name) == false) { a->CommonName = CopyUniStr(common_name); } if (serial != NULL && serial->size >= 1) { a->Serial = CloneXSerial(serial); } return a; }
// Get the help string for parameter void GetCommandParamHelpStr(char *command_name, char *param_name, wchar_t **description) { char tmp[160]; if (description == NULL) { return; } Format(tmp, sizeof(tmp), "CMD_%s_%s", command_name, param_name); *description = _UU(tmp); if (UniIsEmptyStr(*description)) { *description = _UU("CMD_UNKNOWN_PARAM"); } }
// Get the current installation state for the given architecture void ViLoadCurrentInstalledStatusForArch(VI_SETTING_ARCH *a) { char tmp[MAX_SIZE]; UINT build; wchar_t *dir; // Validate arguments if (a == NULL) { return; } if (a->Supported == false) { // Unsupported return; } // Read from the registry Format(tmp, sizeof(tmp), "%s\\%s", SW_REG_KEY, "vpnclient"); build = MsRegReadIntEx2(REG_LOCAL_MACHINE, tmp, "InstalledBuild", false, true); dir = MsRegReadStrEx2W(REG_LOCAL_MACHINE, tmp, "InstalledDir", false, true); if (build == 0 || UniIsEmptyStr(dir)) { // Not installed a->CurrentInstalled = false; } else { // Installed a->CurrentInstalled = true; a->CurrentInstalledBuild = build; UniStrCpy(a->CurrentInstalledPathW, sizeof(a->CurrentInstalledPathW), dir); } Free(dir); }
// Add a UPnP port bool Win32UPnPAddPort(UINT outside_port, UINT inside_port, bool udp, char *local_ip, wchar_t *description, bool remove_before_add) { bool ret = false; HRESULT hr; IUPnPNAT *nat = NULL; wchar_t ip_str[MAX_SIZE]; BSTR bstr_ip, bstr_description, bstr_protocol; wchar_t *protocol_str = (udp ? L"UDP" : L"TCP"); // Validate arguments if (outside_port == 0 || outside_port >= 65536 || inside_port == 0 || inside_port >= 65536 || IsEmptyStr(local_ip) || UniIsEmptyStr(description)) { return false; } StrToUni(ip_str, sizeof(ip_str), local_ip); bstr_ip = SysAllocString(ip_str); bstr_description = SysAllocString(description); bstr_protocol = SysAllocString(protocol_str); hr = CoCreateInstance(CLSID_UPnPNAT, NULL, CLSCTX_INPROC_SERVER, IID_IUPnPNAT, (void **)&nat); if (SUCCEEDED(hr)) { if (nat != NULL) { IStaticPortMappingCollection *collection = NULL; hr = nat->get_StaticPortMappingCollection(&collection); if (SUCCEEDED(hr)) { if (collection != NULL) { IStaticPortMapping *mapping = NULL; if (remove_before_add) { hr = collection->Remove((long)outside_port, bstr_protocol); } hr = collection->Add((long)outside_port, bstr_protocol, (long)inside_port, bstr_ip, VARIANT_TRUE, bstr_description, &mapping); if (SUCCEEDED(hr)) { ret = true; if (mapping != NULL) { mapping->Release(); } } collection->Release(); } else { WHERE; } } else { WHERE; } nat->Release(); } else { WHERE; } } else { WHERE; } SysFreeString(bstr_ip); SysFreeString(bstr_description); SysFreeString(bstr_protocol); return ret; }
// Certificate authentication of user bool SamAuthUserByCert(HUB *h, char *username, X *x) { bool b = false; // Validate arguments if (h == NULL || username == NULL || x == NULL) { return false; } if (GetGlobalServerFlag(GSF_DISABLE_CERT_AUTH) != 0) { return false; } // Check expiration date if (CheckXDateNow(x) == false) { return false; } // Check the Certification Revocation List if (IsValidCertInHub(h, x) == false) { // Bad wchar_t tmp[MAX_SIZE * 2]; // Log the contents of the certificate GetAllNameFromX(tmp, sizeof(tmp), x); HLog(h, "LH_AUTH_NG_CERT", username, tmp); return false; } AcLock(h); { USER *u; u = AcGetUser(h, username); if (u) { Lock(u->lock); { if (u->AuthType == AUTHTYPE_USERCERT) { // Check whether to matche with the registered certificate AUTHUSERCERT *auth = (AUTHUSERCERT *)u->AuthData; if (CompareX(auth->UserX, x)) { b = true; } } else if (u->AuthType == AUTHTYPE_ROOTCERT) { // Check whether the certificate has been signed by the root certificate AUTHROOTCERT *auth = (AUTHROOTCERT *)u->AuthData; if (h->HubDb != NULL) { LockList(h->HubDb->RootCertList); { X *root_cert; root_cert = GetIssuerFromList(h->HubDb->RootCertList, x); if (root_cert != NULL) { b = true; if (auth->CommonName != NULL && UniIsEmptyStr(auth->CommonName) == false) { // Compare the CN if (UniStrCmpi(x->subject_name->CommonName, auth->CommonName) != 0) { b = false; } } if (auth->Serial != NULL && auth->Serial->size >= 1) { // Compare the serial number if (CompareXSerial(x->serial, auth->Serial) == false) { b = false; } } } } UnlockList(h->HubDb->RootCertList); } } } Unlock(u->lock); ReleaseUser(u); } } AcUnlock(h); if (b) { wchar_t tmp[MAX_SIZE * 2]; // Log the contents of the certificate GetAllNameFromX(tmp, sizeof(tmp), x); HLog(h, "LH_AUTH_OK_CERT", username, tmp); } return b; }
// Plaintext password authentication of user bool SamAuthUserByPlainPassword(CONNECTION *c, HUB *hub, char *username, char *password, bool ast, UCHAR *mschap_v2_server_response_20, RADIUS_LOGIN_OPTION *opt) { bool b = false; wchar_t *name = NULL; bool auth_by_nt = false; HUB *h; // Validate arguments if (hub == NULL || c == NULL || username == NULL) { return false; } if (GetGlobalServerFlag(GSF_DISABLE_RADIUS_AUTH) != 0) { return false; } h = hub; AddRef(h->ref); // Get the user name on authentication system AcLock(hub); { USER *u; u = AcGetUser(hub, ast == false ? username : "******"); if (u) { Lock(u->lock); { if (u->AuthType == AUTHTYPE_RADIUS) { // Radius authentication AUTHRADIUS *auth = (AUTHRADIUS *)u->AuthData; if (ast || auth->RadiusUsername == NULL || UniStrLen(auth->RadiusUsername) == 0) { name = CopyStrToUni(username); } else { name = CopyUniStr(auth->RadiusUsername); } auth_by_nt = false; } else if (u->AuthType == AUTHTYPE_NT) { // NT authentication AUTHNT *auth = (AUTHNT *)u->AuthData; if (ast || auth->NtUsername == NULL || UniStrLen(auth->NtUsername) == 0) { name = CopyStrToUni(username); } else { name = CopyUniStr(auth->NtUsername); } auth_by_nt = true; } } Unlock(u->lock); ReleaseUser(u); } } AcUnlock(hub); if (name != NULL) { if (auth_by_nt == false) { // Radius authentication char radius_server_addr[MAX_SIZE]; UINT radius_server_port; char radius_secret[MAX_SIZE]; char suffix_filter[MAX_SIZE]; wchar_t suffix_filter_w[MAX_SIZE]; UINT interval; Zero(suffix_filter, sizeof(suffix_filter)); Zero(suffix_filter_w, sizeof(suffix_filter_w)); // Get the Radius server information if (GetRadiusServerEx2(hub, radius_server_addr, sizeof(radius_server_addr), &radius_server_port, radius_secret, sizeof(radius_secret), &interval, suffix_filter, sizeof(suffix_filter))) { Unlock(hub->lock); StrToUni(suffix_filter_w, sizeof(suffix_filter_w), suffix_filter); if (UniIsEmptyStr(suffix_filter_w) || UniEndWith(name, suffix_filter_w)) { // Attempt to login b = RadiusLogin(c, radius_server_addr, radius_server_port, radius_secret, StrLen(radius_secret), name, password, interval, mschap_v2_server_response_20, opt); } Lock(hub->lock); } else { HLog(hub, "LH_NO_RADIUS_SETTING", name); } } else { // NT authentication (Not available for non-Win32) #ifdef OS_WIN32 IPC_MSCHAP_V2_AUTHINFO mschap; Unlock(hub->lock); if (ParseAndExtractMsChapV2InfoFromPassword(&mschap, password) == false) { // Plaintext password authentication b = MsCheckLogon(name, password); } else { UCHAR challenge8[8]; UCHAR nt_pw_hash_hash[16]; char nt_name[MAX_SIZE]; UniToStr(nt_name, sizeof(nt_name), name); // MS-CHAPv2 authentication MsChapV2_GenerateChallenge8(challenge8, mschap.MsChapV2_ClientChallenge, mschap.MsChapV2_ServerChallenge, mschap.MsChapV2_PPPUsername); Debug("MsChapV2_PPPUsername = %s, nt_name = %s\n", mschap.MsChapV2_PPPUsername, nt_name); b = MsPerformMsChapV2AuthByLsa(nt_name, challenge8, mschap.MsChapV2_ClientResponse, nt_pw_hash_hash); if (b) { if (mschap_v2_server_response_20 != NULL) { MsChapV2Server_GenerateResponse(mschap_v2_server_response_20, nt_pw_hash_hash, mschap.MsChapV2_ClientResponse, challenge8); } } } Lock(hub->lock); #else // OS_WIN32 // Nothing to do other than Win32 #endif // OS_WIN32 } // Memory release Free(name); } ReleaseHub(h); return b; }
// Create a Link LINK *NewLink(CEDAR *cedar, HUB *hub, CLIENT_OPTION *option, CLIENT_AUTH *auth, POLICY *policy) { CLIENT_OPTION *o; LINK *k; CLIENT_AUTH *a; // Validate arguments if (cedar == NULL || hub == NULL || option == NULL || auth == NULL || policy == NULL) { return NULL; } if (hub->Halt) { return NULL; } if (LIST_NUM(hub->LinkList) >= MAX_HUB_LINKS) { return NULL; } if (UniIsEmptyStr(option->AccountName)) { return NULL; } // Limitation of authentication method if (auth->AuthType != CLIENT_AUTHTYPE_ANONYMOUS && auth->AuthType != CLIENT_AUTHTYPE_PASSWORD && auth->AuthType != CLIENT_AUTHTYPE_PLAIN_PASSWORD && auth->AuthType != CLIENT_AUTHTYPE_CERT) { // Authentication method other than anonymous authentication, password authentication, plain password, certificate authentication cannot be used return NULL; } // Copy of the client options (for modification) o = ZeroMalloc(sizeof(CLIENT_OPTION)); Copy(o, option, sizeof(CLIENT_OPTION)); StrCpy(o->DeviceName, sizeof(o->DeviceName), LINK_DEVICE_NAME); o->RequireBridgeRoutingMode = true; // Request the bridge mode o->RequireMonitorMode = false; // Not to require the monitor mode o->NumRetry = INFINITE; // Retry the connection infinitely o->RetryInterval = 10; // Retry interval is 10 seconds o->NoRoutingTracking = true; // Stop the routing tracking // Copy the authentication data a = CopyClientAuth(auth); a->SecureSignProc = NULL; a->CheckCertProc = NULL; // Link object k = ZeroMalloc(sizeof(LINK)); k->lock = NewLock(); k->ref = NewRef(); k->Cedar = cedar; k->Option = o; k->Auth = a; k->Hub = hub; // Copy the policy k->Policy = ZeroMalloc(sizeof(POLICY)); Copy(k->Policy, policy, sizeof(POLICY)); // Normalize the policy NormalizeLinkPolicy(k->Policy); // Register in the link list of the HUB LockList(hub->LinkList); { Add(hub->LinkList, k); AddRef(k->ref); } UnlockList(hub->LinkList); return k; }
// 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; }
wchar_t *ParseCommandEx(wchar_t *str, wchar_t *name, TOKEN_LIST **param_list) { UNI_TOKEN_LIST *t; UINT i; wchar_t *tmp; wchar_t *ret = NULL; LIST *o; // Validate arguments if (str == NULL) { return NULL; } if (name != NULL && UniIsEmptyStr(name)) { name = NULL; } o = NULL; if (param_list != NULL) { o = NewListFast(CompareStr); } tmp = CopyUniStr(str); UniTrim(tmp); i = UniSearchStrEx(tmp, L"/CMD ", 0, false); if (i != INFINITE && i >= 1 && tmp[i - 1] == L'/') { i = INFINITE; } if (i == INFINITE) { i = UniSearchStrEx(tmp, L"/CMD\t", 0, false); if (i != INFINITE && i >= 1 && tmp[i - 1] == L'/') { i = INFINITE; } } if (i == INFINITE) { i = UniSearchStrEx(tmp, L"/CMD:", 0, false); if (i != INFINITE && i >= 1 && tmp[i - 1] == L'/') { i = INFINITE; } } if (i == INFINITE) { i = UniSearchStrEx(tmp, L"/CMD=", 0, false); if (i != INFINITE && i >= 1 && tmp[i - 1] == L'/') { i = INFINITE; } } if (i == INFINITE) { i = UniSearchStrEx(tmp, L"-CMD ", 0, false); if (i != INFINITE && i >= 1 && tmp[i - 1] == L'-') { i = INFINITE; } } if (i == INFINITE) { i = UniSearchStrEx(tmp, L"-CMD\t", 0, false); if (i != INFINITE && i >= 1 && tmp[i - 1] == L'-') { i = INFINITE; } } if (i == INFINITE) { i = UniSearchStrEx(tmp, L"-CMD:", 0, false); if (i != INFINITE && i >= 1 && tmp[i - 1] == L'-') { i = INFINITE; } } if (i == INFINITE) { i = UniSearchStrEx(tmp, L"-CMD=", 0, false); if (i != INFINITE && i >= 1 && tmp[i - 1] == L'-') { i = INFINITE; } } if (i != INFINITE) { char *s = CopyStr("CMD"); if (InsertStr(o, s) == false) { Free(s); } if (UniStrCmpi(name, L"CMD") == 0) { ret = CopyUniStr(&str[i + 5]); UniTrim(ret); } else { tmp[i] = 0; } } if (ret == NULL) { t = UniParseCmdLine(tmp); if (t != NULL) { for (i = 0;i < t->NumTokens;i++) { wchar_t *token = t->Token[i]; if ((token[0] == L'-' && token[1] != L'-') || (UniStrCmpi(token, L"--help") == 0) || (token[0] == L'/' && token[1] != L'/')) { UINT i; // Named parameter // Examine whether there is a colon character if (UniStrCmpi(token, L"--help") == 0) { token++; } i = UniSearchStrEx(token, L":", 0, false); if (i == INFINITE) { i = UniSearchStrEx(token, L"=", 0, false); } if (i != INFINITE) { wchar_t *tmp; char *a; // There is a colon character tmp = CopyUniStr(token); tmp[i] = 0; a = CopyUniToStr(&tmp[1]); if (InsertStr(o, a) == false) { Free(a); } if (UniStrCmpi(name, &tmp[1]) == 0) { if (ret == NULL) { // Content ret = UniCopyStr(&token[i + 1]); } } Free(tmp); } else { // There is no colon character char *a; a = CopyUniToStr(&token[1]); if (InsertStr(o, a) == false) { Free(a); } if (UniStrCmpi(name, &token[1]) == 0) { if (ret == NULL) { // Empty character ret = UniCopyStr(L""); } } } } else { // Nameless argument if (name == NULL) { if (ret == NULL) { if (token[0] == L'-' && token[1] == L'-') { ret = UniCopyStr(&token[1]); } else if (token[0] == L'/' && token[1] == L'/') { ret = UniCopyStr(&token[1]); } else { ret = UniCopyStr(token); } } } } } UniFreeToken(t); } } Free(tmp); if (o != NULL) { TOKEN_LIST *t = ZeroMalloc(sizeof(TOKEN_LIST)); UINT i; t->NumTokens = LIST_NUM(o); t->Token = ZeroMalloc(sizeof(char *) * t->NumTokens); for (i = 0;i < t->NumTokens;i++) { t->Token[i] = LIST_DATA(o, i); } ReleaseList(o); *param_list = t; } if (UniStrCmpi(ret, L"none") == 0 || UniStrCmpi(ret, L"null") == 0) { // Null and none are reserved words ret[0] = 0; } return ret; }
// Separate the command line into the command and the parameters bool SeparateCommandAndParam(wchar_t *src, char **cmd, wchar_t **param) { UINT i, len, wp; wchar_t *tmp; wchar_t *src_tmp; // Validate arguments if (src == NULL) { return false; } if (cmd != NULL) { *cmd = NULL; } if (param != NULL) { *param = NULL; } src_tmp = UniCopyStr(src); UniTrimCrlf(src_tmp); UniTrim(src_tmp); len = UniStrLen(src_tmp); tmp = Malloc(sizeof(wchar_t) * (len + 32)); wp = 0; for (i = 0;i < (len + 1);i++) { wchar_t c = src_tmp[i]; switch (c) { case 0: case L' ': case L'\t': tmp[wp] = 0; if (UniIsEmptyStr(tmp)) { Free(tmp); Free(src_tmp); return false; } if (cmd != NULL) { *cmd = CopyUniToStr(tmp); Trim(*cmd); } goto ESCAPE; default: tmp[wp++] = c; break; } } ESCAPE: if (param != NULL) { *param = CopyUniStr(&src_tmp[wp]); UniTrim(*param); } Free(tmp); Free(src_tmp); return true; }
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; }
// 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); }
// リンクの作成 LINK *NewLink(CEDAR *cedar, HUB *hub, CLIENT_OPTION *option, CLIENT_AUTH *auth, POLICY *policy) { CLIENT_OPTION *o; LINK *k; CLIENT_AUTH *a; // 引数チェック if (cedar == NULL || hub == NULL || option == NULL || auth == NULL || policy == NULL) { return NULL; } if (hub->Halt) { return NULL; } if (LIST_NUM(hub->LinkList) >= MAX_HUB_LINKS) { return NULL; } if (UniIsEmptyStr(option->AccountName)) { return NULL; } // 認証方法の制限 if (auth->AuthType != CLIENT_AUTHTYPE_ANONYMOUS && auth->AuthType != CLIENT_AUTHTYPE_PASSWORD && auth->AuthType != CLIENT_AUTHTYPE_PLAIN_PASSWORD && auth->AuthType != CLIENT_AUTHTYPE_CERT) { // 匿名認証、パスワード認証、プレーンパスワード、証明書認証以外の認証方法は使用できない return NULL; } // クライアントオプションのコピー (改変用) o = ZeroMalloc(sizeof(CLIENT_OPTION)); Copy(o, option, sizeof(CLIENT_OPTION)); StrCpy(o->DeviceName, sizeof(o->DeviceName), LINK_DEVICE_NAME); o->RequireBridgeRoutingMode = true; // ブリッジモードを要求する o->RequireMonitorMode = false; // モニタモードは要求しない o->NumRetry = INFINITE; // 接続の再試行は無限に行う o->RetryInterval = 10; // 再試行間隔は 10 秒 o->NoRoutingTracking = true; // ルーティング追跡停止 // 認証データのコピー a = CopyClientAuth(auth); a->SecureSignProc = NULL; a->CheckCertProc = NULL; // リンク オブジェクト k = ZeroMalloc(sizeof(LINK)); k->lock = NewLock(); k->ref = NewRef(); k->Cedar = cedar; k->Option = o; k->Auth = a; k->Hub = hub; // ポリシーのコピー k->Policy = ZeroMalloc(sizeof(POLICY)); Copy(k->Policy, policy, sizeof(POLICY)); // ポリシーの正規化 NormalizeLinkPolicy(k->Policy); // HUB のリンクリストに登録する LockList(hub->LinkList); { Add(hub->LinkList, k); AddRef(k->ref); } UnlockList(hub->LinkList); return k; }