grub-mkpasswd.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * BURG - Brand-new Universal loadeR from GRUB
  3. * Copyright 2009 Bean Lee - All Rights Reserved
  4. *
  5. * BURG is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * BURG is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with BURG. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/types.h>
  19. #include <grub/util/misc.h>
  20. #include <grub/lib.h>
  21. #include <grub/handler.h>
  22. #include <grub/i18n.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <getopt.h>
  28. #include <time.h>
  29. #include "progname.h"
  30. void
  31. grub_putchar (int c)
  32. {
  33. putchar (c);
  34. }
  35. void
  36. grub_refresh (void)
  37. {
  38. fflush (stdout);
  39. }
  40. struct grub_handler_class grub_term_input_class;
  41. struct grub_handler_class grub_term_output_class;
  42. int
  43. grub_getkey (void)
  44. {
  45. return 0;
  46. }
  47. char *
  48. grub_env_get (const char *name __attribute__ ((unused)))
  49. {
  50. return NULL;
  51. }
  52. static struct option options[] = {
  53. {"help", no_argument, 0, 'h'},
  54. {"version", no_argument, 0, 'V'},
  55. {"verbose", no_argument, 0, 'v'},
  56. {0, 0, 0, 0}
  57. };
  58. static void
  59. usage (int status)
  60. {
  61. if (status)
  62. fprintf (stderr, "Try ``grub-mkpasswd --help'' for more information.\n");
  63. else
  64. printf ("\
  65. Usage: grub-mkpasswd [OPTIONS] PASSWORD\n\
  66. \n\
  67. Tool to generate md5 password.\n\
  68. \nOptions:\n\
  69. -h, --help display this message and exit\n\
  70. -V, --version print version information and exit\n\
  71. -v, --verbose print verbose messages\n\
  72. \n\
  73. Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
  74. exit (status);
  75. }
  76. static void
  77. generate_password (char *password)
  78. {
  79. char crypted[36];
  80. char key[32];
  81. time_t t;
  82. int i;
  83. const char *const seedchars =
  84. "./0123456789ABCDEFGHIJKLMNOPQRST"
  85. "UVWXYZabcdefghijklmnopqrstuvwxyz";
  86. /* First create a salt. */
  87. /* The magical prefix. */
  88. memset (crypted, 0, sizeof (crypted));
  89. memcpy (crypted, "$1$", 3);
  90. srand (time (&t));
  91. /* Generate a salt. */
  92. for (i = 0; i < 7; i++)
  93. {
  94. /* FIXME: This should be more random. */
  95. crypted[3 + i] = seedchars[rand () & 0x3f];
  96. }
  97. /* A salt must be terminated with `$', if it is less than 8 chars. */
  98. crypted[3 + i] = '$';
  99. strncpy (key, password, sizeof (key));
  100. key[sizeof (key) - 1] = 0;
  101. grub_make_md5_password (key, crypted);
  102. printf ("%s\n", crypted);
  103. }
  104. int
  105. main (int argc, char *argv[])
  106. {
  107. set_program_name (argv[0]);
  108. setlocale (LC_ALL, "");
  109. bindtextdomain (PACKAGE, LOCALEDIR);
  110. textdomain (PACKAGE);
  111. /* Check for options. */
  112. while (1)
  113. {
  114. int c = getopt_long (argc, argv, "hVv", options, 0);
  115. if (c == -1)
  116. break;
  117. else
  118. switch (c)
  119. {
  120. case 'h':
  121. usage (0);
  122. break;
  123. case 'V':
  124. printf ("grub-mkpasswd (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
  125. return 0;
  126. case 'v':
  127. verbosity++;
  128. break;
  129. default:
  130. usage (1);
  131. break;
  132. }
  133. }
  134. /* Obtain the filename. */
  135. if (optind >= argc)
  136. {
  137. fprintf (stderr, "no password specified\n");
  138. usage (1);
  139. }
  140. generate_password (argv[optind]);
  141. return 0;
  142. }