archelp.c 6.8 KB

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