tss2_exportpolicy.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "tools/fapi/tss2_template.h"
  7. /* Context struct used to store passed command line parameters */
  8. static struct cxt {
  9. char *path;
  10. char *jsonPolicy;
  11. bool overwrite;
  12. } ctx;
  13. /* Parse command line parameters */
  14. static bool on_option(char key, char *value) {
  15. switch (key) {
  16. case 'f':
  17. ctx.overwrite = true;
  18. break;
  19. case 'o':
  20. ctx.jsonPolicy = value;
  21. break;
  22. case 'p':
  23. ctx.path = value;
  24. break;
  25. }
  26. return true;
  27. }
  28. /* Define possible command line parameters */
  29. static bool tss2_tool_onstart(tpm2_options **opts) {
  30. struct option topts[] = {
  31. {"force", no_argument , NULL, 'f'},
  32. {"path", required_argument, NULL, 'p'},
  33. {"jsonPolicy", required_argument, NULL, 'o'},
  34. };
  35. return (*opts = tpm2_options_new ("fo:p:", ARRAY_LEN(topts), topts,
  36. on_option, NULL, 0)) != NULL;
  37. }
  38. /* Execute specific tool */
  39. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  40. /* Check availability of required parameters */
  41. if (!ctx.path) {
  42. fprintf (stderr, "path parameter is missing, pass --path\n");
  43. return -1;
  44. }
  45. if (!ctx.jsonPolicy) {
  46. fprintf (stderr, "parameter jsonPolicy is missing, pass --jsonPolicy\n");
  47. return -1;
  48. }
  49. /* Execute FAPI command with passed arguments */
  50. char *jsonPolicy;
  51. TSS2_RC r = Fapi_ExportPolicy (fctx, ctx.path, &jsonPolicy);
  52. if (r != TSS2_RC_SUCCESS) {
  53. LOG_PERR ("Fapi_PolicyExport", r);
  54. return 1;
  55. }
  56. /* Write returned data to file(s) */
  57. r = open_write_and_close (ctx.jsonPolicy, ctx.overwrite, jsonPolicy,
  58. strlen(jsonPolicy));
  59. if (r){
  60. Fapi_Free (jsonPolicy);
  61. return 1;
  62. }
  63. Fapi_Free (jsonPolicy);
  64. return 0;
  65. }
  66. TSS2_TOOL_REGISTER("exportpolicy", tss2_tool_onstart, tss2_tool_onrun, NULL)