/** @file DogAuthImpl.cpp @brief 加密狗认证接口类的实现文件 @author 周正龙 */ #include "DogAuthImpl.h" #include "Common.h" #include "public/pub_logger_api/logger.h" #include "pub_utility_api/CommonConfigParse.h" #include "pub_utility_api/FileUtil.h" #include #include "boost/concept_check.hpp" #ifndef DISABLE_DOG_AUTH #ifdef KBD_X86 #include "sys_dog_auth_api/vendor_code.h" #endif #endif #define AUTH_MODEL_LICENCE 1 #define AUTH_MODEL_SUPPERDOG 2 #define MEM_MEMBUFFER_SIZE 128 #define PRODUCT_ID_PLAT 40 #define PRODUCT_ID_ISCS 41 #define PRODUCT_ID_PSCADA 42 #define PRODUCT_ID_EMS 43 #define PRODUCT_ID_PSMS 44 iot_sys::CDogAuthImpl::CDogAuthImpl() { #ifndef DISABLE_DOG_AUTH m_nProductId = PRODUCT_ID_PSCADA; m_ptrLicenseAuth = NULL; #ifdef KBD_X86 m_dogStatus = DOG_NOT_FOUND ; m_dogHandle = 0 ; #endif #endif } iot_sys::CDogAuthImpl::~CDogAuthImpl() { #ifndef DISABLE_DOG_AUTH #ifdef KBD_X86 if (m_dogHandle ) { dog_logout(m_dogHandle); m_dogStatus = DOG_NOT_FOUND ; m_dogHandle = 0 ; } #endif #endif } //检查系统授权状态 int iot_sys::CDogAuthImpl::checkAuthStatus() { // if(iotSuccess == checkDogAuthStatus()) // { // return iotSuccess; // } if(iotSuccess == checLicenseAuthStatus()) { return iotSuccess; } return iotFailed; } /** @brief 检查License授权状态 @return 成功返回iotSuccess,失败返回相应错误码 */ int iot_sys::CDogAuthImpl::checLicenseAuthStatus() { #ifndef DISABLE_DOG_AUTH QString strAppId ; std::string strLicensePath = iot_public::CFileUtil::getAbsolutePath( "../../product/common/license/" ) ; if(!m_ptrLicenseAuth->checkRegistration(QString::fromStdString(strLicensePath), "license.txt",strAppId) ) { LOGINFO("授权校验失败,AppId=[%s]!\n",strAppId.toStdString().c_str()); return iotFailed; } if(strAppId == "PSCADA" || strAppId == "EMS") { return iotSuccess; } else { LOGINFO("授权产品ID不匹配,AppId=[%s]!\n", strAppId.toStdString().c_str()); return iotFailed; } #endif return iotSuccess; } /** @brief 检查超级狗授权状态 @return 成功返回iotSuccess,失败返回相应错误码 */ int iot_sys::CDogAuthImpl::checkDogAuthStatus() { #ifndef DISABLE_DOG_AUTH #ifdef KBD_X86 unsigned char membuffer[MEM_MEMBUFFER_SIZE]; //如果狗状态异常,进行一次登录操作 //==================================================================================== if (DOG_STATUS_OK != m_dogStatus) //读写失败返回错误码 { loginSupperDog(m_nProductId); } //< //狗状态正常:dog_read read from data file //==================================================================================== m_dogStatus = dog_read(m_dogHandle,DOG_FILEID_RW, /* file ID */0, /* offset */ MEM_MEMBUFFER_SIZE,/* length */&membuffer[0]); /* file data */ switch (m_dogStatus) { case DOG_STATUS_OK: LOGDEBUG("超级狗文件读写状态:OK\n"); break; case DOG_INV_HND: LOGDEBUG("超级狗文件读写状态:handle not active\n"); break; case DOG_INV_FILEID: LOGDEBUG("超级狗文件读写状态:invalid file id\n"); break; case DOG_MEM_RANGE: LOGDEBUG("超级狗文件读写状态:exceeds data file range\n"); break; case DOG_NOT_FOUND: LOGDEBUG("超级狗文件读写状态:key/license container not available\n"); break; default: LOGDEBUG("超级狗文件读写状态:read memory failed\n"); } if (DOG_STATUS_OK != m_dogStatus) { //读写失败返回错误码 return m_dogStatus; // } #else return iotFailed; #endif #endif return iotSuccess; } /* @brief 初始化 */ int iot_sys::CDogAuthImpl::initialize() { #ifndef DISABLE_DOG_AUTH //获取软件产品编号 不同编号对应不同ID //==================================================================================== iot_public::CCommonConfigParse objCfgParse; //加载配置文件,失败则使用默认值,构造函数中已设置 m_nProductId = PRODUCT_ID_PSCADA if(iotSuccess == objCfgParse.load( iot_public::CFileUtil::getPathOfCfgFile("setup_config.xml",CN_DIR_PRODUCT))) { //获取产品ID if(iotSuccess != objCfgParse.getIntValue("setup_config","software_product_id", m_nProductId)) { LOGWARN("CDogAuthImpl::initialize() get product id fail, use default.\n"); } } else { LOGWARN("CDogAuthImpl::initialize() load setup_config.xml failed, use default.\n"); } m_ptrLicenseAuth = getLicenseAuthInstance(); // if(NULL == m_ptrLicenseAuth) { LOGERROR("CDogAuthImpl::initialize() getLicenseAuthInstance fail .\n"); return iotFailed; } #endif return iotSuccess; } /** * @brief :CDogAuthImpl::loginSupperDog() * 超级狗登录 * @return 成功返回iotSuccess,失败返回iotFailed */ int iot_sys::CDogAuthImpl::loginSupperDog(int nProductId) { boost::ignore_unused_variable_warning(nProductId); #ifndef DISABLE_DOG_AUTH #ifdef KBD_X86 //超级狗登陆状态不正常,退出 //==================================================================================== if (m_dogStatus != DOG_STATUS_OK) //登陆状态不正常 { //< 重新登陆 LOGDEBUG("初始化加密狗授权信息,产品ID[%d].\n ", nProductId); m_dogStatus = dog_login(nProductId, (dog_vendor_code_t *)vendorCode, &m_dogHandle); switch (m_dogStatus) { case DOG_STATUS_OK: LOGDEBUG("超级狗登陆状态:OK\n"); break; case DOG_FEATURE_NOT_FOUND: LOGDEBUG("超级狗登陆状态:login to default feature failed\n"); break; case DOG_NOT_FOUND: LOGDEBUG("超级狗登陆状态:no SuperDog with vendor code DEMOMA found\n"); break; case DOG_INV_VCODE: LOGDEBUG("超级狗登陆状态:invalid vendor code\n"); break; case DOG_LOCAL_COMM_ERR: LOGDEBUG("超级狗登陆状态:communication error between API and local SuperDog License Manager\n"); break; default: LOGDEBUG("超级狗登陆状态:login to default feature failed with status %d\n", m_dogStatus); } if (DOG_STATUS_OK != m_dogStatus) //登陆失败,句柄=-1; { dog_logout(m_dogHandle); } } #else return iotFailed; #endif #endif return iotSuccess; }