4
0

corosync-keygen.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2004 MontaVista Software, Inc.
  3. * Copyright (c) 2005-2017 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 TOTEM_PRIVATE_KEY_LEN_MIN
  50. #define DEFAULT_RANDOM_DEV "/dev/random"
  51. static const char usage[] =
  52. "Usage: corosync-keygen [-k <keyfile>] [-l] [-h]\n"
  53. " -k / --key-file=<filename> - Write to the specified keyfile\n"
  54. " instead of the default " DEFAULT_KEYFILE ".\n"
  55. " -l / --less-secure - Use a less secure random number source\n"
  56. " (/dev/urandom) that is guaranteed not to require user\n"
  57. " input for entropy. This can be used when this\n"
  58. " application is used from a script.\n"
  59. " -s / --size - Length of key.\n"
  60. " -h / --help - Print basic usage.\n";
  61. int main (int argc, char *argv[])
  62. {
  63. int authkey_fd;
  64. int random_fd;
  65. char *keyfile = NULL;
  66. unsigned char key[TOTEM_PRIVATE_KEY_LEN_MAX];
  67. ssize_t res;
  68. ssize_t bytes_read;
  69. size_t key_len = DEFAULT_KEYFILE_LEN;
  70. const char *random_dev = DEFAULT_RANDOM_DEV;
  71. long long int tmpll;
  72. char *ep;
  73. int c;
  74. int option_index;
  75. int less_secure = 0;
  76. static struct option long_options[] = {
  77. { "key-file", required_argument, NULL, 'k' },
  78. { "less-secure", no_argument, NULL, 'l' },
  79. { "size", required_argument, NULL, 's' },
  80. { "help", no_argument, NULL, 'h' },
  81. { 0, 0, NULL, 0 },
  82. };
  83. while ((c = getopt_long (argc, argv, "k:s:lh",
  84. long_options, &option_index)) != -1) {
  85. switch (c) {
  86. case 'k':
  87. keyfile = optarg;
  88. break;
  89. case 'l':
  90. less_secure = 1;
  91. random_dev = "/dev/urandom";
  92. break;
  93. case 's':
  94. tmpll = strtoll(optarg, &ep, 10);
  95. if (tmpll < TOTEM_PRIVATE_KEY_LEN_MIN ||
  96. tmpll > TOTEM_PRIVATE_KEY_LEN_MAX ||
  97. errno != 0 || *ep != '\0') {
  98. printf ("Unsupported key size (supported <%u,%u>)\n",
  99. TOTEM_PRIVATE_KEY_LEN_MIN,
  100. TOTEM_PRIVATE_KEY_LEN_MAX);
  101. exit(1);
  102. }
  103. key_len = (size_t)tmpll;
  104. break;
  105. case 'h':
  106. printf ("%s\n", usage);
  107. exit(0);
  108. break;
  109. default:
  110. printf ("Error parsing command line options.\n");
  111. exit (1);
  112. }
  113. }
  114. printf ("Corosync Cluster Engine Authentication key generator.\n");
  115. if (!keyfile) {
  116. keyfile = (char *)DEFAULT_KEYFILE;
  117. }
  118. printf ("Gathering %lu bits for key from %s.\n", (unsigned long)(key_len * 8), random_dev);
  119. random_fd = open (random_dev, O_RDONLY);
  120. if (random_fd == -1) {
  121. err (1, "Failed to open random source");
  122. }
  123. if (!less_secure) {
  124. printf ("Press keys on your keyboard to generate entropy.\n");
  125. }
  126. /*
  127. * Read random data
  128. */
  129. bytes_read = 0;
  130. retry_read:
  131. res = read (random_fd, &key[bytes_read], key_len - bytes_read);
  132. if (res == -1) {
  133. err (1, "Could not read /dev/random");
  134. }
  135. bytes_read += res;
  136. if (bytes_read != key_len) {
  137. printf ("Press keys on your keyboard to generate entropy (bits = %d).\n", (int)(bytes_read * 8));
  138. goto retry_read;
  139. }
  140. close (random_fd);
  141. /*
  142. * Open key
  143. */
  144. authkey_fd = open (keyfile, O_CREAT|O_WRONLY|O_TRUNC, 0600);
  145. if (authkey_fd == -1) {
  146. err (2, "Could not create %s", keyfile);
  147. }
  148. if (fchmod (authkey_fd, 0400)) {
  149. err (3, "Failed to set key file permissions to 0400");
  150. }
  151. printf ("Writing corosync key to %s.\n", keyfile);
  152. /*
  153. * Write key
  154. */
  155. res = write (authkey_fd, key, key_len);
  156. if (res != key_len) {
  157. err (4, "Could not write %s", keyfile);
  158. }
  159. if (close (authkey_fd)) {
  160. err (5, "Could not close %s", keyfile);
  161. }
  162. return (0);
  163. }