4
0

corosync-keygen.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2004 MontaVista Software, Inc.
  3. * Copyright (c) 2005-2019 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. * Jan Friesse (jfriesse@redhat.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <config.h>
  37. #include <err.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <unistd.h>
  41. #include <fcntl.h>
  42. #include <string.h>
  43. #include <getopt.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <netinet/in.h>
  47. #include <corosync/totem/totem.h>
  48. #define DEFAULT_KEYFILE COROSYSCONFDIR "/authkey"
  49. #define DEFAULT_KEYFILE_LEN 256
  50. #define DEFAULT_RANDOM_DEV "/dev/urandom"
  51. static const char usage[] =
  52. "Usage: corosync-keygen [-k <keyfile>] [-s size] [-m <randomfile>] [-l] [-h]\n"
  53. " -k / --key-file=<filename> - Write to the specified keyfile\n"
  54. " instead of the default " DEFAULT_KEYFILE ".\n"
  55. " -r / --random-file - Random number source file. Default is \n"
  56. " /dev/urandom. As an example /dev/random may be requested\n"
  57. " (that may require user input for entropy).\n"
  58. " -l / --less-secure - Not used, option is kept only\n"
  59. " for compatibility.\n"
  60. " -s / --size - Length of key.\n"
  61. " -h / --help - Print basic usage.\n";
  62. int main (int argc, char *argv[])
  63. {
  64. int authkey_fd;
  65. int random_fd;
  66. char *keyfile = NULL;
  67. unsigned char key[TOTEM_PRIVATE_KEY_LEN_MAX];
  68. ssize_t res;
  69. ssize_t bytes_read;
  70. size_t key_len = DEFAULT_KEYFILE_LEN;
  71. const char *random_dev = DEFAULT_RANDOM_DEV;
  72. long long int tmpll;
  73. char *ep;
  74. int c;
  75. int option_index;
  76. static struct option long_options[] = {
  77. { "key-file", required_argument, NULL, 'k' },
  78. { "less-secure", no_argument, NULL, 'l' },
  79. { "random-file", required_argument, NULL, 'r' },
  80. { "size", required_argument, NULL, 's' },
  81. { "help", no_argument, NULL, 'h' },
  82. { 0, 0, NULL, 0 },
  83. };
  84. while ((c = getopt_long (argc, argv, "k:r:s:lh",
  85. long_options, &option_index)) != -1) {
  86. switch (c) {
  87. case 'k':
  88. keyfile = optarg;
  89. break;
  90. case 'l':
  91. /*
  92. * Only kept for compatibility
  93. */
  94. break;
  95. case 'r':
  96. random_dev = optarg;
  97. break;
  98. case 's':
  99. tmpll = strtoll(optarg, &ep, 10);
  100. if (tmpll < TOTEM_PRIVATE_KEY_LEN_MIN ||
  101. tmpll > TOTEM_PRIVATE_KEY_LEN_MAX ||
  102. errno != 0 || *ep != '\0') {
  103. errx (1, "Unsupported key size (supported <%u,%u>)\n",
  104. TOTEM_PRIVATE_KEY_LEN_MIN,
  105. TOTEM_PRIVATE_KEY_LEN_MAX);
  106. }
  107. key_len = (size_t)tmpll;
  108. break;
  109. case 'h':
  110. printf ("%s\n", usage);
  111. exit(0);
  112. break;
  113. default:
  114. printf ("Error parsing command line options.\n");
  115. exit (1);
  116. }
  117. }
  118. printf ("Corosync Cluster Engine Authentication key generator.\n");
  119. if (!keyfile) {
  120. keyfile = (char *)DEFAULT_KEYFILE;
  121. }
  122. printf ("Gathering %lu bits for key from %s.\n", (unsigned long)(key_len * 8), random_dev);
  123. random_fd = open (random_dev, O_RDONLY);
  124. if (random_fd == -1) {
  125. err (1, "Failed to open random source");
  126. }
  127. if (strcmp(random_dev, "/dev/random") == 0) {
  128. printf ("Press keys on your keyboard to generate entropy.\n");
  129. }
  130. /*
  131. * Read random data
  132. */
  133. bytes_read = 0;
  134. while (bytes_read < key_len) {
  135. res = read (random_fd, &key[bytes_read], key_len - bytes_read);
  136. if (res == -1) {
  137. err (1, "Could not read %s", random_dev);
  138. }
  139. if (res == 0) {
  140. errx (1, "Unexpected end of %s", random_dev);
  141. }
  142. bytes_read += res;
  143. if (bytes_read != key_len) {
  144. printf ("Press keys on your keyboard to generate entropy (%zu bits still needed).\n",
  145. (size_t)((key_len - bytes_read) * 8));
  146. }
  147. }
  148. close (random_fd);
  149. /*
  150. * Open key
  151. */
  152. authkey_fd = open (keyfile, O_CREAT|O_WRONLY|O_TRUNC, 0600);
  153. if (authkey_fd == -1) {
  154. err (2, "Could not create %s", keyfile);
  155. }
  156. if (fchmod (authkey_fd, 0400)) {
  157. err (3, "Failed to set key file permissions to 0400");
  158. }
  159. printf ("Writing corosync key to %s.\n", keyfile);
  160. /*
  161. * Write key
  162. */
  163. res = write (authkey_fd, key, key_len);
  164. if (res != key_len) {
  165. err (4, "Could not write %s", keyfile);
  166. }
  167. if (close (authkey_fd)) {
  168. err (5, "Could not close %s", keyfile);
  169. }
  170. return (0);
  171. }