SymKey *DekClient::decrypt(string alias, EncKey *key) { printf("DekClient::decrypt\n"); int sock = doConnect(); if(sock < 0) { printf("Cannot connect\n"); return false; } shared_ptr<SerializedItem> sKey(key->serialize()); sKey->dump("DekClient::decrypt"); string cmd = "0 enc " + std::to_string(CommandCode::CommandDecrypt) + " " + alias + " " + sKey->toString(); printf("cmd :: %s\n", cmd.c_str()); write(sock, cmd.c_str(), cmd.size() + 1); shared_ptr<DaemonEvent> event(monitor(sock)); close(sock); if(event == NULL) { printf("Failed to encrypt : %d\n", __LINE__); return NULL; } if(event->code == ResponseCode::CommandOkay) { int alg = std::stoi(event->message[0]); if(alg != CryptAlg::PLAIN) { printf("Failed to decrypt : result is not plain text\n"); return NULL; } shared_ptr<SerializedItem> sItem( new SerializedItem(CryptAlg::PLAIN, event->message[1].c_str(), "?", "?")); return (SymKey *) sItem->deserialize(); } else { printf("Failed to decrypt [%d]\n", (event == NULL) ? ResponseCode::CommandFailed : event->code); return NULL; } return NULL; }
bool FileMisc::DeleteFolderContents(const TCHAR* szFolder, BOOL bIncludeSubFolders, const TCHAR* szFileMask, HANDLE hTerminate, BOOL bProcessMsgLoop) { // if the dir does not exists just return if (!FolderExists(szFolder)) { return true; } // if a file mask has been specified with subfolders we need to do 2 passes on each folder, // one for the files and one for the sub folders int nPasses = (bIncludeSubFolders && (szFileMask && lstrlen(szFileMask))) ? 2 : 1; bool bResult = true; bool bStopped = (WaitForSingleObject(hTerminate, 0) == WAIT_OBJECT_0); for (int nPass = 0; !bStopped && nPass < nPasses; nPass++) { CString sSearchSpec(szFolder), sMask(szFileMask); if (sMask.IsEmpty() || nPass == 1) // (nPass == 1) == 2nd pass (for folders) { sMask = "*.*"; } TerminatePath(sSearchSpec); sSearchSpec += sMask; WIN32_FIND_DATA finfo; HANDLE hSearch = NULL; if ((hSearch = FindFirstFile(sSearchSpec, &finfo)) != INVALID_HANDLE_VALUE) { do { if (bProcessMsgLoop) { Misc::ProcessMsgLoop(); } if (finfo.cFileName[0] != '.') { CString sItem(szFolder); sItem += "\\"; sItem += finfo.cFileName; if (finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (bIncludeSubFolders && (nPass == 1 || nPasses == 1)) { if (DeleteFolderContents(sItem, TRUE, szFileMask, hTerminate, bProcessMsgLoop)) { if (!szFileMask || !lstrlen(szFileMask)) { bResult = (RemoveDirectory(sItem) == TRUE); } } } } else { bResult = (DeleteFile(sItem) == TRUE); } } bStopped = (WaitForSingleObject(hTerminate, 0) == WAIT_OBJECT_0); } while (!bStopped && bResult && FindNextFile(hSearch, &finfo)); FindClose(hSearch); } } return (!bStopped && bResult); }