corosync-keygen.c 4.7 KB

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