archelp.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2007,2008,2009,2013 Free Software Foundation, Inc.
  4. *
  5. * This program 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. * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/archelp.h>
  19. #include <grub/err.h>
  20. #include <grub/fs.h>
  21. #include <grub/disk.h>
  22. #include <grub/dl.h>
  23. GRUB_MOD_LICENSE ("GPLv3+");
  24. static inline void
  25. canonicalize (char *name)
  26. {
  27. char *iptr, *optr;
  28. for (iptr = name, optr = name; *iptr; )
  29. {
  30. while (*iptr == '/')
  31. iptr++;
  32. if (iptr[0] == '.' && (iptr[1] == '/' || iptr[1] == 0))
  33. {
  34. iptr++;
  35. continue;
  36. }
  37. if (iptr[0] == '.' && iptr[1] == '.' && (iptr[2] == '/' || iptr[2] == 0))
  38. {
  39. iptr += 2;
  40. if (optr == name)
  41. continue;
  42. for (optr -= 2; optr >= name && *optr != '/'; optr--);
  43. optr++;
  44. continue;
  45. }
  46. while (*iptr && *iptr != '/')
  47. *optr++ = *iptr++;
  48. if (*iptr)
  49. *optr++ = *iptr++;
  50. }
  51. *optr = 0;
  52. }
  53. static grub_err_t
  54. handle_symlink (struct grub_archelp_data *data,
  55. struct grub_archelp_ops *arcops,
  56. const char *fn, char **name,
  57. grub_uint32_t mode, int *restart)
  58. {
  59. grub_size_t flen;
  60. char *target;
  61. char *ptr;
  62. char *lastslash;
  63. grub_size_t prefixlen;
  64. char *rest;
  65. char *linktarget;
  66. grub_size_t linktarget_len;
  67. *restart = 0;
  68. if ((mode & GRUB_ARCHELP_ATTR_TYPE) != GRUB_ARCHELP_ATTR_LNK
  69. || !arcops->get_link_target)
  70. return GRUB_ERR_NONE;
  71. flen = grub_strlen (fn);
  72. if (grub_memcmp (*name, fn, flen) != 0
  73. || ((*name)[flen] != 0 && (*name)[flen] != '/'))
  74. return GRUB_ERR_NONE;
  75. rest = *name + flen;
  76. lastslash = rest;
  77. if (*rest)
  78. rest++;
  79. while (lastslash >= *name && *lastslash != '/')
  80. lastslash--;
  81. if (lastslash >= *name)
  82. prefixlen = lastslash - *name;
  83. else
  84. prefixlen = 0;
  85. if (prefixlen)
  86. prefixlen++;
  87. linktarget = arcops->get_link_target (data);
  88. if (!linktarget)
  89. return grub_errno;
  90. if (linktarget[0] == '\0')
  91. return GRUB_ERR_NONE;
  92. linktarget_len = grub_strlen (linktarget);
  93. target = grub_malloc (linktarget_len + grub_strlen (*name) + 2);
  94. if (!target)
  95. return grub_errno;
  96. grub_strcpy (target + prefixlen, linktarget);
  97. grub_free (linktarget);
  98. if (target[prefixlen] == '/')
  99. {
  100. ptr = grub_stpcpy (target, target + prefixlen);
  101. ptr = grub_stpcpy (ptr, rest);
  102. *ptr = 0;
  103. grub_dprintf ("archelp", "symlink redirected %s to %s\n",
  104. *name, target);
  105. grub_free (*name);
  106. canonicalize (target);
  107. *name = target;
  108. *restart = 1;
  109. return GRUB_ERR_NONE;
  110. }
  111. if (prefixlen)
  112. {
  113. grub_memcpy (target, *name, prefixlen);
  114. target[prefixlen-1] = '/';
  115. }
  116. grub_strcpy (target + prefixlen + linktarget_len, rest);
  117. grub_dprintf ("archelp", "symlink redirected %s to %s\n",
  118. *name, target);
  119. grub_free (*name);
  120. canonicalize (target);
  121. *name = target;
  122. *restart = 1;
  123. return GRUB_ERR_NONE;
  124. }
  125. grub_err_t
  126. grub_archelp_dir (struct grub_archelp_data *data,
  127. struct grub_archelp_ops *arcops,
  128. const char *path_in,
  129. grub_fs_dir_hook_t hook, void *hook_data)
  130. {
  131. char *prev, *name, *path, *ptr;
  132. grub_size_t len;
  133. int symlinknest = 0;
  134. path = grub_strdup (path_in + 1);
  135. if (!path)
  136. return grub_errno;
  137. canonicalize (path);
  138. for (ptr = path + grub_strlen (path) - 1; ptr >= path && *ptr == '/'; ptr--)
  139. *ptr = 0;
  140. prev = 0;
  141. len = grub_strlen (path);
  142. while (1)
  143. {
  144. grub_int32_t mtime;
  145. grub_uint32_t mode;
  146. grub_err_t err;
  147. if (arcops->find_file (data, &name, &mtime, &mode))
  148. goto fail;
  149. if (mode == GRUB_ARCHELP_ATTR_END)
  150. break;
  151. canonicalize (name);
  152. if (grub_memcmp (path, name, len) == 0
  153. && (name[len] == 0 || name[len] == '/' || len == 0))
  154. {
  155. char *p, *n;
  156. n = name + len;
  157. while (*n == '/')
  158. n++;
  159. p = grub_strchr (n, '/');
  160. if (p)
  161. *p = 0;
  162. if (((!prev) || (grub_strcmp (prev, name) != 0)) && *n != 0)
  163. {
  164. struct grub_dirhook_info info;
  165. grub_memset (&info, 0, sizeof (info));
  166. info.dir = (p != NULL) || ((mode & GRUB_ARCHELP_ATTR_TYPE)
  167. == GRUB_ARCHELP_ATTR_DIR);
  168. if (!(mode & GRUB_ARCHELP_ATTR_NOTIME))
  169. {
  170. info.mtime = mtime;
  171. info.mtimeset = 1;
  172. }
  173. if (hook (n, &info, hook_data))
  174. {
  175. grub_free (name);
  176. goto fail;
  177. }
  178. grub_free (prev);
  179. prev = name;
  180. }
  181. else
  182. {
  183. int restart = 0;
  184. err = handle_symlink (data, arcops, name,
  185. &path, mode, &restart);
  186. grub_free (name);
  187. if (err)
  188. goto fail;
  189. if (restart)
  190. {
  191. len = grub_strlen (path);
  192. if (++symlinknest == 8)
  193. {
  194. grub_error (GRUB_ERR_SYMLINK_LOOP,
  195. N_("too deep nesting of symlinks"));
  196. goto fail;
  197. }
  198. arcops->rewind (data);
  199. }
  200. }
  201. }
  202. else
  203. grub_free (name);
  204. }
  205. fail:
  206. grub_free (path);
  207. grub_free (prev);
  208. return grub_errno;
  209. }
  210. grub_err_t
  211. grub_archelp_open (struct grub_archelp_data *data,
  212. struct grub_archelp_ops *arcops,
  213. const char *name_in)
  214. {
  215. char *fn;
  216. char *name = grub_strdup (name_in + 1);
  217. int symlinknest = 0;
  218. if (!name)
  219. return grub_errno;
  220. canonicalize (name);
  221. while (1)
  222. {
  223. grub_uint32_t mode;
  224. grub_int32_t mtime;
  225. int restart;
  226. if (arcops->find_file (data, &fn, &mtime, &mode))
  227. goto fail;
  228. if (mode == GRUB_ARCHELP_ATTR_END)
  229. {
  230. grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), name_in);
  231. break;
  232. }
  233. canonicalize (fn);
  234. if (handle_symlink (data, arcops, fn, &name, mode, &restart))
  235. {
  236. grub_free (fn);
  237. goto fail;
  238. }
  239. if (restart)
  240. {
  241. arcops->rewind (data);
  242. if (++symlinknest == 8)
  243. {
  244. grub_error (GRUB_ERR_SYMLINK_LOOP,
  245. N_("too deep nesting of symlinks"));
  246. goto fail;
  247. }
  248. goto no_match;
  249. }
  250. if (grub_strcmp (name, fn) != 0)
  251. goto no_match;
  252. grub_free (fn);
  253. grub_free (name);
  254. return GRUB_ERR_NONE;
  255. no_match:
  256. grub_free (fn);
  257. }
  258. fail:
  259. grub_free (name);
  260. return grub_errno;
  261. }