crypto.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* crypto.c - support crypto autoload */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/mm.h>
  21. #include <grub/env.h>
  22. #include <grub/misc.h>
  23. #include <grub/crypto.h>
  24. #include <grub/normal.h>
  25. #include <grub/file.h>
  26. #include <grub/lib.h>
  27. struct load_spec
  28. {
  29. struct load_spec *next;
  30. char *name;
  31. char *modname;
  32. };
  33. struct load_spec *crypto_specs = NULL;
  34. static void
  35. grub_crypto_autoload (const char *name)
  36. {
  37. struct load_spec *cur;
  38. grub_dl_t mod;
  39. for (cur = crypto_specs; cur; cur = cur->next)
  40. if (grub_strcasecmp (name, cur->name) == 0)
  41. {
  42. mod = grub_dl_load (cur->modname);
  43. if (mod)
  44. grub_dl_ref (mod);
  45. grub_errno = GRUB_ERR_NONE;
  46. }
  47. }
  48. static void
  49. grub_crypto_spec_free (void)
  50. {
  51. struct load_spec *cur, *next;
  52. for (cur = crypto_specs; cur; cur = next)
  53. {
  54. next = cur->next;
  55. grub_free (cur->name);
  56. grub_free (cur->modname);
  57. grub_free (cur);
  58. }
  59. crypto_specs = NULL;
  60. }
  61. /* Read the file crypto.lst for auto-loading. */
  62. void
  63. read_crypto_list (const char *prefix)
  64. {
  65. char *filename;
  66. grub_file_t file;
  67. char *buf = NULL;
  68. if (!prefix)
  69. {
  70. grub_errno = GRUB_ERR_NONE;
  71. return;
  72. }
  73. filename = grub_xasprintf ("%s/crypto.lst", prefix);
  74. if (!filename)
  75. {
  76. grub_errno = GRUB_ERR_NONE;
  77. return;
  78. }
  79. file = grub_file_open (filename);
  80. grub_free (filename);
  81. if (!file)
  82. {
  83. grub_errno = GRUB_ERR_NONE;
  84. return;
  85. }
  86. /* Override previous crypto.lst. */
  87. grub_crypto_spec_free ();
  88. for (;; grub_free (buf))
  89. {
  90. char *p, *name;
  91. struct load_spec *cur;
  92. buf = grub_getline (file);
  93. if (! buf)
  94. break;
  95. name = buf;
  96. p = grub_strchr (name, ':');
  97. if (! p)
  98. continue;
  99. *p = '\0';
  100. while (*++p == ' ')
  101. ;
  102. cur = grub_malloc (sizeof (*cur));
  103. if (!cur)
  104. {
  105. grub_errno = GRUB_ERR_NONE;
  106. continue;
  107. }
  108. cur->name = grub_strdup (name);
  109. if (! name)
  110. {
  111. grub_errno = GRUB_ERR_NONE;
  112. grub_free (cur);
  113. continue;
  114. }
  115. cur->modname = grub_strdup (p);
  116. if (! cur->modname)
  117. {
  118. grub_errno = GRUB_ERR_NONE;
  119. grub_free (cur);
  120. grub_free (cur->name);
  121. continue;
  122. }
  123. cur->next = crypto_specs;
  124. crypto_specs = cur;
  125. }
  126. grub_file_close (file);
  127. grub_errno = GRUB_ERR_NONE;
  128. grub_crypto_autoload_hook = grub_crypto_autoload;
  129. }