archelp.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 ((*n == 0) && ((mode & GRUB_ARCHELP_ATTR_TYPE)
  163. != GRUB_ARCHELP_ATTR_DIR))
  164. {
  165. grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
  166. grub_free (name);
  167. goto fail;
  168. }
  169. if (((!prev) || (grub_strcmp (prev, name) != 0)) && *n != 0)
  170. {
  171. struct grub_dirhook_info info;
  172. grub_memset (&info, 0, sizeof (info));
  173. info.dir = (p != NULL) || ((mode & GRUB_ARCHELP_ATTR_TYPE)
  174. == GRUB_ARCHELP_ATTR_DIR);
  175. if (!(mode & GRUB_ARCHELP_ATTR_NOTIME))
  176. {
  177. info.mtime = mtime;
  178. info.mtimeset = 1;
  179. }
  180. if (hook (n, &info, hook_data))
  181. {
  182. grub_free (name);
  183. goto fail;
  184. }
  185. grub_free (prev);
  186. prev = name;
  187. }
  188. else
  189. {
  190. int restart = 0;
  191. err = handle_symlink (data, arcops, name,
  192. &path, mode, &restart);
  193. grub_free (name);
  194. if (err)
  195. goto fail;
  196. if (restart)
  197. {
  198. len = grub_strlen (path);
  199. if (++symlinknest == 8)
  200. {
  201. grub_error (GRUB_ERR_SYMLINK_LOOP,
  202. N_("too deep nesting of symlinks"));
  203. goto fail;
  204. }
  205. arcops->rewind (data);
  206. }
  207. }
  208. }
  209. else
  210. grub_free (name);
  211. }
  212. fail:
  213. grub_free (path);
  214. grub_free (prev);
  215. return grub_errno;
  216. }
  217. grub_err_t
  218. grub_archelp_open (struct grub_archelp_data *data,
  219. struct grub_archelp_ops *arcops,
  220. const char *name_in)
  221. {
  222. char *fn;
  223. char *name = grub_strdup (name_in + 1);
  224. int symlinknest = 0;
  225. if (!name)
  226. return grub_errno;
  227. canonicalize (name);
  228. while (1)
  229. {
  230. grub_uint32_t mode;
  231. grub_int32_t mtime;
  232. int restart;
  233. if (arcops->find_file (data, &fn, &mtime, &mode))
  234. goto fail;
  235. if (mode == GRUB_ARCHELP_ATTR_END)
  236. {
  237. grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), name_in);
  238. break;
  239. }
  240. canonicalize (fn);
  241. if (handle_symlink (data, arcops, fn, &name, mode, &restart))
  242. {
  243. grub_free (fn);
  244. goto fail;
  245. }
  246. if (restart)
  247. {
  248. arcops->rewind (data);
  249. if (++symlinknest == 8)
  250. {
  251. grub_error (GRUB_ERR_SYMLINK_LOOP,
  252. N_("too deep nesting of symlinks"));
  253. goto fail;
  254. }
  255. goto no_match;
  256. }
  257. if (grub_strcmp (name, fn) != 0)
  258. goto no_match;
  259. grub_free (fn);
  260. grub_free (name);
  261. return GRUB_ERR_NONE;
  262. no_match:
  263. grub_free (fn);
  264. }
  265. fail:
  266. grub_free (name);
  267. return grub_errno;
  268. }