www.gusucode.com > 一些VC++加密算法+实例源码源码程序 > 一些VC++加密算法+实例源码/优化后的加密注册模块/优化后的加密注册模块/InvCall(optimized)/CallInventory.cpp

    //CallInventory.cpp
// Download by http://www.codesc.net
#include "stdafx.h"
#include "CallInventory.h"
#include "am_lv1.h"
#include "Encry.h"
#include "module.h"

#include "inventory_i.c" 
#include "sqlservice_i.c"

//define some global variables
unsigned char	PublicKey[8];		//public key for communication
LPSTR			service_ip;			//ip address of service
long			RecordNum;			//the number of records query from SQL 

//*******************************************************************************
int GetPublicKey(IDBObject *m_SQLObject,const char *dbip,char *pKey)
{
	unsigned int ret = 0 , 
				 ipsize = IP_LEN ;

	HRESULT hRes = 0 ;

	BSTR	wdes_localip =  BSTRAS,
			wencrypt_pkey = BSTRAS,
			wdecrypt_pkey = BSTRAS,
			wlocalip ;

	char	localip[IP_LEN], 
			md5_localip[MD5KLEN],
			md5_dbip[MD5KLEN],
			tkey[9];


	if ( c_GetIpAddr(localip,&ipsize))		//get local ipaddress
		return FatalError;

	wlocalip = MakeWideStrFromAnsi(localip);

	md5hash((unsigned char *)dbip, strlen(dbip),	
		(unsigned char *)md5_dbip);			//encrypt  db ipaddress by MD5
	
	DesBSTREn(wlocalip, &wdes_localip, (unsigned char *)md5_dbip);	//encrypt local ipaddr by DES	

	hRes = m_SQLObject->GetCryptKey(wdes_localip, &wencrypt_pkey);	//get the public key
	if(hRes!=0)
		return FatalError;

	md5hash((unsigned char *)localip, strlen(localip), (unsigned char *)md5_localip);	//encrypt local ipaddr by MD5

	DesBSTRDe(wencrypt_pkey, &wdecrypt_pkey, (unsigned char *)md5_localip);	//decrypt  the public key

	WideCharToMultiByte(CP_ACP, 0, wdecrypt_pkey, -1, tkey , 9 ,NULL, NULL);
	memcpy(pKey,tkey,8);

	SysFreeString(wlocalip);
	SysFreeString(wdes_localip);
	SysFreeString(wdecrypt_pkey);
	SysFreeString(wencrypt_pkey);

	return 0;
}


//************************************************************************************
int conndb(IDBObject **m_SQLObject,LPSTR dbip)
{
	char				path[260];
	unsigned int		cbsize = 260,
						ipsize = IP_LEN;
	
	if(am_LocaConfFile(ArkoMasterDbReg,ArkoDBValue,path,&cbsize))	//get the path of ARKODB.INI from register
		return 502;					
	strcat(path,ArkoDbFileName);
	if(am_GetConfIp (path,dbip,&ipsize))		//read the ARKODB.INI to get sqlservice's ipaddr
		return 502;	
	
	//connect the "sqlservice"
	COSERVERINFO srvinfo;
	memset(&srvinfo,0,sizeof(srvinfo));
		
	srvinfo.pwszName=MakeWideStrFromAnsi(dbip);

    MULTI_QI qi[1];   
	memset(qi, 0, sizeof(qi));    // initialize COM   

	qi[0].pIID =&IID_IDBObject;

	HRESULT hRes=CoCreateInstanceEx(
		CLSID_DBObject,				// Request an instance of class CLSID_MyBackupService
		NULL,							// No aggregation
		CLSCTX_SERVER,					// Any server is fine
		&srvinfo,						// Contains remote server name
		sizeof(qi)/sizeof(qi[0]),		// number of interfaces we are requesting (2)
		qi);

	SysFreeString(srvinfo.pwszName);

	if(hRes==S_OK)
		*m_SQLObject=(IDBObject *)qi[0].pItf;
	else
		return 501;
	return 0;
}


//******************************************************************************
int GetServiceIP(IDBObject *db_ptr,unsigned int serv_id)
{
	HRESULT		hRes=0;
	unsigned char		e_str128[128];
	tab_record	records;
	int			rec_number=0,
				offset=0,
				start=0;
	db_table select_table;
	select_table.encrypt_id=0;
	select_table.db_table_id=TABLE_SERVICE_REG|DB_SERVICE;

	sprintf((char *)select_table.sql_statement,
			"SELECT * FROM SERVICEREGTAB WHERE ServiceID=%u",serv_id);	//select the table basis on service identity

	DesStrEn(select_table.sql_statement,e_str128,PublicKey);
	strcpy((char *)select_table.sql_statement,(char *)e_str128);

	hRes=db_ptr->av_dbsql(&select_table,&records,&rec_number,&offset,start);
	if(hRes==1001)
		return ServiceNotStart;
	if(hRes!=0)
		return 503;

	tables_union *ret_records=(tables_union *)(records.records_buf);
	service_ip=(LPSTR)LocalAlloc(LMEM_FIXED, IP_LEN*sizeof(char));
	strcpy(service_ip,(char *)ret_records->table_svr_reg.IPAddress);

	return 0;
}


//************************************************************************************
int connect(IInventoryObj **s_ptr,LPSTR serv_ip)
{
	COSERVERINFO srvinfo;
	memset(&srvinfo,0,sizeof(srvinfo));
	srvinfo.pwszName=MakeWideStrFromAnsi(serv_ip);

    MULTI_QI qi[1];   
	memset(qi, 0, sizeof(qi));    // initialize COM   

	qi[0].pIID =&IID_IInventoryObj;
	HRESULT hRes=CoCreateInstanceEx(
		CLSID_InventoryObj,				// Request an instance of class CLSID_MyBackupService
		NULL,							// No aggregation
		CLSCTX_SERVER,					// Any server is fine
		&srvinfo,						// Contains remote server name
		sizeof(qi)/sizeof(qi[0]),		// number of interfaces we are requesting (2)
		qi);

	SysFreeString(srvinfo.pwszName);

	if(hRes==S_OK)
		*s_ptr=(IInventoryObj *)qi[0].pItf;
	else
		return -1;

	return 0;
}


//*************************************************************************************
int	WINAPI InvInit()
{
	CoInitialize(NULL);

	IDBObject	*db_ptr=NULL;
	char		dbip[IP_LEN];

	int			ret=0;

	if((ret=conndb(&db_ptr,dbip))!=0)		//connect database service,and return its ip.
		return ret;

	if((ret=GetPublicKey(db_ptr,dbip,(char *)PublicKey))!=0)		//	get the PublicKey
		return ret;

	if((ret=GetServiceIP(db_ptr,SERVICEID))!=0)			//	query corresponding service IP
		return ret;

	db_ptr->Release();

	return 0;
}


//*********************************************************************************
int WINAPI InvCheck(BSTR UserName,int *permission)
{
	IInventoryObj *s_ptr=0;
					
	if(connect(&s_ptr,service_ip)==-1)
		return -1;

	BSTR des_name=SysAllocStringByteLen(NULL, STR32*2);
	int x=DesBSTREn(UserName,&des_name,PublicKey);

	HRESULT hRes = s_ptr->Check(des_name, permission);
	if(hRes!=501 && hRes!=503)
		s_ptr->Release();

	SysFreeString(des_name);

	return hRes;
	
}


//*********************************************************************************
int WINAPI RecCount(BSTR DBname,long FAR *count)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	BSTR des_name=SysAllocStringByteLen(NULL, STR32*2);
	DesBSTREn(DBname, &des_name, PublicKey);

	HRESULT hRes=p_InvObj->RecCount(des_name, count);
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	RecordNum = *count;		//save the records number to this global variant

	SysFreeString(des_name);

	return hRes;
}


//*********************************************************************************
int	WINAPI SendPol(BSTR name,long andor1,long andor2,long andor3,long andor4,long andor5,long andor6,long andor7,BSTR CPUMin,BSTR CPUMax,BSTR MemMin,BSTR MemMax,BSTR DskMin,BSTR DskMax,long OSIndex, BSTR Equ,BSTR IPMin,BSTR IPMax,BSTR Mac)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	BSTR des_name=SysAllocStringByteLen(NULL, STR32*2);
	DesBSTREn(name, &des_name, PublicKey);
	BSTR des_CPUMin=SysAllocStringByteLen(NULL, STR32*2);
	DesBSTREn(CPUMin, &des_CPUMin, PublicKey);
	BSTR des_CPUMax=SysAllocStringByteLen(NULL, STR32*2);
	DesBSTREn(CPUMax, &des_CPUMax, PublicKey);
	BSTR des_MemMin=SysAllocStringByteLen(NULL, STR32*2);
	DesBSTREn(MemMin, &des_MemMin, PublicKey);
	BSTR des_MemMax=SysAllocStringByteLen(NULL, STR32*2);
	DesBSTREn(MemMax, &des_MemMax, PublicKey);
	BSTR des_DskMin=SysAllocStringByteLen(NULL, STR32*2);
	DesBSTREn(DskMin, &des_DskMin, PublicKey);
	BSTR des_DskMax=SysAllocStringByteLen(NULL, STR32*2);
	DesBSTREn(DskMax, &des_DskMax, PublicKey);
	BSTR des_Equ=SysAllocStringByteLen(NULL, STR128*2);
	DesBSTREn(Equ, &des_Equ, PublicKey);
	BSTR des_Mac=SysAllocStringByteLen(NULL, MAC_LEN*2);
	DesBSTREn(Mac, &des_Mac, PublicKey);

	HRESULT hRes=p_InvObj->SendPol(des_name, andor1, andor2, andor3, andor4, andor5, andor6, andor7, des_CPUMin, des_CPUMax, 
		                           des_MemMin, des_MemMax, des_DskMin, des_DskMax, OSIndex, des_Equ, IPMin, IPMax, Mac);
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	SysFreeString(des_name);
	SysFreeString(des_CPUMin);
	SysFreeString(des_CPUMax);
	SysFreeString(des_MemMin);
	SysFreeString(des_MemMax);
	SysFreeString(des_DskMin);
	SysFreeString(des_DskMax);
	SysFreeString(des_Equ);
	SysFreeString(des_Mac);

	return hRes;
}


//*********************************************************************************
int WINAPI SelectDyn()
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	HRESULT hRes=p_InvObj->SelectDyn();
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	return hRes;
}


//*********************************************************************************
int WINAPI SelectDyn1()
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	HRESULT hRes=p_InvObj->SelectDyn1();
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	return hRes;
}


//***********************************************************************************
int WINAPI SelectDyn2()
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	HRESULT hRes=p_InvObj->SelectDyn2();
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	return hRes;
}


//*********************************************************************************
int WINAPI SendPolNet(BSTR name,long andor1,long andor2,long andor3,BSTR Sysname,BSTR Vender,long Services)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	BSTR des_name=SysAllocStringByteLen(NULL, STR32*2);
	DesBSTREn(name, &des_name, PublicKey);
	BSTR des_Sysname=SysAllocStringByteLen(NULL, STR256*2);
	DesBSTREn(Sysname, &des_Sysname, PublicKey);
	BSTR des_Vender=SysAllocStringByteLen(NULL, STR128*2);
	DesBSTREn(Vender, &des_Vender, PublicKey);


	HRESULT hRes=p_InvObj->SendPolNet(des_name, andor1, andor2, andor3, des_Sysname, des_Vender, Services);
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	SysFreeString(des_name);
	SysFreeString(des_Sysname);
	SysFreeString(des_Vender);

	return hRes;
}


//*********************************************************************************
int WINAPI Sel_NetChart(BSTR EndSystemID, BSTR FromTime,BSTR ToTime)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	HRESULT hRes=p_InvObj->Sel_NetChart(EndSystemID, FromTime, ToTime);
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	return hRes;
}


//**********************************************************************************
int WINAPI Sel_StaChart(BSTR EndSystemID, BSTR FromTime,BSTR ToTime)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	HRESULT hRes=p_InvObj->Sel_StaChart(EndSystemID, FromTime, ToTime);
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	return hRes;
}


//**********************************************************************************
int WINAPI SelectNet(BSTR ObjectID, BSTR DeviceSysName)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	BSTR des_ObjectID = SysAllocStringByteLen(NULL, STR256*2);
	BSTR des_DeviceSysName=SysAllocStringByteLen(NULL, STR256*2);
	DesBSTREn(ObjectID, &des_ObjectID, PublicKey);
	DesBSTREn(DeviceSysName, &des_DeviceSysName, PublicKey);

	HRESULT hRes=p_InvObj->SelectNet(ObjectID, des_DeviceSysName);
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	SysFreeString(des_ObjectID);
	SysFreeString(des_DeviceSysName);

	return hRes;
}


//***********************************************************************************
int WINAPI ReceiveCONDEV(VARIANT FAR *oarray)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	long count=RecordNum;

	HRESULT hRes=p_InvObj->ReceiveCONDEV(oarray);
	if(hRes==0)
	{
		BSTR bstr;
		for(long index=0; index<9*count; index++)
		{
			//decrypt all elements in the VARIANT
			BSTR temp_bstr=SysAllocStringByteLen(NULL, STR256*2);
			hRes=SafeArrayGetElement(oarray->parray, &index, (void *)&bstr);
			DesBSTRDe(bstr, &temp_bstr, PublicKey);
			hRes=SafeArrayPutElement(oarray->parray, &index, (void *)temp_bstr);
			SysFreeString(temp_bstr);
			SysFreeString(bstr);
		}
	}
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	return hRes;
}


//***********************************************************************************
int WINAPI ReceiveCPU(VARIANT FAR *oarray)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	long count=RecordNum;

	HRESULT hRes=p_InvObj->ReceiveCPU(oarray);
	if(hRes==0)
	{
		BSTR bstr;
		for(long index=0; index<5*count; index++)
		{
			//decrypt all elements in the VARIANT
			BSTR temp_bstr=SysAllocStringByteLen(NULL, STR256*2);
			hRes=SafeArrayGetElement(oarray->parray, &index, (void *)&bstr);
			DesBSTRDe(bstr, &temp_bstr, PublicKey);
			hRes=SafeArrayPutElement(oarray->parray, &index, (void *)temp_bstr);
			SysFreeString(temp_bstr);
			SysFreeString(bstr);
		}
	}
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	return hRes;
}


//*******************************************************************************
int WINAPI ReceiveDEVICE(VARIANT FAR *oarray)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	long count=RecordNum;

	HRESULT hRes=p_InvObj->ReceiveDEVICE(oarray);
	if(hRes==0)
	{
		BSTR bstr;
		for(long index=0; index<8*count; index++)
		{
			//decrypt all elements in the VARIANT
			BSTR temp_bstr=SysAllocStringByteLen(NULL, STR256*2);
			hRes=SafeArrayGetElement(oarray->parray, &index, (void *)&bstr);
			DesBSTRDe(bstr, &temp_bstr, PublicKey);
			hRes=SafeArrayPutElement(oarray->parray, &index, (void *)temp_bstr);
			SysFreeString(temp_bstr);
			SysFreeString(bstr);
		}
	}
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	return hRes;
}


//*******************************************************************************
int WINAPI ReceiveFILE(VARIANT FAR *oarray)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	long count=RecordNum;

	HRESULT hRes=p_InvObj->ReceiveFILE(oarray);
	if(hRes==0)
	{
		BSTR bstr;
		for(long index=0; index<6*count; index++)
		{
			//decrypt all elements in the VARIANT
			BSTR temp_bstr=SysAllocStringByteLen(NULL, STR256*2);
			hRes=SafeArrayGetElement(oarray->parray, &index, (void *)&bstr);
			DesBSTRDe(bstr, &temp_bstr, PublicKey);
			hRes=SafeArrayPutElement(oarray->parray, &index, (void *)temp_bstr);
			SysFreeString(temp_bstr);
			SysFreeString(bstr);
		}
	}
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	return hRes;
}


//*****************************************************************************
int WINAPI ReceiveMAP(VARIANT FAR *oarray)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	long count=RecordNum;

	HRESULT hRes=p_InvObj->ReceiveMAP(oarray);
	if(hRes==0)
	{
		BSTR bstr;
		for(long index=0; index<4*count; index++)
		{
			//decrypt all elements in the VARIANT
			BSTR temp_bstr=SysAllocStringByteLen(NULL, STR256*2);
			hRes=SafeArrayGetElement(oarray->parray, &index, (void *)&bstr);
			DesBSTRDe(bstr, &temp_bstr, PublicKey);
			hRes=SafeArrayPutElement(oarray->parray, &index, (void *)temp_bstr);
			SysFreeString(temp_bstr);
			SysFreeString(bstr);
		}
	}
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	return hRes;
}


//*****************************************************************************
int WINAPI ReceiveMEM(VARIANT FAR *oarray)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	long count=RecordNum;

	HRESULT hRes=p_InvObj->ReceiveMEM(oarray);
	if(hRes==0)
	{
		BSTR bstr;
		for(long index=0; index<4*count; index++)
		{
			//decrypt all elements in the VARIANT
			BSTR temp_bstr=SysAllocStringByteLen(NULL, STR256*2);
			hRes=SafeArrayGetElement(oarray->parray, &index, (void *)&bstr);
			DesBSTRDe(bstr, &temp_bstr, PublicKey);
			hRes=SafeArrayPutElement(oarray->parray, &index, (void *)temp_bstr);
			SysFreeString(temp_bstr);
			SysFreeString(bstr);
		}
	}
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	return hRes;
}


//****************************************************************************
int WINAPI ReceiveNETSTAT(VARIANT FAR *oarray)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	long count=RecordNum;

	HRESULT hRes=p_InvObj->ReceiveNETSTAT(oarray);
	if(hRes==0)
	{
		BSTR bstr;
		for(long index=0; index<12*count; index++)
		{
			//decrypt all elements in the VARIANT
			BSTR temp_bstr=SysAllocStringByteLen(NULL, STR256*2);
			hRes=SafeArrayGetElement(oarray->parray, &index, (void *)&bstr);
			DesBSTRDe(bstr, &temp_bstr, PublicKey);
			hRes=SafeArrayPutElement(oarray->parray, &index, (void *)temp_bstr);
			SysFreeString(temp_bstr);
			SysFreeString(bstr);
		}
	}
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	return hRes;
}


//****************************************************************************
int WINAPI ReceiveNETWORK(VARIANT FAR *oarray)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	long count=RecordNum;

	HRESULT hRes=p_InvObj->ReceiveNETWORK(oarray);
	if(hRes==0)
	{
		BSTR bstr;
		for(long index=0; index<8*count; index++)
		{
			//decrypt all elements in the VARIANT
			BSTR temp_bstr=SysAllocStringByteLen(NULL, STR256*2);
			hRes=SafeArrayGetElement(oarray->parray, &index, (void *)&bstr);
			DesBSTRDe(bstr, &temp_bstr, PublicKey);
			hRes=SafeArrayPutElement(oarray->parray, &index, (void *)temp_bstr);
			SysFreeString(temp_bstr);
			SysFreeString(bstr);
		}
	}
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	return hRes;
}


//********************************************************************************
int WINAPI ReceiveOS(VARIANT FAR *oarray)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	long count=RecordNum;

	HRESULT hRes=p_InvObj->ReceiveOS(oarray);
	if(hRes==0)
	{
		BSTR bstr;
		for(long index=0; index<6*count; index++)
		{
			//decrypt all elements in the VARIANT
			BSTR temp_bstr=SysAllocStringByteLen(NULL, STR256*2);
			hRes=SafeArrayGetElement(oarray->parray, &index, (void *)&bstr);
			DesBSTRDe(bstr, &temp_bstr, PublicKey);
			hRes=SafeArrayPutElement(oarray->parray, &index, (void *)temp_bstr);
			SysFreeString(temp_bstr);
			SysFreeString(bstr);
		}
	}
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();


	return hRes;
}


//********************************************************************************
int WINAPI ReceiveSTAT(VARIANT FAR *oarray)
{
	IInventoryObj *p_InvObj=0;

	if(connect(&p_InvObj, service_ip)==-1)
		return -1;

	long count=RecordNum;

	HRESULT hRes=p_InvObj->ReceiveSTAT(oarray);
	if(hRes==0)
	{
		BSTR bstr;
		for(long index=0; index<6*count; index++)
		{
			//decrypt all elements in the VARIANT
			BSTR temp_bstr=SysAllocStringByteLen(NULL, STR256*2);
			hRes=SafeArrayGetElement(oarray->parray, &index, (void *)&bstr);
			DesBSTRDe(bstr, &temp_bstr, PublicKey);
			hRes=SafeArrayPutElement(oarray->parray, &index, (void *)temp_bstr);
			SysFreeString(temp_bstr);
			SysFreeString(bstr);
		}
	}
	if(hRes!=501 && hRes!=503)
		p_InvObj->Release();

	return hRes;
}