CSHelper.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:18k
- #include "stdafx.h"
- #include "trfAgent.h"
- #include "CSHelper.h"
- #include "register.h"
- #include "passwrd.h"
- #include "variantex.h"
- #include "NetAddress.h"
- CCSHelper::CCSHelper()
- {
- this->m_bactived= false;
- this->m_nAGID = 0;
-
- this->m_dwserverport = 5557;
- this->m_hrefthread = NULL;
- this->m_hconcsthread = NULL;
- this->m_bcanterminate = false;
- this->m_bcanexitconnectcs = false;
- this->m_chstate = CCSHelper::CHS_IDLE;
- }
- CCSHelper::~CCSHelper()
- {
- }
- //read connection configure.
- bool CCSHelper::ReadConnConfigure(void)
- {
- //open the register.
- CRegister regkey;
- if(!regkey.Open(HKEY_CURRENT_USER, REG_SSYSSETTINGKEY))
- {
- return false;
- }
- DWORD dwaddr = regkey.Read_DWord_Value(REG_STRFSERVER);
- DWORD dwport = regkey.Read_DWord_Value(REG_NTRFDIRSVRPORT);
-
- regkey.Close();
-
- //get the ip and port info.
- BYTE ipfld[4];
- for(int i = 4; i > 0; --i)
- {
- ipfld[i-1] = dwaddr & 0x000000FF;
- dwaddr >>= 8;
- }
-
- //create the socket object.
- char saddress[MAX_IP_ADDR_SIZE];
- sprintf(saddress, "%d.%d.%d.%d", ipfld[0], ipfld[1], ipfld[2], ipfld[3]);
- //get it!
- this->m_dwserverport = dwport;
- this->m_sserverip = saddress;
-
- return true;
- }
- //start the cs helper functions.
- bool CCSHelper::Start(void)
- {
- //read connection configure.
- if(!this->ReadConnConfigure()) return false;
- this->m_hlpsock.Set_Socket_Address(this->m_dwserverport, this->m_sserverip.c_str());
- if(!this->m_hlpsock.Create() || !this->m_hlpsock.Open())
- {
- MSGBOX_INFO(_LoadString(IDS_DONTCONNSVR).GetBuffer(0));
- }
- //create cycconnecting server thread.
-
- DWORD dwthreadid = 0x0000;
- m_hconcsthread = CreateThread(NULL, 0, CycConnectCSThread, this, 0, &dwthreadid);
- if(!m_hconcsthread) return false;
- this->FirstAuthor();
-
- //create service thread
- this->m_hrefthread = ::CreateThread(NULL, 0, CSRefThreadProc, this, 0, &dwthreadid);
-
- this->m_bactived = true;
- return true;
- }
- //stop the cs helper functions.
- void CCSHelper::Stop(void)
- {
- if(this->m_bactived)
- {
- this->m_hlpsock.Open(false);
-
- //terminate threads.
- this->m_bcanexitconnectcs = true;
- ::WaitForSingleObject(this->m_hconcsthread, 10000);
- ::CloseHandle(this->m_hconcsthread);
- this->m_hconcsthread = NULL;
-
- this->m_bcanterminate = true;
- ::WaitForSingleObject(this->m_hrefthread, 10000);
- ::CloseHandle(this->m_hrefthread);
- this->m_hrefthread = NULL;
- //set the cshelper state.
- this->m_bactived = false;
- this->m_chstate = CCSHelper::CHS_IDLE;
- }
- }
- //get local agent's identity.
- const int CCSHelper::GetClientID()
- {
- return this->m_nAGID;
- }
- const string &CCSHelper::GetClientName()
- {
- return this->m_sAGName;
- }
- //first authentication action
- bool CCSHelper::FirstAuthor(void)
- {
- //get the local agent's id info.
- bool bsuccessful = true;
- CGuiRecordSet *prs, rsu;
- CVariantEx varex;
- char sql[MAX_SQL_SIZE];
-
- try
- {
- //===get the local agent id and name===
- _snprintf(sql, MAX_SQL_SIZE-1, SQL::SERVER_GET_AGENTINFO, theApp.m_oAppInstance.m_sHDID);
- //send first state package.
-
- if(this->SendAGState())
- {
- prs = this->SendAGUsrSql(sql, bsuccessful);
-
- if(!prs)// || prs->IsEof())//prs->GetRecordCount() == 0)
- {
- _DELETE(prs);
- return false;
- }
- prs->GetCollect("id", varex.get_variant_t());
- this->m_nAGID = varex.AsInteger();
- prs->GetCollect("name", varex.get_variant_t());
- this->m_sAGName = varex.AsString();
- delete prs;
- return true;
- }
- else
- {
- if(!rsu.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql))
- {
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED).GetBuffer(0));
- return false;
- }
- rsu.MoveFirst();
- if(!rsu.IsEof())
- {
- rsu.GetCollect("id", varex.get_variant_t());
- this->m_nAGID = varex.AsInteger();
-
- rsu.GetCollect("name", varex.get_variant_t());
- this->m_sAGName = varex.AsString();
- }
- rsu.Close();
- }
- }
- catch (...)
- {
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED).GetBuffer(0));
- return false;
- }
- return true;
- }
- //process agent rename action
- bool CCSHelper::AGRename(LPCSTR lpszNewName, const int nAGID)
- {
- bool bsuccessful = true;
- char sql[MAX_SQL_SIZE];
- _snprintf(sql, MAX_SQL_SIZE-1, SQL::SERVER_UPDATE_AGENTNAME, lpszNewName, nAGID);
-
- CGuiRecordSet *prs = this->SendAGUsrSql(sql, bsuccessful, false);
- _DELETE(prs);
- if(bsuccessful && nAGID == this->m_nAGID)
- {
- this->m_sAGName = lpszNewName;
- }
- return bsuccessful;
- }
- //verifying current administrator user twice
- bool CCSHelper::VerifyCurrentUser(void)
- {
- AUTHORINFO authorinfo;
- memcpy(&authorinfo, &(theApp.m_oAppInstance.m_authorinfo), sizeof(authorinfo));
-
- if(!theApp.m_oCSHelper.SendAGAuthor(theApp.m_oAppInstance.m_authorinfo))
- {
- theApp.m_oAppInstance.m_binadminmode = false;
-
- return false;
- }
-
- //用户名和用户密码加密后如果和返回的authorinfo结构中对应内容相同则表明该用户合法,否则非法.
- vncDecryptBytes((unsigned char*)theApp.m_oAppInstance.m_authorinfo.username, ENCRYPTKEY);
-
- //unencrypting the password
- CPasswrd::ToText passwrd_encry(theApp.m_oAppInstance.m_authorinfo.passwrd);
-
- memset(theApp.m_oAppInstance.m_authorinfo.passwrd, 0x0, sizeof(theApp.m_oAppInstance.m_authorinfo.passwrd));
- memcpy(theApp.m_oAppInstance.m_authorinfo.passwrd, (const char*)passwrd_encry, sizeof(theApp.m_oAppInstance.m_authorinfo.passwrd));
-
- if( stricmp(theApp.m_oAppInstance.m_authorinfo.username, authorinfo.username) ||
- stricmp(theApp.m_oAppInstance.m_authorinfo.passwrd, authorinfo.passwrd) )
- {
- theApp.m_oAppInstance.m_binadminmode = false;
-
- return false;
- }
- return true;
- }
- bool CCSHelper::RecvAnswer(KAGENTPACK &agkpack)
- {
- int size = sizeof(agkpack);
-
- //for(int i = 0; i < 3; ++i)
- {
- int recved = this->m_hlpsock.ReceiveBuf(&agkpack, size);
- if(-1 == recved)
- {
- this->m_hlpsock.Open(false);
- //MSGBOX_ERROR(_LoadString(IDS_SOCKINTERCEPT).GetBuffer(0));
- return false;
- }
- else if(recved == size)
- {
- return true;
- }
- }
- return false;
- }
- bool CCSHelper::SendAGState(void)
- {
- if(!this->m_hlpsock.Socket_IsValid() || !this->m_hlpsock.Socket_IsOpened()) return false;
- char pszdata[256+32], pszhostname[256];
- DWORD dwcmpsize = sizeof(pszhostname);
- memset(pszdata, 0x0, sizeof(pszdata));
- memset(pszhostname, 0x0, sizeof(pszhostname));
- ::GetComputerName(pszhostname, &dwcmpsize);
- _snprintf(pszdata, sizeof(pszdata), "%s|%s", theApp.m_oAppInstance.m_sHDID, pszhostname);
- //状态包中包含了客户端旧的硬件ID和当前计算机名称的信息.
- RAGENTPACK agrpack;
-
- agrpack.type = RAG_STATE;
- agrpack.size = strlen(pszdata);/*theApp.m_oAppInstance.m_sHDID*/
- agrpack.data = (LPVOID)pszdata;/*theApp.m_oAppInstance.m_sHDID*/;
-
- int size = sizeof(agrpack);
- if(size == this->m_hlpsock.SendBuf(&agrpack, size))
- {
- //@1
- if(agrpack.size > 0)
- {
- int sended = this->m_hlpsock.SendBuf(agrpack.data, agrpack.size);
- if(-1 == sended)
- {
- this->m_hlpsock.Open(false);
- //MSGBOX_ERROR(_LoadString(IDS_SOCKINTERCEPT).GetBuffer(0));
- return false;
- }
- if(sended != agrpack.size) return false;
- }
- //@2
- KAGENTPACK agkpack;
- if(this->RecvAnswer(agkpack))
- {
- bool result = (agkpack.type == KAG_STATE) && (agkpack.ret == K_OK);
- return result;
- }
- }
- return false;
- }
- bool CCSHelper::SendAGAuthor(AUTHORINFO &authorinfo)
- {
- if(!this->m_hlpsock.Socket_IsValid() || !this->m_hlpsock.Socket_IsOpened()) return false;
- RAGENTPACK agrpack;
- agrpack.size = sizeof(authorinfo);
- agrpack.data = &authorinfo;
- agrpack.type = RAG_VERIFYPWD;
-
- int size = sizeof(agrpack), sended = this->m_hlpsock.SendBuf(&agrpack, size);
- if(size == sended)
- {
- //encrypting the user name.
- vncEncryptBytes((unsigned char*)authorinfo.username, ENCRYPTKEY);
-
- //encrypting the password
- CPasswrd::FromText passwrd_encry(authorinfo.passwrd);
- memcpy(authorinfo.passwrd, (const char*)passwrd_encry, MAXPWLEN);
- //@1
- int sended = this->m_hlpsock.SendBuf(agrpack.data, agrpack.size);
- if(-1 == sended || sended != agrpack.size)
- {
- if(-1 == sended)
- {
- this->m_hlpsock.Open(false);
- MSGBOX_ERROR(_LoadString(IDS_SOCKINTERCEPT).GetBuffer(0));
- }
- return false;
- }
- //@2
- KAGENTPACK kagpack;
- memset(&kagpack, 0x0, sizeof(kagpack));
- if(this->RecvAnswer(kagpack))
- {
- if(kagpack.ret == K_FAILED) return false;
- //@3
- int recved = this->m_hlpsock.ReceiveBuf(&authorinfo, sizeof(authorinfo));
-
- if(-1 == recved || recved != sizeof(authorinfo))
- {
- if(-1 == recved)
- {
- this->m_hlpsock.Open(false);
- MSGBOX_ERROR(_LoadString(IDS_SOCKINTERCEPT).GetBuffer(0));
- }
- return false;
- }
- else return true;
- }
- }
- else if(-1 == sended)
- {
- this->m_hlpsock.Open(false);
- MSGBOX_ERROR(_LoadString(IDS_SOCKINTERCEPT).GetBuffer(0));
- }
- return false;
- }
- CGuiRecordSet* CCSHelper::SendAGUsrSql(LPCSTR lpszUsrSql, bool &bSuccessful, bool bReturnRecordset)
- {
- if(!this->m_hlpsock.Socket_IsValid() || !this->m_hlpsock.Socket_IsOpened()) return NULL;
-
- string sUsrSql = lpszUsrSql;
- if(sUsrSql.empty()) return NULL;
-
- CGuiRecordSet *prs = NULL;
-
- //@1
- string szXMLFile("");
- bSuccessful = this->ProcessSQLRSXML(lpszUsrSql, szXMLFile, bReturnRecordset);
- if(!bSuccessful) return prs;
-
- if(bReturnRecordset && szXMLFile.empty()) bSuccessful = false;
- if(bReturnRecordset)
- {
- prs = new CGuiRecordSet();
- if(!prs->LoadFromXMLFile((LPCTSTR)szXMLFile.data()))
- {
- bSuccessful = false;
- _DELETE(prs);
- }
- ::DeleteFile(szXMLFile.c_str());
- }
- return prs;
- }
- bool CCSHelper::ProcessSQLRSXML(LPCSTR szSQLTxt, string &sFileName, bool bReturnRecordset)
- {
- sFileName.erase();
-
- //@1
- EXECSQLINFO execsqlinfo;
-
- memset(&execsqlinfo, 0x0, sizeof(execsqlinfo));
- _snprintf(execsqlinfo.sql, MAX_SQL_SIZE-1, "%s", szSQLTxt);
- execsqlinfo.needretval = bReturnRecordset ? 1:0;
-
- RAGENTPACK agrpack;
-
- agrpack.size = sizeof(execsqlinfo);
- agrpack.data = &execsqlinfo;
- agrpack.type = RAG_EXECUSRSQL;
-
- //@1
- int size = sizeof(agrpack);
- int sended = this->m_hlpsock.SendBuf(&agrpack, size);
-
- if(-1 == sended || sended != size)
- {
- this->m_hlpsock.Open(false);
- //MSGBOX_ERROR(_LoadString(IDS_SOCKINTERCEPT).GetBuffer(0));
- return false;
- }
-
- //@2
- sended = this->m_hlpsock.SendBuf(agrpack.data, agrpack.size);
- if(-1 == sended || sended != agrpack.size)
- {
- if(-1 == sended)
- {
- this->m_hlpsock.Open(false);
- //MSGBOX_ERROR(_LoadString(IDS_SOCKINTERCEPT).GetBuffer(0));
- }
- return false;
- }
-
- //@3
- KAGENTPACK agkpack;
- if(this->RecvAnswer(agkpack))
- {
- if(bReturnRecordset && !ReceiveFile(sFileName))
- {
- sFileName.erase();
- return false;
- }
- }
- if(this->RecvAnswer(agkpack))
- {
- if(agkpack.ret == K_FAILED)
- {
- sFileName.erase();
- return false;
- }
- }
- return true;
- }
- bool CCSHelper::ReceiveFile(string &sDestFile)
- {
- if(!this->m_hlpsock.Socket_IsValid() || !this->m_hlpsock.Socket_IsOpened()) return false;
-
- //@1
- char szTempPath[_MAX_PATH], szRSXMLFile[_MAX_PATH];
-
- memset(szTempPath, 0x0, sizeof(szTempPath));
- memset(szRSXMLFile, 0x0, sizeof(szRSXMLFile));
-
- GetTempPath(sizeof(szTempPath), (LPSTR)&szTempPath[0]);
- GetTempFileName((char*)szTempPath, "xml", 0, szRSXMLFile);
-
- sDestFile = (char*)szRSXMLFile;
-
- //@2
- HANDLE hFile = CreateFile((char*)szRSXMLFile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ,
- NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
-
- if(INVALID_HANDLE_VALUE == hFile)
- {
- //TRACE0("Failed to open the specialed filen");
- sDestFile.erase();
- return false;
- }
-
- //@3--Recv the src file size.
- char cRes = 'O';
- DWORD flesize = 0;
- int size = sizeof(DWORD), errtimes = 0;
- do
- {
- if(size != this->m_hlpsock.ReceiveBuf(&flesize, size)) ++errtimes;
- else{
- //@3--wait for client...
- size = sizeof(char);
-
- if(size != this->m_hlpsock.SendBuf(&cRes, size)) ++errtimes;
- else break;
- }
- }
- while(errtimes < 3);
-
- //3.1
- if(errtimes == 3)
- {
- //TRACE0("Failed to recv the file size infon");
- CloseHandle(hFile);
- DeleteFile(sDestFile.c_str());
- sDestFile.erase();
- return false;
- }
-
- //@4
- #define MAX_NET_PACKET 8192
-
- int nrecved = 0, maxsize = 0;
- DWORD dwleft = flesize, dwrite = 0;
- char buf[MAX_NET_PACKET];
-
- while(dwleft > 0)
- {
- memset((char*)buf, 0x0, sizeof(buf));
-
- maxsize = (dwleft > MAX_NET_PACKET) ? MAX_NET_PACKET : dwleft;
-
- nrecved = this->m_hlpsock.ReceiveBuf(buf, maxsize);
- if(nrecved < 0)
- {
- CloseHandle(hFile);
- DeleteFile(sDestFile.c_str());
- sDestFile.erase();
- return false;
- }
-
- XorUncryptStream(buf, nrecved);
-
- WriteFile(hFile, buf, nrecved, &dwrite, NULL);
-
- //wait for client...
- cRes = 'O';
- size = sizeof(char);
- if(size != this->m_hlpsock.SendBuf(&cRes, size))
- {
- //TRACE0("Failed to send the file size infon");
- CloseHandle(hFile);
- DeleteFile(sDestFile.c_str());
-
- sDestFile.erase();
- return false;
- }
- dwleft -= nrecved;
- }
- CloseHandle(hFile);
-
- return true;
- }
- //update local "agents.db" table
- BOOL CCSHelper::UpdateAgentsContents(void)
- {
- bool bsuccessful = true;
- CGuiRecordSet *prs = this->SendAGUsrSql(SQL::SERVER_AGENT_GETALL, bsuccessful);
- if(NULL != prs)
- {
- CGuiRecordSet rs;
- try
- {
- if(rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), SQL::CLIENT_CLEAR_AGSTAB))
- {
- rs.Close();
- char sqltxt[MAX_SQL_SIZE];
-
- for(prs->MoveFirst(); !prs->IsEof(); prs->MoveNext())
- {
- //1---get fields value.
- int id, departid, state;
- string hostname, ipaddr, hdid, agname;
-
- CVariantEx varex;
-
- prs->GetCollect("id", varex.get_variant_t());
- id = varex.AsInteger();
- prs->GetCollect("departid", varex.get_variant_t());
- departid = varex.AsInteger();
- prs->GetCollect("state", varex.get_variant_t());
- state = varex.AsInteger();
- prs->GetCollect("hostname", varex.get_variant_t());
- hostname = varex.AsString();
- prs->GetCollect("ipaddr", varex.get_variant_t());
- ipaddr = varex.AsString();
- prs->GetCollect("hdid", varex.get_variant_t());
- hdid = varex.AsString();
- prs->GetCollect("name", varex.get_variant_t());
- agname = varex.AsString();;
- //2---make sql text.
- memset(sqltxt, 0x0, sizeof(sqltxt));
- _snprintf(sqltxt, MAX_SQL_SIZE-1, SQL::CLIENT_INSERT_AGINFO,
- id, hostname.c_str(), ipaddr.c_str(), hdid.c_str(),
- departid, state, agname.c_str());
-
- if(!rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sqltxt))
- {
- _DELETE(prs);
- return FALSE;
- }
- }
- }
- _DELETE(prs);
- return TRUE;
- }
- catch (...)
- {
- _DELETE(prs);
- return FALSE;
- }
- }
- return FALSE;
- }
- //update local "departs.db" table
- BOOL CCSHelper::UpdateDepartsContents(void)
- {
- bool bsuccessful = true;
- CGuiRecordSet *prs = this->SendAGUsrSql(SQL::SERVER_DEPART_GETALL, bsuccessful);
- if(NULL != prs)
- {
- CGuiRecordSet rs;
- try
- {
- if(rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), SQL::CLIENT_CLEAR_DEPARTSTAB))
- {
- rs.Close();
- char sqltxt[MAX_SQL_SIZE];
-
- for(prs->MoveFirst(); !prs->IsEof(); prs->MoveNext())
- {
- //1---get fields value.
- int id = 0, parentid = 0;
- string name("");
-
- CVariantEx varex;
-
- prs->GetCollect("id", varex.get_variant_t());
- id = varex.AsInteger();
-
- prs->GetCollect("parentid", varex.get_variant_t());
- parentid = varex.AsInteger();
-
- prs->GetCollect("name", varex.get_variant_t());
- name = varex.AsString();
- //2---make sql text.
- memset(sqltxt, 0x0, sizeof(sqltxt));
- _snprintf( sqltxt, MAX_SQL_SIZE-1, SQL::CLIENT_INSERT_DEPARTINFO,
- id, name.c_str(), parentid);
-
- if(!rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sqltxt))
- {
- _DELETE(prs);
- return FALSE;
- }
- }
- }
- _DELETE(prs);
- return TRUE;
- }
- catch (...)
- {
- _DELETE(prs);
- return FALSE;
- }
- }
- return FALSE;
- }
- //return refresh thread can be terminated flag.
- const bool CCSHelper::RefThreadCanTerminate(void)
- {
- return this->m_bcanterminate;
- }
- //return cycconnecting server thread can be terminated flag.
- const bool CCSHelper::CycConnThreadCanTerminate(void)
- {
- return this->m_bcanexitconnectcs;
- }
- //main thread entry.
- DWORD CALLBACK CCSHelper::CSRefThreadProc(LPVOID lpParameter)
- {
- CCSHelper *pthis = (CCSHelper*)lpParameter;
- if(NULL == pthis) return 0;
-
- DWORD dwcounts = 0;
- while(!pthis->RefThreadCanTerminate())
- {
- if(dwcounts % 5 == 0) //5 sec
- {
- if(pthis->m_chstate == CCSHelper::CHS_IDLE)
- pthis->m_chstate = CCSHelper::CHS_UPTAGSTATE;
- else
- pthis->m_chstate = (CCSHelper::CSHLPERSTATE)(pthis->m_chstate | CCSHelper::CHS_UPTAGSTATE);
-
- //===update the agent state===
- pthis->SendAGState();
-
- pthis->m_chstate = (CCSHelper::CSHLPERSTATE)(pthis->m_chstate ^ CCSHelper::CHS_UPTAGSTATE);
- }
- if(dwcounts % 120 == 0) //2 min
- {
- if(pthis->m_chstate == CCSHelper::CHS_IDLE)
- pthis->m_chstate = CCSHelper::CHS_UPTAGUSRLIST;
- else
- pthis->m_chstate = (CCSHelper::CSHLPERSTATE)(pthis->m_chstate | CCSHelper::CHS_UPTAGUSRLIST);
-
- //===update the local database contents===
- pthis->UpdateDepartsContents();
- pthis->UpdateAgentsContents();
-
- pthis->m_chstate = (CCSHelper::CSHLPERSTATE)(pthis->m_chstate ^ CCSHelper::CHS_UPTAGUSRLIST);
-
- if(theApp.m_pMainWnd && theApp.m_pMainWnd->GetSafeHwnd())
- PostMessage(theApp.m_pMainWnd->GetSafeHwnd(), UWM_REFRESHUSRLST, 0, 0);
- dwcounts = 0;
- }
- ++dwcounts;
- Sleep(1000);
- }
- return 0;
- }
- //c-s socket loop connecting thread.
- DWORD CALLBACK CCSHelper::CycConnectCSThread(LPVOID lpParameters)
- {
- CCSHelper* pthis = (CCSHelper*)lpParameters;
- if(NULL == pthis) return 0;
-
- while(!pthis->CycConnThreadCanTerminate())
- {
- if(!pthis->m_hlpsock.Socket_IsValid() || !pthis->m_hlpsock.Socket_IsOpened())
- {
- pthis->m_hlpsock.Set_Socket_Address(pthis->m_dwserverport, pthis->m_sserverip.c_str());
-
- if(!pthis->m_hlpsock.Create() || !pthis->m_hlpsock.Open())
- {
- if(pthis->m_hlpsock.Socket_IsValid())
- {
- pthis->m_hlpsock.Open(false);
- }
- }
- }
- Sleep(2000);
- }
- return 0;
- }