crypto.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. struct load_spec
  26. {
  27. struct load_spec *next;
  28. char *name;
  29. char *modname;
  30. };
  31. static struct load_spec *crypto_specs = NULL;
  32. static void
  33. grub_crypto_autoload (const char *name)
  34. {
  35. struct load_spec *cur;
  36. grub_dl_t mod;
  37. static int depth = 0;
  38. /* Some bufio of filesystems may want some crypto modules.
  39. It may result in infinite recursion. Hence this check. */
  40. if (depth)
  41. return;
  42. depth++;
  43. for (cur = crypto_specs; cur; cur = cur->next)
  44. if (grub_strcasecmp (name, cur->name) == 0)
  45. {
  46. mod = grub_dl_load (cur->modname);
  47. if (mod)
  48. grub_dl_ref (mod);
  49. grub_errno = GRUB_ERR_NONE;
  50. }
  51. depth--;
  52. }
  53. static void
  54. grub_crypto_spec_free (void)
  55. {
  56. struct load_spec *cur, *next;
  57. for (cur = crypto_specs; cur; cur = next)
  58. {
  59. next = cur->next;
  60. grub_free (cur->name);
  61. grub_free (cur->modname);
  62. grub_free (cur);
  63. }
  64. crypto_specs = NULL;
  65. }
  66. /* Read the file crypto.lst for auto-loading. */
  67. void
  68. read_crypto_list (const char *prefix)
  69. {
  70. char *filename;
  71. grub_file_t file;
  72. char *buf = NULL;
  73. if (!prefix)
  74. {
  75. grub_errno = GRUB_ERR_NONE;
  76. return;
  77. }
  78. filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM
  79. "/crypto.lst", prefix);
  80. if (!filename)
  81. {
  82. grub_errno = GRUB_ERR_NONE;
  83. return;
  84. }
  85. file = grub_file_open (filename, GRUB_FILE_TYPE_GRUB_MODULE_LIST);
  86. grub_free (filename);
  87. if (!file)
  88. {
  89. grub_errno = GRUB_ERR_NONE;
  90. return;
  91. }
  92. /* Override previous crypto.lst. */
  93. grub_crypto_spec_free ();
  94. for (;; grub_free (buf))
  95. {
  96. char *p, *name;
  97. struct load_spec *cur;
  98. buf = grub_file_getline (file);
  99. if (! buf)
  100. break;
  101. name = buf;
  102. while (grub_isspace (name[0]))
  103. name++;
  104. p = grub_strchr (name, ':');
  105. if (! p)
  106. continue;
  107. *p = '\0';
  108. p++;
  109. while (*p == ' ' || *p == '\t')
  110. p++;
  111. cur = grub_malloc (sizeof (*cur));
  112. if (!cur)
  113. {
  114. grub_errno = GRUB_ERR_NONE;
  115. continue;
  116. }
  117. cur->name = grub_strdup (name);
  118. if (! cur->name)
  119. {
  120. grub_errno = GRUB_ERR_NONE;
  121. grub_free (cur);
  122. continue;
  123. }
  124. cur->modname = grub_strdup (p);
  125. if (! cur->modname)
  126. {
  127. grub_errno = GRUB_ERR_NONE;
  128. grub_free (cur->name);
  129. grub_free (cur);
  130. continue;
  131. }
  132. cur->next = crypto_specs;
  133. crypto_specs = cur;
  134. }
  135. grub_file_close (file);
  136. grub_errno = GRUB_ERR_NONE;
  137. grub_crypto_autoload_hook = grub_crypto_autoload;
  138. }