| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309 |
- #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<CString > listLineStr = COTSHelper::LoadTextFileToCStingList(a_strFilePathName);
- // process string line
- BOOL bRet = FALSE;
- for (auto strLine : listLineStr)
- {
- // split the string line with ":"
- std::vector<CString > 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);
- }
- }
|