#include "stdafx.h" #include "OTSLicMgr.h" #include "resource.h" #include "OTSFileSys.h" #include "OTSHelper.h" #include "OTSCrypt.h" #include "COTSUtilityDllFunExport.h" #include "MultiLang.h" namespace OTSMODEL { // COTSLicenseFile class // OTS license/license key file IMPLEMENT_SERIAL(COTSLicenseFile, CObject, 1) // constructor COTSLicenseFile::COTSLicenseFile() { Init(); } // copy constructor COTSLicenseFile::COTSLicenseFile(COTSLicenseFile* a_poSource) { // input check ASSERT(a_poSource); if (!a_poSource) { return; } // can't copy itself if (a_poSource == this) { return; } // copy data over Duplicate(*a_poSource); } // copy constructor COTSLicenseFile::COTSLicenseFile(const COTSLicenseFile& a_oSource) { // can't copy itself if (&a_oSource == this) { return; } // copy data over Duplicate(a_oSource); } // =operator COTSLicenseFile& COTSLicenseFile::operator=(const COTSLicenseFile& a_oSource) { // cleanup Cleanup(); // copy the class data over Duplicate(a_oSource); // return class return *this; } // detractor COTSLicenseFile::~COTSLicenseFile() { // cleanup Cleanup(); } // COTSLicenseFile member functions void COTSLicenseFile::SetLicenseInfo(COTSLicenseInfoPtr a_pLicenseInfo) { ASSERT(a_pLicenseInfo); if (!a_pLicenseInfo) { return; } m_pLicenseInfo = COTSLicenseInfoPtr(new COTSLicenseInfo(a_pLicenseInfo.get())); } // Load/Save // NOTE: be sure a_strPathName is a valid file pathname BOOL COTSLicenseFile::Load(CString a_strPathName) { // check the file pathname a_strPathName.Trim(); if (a_strPathName.IsEmpty()) { LogErrorTrace(__FILE__, __LINE__, _T("Load: file pathname string is empty.")); return FALSE; } // open the license file CFile hFile; CFileException ex; if (!hFile.Open(a_strPathName, CFile::modeRead, &ex)) { // failed to open the file TCHAR szCause[255]; ex.GetErrorMessage(szCause, 255); LogErrorTrace(__FILE__, __LINE__, _T("Load: can't open file %s. error: %s"), a_strPathName, szCause); return FALSE; } // create license info point m_pLicenseInfo = COTSLicenseInfoPtr(new COTSLicenseInfo()); // create a loading archive CArchive ar(&hFile, CArchive::load); // file serialization (load) Serialize(ar); ar.Close(); // file pathname m_strPathName = a_strPathName; // ok, return TRUE return TRUE; } BOOL COTSLicenseFile::Save(CString a_strPathName) { // check the file pathname a_strPathName.Trim(); if (a_strPathName.IsEmpty()) { LogErrorTrace(__FILE__, __LINE__, _T("Save: file pathname string is empty.")); return FALSE; } // can do nothing if there is no license file info ASSERT(m_pLicenseInfo); if (!m_pLicenseInfo) { LogErrorTrace(__FILE__, __LINE__, _T("Save: invalid license file")); return FALSE; } // get path of the file CString strFilePath = COTSHelper::GetFolderName(a_strPathName); if (!strFilePath.IsEmpty()) { // check if file folder exists if (!COTSFileSys::Exists(strFilePath)) { // create the file folder if (!COTSFileSys::CreateFolder(strFilePath)) { // failed to create the file folder LogErrorTrace(__FILE__, __LINE__, _T("Save: failed to create file folder. %s"), strFilePath); return FALSE; } } } // Open the file CFile hFile; CFileException ex; if (!hFile.Open(a_strPathName, CFile::modeCreate | CFile::modeWrite, &ex)) { // failed to open the file TCHAR szCause[255]; ex.GetErrorMessage(szCause, 255); LogErrorTrace(__FILE__, __LINE__, _T("Save: can't open file %s. error: %s"), a_strPathName, szCause); return FALSE; } // store to file CArchive ar(&hFile, CArchive::store); // file serialization (store) Serialize(ar); ar.Close(); // file pathname m_strPathName = a_strPathName; // ok, return TRUE return TRUE; } // serialization CString strLicense; void COTSLicenseFile::Serialize(CArchive& ar) { // store? if (ar.IsStoring()) { // store ar << LICENNSE_FILE_MARK; ar << LICENNSE_FILE_VERSION; // license string strLicense = COTSLicMgr::EncryptLicenseInfo(m_pLicenseInfo); ar << strLicense; } else { // load // check if file mark int nFileNark; ar >> nFileNark; if (nFileNark != LICENNSE_FILE_MARK) { // invalid license file LogErrorTrace(__FILE__, __LINE__, _T("Serialize: invalid license file.")); return; } // file version string; CString strFileVersion; ar >> strFileVersion; // license string CString strLicense; ar >> strLicense; m_pLicenseInfo = COTSLicMgr::DecryptLicenseInfo(strLicense); } // base object serialization CObject::Serialize(ar); } void COTSLicenseFile::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode) { xmls::xInt xLicennseFileMark; xmls::xString xLicennseFileVersion; xmls::xString xstrLicense; this->Register("LicennseFileMark", &xLicennseFileMark); this->Register("LicennseFileVersion", &xLicennseFileVersion); this->Register("strLicense", &xstrLicense); if (isStoring) { xLicennseFileMark = LICENNSE_FILE_MARK; xLicennseFileVersion = LICENNSE_FILE_VERSION; xstrLicense = strLicense; Slo::Serialize(true, classDoc, rootNode); } else { xmls::Slo::Serialize(false, classDoc, rootNode); strLicense = xstrLicense.value().c_str(); } } // cleanup void COTSLicenseFile::Cleanup() { } // initialization void COTSLicenseFile::Init() { m_strPathName = _T(""); } // duplication void COTSLicenseFile::Duplicate(const COTSLicenseFile& a_oSource) { // initialization Init(); // copy data over m_pLicenseInfo = COTSLicenseInfoPtr(new COTSLicenseInfo((a_oSource.m_pLicenseInfo).get())); m_strPathName = a_oSource.m_strPathName; } // COTSLicInfoFile class // license manager app file IMPLEMENT_SERIAL(COTSLicInfoFile, CObject, 1) // constructor COTSLicInfoFile::COTSLicInfoFile() { Init(); } // copy constructor COTSLicInfoFile::COTSLicInfoFile(const COTSLicInfoFile& a_oSource) { // can't copy itself if (&a_oSource == this) { return; } // copy data over Duplicate(a_oSource); } // copy constructor COTSLicInfoFile::COTSLicInfoFile(COTSLicInfoFile* a_poSource) { // input check ASSERT(a_poSource); if (!a_poSource) { return; } // can't copy itself if (a_poSource == this) { return; } // copy data over Duplicate(*a_poSource); } // =operator COTSLicInfoFile& COTSLicInfoFile::operator=(const COTSLicInfoFile& a_oSource) { // cleanup Cleanup(); // copy the class data over Duplicate(a_oSource); // return class return *this; } // detractor COTSLicInfoFile::~COTSLicInfoFile() { // cleanup Cleanup(); } // COTSLicInfoFile member functions // Load/Save // NOTE: be sure a_strPathName is a valid file pathname BOOL COTSLicInfoFile::Load(CString a_strPathName) { // open the license file CFile hFile; CFileException ex; if (!hFile.Open(a_strPathName, CFile::modeRead, &ex)) { // failed to open the file TCHAR szCause[255]; ex.GetErrorMessage(szCause, 255); LogErrorTrace(__FILE__, __LINE__, _T("Load: can't open file %s. error: %s"), a_strPathName, szCause); return FALSE; } // create a loading archive CArchive ar(&hFile, CArchive::load); m_listLicenseInfo.clear(); Serialize(ar); ar.Close(); // file pathname m_strPathName = a_strPathName; // ok, return TRUE return TRUE; } BOOL COTSLicInfoFile::Save() { // check if file pathname is empty m_strPathName.Trim(); if (m_strPathName.IsEmpty()) { LogErrorTrace(__FILE__, __LINE__, _T("Save: file pathname is empty.")); return FALSE; } // Open the file CFile hFile; CFileException ex; if (!hFile.Open(m_strPathName, CFile::modeCreate | CFile::modeWrite, &ex)) { // failed to open the file TCHAR szCause[255]; ex.GetErrorMessage(szCause, 255); LogErrorTrace(__FILE__, __LINE__, _T("Save: can't open file %s. error: %s"), m_strPathName, szCause); return FALSE; } // store to file CArchive ar(&hFile, CArchive::store); Serialize(ar); ar.Close(); // ok, return TRUE return TRUE; } // serialization void COTSLicInfoFile::Serialize(CArchive& ar) { // store? if (ar.IsStoring()) { // store ar << LICENSE_INFO_FILE_MARK; ar << LICENSE_InFO_FILE_VERSION; ar << (int)m_listLicenseInfo.size(); for (auto pLicenseInfo : m_listLicenseInfo) { pLicenseInfo->Serialize(ar); } } else { // load // check if file mark int nFileNark; ar >> nFileNark; if (nFileNark != LICENSE_INFO_FILE_MARK) { // invalid license file LogErrorTrace(__FILE__, __LINE__, _T("Serialize: invalid license info file.")); return; } // file version string; CString strFileVersion; ar >> strFileVersion; // license info class number int nSize = 0; ar >> nSize; for (int i = 0; i < nSize; ++i) { COTSLicenseInfoPtr pLicenseInfo(new COTSLicenseInfo()); pLicenseInfo->Serialize(ar); m_listLicenseInfo.push_back(pLicenseInfo); } } // base object serialization CObject::Serialize(ar); } // cleanup void COTSLicInfoFile::Cleanup() { } // initialization void COTSLicInfoFile::Init() { m_listLicenseInfo.clear(); m_bModify = FALSE; } // duplication void COTSLicInfoFile::Duplicate(const COTSLicInfoFile& a_oSource) { // initialization Init(); // copy data over for (auto pLicenseInfoSource : a_oSource.m_listLicenseInfo) { COTSLicenseInfoPtr pLicenseInfoNew = COTSLicenseInfoPtr(new COTSLicenseInfo(pLicenseInfoSource.get())); m_listLicenseInfo.push_back(pLicenseInfoNew); } m_strPathName = a_oSource.m_strPathName; } // constructor COTSLicMgr::COTSLicMgr() : m_nPackId(OTS_SOFT_PACKAGE_ID::INVALID) , m_nLicStatus(OTS_LICENSE_STATUS::INVALID) , m_pOTSLicenseFile(nullptr) { MultiLang::GetInstance().LoadStringFromXml(); } // destructor COTSLicMgr::~COTSLicMgr() { } // COTSLicMgr member functions // check if there is valid license of the given software package BOOL COTSLicMgr::IsThereValidPackLicense(OTS_SOFT_PACKAGE_ID a_nPackId) { // make sure software package id is valid if (a_nPackId == OTS_SOFT_PACKAGE_ID::INVALID) { LogErrorTrace(__FILE__, __LINE__, _T("IsThereValidPackLicense: invalid software package id. %d"), (int)a_nPackId); return FALSE; } // get software package license file pathname CString strFilePathName = GetPackLicenseFilePathName(a_nPackId); strFilePathName.Trim(); if (strFilePathName.IsEmpty()) { LogErrorTrace(__FILE__, __LINE__, _T("IsThereValidPackLicense: failed to find software package license file pathname. software package id: %d"), (int)a_nPackId); m_nLicStatus = OTS_LICENSE_STATUS::INVALID; return FALSE; } // create a license file pointer COTSLicenseFilePtr pOTSLicenseFilePtr = COTSLicenseFilePtr(new COTSLicenseFile()); // load license file if (!pOTSLicenseFilePtr->Load(strFilePathName)) { // failed to load the software package license file LogErrorTrace(__FILE__, __LINE__, _T("IsThereValidPackLicense: failed to load the software package license file. pathname: %s"), strFilePathName); m_nLicStatus = OTS_LICENSE_STATUS::NO_FILE; return FALSE; } m_pOTSLicenseFile = pOTSLicenseFilePtr; m_nPackId = a_nPackId; // return the license file validation result return COTSLicMgr::IsValidLicense(m_nPackId, m_pOTSLicenseFile->GetLicenseInfo(), m_nLicStatus); } // load license info file // BOOL COTSLicMgr::LoadLicenseInfoFile() { // create a license info file pointer COTSLicInfoFilePtr pOTSLicInfoFilePtr = COTSLicInfoFilePtr(new COTSLicInfoFile()); // get company log path CString strCompanySysDataPathName = COTSFileSys::GetCompanySysDataPathName(); if (strCompanySysDataPathName.IsEmpty()) { LogErrorTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFile: failed to company system data pathname.")); return FALSE; } // get license info file pathname CString strLicenseInfoPathName = GetLicenseInfoFilePathName(); if (strLicenseInfoPathName.IsEmpty()) { LogErrorTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFile: failed to find license info file pathname.")); return FALSE; } // check if company system data path exist if (COTSFileSys::Exists(strCompanySysDataPathName)) { // yes, try to open license info file pathname // check if the file is there if (!COTSFileSys::Exists(strLicenseInfoPathName)) { // license info file is missing LogTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFile: license info file is missing. pathname: %s"), strLicenseInfoPathName); pOTSLicInfoFilePtr->SetPathName(strLicenseInfoPathName); } // load license info file else if (!pOTSLicInfoFilePtr->Load(strLicenseInfoPathName)) { // failed to load the license info file LogErrorTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFile: failed to load license info file. pathname: %s"), strLicenseInfoPathName); return FALSE; } } // create company system data path else if (COTSFileSys::CreateFolder(strCompanySysDataPathName)) { // company system data path is missing LogTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFile: company system data path is missing.")); pOTSLicInfoFilePtr->SetPathName(strLicenseInfoPathName); } else { // failed to create company system data path LogErrorTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFile: failed to create company system data path.")); return FALSE; } // assign license info file point m_pOTSLicenseInfoFile = pOTSLicInfoFilePtr; // ok, return TRUE return TRUE; } // get software package license file pathname CString COTSLicMgr::GetPackLicenseFilePathName(OTS_SOFT_PACKAGE_ID a_nPackId) { // get software package system data pathname CString strOTSPackSysDataPathName = COTSFileSys::GetOTSPackSysDataPathName(a_nPackId); // check if software package system data pathname is right if (strOTSPackSysDataPathName.IsEmpty()) { // failed to get software package system data pathname LogErrorTrace(__FILE__, __LINE__, _T("GetPackLicenseFilePathName: failed to get software package system data path string.")); return _T(""); } // software package license file pathname // i.e. "c:\ProgramData\Config\SysData\OTSLicense.lic" CString strOTSPackLicFileNathName = strOTSPackSysDataPathName + LICENSE_FILENAME; // return software package license file pathname return strOTSPackLicFileNathName; } // get license info file pathname CString COTSLicMgr::GetLicenseInfoFilePathName() { // get company system data path CString strOTSSysDataPathName = COTSFileSys::GetCompanySysDataPathName(); // check if software package system data pathname is right if (strOTSSysDataPathName.IsEmpty()) { // failed to get company system data pathname LogErrorTrace(__FILE__, __LINE__, _T("GetLicenseInfoFilePathName: failed to get company system data pathname.")); return _T(""); } // license info file pathname // i.e. "c:\ProgramData\Config\OTSLicenseInfo.lcf" CString strLicenseInfoFileNathName = strOTSSysDataPathName + LICENSE_INFO_FILE_NAME; return strLicenseInfoFileNathName; } // check if COTSLicenseInfo is valid license info BOOL COTSLicMgr::IsValidLicense(OTS_SOFT_PACKAGE_ID a_nPackId, COTSLicenseInfoPtr a_poLicenseInfo, OTS_LICENSE_STATUS& a_nLicStatus, BOOL a_bCheckMachinId /*= TRUE*/, BOOL a_bTraceInfo /* = TRUE*/) { // make sure license file is valid ASSERT(a_poLicenseInfo); if (!a_poLicenseInfo) { // invalid license info pointer a_nLicStatus = OTS_LICENSE_STATUS::INVALID; LogErrorTrace(__FILE__, __LINE__, _T("IsValidLicense: invalid license info pointer.")); return FALSE; } // computer nick name CString strComputerNickName = a_poLicenseInfo->GetComputerNickName(); strComputerNickName.Trim(); if (strComputerNickName.IsEmpty()) { // nick name is empty if(a_bTraceInfo) { LogTrace(__FILE__, __LINE__, _T("IsValidLicense: computer nick name is empty.")); } a_nLicStatus = OTS_LICENSE_STATUS::COMPUTER_NICK_NAME_EMPTY; return FALSE; } // machine id CString strMachineId = a_poLicenseInfo->GetMachineId(); strMachineId.Trim(); if (strMachineId.GetLength() != MACHINEID_STR_LEN) { // invalid machine id string if (a_bTraceInfo) { LogTrace(__FILE__, __LINE__, _T("IsValidLicense: invalid machine id string.")); } a_nLicStatus = OTS_LICENSE_STATUS::INVALID_FILE; return FALSE; } else if (a_bCheckMachinId) { CString strLocalMachinId = COTSHelper::GetMachineId(); if (strMachineId.Compare(strLocalMachinId) != 0) { // machine id not match if (a_bTraceInfo) { LogTrace(__FILE__, __LINE__, _T("IsValidLicense: machine id not match. license: %s, local machine: %s"), strMachineId, strLocalMachinId); } a_nLicStatus = OTS_LICENSE_STATUS::MACHINEID_NOT_MATCH; return FALSE; } } // software package id if (a_poLicenseInfo->GetPackId() < OTS_SOFT_PACKAGE_ID::MIN || a_poLicenseInfo->GetPackId() > OTS_SOFT_PACKAGE_ID::MAX) { if (a_bTraceInfo) { LogTrace(__FILE__, __LINE__, _T("IsValidLicense: invalid software pack id.")); } a_nLicStatus = OTS_LICENSE_STATUS::INVALID_FILE; return FALSE; } else if (a_nPackId != a_poLicenseInfo->GetPackId()) { // software package id not match if (a_bTraceInfo) { LogTrace(__FILE__, __LINE__, _T("IsValidLicense: software pack id doesn't match.")); } a_nLicStatus = OTS_LICENSE_STATUS::SOFTWARE_PACKID_NOT_MATCH; return FALSE; } // license type if (a_poLicenseInfo->GetLicType() < OTS_LICENSE_TYPE::MIN || a_poLicenseInfo->GetLicType() > OTS_LICENSE_TYPE::MAX) { if (a_bTraceInfo) { LogTrace(__FILE__, __LINE__, _T("IsValidLicense: invalid license type id.")); } a_nLicStatus = OTS_LICENSE_STATUS::INVALID_FILE; return FALSE; } // expire date COleDateTime timeCurrent(COleDateTime::GetCurrentTime()); if (timeCurrent >= a_poLicenseInfo->GetExpireDate()) { // expired if (a_bTraceInfo) { LogTrace(__FILE__, __LINE__, _T("IsValidLicense: expired.")); } a_nLicStatus = OTS_LICENSE_STATUS::EXPIRED; return FALSE; } // is close to expire? COleDateTimeSpan oLeftTime = a_poLicenseInfo->GetExpireDate() - timeCurrent; int nLeftDays = oLeftTime.GetDays(); if (nLeftDays <= CLOSE_OVERDUE_DAYS) { // close to overdue if (a_bTraceInfo) { LogTrace(__FILE__, __LINE__, _T("IsValidLicense: nearly expired.")); } a_nLicStatus = OTS_LICENSE_STATUS::CLOSE_OVERDUE; return TRUE; } // Ok, return TRUE a_nLicStatus = OTS_LICENSE_STATUS::VALID; return TRUE; } // load a license file (license key file) COTSLicenseInfoPtr COTSLicMgr::LoadLicenseInfoFromFile() { // file pathname CString strFilePathName = _T(""); AFX_MANAGE_STATE(AfxGetStaticModuleState()); // open file dialog CFileDialog dlg(TRUE, LICENSEKEY_FILE_EXT, NULL, OFN_FILEMUSTEXIST, LICENSEKEY_FILTER); if (dlg.DoModal() != IDOK) { return nullptr; } // get file pathname strFilePathName = dlg.GetPathName(); // create a license file pointer COTSLicenseFilePtr pOTSLicenseFilePtr = COTSLicenseFilePtr(new COTSLicenseFile()); if (!pOTSLicenseFilePtr->Load(strFilePathName)) { // failed to load the file LogTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFromFile: failed to load the file %s."), strFilePathName); return nullptr; } // return license info return pOTSLicenseFilePtr->GetLicenseInfo(); } // create license file for the software package pointed by the license info BOOL COTSLicMgr::CreateLicenseFile(COTSLicenseInfoPtr a_pOTSLicenseInfo) { // check the license info ASSERT(a_pOTSLicenseInfo); if (!a_pOTSLicenseInfo) { // invalid license info pointer LogErrorTrace(__FILE__, __LINE__, _T("CreateLicenseFile: invalid license info pointer.")); return FALSE; } // get software package system data folder name CString strOTSPackSysDataPathName = COTSFileSys::GetOTSPackSysDataPathName(a_pOTSLicenseInfo->GetPackId()); // check if the folder name is right if (strOTSPackSysDataPathName.IsEmpty()) { // failed to get software package system data folder name LogErrorTrace(__FILE__, __LINE__, _T("CreateLicenseFile: failed to get software package system data folder string.")); return FALSE; } // check if the path exist if (!COTSFileSys::Exists(strOTSPackSysDataPathName)) { // create the folder if (!COTSFileSys::CreateFolder(strOTSPackSysDataPathName)) { // failed to create software package system data folder LogErrorTrace(__FILE__, __LINE__, _T("CreateLicenseFile: failed to create software package system data folder.")); return FALSE; } } // get software package license file pathname CString strFilePathName = GetPackLicenseFilePathName(a_pOTSLicenseInfo->GetPackId()); strFilePathName.Trim(); if (strFilePathName.IsEmpty()) { LogErrorTrace(__FILE__, __LINE__, _T("IsThereValidPackLicense: failed to find software package license file pathname. software package id: %d"), (int)a_pOTSLicenseInfo->GetPackId()); return FALSE; } return CreateLicenseFile( strFilePathName, a_pOTSLicenseInfo); } // create license file (a license key) BOOL COTSLicMgr::CreateLicenseFile(CString a_strFilePathName, COTSLicenseInfoPtr a_pOTSLicenseInfo) { // check if the file pathname is empty a_strFilePathName.Trim(); if (a_strFilePathName.IsEmpty()) { LogErrorTrace(__FILE__, __LINE__, _T("CreateLicenseFile: file pathname is empty.")); return FALSE; } // check the license info ASSERT(a_pOTSLicenseInfo); if (!a_pOTSLicenseInfo) { // invalid license info pointer LogErrorTrace(__FILE__, __LINE__, _T("CreateLicenseFile: invalid license info pointer.")); return FALSE; } // create a license file pointer COTSLicenseFilePtr pOTSLicenseFilePtr = COTSLicenseFilePtr(new COTSLicenseFile()); pOTSLicenseFilePtr->SetLicenseInfo(a_pOTSLicenseInfo); // return save license file result return pOTSLicenseFilePtr->Save(a_strFilePathName); } // create license info from a text file BOOL COTSLicMgr::CreateLicenseInfoFileText(COTSLicenseInfoPtr a_pOTSLicenseInfo) { // check the license info ASSERT(a_pOTSLicenseInfo); if (!a_pOTSLicenseInfo) { // invalid license info pointer LogErrorTrace(__FILE__, __LINE__, _T("CreateLicenseInfoFileText: invalid license info pointer.")); return FALSE; } // file save as dialog CFileDialog dlg(FALSE, TEXTFILE_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, TEXTFILE_FILTER); if (dlg.DoModal() != IDOK) { return FALSE; } // get file pathname CString strPathName = dlg.GetPathName(); // create the file CStdioFile file; CFileException ex; if (!file.Open(strPathName, CFile::modeCreate | CFile::modeWrite, &ex)) { // failed to open file LogErrorTrace(__FILE__, __LINE__, _T("CreateLicenseInfoFileText: failed to create file. %s"), strPathName); return FALSE; } // get license info text body CString strLicenseInfoTextBody = COTSLicMgr::GetLicenseInfoTextBody(a_pOTSLicenseInfo); file.WriteString(strLicenseInfoTextBody); file.Close(); // Ok, return TRUE return TRUE; } // load license info from a text file COTSLicenseInfoPtr COTSLicMgr::LoadLicenseInfoFromTextFile(CString a_strFilePathName) { // license info COTSLicenseInfoPtr pOTSLicenseInfo = COTSLicenseInfoPtr(new COTSLicenseInfo()); // check file name a_strFilePathName.Trim(); if (a_strFilePathName.IsEmpty()) { LogErrorTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFromTextFile: file pathname is empty.")); return nullptr; } // load string lines from the file std::vector listLineStr = COTSHelper::LoadTextFileToCStingList(a_strFilePathName); // process string line BOOL bRet = FALSE; for (auto strLine : listLineStr) { // split the string line with ":" std::vector listStr = COTSHelper::SplitString(strLine, FILE_TITLE_SPLIT); // jump over the string if it is invalid // it should have a title string and item string if ((int)listStr.size() != TEXTFILE_ITEM_COLUMN_NUMBER) { continue; } CString strTitle = listStr[0]; CString strItem = listStr[1]; // get license info item bRet = ( COTSLicMgr::GetLicnseInfoItem(pOTSLicenseInfo, strTitle, strItem) || bRet); } // check read result if (!bRet) { LogErrorTrace(__FILE__, __LINE__, _T("LoadLicenseInfoFromTextFile: %s contains no license info item."), a_strFilePathName); return nullptr; } // return the license info return pOTSLicenseInfo; } // get license info item BOOL COTSLicMgr::GetLicnseInfoItem(COTSLicenseInfoPtr a_pOTSLicenseInfo, CString a_strTitle, CString a_strValue) { // make sure license info handle is valid ASSERT(a_pOTSLicenseInfo); if (!a_pOTSLicenseInfo) { // invalid license info pointer LogErrorTrace(__FILE__, __LINE__, _T("GetLicnseInfoItem: invalid license info pointer.")); return FALSE; } // check title, value strings a_strTitle.Trim(); a_strValue.Trim(); if (a_strTitle.IsEmpty() || a_strValue.IsEmpty()) { LogErrorTrace(__FILE__, __LINE__, _T("GetLicnseInfoItem: title string or value string is empty.")); return FALSE; } // license info item for (int i = 0; i <= (int)OTS_LICENSE_INFO_ITEMS::MAX; ++i) { CString strFileItemTitle; strFileItemTitle= MultiLang::GetInstance().GetCStringByKey(IDS_LICENSE_INFO_TITLE_FIRST + i); if (a_strTitle.CompareNoCase(strFileItemTitle) == 0) { switch ((OTS_LICENSE_INFO_ITEMS)i) { case OTS_LICENSE_INFO_ITEMS::COMPUTER_NICK_NAME: { a_pOTSLicenseInfo->SetComputerNickName(a_strValue); return TRUE; } break; case OTS_LICENSE_INFO_ITEMS::MACHINE_ID: { if (a_strValue.GetLength() == MACHINEID_STR_LEN) { a_pOTSLicenseInfo->SetMachineId(a_strValue); return TRUE; } } break; case OTS_LICENSE_INFO_ITEMS::SOFTWARE_PACK_ID: { int nPackId; if (COTSHelper::StringToInt(a_strValue, nPackId) && (OTS_SOFT_PACKAGE_ID)nPackId >= OTS_SOFT_PACKAGE_ID::MIN && (OTS_SOFT_PACKAGE_ID)nPackId <= OTS_SOFT_PACKAGE_ID::MAX) { a_pOTSLicenseInfo->SetPackId((OTS_SOFT_PACKAGE_ID)nPackId); return TRUE; } } break; case OTS_LICENSE_INFO_ITEMS::LICENSE_TYPE: { int nLicType; if (COTSHelper::StringToInt(a_strValue, nLicType) && (OTS_LICENSE_TYPE)nLicType >= OTS_LICENSE_TYPE::MIN && (OTS_LICENSE_TYPE)nLicType <= OTS_LICENSE_TYPE::MAX) { a_pOTSLicenseInfo->SetLicType((OTS_LICENSE_TYPE)nLicType); return TRUE; } } break; case OTS_LICENSE_INFO_ITEMS::EXPIRE_DATE: { // Expire data format "%d%m%Y" if (COTSHelper::IsDigitString(a_strValue) && a_strValue.GetLength() == EXPIRE_DATE_STR_LENGTH) { CString strDay = a_strValue.Left(2); int nDay = 0; COTSHelper::StringToInt(strDay, nDay); CString strMonth = a_strValue.Mid(2, 2); int nMonth = 0; COTSHelper::StringToInt(strMonth, nMonth); CString strYear = a_strValue.Right(4); int nYear = 0; COTSHelper::StringToInt(strYear, nYear); COleDateTime oDataTime; oDataTime.SetDate(nYear, nMonth, nDay); a_pOTSLicenseInfo->SetExpireDate(oDataTime); return TRUE; } } break; } // value string is invalid , return FALSE LogErrorTrace(__FILE__, __LINE__, _T("GetLicnseInfoItem: title string or value string is empty. title: %s value: %s"), a_strTitle, a_strValue); return FALSE; } } // can't find matching title, return FALSE LogErrorTrace(__FILE__, __LINE__, _T("GetLicnseInfoItem: can't find matching title. title: %s"), a_strTitle); return FALSE; } // get software pack id string CString COTSLicMgr::GetSoftwarePackIdString(OTS_SOFT_PACKAGE_ID a_nPackId) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); CString strPackId = _T(""); if (a_nPackId >= OTS_SOFT_PACKAGE_ID::MIN && a_nPackId <= OTS_SOFT_PACKAGE_ID::MAX) { strPackId=MultiLang::GetInstance().GetCStringByKey(IDS_SOFTWARETYPEID_FIRST + (int)a_nPackId); } return strPackId; } // get license type string CString COTSLicMgr::GetLicenseTypeIdString(OTS_LICENSE_TYPE a_nLicTypeId) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); CString strLicType = _T(""); if (a_nLicTypeId >= OTS_LICENSE_TYPE::MIN && a_nLicTypeId <= OTS_LICENSE_TYPE::MAX) { strLicType=MultiLang::GetInstance().GetCStringByKey(IDS_LICENSETYPEID_FIRST + (int)a_nLicTypeId); } return strLicType; } // create an encrypt string containing the given info CString COTSLicMgr::EncryptLicenseInfo(COTSLicenseInfoPtr a_pOTSLicenseInfo) { // make sure license info handle is valid ASSERT(a_pOTSLicenseInfo); if (!a_pOTSLicenseInfo) { // invalid license info pointer LogErrorTrace(__FILE__, __LINE__, _T("GetLicnseInfoItem: invalid license info pointer.")); return _T(""); } // license string // computer machine id string -- length MACHINEID_STR_LEN (19) // software package id string -- length 1 // license type id string -- length 1 // expired date string -- length 8 // computer nick name CString strPackId; strPackId.Format(_T("%i"), (int)a_pOTSLicenseInfo->GetPackId()); CString strLicType; strLicType.Format(_T("%i"), (int)a_pOTSLicenseInfo->GetLicType()); CString strExpiredDate; strExpiredDate = a_pOTSLicenseInfo->GetExpireDate().Format(EXPIRE_DATA_FORMAT); CString strLicense; strLicense = a_pOTSLicenseInfo->GetMachineId() + strPackId + strLicType + strExpiredDate + a_pOTSLicenseInfo->GetComputerNickName(); // encrypt license string COTSCrypt oCrypt; CString strKey = CRYTP_KEY_STRING; try { oCrypt.Encrypt(strKey, strLicense); } catch(...) { LogErrorTrace(__FILE__, __LINE__, _T("EncryptLicenseInfo: failed to encrypt license string.")); } // return the encrypt license string return strLicense; } COTSLicenseInfoPtr COTSLicMgr::DecryptLicenseInfo(CString a_strLicense) { // make sure the license string is valid a_strLicense.Trim(); if (a_strLicense.IsEmpty()) { // invalid license string LogErrorTrace(__FILE__, __LINE__, _T("DecryptLicenseInfo: license string is empty.")); return nullptr; } // decrypt the license string COTSCrypt oCrypt; CString strKey = CRYTP_KEY_STRING; try { oCrypt.Decrypt(strKey, a_strLicense); } catch (...) { LogErrorTrace(__FILE__, __LINE__, _T("DecryptLicenseInfo: failed to decrypt license string.")); } // make sure license string is longer enough if (a_strLicense.GetLength() < LICENSE_STR_MIN_LENGTH) { // invalid license string return nullptr; } // separate the license string // computer machine id string -- length MACHINEID_STR_LEN (19) // software package id string -- length 1 // license type id string -- length 1 // expired date string -- EXPIRED_DATE_STR_LENGTH (8) // computer nick name CString strMachineId = a_strLicense.Left(MACHINEID_STR_LEN); CString strPackId = a_strLicense.Mid(MACHINEID_STR_LEN, 1); CString strLicTypeId = a_strLicense.Mid(MACHINEID_STR_LEN + 1, 1); CString strExpiredDate = a_strLicense.Mid(MACHINEID_STR_LEN + 1 + 1, EXPIRE_DATE_STR_LENGTH); int nComputerNickNameStrLength = a_strLicense.GetLength() - MACHINEID_STR_LEN - 1 - 1 - EXPIRE_DATE_STR_LENGTH; CString strComputerNickName = a_strLicense.Right(nComputerNickNameStrLength); // create an license info COTSLicenseInfoPtr pOTSLicenseInfo = COTSLicenseInfoPtr(new COTSLicenseInfo()); // assign machine id strMachineId.Trim(); if (strMachineId.GetLength() != MACHINEID_STR_LEN) { // invalid machine id return nullptr; } pOTSLicenseInfo->SetMachineId(strMachineId); // assign software package id int nPackId; if (!COTSHelper::StringToInt(strPackId, nPackId) || (OTS_SOFT_PACKAGE_ID)nPackId < OTS_SOFT_PACKAGE_ID::MIN || (OTS_SOFT_PACKAGE_ID)nPackId > OTS_SOFT_PACKAGE_ID::MAX) { // invalid package id value return nullptr; } pOTSLicenseInfo->SetPackId((OTS_SOFT_PACKAGE_ID)nPackId); // assign license type int nLicType; if (!COTSHelper::StringToInt(strLicTypeId, nLicType) || (OTS_LICENSE_TYPE)nLicType < OTS_LICENSE_TYPE::MIN || (OTS_LICENSE_TYPE)nLicType > OTS_LICENSE_TYPE::MAX) { // invalid license type value return nullptr; } pOTSLicenseInfo->SetLicType((OTS_LICENSE_TYPE)nLicType); // assign expire data // Expire data format "%d%m%Y" if (!COTSHelper::IsDigitString(strExpiredDate)) { // invalid expire data string return nullptr; } CString strDay = strExpiredDate.Left(2); int nDay = 0; COTSHelper::StringToInt(strDay, nDay); CString strMonth = strExpiredDate.Mid(2,2); int nMonth = 0; COTSHelper::StringToInt(strMonth, nMonth); CString strYear = strExpiredDate.Right(4); int nYear = 0; COTSHelper::StringToInt(strYear, nYear); COleDateTime oDataTime; oDataTime.SetDate(nYear, nMonth, nDay); pOTSLicenseInfo->SetExpireDate(oDataTime); // assign computer nick name strComputerNickName.Trim(); if (strComputerNickName.IsEmpty()) { // invalid computer nick name return nullptr; } pOTSLicenseInfo->SetComputerNickName(strComputerNickName); // return the license info return pOTSLicenseInfo; } // get license info text body CString COTSLicMgr::GetLicenseInfoTextBody(COTSLicenseInfoPtr a_pOTSLicenseInfo) { // license info text body CString strLicenseInfoTextBody = _T(""); // make sure license info handle is valid ASSERT(a_pOTSLicenseInfo); if (!a_pOTSLicenseInfo) { // invalid license info pointer LogErrorTrace(__FILE__, __LINE__, _T("GetLicenseInfoTextBody: invalid license info pointer.")); return strLicenseInfoTextBody; } CString strTitle; // nick name strTitle=MultiLang::GetInstance().GetCStringByKey(IDS_LICENSE_INFO_TITLE_FIRST); strLicenseInfoTextBody = strTitle + FILE_TITLE_SPLIT + _T(" ") + a_pOTSLicenseInfo->GetComputerNickName() + LINE_END; // machine id strTitle=MultiLang::GetInstance().GetCStringByKey(IDS_LICENSE_INFO_TITLE_FIRST + 1); strLicenseInfoTextBody += strTitle + FILE_TITLE_SPLIT + _T(" ") + a_pOTSLicenseInfo->GetMachineId() + LINE_END; // software package id strTitle=MultiLang::GetInstance().GetCStringByKey(IDS_LICENSE_INFO_TITLE_FIRST + 2); CString strPackId; strPackId.Format(_T("%i"), (int)a_pOTSLicenseInfo->GetPackId()); strLicenseInfoTextBody += strTitle + FILE_TITLE_SPLIT + _T(" ") + strPackId + LINE_END; // license type id strTitle=MultiLang::GetInstance().GetCStringByKey(IDS_LICENSE_INFO_TITLE_FIRST + 3); CString strLicType; strLicType.Format(_T("%i"), (int)a_pOTSLicenseInfo->GetLicType()); strLicenseInfoTextBody += strTitle + FILE_TITLE_SPLIT + _T(" ") + strLicType + LINE_END; // expire date strTitle=MultiLang::GetInstance().GetCStringByKey(IDS_LICENSE_INFO_TITLE_FIRST + 4); CString strExpiredDate; strExpiredDate = a_pOTSLicenseInfo->GetExpireDate().Format(EXPIRE_DATA_FORMAT); strLicenseInfoTextBody += strTitle + FILE_TITLE_SPLIT + _T(" ") + strExpiredDate + LINE_END; // return license info text body return strLicenseInfoTextBody; } // private: // is valid machine id string BOOL COTSLicMgr::IsValidMachineIdString(CString a_strMachineId) { a_strMachineId.Trim(); return (!a_strMachineId.IsEmpty() && a_strMachineId.GetLength() == MACHINEID_STR_LEN); } }