password.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/command.h>
  25. #include <grub/dl.h>
  26. #include <grub/i18n.h>
  27. static grub_dl_t my_mod;
  28. static grub_err_t
  29. check_password (const char *user, const char *entered,
  30. void *password)
  31. {
  32. if (grub_crypto_memcmp (entered, password, GRUB_AUTH_MAX_PASSLEN) != 0)
  33. return GRUB_ACCESS_DENIED;
  34. grub_auth_authenticate (user);
  35. return GRUB_ERR_NONE;
  36. }
  37. static grub_err_t
  38. grub_cmd_password (grub_command_t cmd __attribute__ ((unused)),
  39. int argc, char **args)
  40. {
  41. grub_err_t err;
  42. char *pass;
  43. int copylen;
  44. if (argc != 2)
  45. return grub_error (GRUB_ERR_BAD_ARGUMENT, "two arguments expected");
  46. pass = grub_zalloc (GRUB_AUTH_MAX_PASSLEN);
  47. if (!pass)
  48. return grub_errno;
  49. copylen = grub_strlen (args[1]);
  50. if (copylen >= GRUB_AUTH_MAX_PASSLEN)
  51. copylen = GRUB_AUTH_MAX_PASSLEN - 1;
  52. grub_memcpy (pass, args[1], copylen);
  53. err = grub_auth_register_authentication (args[0], check_password, pass);
  54. if (err)
  55. {
  56. grub_free (pass);
  57. return err;
  58. }
  59. grub_dl_ref (my_mod);
  60. return GRUB_ERR_NONE;
  61. }
  62. static grub_command_t cmd;
  63. GRUB_MOD_INIT(password)
  64. {
  65. my_mod = mod;
  66. cmd = grub_register_command ("password", grub_cmd_password,
  67. N_("USER PASSWORD"),
  68. N_("Set user password (plaintext). "
  69. "Unrecommended and insecure."));
  70. }
  71. GRUB_MOD_FINI(password)
  72. {
  73. grub_unregister_command (cmd);
  74. }