From 61233aac971d1acb37d323ed47f5c62341d64ce7 Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Mon, 12 Jun 2023 10:49:35 +0800 Subject: [PATCH] =?UTF-8?q?TSG-15458=20=E5=9F=BA=E4=BA=8ESentinel=20LDK?= =?UTF-8?q?=E7=9A=84C=20API=E6=A3=80=E6=9F=A5=E8=BD=AF=E9=94=81/=E7=A1=AC?= =?UTF-8?q?=E9=94=81=E7=9A=84=E6=8E=88=E6=9D=83=E6=98=AF=E5=90=A6=E6=9C=89?= =?UTF-8?q?=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitlab-ci.yml | 34 ++--- CMakeLists.txt | 6 +- ci/travis.sh | 6 +- cmake/Package.cmake | 8 +- conf/hasp-tools.conf | 2 + platform/CMakeLists.txt | 15 +- platform/include/hasp_verify.h | 17 +++ platform/src/hasp_verify.c | 230 +++++++++++++++++++++++++++++++ platform/test/CMakeLists.txt | 6 + platform/test/hasp_verify_test.c | 30 ++++ 10 files changed, 325 insertions(+), 29 deletions(-) create mode 100644 conf/hasp-tools.conf create mode 100644 platform/include/hasp_verify.h create mode 100644 platform/src/hasp_verify.c create mode 100644 platform/test/CMakeLists.txt create mode 100644 platform/test/hasp_verify_test.c diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a1826e6..38984a3 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,7 +1,7 @@ variables: GIT_STRATEGY: "clone" BUILD_PADDING_PREFIX: /tmp/padding_for_CPACK_RPM_BUILD_SOURCE_DIRS_PREFIX_PREFIX_PREFIX_PREFIX_PREFIX_PREFIX/ - INSTALL_PREFIX: "/opt/tsg/hasp_update" + INSTALL_PREFIX: "/opt/tsg/hasp-tools" TESTING_VERSION_BUILD: 0 BUILD_IMAGE_CENTOS7: "git.mesalab.cn:7443/mesa_platform/build-env:master" BUILD_IMAGE_CENTOS8: "git.mesalab.cn:7443/mesa_platform/build-env:rockylinux" @@ -52,13 +52,13 @@ develop_build_debug_centos7: extends: .build_by_travis_for_centos7 variables: TESTING_VERSION_BUILD: 1 - UPLOAD_SYMBOL_FILES: 1 + #UPLOAD_SYMBOL_FILES: 0 BUILD_TYPE: Debug PACKAGE: 1 PULP3_REPO_NAME: tsg-testing-x86_64.el7 PULP3_DIST_NAME: tsg-testing-x86_64.el7 artifacts: - name: "hasp_update-develop-$CI_COMMIT_REF_NAME-debug" + name: "hasp-tools-develop-$CI_COMMIT_REF_NAME-debug" paths: - build/*.rpm only: @@ -70,13 +70,13 @@ develop_build_release_centos7: extends: .build_by_travis_for_centos7 variables: TESTING_VERSION_BUILD: 1 - UPLOAD_SYMBOL_FILES: 1 + #UPLOAD_SYMBOL_FILES: 0 BUILD_TYPE: RelWithDebInfo PACKAGE: 1 PULP3_REPO_NAME: tsg-testing-x86_64.el7 PULP3_DIST_NAME: tsg-testing-x86_64.el7 artifacts: - name: "hasp_update-develop-$CI_COMMIT_REF_NAME-release" + name: "hasp-tools-develop-$CI_COMMIT_REF_NAME-release" paths: - build/*.rpm only: @@ -86,14 +86,14 @@ develop_build_release_centos7: release_build_debug_centos7: stage: build variables: - UPLOAD_SYMBOL_FILES: 1 + #UPLOAD_SYMBOL_FILES: 0 BUILD_TYPE: Debug PACKAGE: 1 PULP3_REPO_NAME: tsg-stable-x86_64.el7 PULP3_DIST_NAME: tsg-stable-x86_64.el7 extends: .build_by_travis_for_centos7 artifacts: - name: "hasp_update-install-$CI_COMMIT_REF_NAME-debug" + name: "hasp-tools-install-$CI_COMMIT_REF_NAME-debug" paths: - build/*.rpm only: @@ -103,13 +103,13 @@ release_build_release_centos7: stage: build variables: BUILD_TYPE: RelWithDebInfo - UPLOAD_SYMBOL_FILES: 1 + #UPLOAD_SYMBOL_FILES: 0 PACKAGE: 1 PULP3_REPO_NAME: tsg-stable-x86_64.el7 PULP3_DIST_NAME: tsg-stable-x86_64.el7 extends: .build_by_travis_for_centos7 artifacts: - name: "hasp_update-install-$CI_COMMIT_REF_NAME-release" + name: "hasp-tools-install-$CI_COMMIT_REF_NAME-release" paths: - build/*.rpm only: @@ -158,13 +158,13 @@ develop_build_debug_centos8: extends: .build_by_travis_for_centos8 variables: TESTING_VERSION_BUILD: 1 - UPLOAD_SYMBOL_FILES: 1 + #UPLOAD_SYMBOL_FILES: 0 BUILD_TYPE: Debug PACKAGE: 1 PULP3_REPO_NAME: tsg-testing-x86_64.el8 PULP3_DIST_NAME: tsg-testing-x86_64.el8 artifacts: - name: "hasp_update-develop-$CI_COMMIT_REF_NAME-debug" + name: "hasp-tools-develop-$CI_COMMIT_REF_NAME-debug" paths: - build/*.rpm only: @@ -176,13 +176,13 @@ develop_build_release_centos8: extends: .build_by_travis_for_centos8 variables: TESTING_VERSION_BUILD: 1 - UPLOAD_SYMBOL_FILES: 1 + #UPLOAD_SYMBOL_FILES: 0 BUILD_TYPE: RelWithDebInfo PACKAGE: 1 PULP3_REPO_NAME: tsg-testing-x86_64.el8 PULP3_DIST_NAME: tsg-testing-x86_64.el8 artifacts: - name: "hasp_update-develop-$CI_COMMIT_REF_NAME-release" + name: "hasp-tools-develop-$CI_COMMIT_REF_NAME-release" paths: - build/*.rpm only: @@ -192,14 +192,14 @@ develop_build_release_centos8: release_build_debug_centos8: stage: build variables: - UPLOAD_SYMBOL_FILES: 1 + #UPLOAD_SYMBOL_FILES: 0 BUILD_TYPE: Debug PACKAGE: 1 PULP3_REPO_NAME: tsg-stable-x86_64.el8 PULP3_DIST_NAME: tsg-stable-x86_64.el8 extends: .build_by_travis_for_centos8 artifacts: - name: "hasp_update-install-$CI_COMMIT_REF_NAME-debug" + name: "hasp-tools-install-$CI_COMMIT_REF_NAME-debug" paths: - build/*.rpm only: @@ -209,13 +209,13 @@ release_build_release_centos8: stage: build variables: BUILD_TYPE: RelWithDebInfo - UPLOAD_SYMBOL_FILES: 1 + #UPLOAD_SYMBOL_FILES: 0 PACKAGE: 1 PULP3_REPO_NAME: tsg-stable-x86_64.el8 PULP3_DIST_NAME: tsg-stable-x86_64.el8 extends: .build_by_travis_for_centos8 artifacts: - name: "hasp_update-install-$CI_COMMIT_REF_NAME-release" + name: "hasp-tools-install-$CI_COMMIT_REF_NAME-release" paths: - build/*.rpm only: diff --git a/CMakeLists.txt b/CMakeLists.txt index e3a62dc..7a93531 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.5) -project(hasp_update) +project(hasp-tools) set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) include(Version) @@ -14,7 +14,7 @@ if(NOT CMAKE_BUILD_TYPE) endif() if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) - set (CMAKE_INSTALL_PREFIX "/opt/tsg/hasp_update" CACHE PATH "default install path" FORCE ) + set (CMAKE_INSTALL_PREFIX "/opt/tsg/hasp-tools" CACHE PATH "default install path" FORCE ) endif() # Global compile options @@ -58,3 +58,5 @@ add_custom_target("install-program" COMMAND ${CMAKE_COMMAND} ARGS -DCOMPONENT=Pr add_custom_target("install-profile" COMMAND ${CMAKE_COMMAND} ARGS -DCOMPONENT=Profile -P cmake_install.cmake) add_subdirectory(platform) + +install(FILES conf/hasp-tools.conf DESTINATION /etc/ld.so.conf.d/ COMPONENT Profile) \ No newline at end of file diff --git a/ci/travis.sh b/ci/travis.sh index 3186bd8..dfe13aa 100644 --- a/ci/travis.sh +++ b/ci/travis.sh @@ -56,7 +56,7 @@ if [ -n "${PACKAGE}" ]; then fi if [ -n "${UPLOAD_SYMBOL_FILES}" ]; then - rpm -i hasp_update*debuginfo*.rpm - cp /usr/lib/debug/opt/tsg/hasp_update/bin/hasp_update*debug /tmp/hasp_update.debuginfo.${CI_COMMIT_SHORT_SHA} - sentry-cli upload-dif -t elf /tmp/hasp_update.debuginfo.${CI_COMMIT_SHORT_SHA} + rpm -i hasp-tools*debuginfo*.rpm + cp /usr/lib/debug/opt/tsg/hasp-tools/bin/hasp-tools*debug /tmp/hasp-tools.debuginfo.${CI_COMMIT_SHORT_SHA} + sentry-cli upload-dif -t elf /tmp/hasp-tools.debuginfo.${CI_COMMIT_SHORT_SHA} fi \ No newline at end of file diff --git a/cmake/Package.cmake b/cmake/Package.cmake index 916aff1..dcba12e 100644 --- a/cmake/Package.cmake +++ b/cmake/Package.cmake @@ -1,7 +1,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug") - set(CPACK_PACKAGE_NAME "hasp_update-debug") + set(CPACK_PACKAGE_NAME "hasp-tools-debug") else() - set(CPACK_PACKAGE_NAME "hasp_update") + set(CPACK_PACKAGE_NAME "hasp-tools") endif() message(STATUS "Package: ${CPACK_PACKAGE_NAME}") @@ -22,9 +22,9 @@ set(CPACK_RPM_DEBUGINFO_PACKAGE on) # Must uninstall the debug package before install release package if(CMAKE_BUILD_TYPE STREQUAL "Debug") - set(CPACK_RPM_PACKAGE_CONFLICTS "hasp_update") + set(CPACK_RPM_PACKAGE_CONFLICTS "hasp-tools") else() - set(CPACK_RPM_PACKAGE_CONFLICTS "hasp_update-debug") + set(CPACK_RPM_PACKAGE_CONFLICTS "hasp-tools-debug") endif() include(CPack) \ No newline at end of file diff --git a/conf/hasp-tools.conf b/conf/hasp-tools.conf new file mode 100644 index 0000000..086c342 --- /dev/null +++ b/conf/hasp-tools.conf @@ -0,0 +1,2 @@ +# hasp tools library path +/opt/tsg/hasp-tools/lib/ \ No newline at end of file diff --git a/platform/CMakeLists.txt b/platform/CMakeLists.txt index 94ef373..7319f95 100644 --- a/platform/CMakeLists.txt +++ b/platform/CMakeLists.txt @@ -1,8 +1,17 @@ +# compile hasp_update add_executable(hasp_update src/hasp_update.c) - target_include_directories(hasp_update PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include/) - target_link_libraries(hasp_update pthread) target_link_libraries(hasp_update ${CMAKE_SOURCE_DIR}/lib/libhasp_linux_x86_64_25743.a) +install(TARGETS hasp_update RUNTIME DESTINATION bin COMPONENT Program) -install(TARGETS hasp_update RUNTIME DESTINATION bin COMPONENT Program) \ No newline at end of file +# compile lib hasp-tools +add_library(hasp-tools SHARED src/hasp_verify.c) +target_include_directories(hasp-tools PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include) +target_link_libraries(hasp-tools pthread) +target_link_libraries(hasp-tools ${CMAKE_SOURCE_DIR}/lib/libhasp_linux_x86_64_25743.a) +install(TARGETS hasp-tools LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/ COMPONENT LIBRARIES) +install(FILES include/hasp_verify.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/ COMPONENT LIBRARIES) + +# compile hasp_verify +add_subdirectory(test) \ No newline at end of file diff --git a/platform/include/hasp_verify.h b/platform/include/hasp_verify.h new file mode 100644 index 0000000..263755d --- /dev/null +++ b/platform/include/hasp_verify.h @@ -0,0 +1,17 @@ +#ifndef _HASP_VERIFY_H +#define _HASP_VERIFY_H + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include + +void hasp_verify(uint64_t feature_id, uint64_t interval_s); + +#ifdef __cpluscplus +} +#endif + +#endif diff --git a/platform/src/hasp_verify.c b/platform/src/hasp_verify.c new file mode 100644 index 0000000..d02532e --- /dev/null +++ b/platform/src/hasp_verify.c @@ -0,0 +1,230 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "hasp_api.h" +#include "hasp_vcode.h" + +#define LOG_STOUT(time, format, ...) \ + { \ + fprintf(stdout, "%s " format "\n", time, ##__VA_ARGS__); \ + } + +#if ENABLD_LOG_FIEL +#define LOG_FILE(time, format, ...) \ + { \ + FILE *fp = fopen("licenses.log", "a+"); \ + if (fp == NULL) \ + { \ + break; \ + } \ + fprintf(fp, "%s " format "\n", time, ##__VA_ARGS__); \ + fflush(fp); \ + fclose(fp); \ + } +#else +#define LOG_FILE(time, format, ...) +#endif + +#define LOG_INFO(format, ...) \ + do \ + { \ + char buffer[64] = {0}; \ + local_time_string(buffer, sizeof(buffer)); \ + LOG_STOUT(buffer, format, ##__VA_ARGS__); \ + LOG_FILE(buffer, format, ##__VA_ARGS__); \ + } while (0) + +#define DEFAULT_INTERVAL_S (30 * 60) + +static uint64_t g_interval_s = 0; +static uint64_t g_feature_id = 0; + +static void local_time_string(char *buff, int size) +{ + static unsigned char weekday_str[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; + static unsigned char month_str[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + + time_t now; + struct tm local_time; + time(&now); + if (NULL == (localtime_r(&now, &local_time))) + { + return; + } + snprintf(buff, size, + "%s %s %d %02d:%02d:%02d %d", + weekday_str[local_time.tm_wday], + month_str[local_time.tm_mon], + local_time.tm_mday, + local_time.tm_hour, + local_time.tm_min, + local_time.tm_sec, + local_time.tm_year + 1900); +} + +// return 0: error +// reutrn 1: succes +static int verify(uint64_t feature_id) +{ + int ret = 0; + hasp_handle_t handle; + hasp_status_t status = hasp_login(feature_id, (hasp_vendor_code_t)vendor_code, &handle); + if (status == HASP_STATUS_OK) + { + ret = 1; + } + else + { + switch (status) + { + case HASP_STATUS_OK: + LOG_INFO("hasp_verify: Request was successfully completed"); + break; + case HASP_HASP_NOT_FOUND: + LOG_INFO("hasp_verify: Required Sentinel protection key not found"); + break; + case HASP_FEATURE_NOT_FOUND: + LOG_INFO("hasp_verify: Cannot find requested Feature"); + break; + case HASP_FEATURE_TYPE_NOT_IMPL: + LOG_INFO("hasp_verify: Requested Feature type not available"); + break; + case HASP_TMOF: + LOG_INFO("hasp_verify: Too many open login sessions"); + break; + case HASP_INSUF_MEM: + LOG_INFO("hasp_verify: Out of memory"); + break; + case HASP_INV_VCODE: + LOG_INFO("hasp_verify: Invalid Vendor Code"); + break; + case HASP_NO_DRIVER: + LOG_INFO("hasp_verify: Driver not installed"); + break; + case HASP_NO_VLIB: + LOG_INFO("hasp_verify: Vendor library cannot be found"); + break; + case HASP_INV_VLIB: + LOG_INFO("hasp_verify: Vendor library cannot be loaded"); + break; + case HASP_OLD_DRIVER: + LOG_INFO("hasp_verify: Driver too old"); + break; + case HASP_UNKNOWN_VCODE: + LOG_INFO("hasp_verify: Vendor Code not recognized"); + break; + case HASP_FEATURE_EXPIRED: + LOG_INFO("hasp_verify: Feature has expired"); + break; + case HASP_TOO_MANY_USERS: + LOG_INFO("hasp_verify: Too many users currently connected"); + break; + case HASP_OLD_LM: + LOG_INFO("hasp_verify: Sentinel License Manager version too old"); + break; + case HASP_DEVICE_ERR: + LOG_INFO("hasp_verify: Input/Output error in Sentinel SL/SL-AdminMode/SL-UserMode secure storage, OR in case of a Sentinel HL key, USB communication error"); + break; + case HASP_TIME_ERR: + LOG_INFO("hasp_verify: System time has been tampered with"); + break; + case HASP_HARDWARE_MODIFIED: + LOG_INFO("hasp_verify: Sentinel SL key incompatible with machine hardware; Sentinel SL key is locked to different hardware"); + break; + case HASP_TS_DETECTED: + LOG_INFO("hasp_verify: Program is running on a Terminal Server"); + break; + case HASP_LOCAL_COMM_ERR: + LOG_INFO("hasp_verify: Communication error between API and local Sentinel License Manager"); + break; + case HASP_REMOTE_COMM_ERR: + LOG_INFO("hasp_verify: Communication error between local and remote Sentinel License Manager"); + break; + case HASP_OLD_VLIB: + LOG_INFO("hasp_verify: Vendor Library version too old"); + break; + case HASP_CLONE_DETECTED: + LOG_INFO("hasp_verify: Cloned Sentinel SL storage detected. Feature unavailable"); + break; + default: + LOG_INFO("hasp_verify: failed with status %u", status); + break; + } + + ret = 0; + } + + hasp_logout(handle); + + return ret; +} + +// APP 启动时若 licenses 无效则直接退出 +// APP 启动后,每隔 g_interval_s 检查一次 licenses;如 2 * g_interval_s 时间内出现两次 licenses 无效则退出 +static void *thread_cycle(void *arg) +{ + uint64_t last_timestamp = 0; + + if (verify(g_feature_id) == 0) + { + exit(0); + } + + while (1) + { + sleep(g_interval_s); + + if (verify(g_feature_id) == 1) + { + continue; + } + else + { + struct timespec current_ts; + clock_gettime(CLOCK_MONOTONIC, ¤t_ts); + + if (last_timestamp == 0) + { + last_timestamp = current_ts.tv_sec; + continue; + } + + if (current_ts.tv_sec - last_timestamp < g_interval_s * 2) + { + exit(0); + } + else + { + last_timestamp = current_ts.tv_sec; + } + } + } +} + +void hasp_verify(uint64_t feature_id, uint64_t interval_s) +{ + pthread_t tid; + + g_feature_id = feature_id; + if (interval_s == 0) + { + g_interval_s = DEFAULT_INTERVAL_S; + } + else + { + g_interval_s = interval_s; + } + + LOG_INFO("hasp_verify: Feature ID: %ld, Check Interval %ld s", g_feature_id, g_interval_s); + + if (pthread_create(&tid, NULL, thread_cycle, NULL) < 0) + { + LOG_INFO("hasp_verify: unable to create thread, error %d: %s", errno, strerror(errno)); + exit(0); + } +} \ No newline at end of file diff --git a/platform/test/CMakeLists.txt b/platform/test/CMakeLists.txt new file mode 100644 index 0000000..6914e4b --- /dev/null +++ b/platform/test/CMakeLists.txt @@ -0,0 +1,6 @@ +add_definitions(-DENABLD_LOG_FIEL) +add_executable(hasp_verify ${CMAKE_SOURCE_DIR}/platform/src/hasp_verify.c ${CMAKE_SOURCE_DIR}/platform/test/hasp_verify_test.c) +target_include_directories(hasp_verify PUBLIC ${CMAKE_SOURCE_DIR}/platform/include/) +target_link_libraries(hasp_verify pthread) +target_link_libraries(hasp_verify ${CMAKE_SOURCE_DIR}/lib/libhasp_linux_x86_64_25743.a) +install(TARGETS hasp_verify RUNTIME DESTINATION bin COMPONENT Program) \ No newline at end of file diff --git a/platform/test/hasp_verify_test.c b/platform/test/hasp_verify_test.c new file mode 100644 index 0000000..8303ae8 --- /dev/null +++ b/platform/test/hasp_verify_test.c @@ -0,0 +1,30 @@ +#include +#include +#include + +#include "hasp_verify.h" + +int main(int argc, char **argv) +{ + uint64_t interval_s = 0; + uint64_t feature_id = 0; + if (argc != 3) + { + fprintf(stdout, "Usage: %s [feature_id] [interval_s]\n", argv[0]); + exit(0); + } + else + { + feature_id = atol(argv[1]); + interval_s = atol(argv[2]); + } + + hasp_verify(feature_id, interval_s); + + while (1) + { + sleep(1); + } + + return 0; +} \ No newline at end of file