password.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB 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. * GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/auth.h>
  19. #include <grub/crypto.h>
  20. #include <grub/list.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/env.h>
  24. #include <grub/normal.h>
  25. #include <grub/dl.h>
  26. #include <grub/i18n.h>
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. static grub_dl_t my_mod;
  29. static grub_err_t
  30. check_password (const char *user, const char *entered,
  31. void *password)
  32. {
  33. if (grub_crypto_memcmp (entered, password, GRUB_AUTH_MAX_PASSLEN) != 0)
  34. return GRUB_ACCESS_DENIED;
  35. grub_auth_authenticate (user);
  36. return GRUB_ERR_NONE;
  37. }
  38. grub_err_t
  39. grub_normal_set_password (const char *user, const char *password)
  40. {
  41. grub_err_t err;
  42. char *pass;
  43. int copylen;
  44. pass = grub_zalloc (GRUB_AUTH_MAX_PASSLEN);
  45. if (!pass)
  46. return grub_errno;
  47. copylen = grub_strlen (password);
  48. if (copylen >= GRUB_AUTH_MAX_PASSLEN)
  49. copylen = GRUB_AUTH_MAX_PASSLEN - 1;
  50. grub_memcpy (pass, password, copylen);
  51. err = grub_auth_register_authentication (user, check_password, pass);
  52. if (err)
  53. {
  54. grub_free (pass);
  55. return err;
  56. }
  57. grub_dl_ref (my_mod);
  58. return GRUB_ERR_NONE;
  59. }
  60. static grub_err_t
  61. grub_cmd_password (grub_command_t cmd __attribute__ ((unused)),
  62. int argc, char **args)
  63. {
  64. if (argc != 2)
  65. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
  66. return grub_normal_set_password (args[0], args[1]);
  67. }
  68. static grub_command_t cmd;
  69. GRUB_MOD_INIT(password)
  70. {
  71. my_mod = mod;
  72. cmd = grub_register_command ("password", grub_cmd_password,
  73. N_("USER PASSWORD"),
  74. N_("Set user password (plaintext). "
  75. "Unrecommended and insecure."));
  76. }
  77. GRUB_MOD_FINI(password)
  78. {
  79. grub_unregister_command (cmd);
  80. }