tss2_changeauth.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "tools/fapi/tss2_template.h"
  6. /* needed to conditionally free variable authValue */
  7. static bool has_asked_for_password = false;
  8. /* Context struct used to store passed commandline parameters */
  9. static struct cxt {
  10. char *entityPath;
  11. char *authValue;
  12. } ctx;
  13. /* Parse commandline parameters */
  14. static bool on_option(char key, char *value) {
  15. switch (key) {
  16. case 'a':
  17. ctx.authValue = value;
  18. break;
  19. case 'p':
  20. ctx.entityPath = value;
  21. break;
  22. }
  23. return true;
  24. }
  25. /* Define possible commandline parameters */
  26. static bool tss2_tool_onstart(tpm2_options **opts) {
  27. struct option topts[] = {
  28. {"authValue", required_argument, NULL, 'a'},
  29. {"entityPath", required_argument, NULL, 'p'}
  30. };
  31. return (*opts = tpm2_options_new ("a:p:", ARRAY_LEN(topts), topts,
  32. on_option, NULL, 0)) != NULL;
  33. }
  34. /* Execute specific tool */
  35. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  36. /* Check availability of required parameters */
  37. if (!ctx.entityPath) {
  38. fprintf (stderr, "No entity path provided, use --entityPath\n");
  39. return -1;
  40. }
  41. /* If no authValue was given, prompt the user interactively */
  42. if (!ctx.authValue) {
  43. ctx.authValue = ask_for_password ();
  44. has_asked_for_password = true;
  45. if (!ctx.authValue){
  46. return 1; /* User entered two different passwords */
  47. }
  48. }
  49. /* Execute FAPI command with passed arguments */
  50. TSS2_RC r = Fapi_ChangeAuth(fctx, ctx.entityPath, ctx.authValue);
  51. if (r != TSS2_RC_SUCCESS) {
  52. if(has_asked_for_password){
  53. free (ctx.authValue);
  54. }
  55. LOG_PERR ("Fapi_ChangeAuth", r);
  56. return 1;
  57. }
  58. if(has_asked_for_password){
  59. free (ctx.authValue);
  60. }
  61. return 0;
  62. }
  63. TSS2_TOOL_REGISTER("changeauth", tss2_tool_onstart, tss2_tool_onrun, NULL)