keyregen.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "platform.h"
  2. #include <sys/types.h>
  3. #include <sys/file.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <termios.h>
  11. #include <unistd.h>
  12. #include "crypto.h"
  13. #include "getopt.h"
  14. #include "humansize.h"
  15. #include "keyfile.h"
  16. #include "keygen.h"
  17. #include "readpass.h"
  18. #include "sysendian.h"
  19. #include "tarsnap_opt.h"
  20. #include "tsnetwork.h"
  21. #include "warnp.h"
  22. static void usage(void);
  23. /* Be noisy about network errors while registering a machine. */
  24. int tarsnap_opt_noisy_warnings = 1;
  25. static void
  26. usage(void)
  27. {
  28. fprintf(stderr, "usage: tarsnap-keyregen %s %s %s %s %s %s %s\n",
  29. "--keyfile key-file", "--oldkey old-key-file",
  30. "--user user-name", "--machine machine-name",
  31. "[--passphrased]", "[--passphrase-mem maxmem]",
  32. "[--passphrase-time maxtime]");
  33. fprintf(stderr, " tarsnap-keyregen --version\n");
  34. exit(1);
  35. /* NOTREACHED */
  36. }
  37. int
  38. main(int argc, char **argv)
  39. {
  40. struct register_internal C;
  41. const char * keyfilename;
  42. const char * oldkeyfilename;
  43. int passphrased;
  44. uint64_t maxmem;
  45. double maxtime;
  46. const char * ch;
  47. WARNP_INIT;
  48. /*
  49. * We have no username, machine name, key filename, or old key
  50. * filename yet.
  51. */
  52. C.user = C.name = NULL;
  53. keyfilename = NULL;
  54. oldkeyfilename = NULL;
  55. /*
  56. * So far we're not using a passphrase, have unlimited RAM, and allow
  57. * up to 1 second of CPU time.
  58. */
  59. passphrased = 0;
  60. maxmem = 0;
  61. maxtime = 1.0;
  62. /* Parse arguments. */
  63. while ((ch = GETOPT(argc, argv)) != NULL) {
  64. GETOPT_SWITCH(ch) {
  65. GETOPT_OPTARG("--user"):
  66. if (C.user != NULL)
  67. usage();
  68. C.user = optarg;
  69. break;
  70. GETOPT_OPTARG("--machine"):
  71. if (C.name != NULL)
  72. usage();
  73. C.name = optarg;
  74. break;
  75. GETOPT_OPTARG("--keyfile"):
  76. if (keyfilename != NULL)
  77. usage();
  78. keyfilename = optarg;
  79. break;
  80. GETOPT_OPTARG("--oldkey"):
  81. if (oldkeyfilename != NULL)
  82. usage();
  83. oldkeyfilename = optarg;
  84. break;
  85. GETOPT_OPTARG("--passphrase-mem"):
  86. if (maxmem != 0)
  87. usage();
  88. if (humansize_parse(optarg, &maxmem)) {
  89. warnp("Cannot parse --passphrase-mem"
  90. " argument: %s", optarg);
  91. exit(1);
  92. }
  93. break;
  94. GETOPT_OPTARG("--passphrase-time"):
  95. if (maxtime != 1.0)
  96. usage();
  97. maxtime = strtod(optarg, NULL);
  98. if ((maxtime < 0.05) || (maxtime > 86400)) {
  99. warn0("Invalid --passphrase-time argument: %s",
  100. optarg);
  101. exit(1);
  102. }
  103. break;
  104. GETOPT_OPT("--passphrased"):
  105. if (passphrased != 0)
  106. usage();
  107. passphrased = 1;
  108. break;
  109. GETOPT_OPT("--version"):
  110. fprintf(stderr, "tarsnap-keyregen %s\n",
  111. PACKAGE_VERSION);
  112. exit(0);
  113. GETOPT_MISSING_ARG:
  114. warn0("Missing argument to %s", ch);
  115. /* FALLTHROUGH */
  116. GETOPT_DEFAULT:
  117. usage();
  118. }
  119. }
  120. argc -= optind;
  121. argv += optind;
  122. /* We should have processed all the arguments. */
  123. if (argc != 0)
  124. usage();
  125. (void)argv; /* argv is not used beyond this point. */
  126. /*
  127. * We must have a user name, machine name, key file, and old key
  128. * file specified.
  129. */
  130. if ((C.user == NULL) || (C.name == NULL) ||
  131. (keyfilename == NULL) || (oldkeyfilename == NULL))
  132. usage();
  133. /*
  134. * It doesn't make sense to specify --passphrase-mem or
  135. * --passphrase-time if we're not using a passphrase.
  136. */
  137. if (((maxmem != 0) || (maxtime != 1.0)) && (passphrased == 0))
  138. usage();
  139. /*
  140. * Use shared code between keygen and keyregen for the actual
  141. * processing.
  142. */
  143. if (keygen_actual(&C, keyfilename, passphrased, maxmem, maxtime,
  144. oldkeyfilename) != 0)
  145. goto err0;
  146. /* Success! */
  147. return (0);
  148. err0:
  149. /* Failure! */
  150. return (1);
  151. }