corosync-keygen.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <stdio.h>
  37. #include <stdlib.h>
  38. #include <unistd.h>
  39. #include <fcntl.h>
  40. #include <errno.h>
  41. #include <getopt.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <netinet/in.h>
  45. #define KEYFILE COROSYSCONFDIR "/authkey"
  46. static const char usage[] =
  47. "Usage: corosync-keygen [-l]\n"
  48. " -l / --less-secure - Use a less secure random number source\n"
  49. " (/dev/urandom) that is guaranteed not to require user\n"
  50. " input for entropy. This can be used when this\n"
  51. " application is used from a script.\n";
  52. int main (int argc, char *argv[])
  53. {
  54. int authkey_fd;
  55. int random_fd;
  56. unsigned char key[128];
  57. ssize_t res;
  58. ssize_t bytes_read;
  59. int c;
  60. int option_index;
  61. int less_secure = 0;
  62. static struct option long_options[] = {
  63. { "less-secure", no_argument, NULL, 'l' },
  64. { "help", no_argument, NULL, 'h' },
  65. { 0, 0, NULL, 0 },
  66. };
  67. while ((c = getopt_long (argc, argv, "lh",
  68. long_options, &option_index)) != -1) {
  69. switch (c) {
  70. case 'l':
  71. less_secure = 1;
  72. break;
  73. case 'h':
  74. printf ("%s\n", usage);
  75. exit(0);
  76. break;
  77. default:
  78. printf ("Error parsing command line options.\n");
  79. exit (1);
  80. }
  81. }
  82. printf ("Corosync Cluster Engine Authentication key generator.\n");
  83. if (geteuid() != 0) {
  84. printf ("Error: Authorization key must be generated as root user.\n");
  85. exit (errno);
  86. }
  87. if (mkdir (COROSYSCONFDIR, 0700)) {
  88. if (errno != EEXIST) {
  89. perror ("Failed to create directory: " COROSYSCONFDIR);
  90. exit (errno);
  91. }
  92. }
  93. if (less_secure) {
  94. random_fd = open ("/dev/urandom", O_RDONLY);
  95. } else {
  96. printf ("Gathering %lu bits for key from /dev/random.\n", (unsigned long)(sizeof (key) * 8));
  97. printf ("Press keys on your keyboard to generate entropy.\n");
  98. random_fd = open ("/dev/random", O_RDONLY);
  99. }
  100. if (random_fd == -1) {
  101. perror ("Failed to open random source\n");
  102. exit (errno);
  103. }
  104. /*
  105. * Read random data
  106. */
  107. bytes_read = 0;
  108. retry_read:
  109. res = read (random_fd, &key[bytes_read], sizeof (key) - bytes_read);
  110. if (res == -1) {
  111. perror ("Could not read /dev/random");
  112. exit (errno);
  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, 600);
  124. if (authkey_fd == -1) {
  125. perror ("Could not create " KEYFILE);
  126. exit (errno);
  127. }
  128. /*
  129. * Set security of authorization key to uid = 0 gid = 0 mode = 0400
  130. */
  131. res = fchown (authkey_fd, 0, 0);
  132. if (res == -1) {
  133. perror ("Could not fchown key to uid 0 and gid 0\n");
  134. exit (errno);
  135. }
  136. if (fchmod (authkey_fd, 0400)) {
  137. perror ("Failed to set key file permissions to 0400\n");
  138. exit (errno);
  139. }
  140. printf ("Writing corosync key to " KEYFILE ".\n");
  141. /*
  142. * Write key
  143. */
  144. res = write (authkey_fd, key, sizeof (key));
  145. if (res != sizeof (key)) {
  146. perror ("Could not write " KEYFILE);
  147. exit (errno);
  148. }
  149. if (close (authkey_fd)) {
  150. perror ("Could not write " KEYFILE);
  151. exit (errno);
  152. }
  153. return (0);
  154. }