4
0

corosync-keygen.c 4.7 KB

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