cse_provision_tool.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. /**
  32. * File:
  33. * cse_provision_tool.cpp
  34. *Description:
  35. * provision CSE Fw
  36. *
  37. */
  38. #include <stdio.h>
  39. #include <stdint.h>
  40. #include <dlfcn.h>
  41. #include <stdarg.h>
  42. #include <unistd.h>
  43. #include <sys/types.h>
  44. static char ICLS_LIB_NAME[] = "libiclsclient.so";
  45. static char ICLS_INIT_FUNC_NAME[] = "iclsInit";
  46. typedef uint32_t(*iclsInit_t)(const void*);
  47. #define STATUS_OK (0)
  48. #define PARA_ERROR (1)
  49. #define ICLS_MISS (2)
  50. #define ICLS_ERROR (3)
  51. static char USAGE_STRING[] = \
  52. "Usage: cse_provision_tool\n"\
  53. "Invoke iclsclient to provision CSE Fw. Root privilege is required.\n";
  54. static char MISSING_STRING[] = \
  55. "libiclsclient.so or iclsInit() cannot be found.\n"\
  56. "Trusted platform service is unavailable. Refer to README for details.\n";
  57. static char ERR_STRING[] = \
  58. "iclsInit() returned error.\n"\
  59. "Trusted platform service is unavailable. "\
  60. "Check log in /opt/Intel/iclsClient/log/iclsClient.log.\n";
  61. uint32_t upse_iclsInit()
  62. {
  63. uint32_t status = STATUS_OK;
  64. void *hmodule = NULL;
  65. do{
  66. //For this to work iclsClient needs to be installed in the system
  67. hmodule = dlopen(ICLS_LIB_NAME, RTLD_LAZY);
  68. if(NULL == hmodule)
  69. {
  70. status = ICLS_MISS;
  71. break;
  72. }
  73. //clear exist error code
  74. dlerror();
  75. iclsInit_t iclsInit = (iclsInit_t)dlsym(hmodule, ICLS_INIT_FUNC_NAME);
  76. if ( NULL != dlerror() || NULL == iclsInit)
  77. {
  78. status = ICLS_MISS;
  79. break;
  80. }
  81. //If you get error, check /opt/Intel/iclsClient/log/iclsClient.log
  82. uint32_t status_provision = iclsInit(0);
  83. if (status_provision != STATUS_OK)
  84. {
  85. status = ICLS_ERROR;
  86. break;
  87. }
  88. } while(0);
  89. if(hmodule)
  90. dlclose(hmodule);
  91. return status;
  92. }
  93. int main(int argc, char* argv[])
  94. {
  95. (void)argv;
  96. if(argc != 1 || getuid() != 0)
  97. {
  98. fprintf(stderr, "%s", USAGE_STRING);
  99. return PARA_ERROR;
  100. }
  101. uint32_t status_provision = upse_iclsInit();
  102. switch (status_provision)
  103. {
  104. case STATUS_OK:
  105. break;
  106. case ICLS_MISS:
  107. fprintf(stderr, "%s", MISSING_STRING);
  108. break;
  109. case ICLS_ERROR:
  110. fprintf(stderr, "%s", ERR_STRING);
  111. break;
  112. }
  113. return status_provision;
  114. }