PageRenderTime 34ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/grub-core/commands/wildcard.c

https://gitlab.com/github-cloud-corporation/grub
C | 626 lines | 492 code | 104 blank | 30 comment | 114 complexity | 79c8ef46ae4d3b057b677c337d3c5314 MD5 | raw file
  1. /* wildcard.c - Wildcard character expansion for GRUB script. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2010 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/mm.h>
  20. #include <grub/fs.h>
  21. #include <grub/env.h>
  22. #include <grub/file.h>
  23. #include <grub/device.h>
  24. #include <grub/script_sh.h>
  25. #include <regex.h>
  26. static inline int isregexop (char ch);
  27. static char ** merge (char **lhs, char **rhs);
  28. static char *make_dir (const char *prefix, const char *start, const char *end);
  29. static int make_regex (const char *regex_start, const char *regex_end,
  30. regex_t *regexp);
  31. static void split_path (const char *path, const char **suffix_end, const char **regex_end);
  32. static char ** match_devices (const regex_t *regexp, int noparts);
  33. static char ** match_files (const char *prefix, const char *suffix_start,
  34. const char *suffix_end, const regex_t *regexp);
  35. static grub_err_t wildcard_expand (const char *s, char ***strs);
  36. struct grub_script_wildcard_translator grub_filename_translator = {
  37. .expand = wildcard_expand,
  38. };
  39. static char **
  40. merge (char **dest, char **ps)
  41. {
  42. int i;
  43. int j;
  44. char **p;
  45. if (! dest)
  46. return ps;
  47. if (! ps)
  48. return dest;
  49. for (i = 0; dest[i]; i++)
  50. ;
  51. for (j = 0; ps[j]; j++)
  52. ;
  53. p = grub_realloc (dest, sizeof (char*) * (i + j + 1));
  54. if (! p)
  55. {
  56. grub_free (dest);
  57. grub_free (ps);
  58. return 0;
  59. }
  60. dest = p;
  61. for (j = 0; ps[j]; j++)
  62. dest[i++] = ps[j];
  63. dest[i] = 0;
  64. grub_free (ps);
  65. return dest;
  66. }
  67. static inline int
  68. isregexop (char ch)
  69. {
  70. return grub_strchr ("*.\\|+{}[]?", ch) ? 1 : 0;
  71. }
  72. static char *
  73. make_dir (const char *prefix, const char *start, const char *end)
  74. {
  75. char ch;
  76. unsigned i;
  77. unsigned n;
  78. char *result;
  79. i = grub_strlen (prefix);
  80. n = i + end - start;
  81. result = grub_malloc (n + 1);
  82. if (! result)
  83. return 0;
  84. grub_strcpy (result, prefix);
  85. while (start < end && (ch = *start++))
  86. if (ch == '\\' && isregexop (*start))
  87. result[i++] = *start++;
  88. else
  89. result[i++] = ch;
  90. result[i] = '\0';
  91. return result;
  92. }
  93. static int
  94. make_regex (const char *start, const char *end, regex_t *regexp)
  95. {
  96. char ch;
  97. int i = 0;
  98. unsigned len = end - start;
  99. char *buffer = grub_malloc (len * 2 + 2 + 1); /* worst case size. */
  100. if (! buffer)
  101. return 1;
  102. buffer[i++] = '^';
  103. while (start < end)
  104. {
  105. /* XXX Only * and ? expansion for now. */
  106. switch ((ch = *start++))
  107. {
  108. case '\\':
  109. buffer[i++] = ch;
  110. if (*start != '\0')
  111. buffer[i++] = *start++;
  112. break;
  113. case '.':
  114. case '(':
  115. case ')':
  116. case '@':
  117. case '+':
  118. case '|':
  119. case '{':
  120. case '}':
  121. case '[':
  122. case ']':
  123. buffer[i++] = '\\';
  124. buffer[i++] = ch;
  125. break;
  126. case '*':
  127. buffer[i++] = '.';
  128. buffer[i++] = '*';
  129. break;
  130. case '?':
  131. buffer[i++] = '.';
  132. break;
  133. default:
  134. buffer[i++] = ch;
  135. }
  136. }
  137. buffer[i++] = '$';
  138. buffer[i] = '\0';
  139. grub_dprintf ("expand", "Regexp is %s\n", buffer);
  140. if (regcomp (regexp, buffer, RE_SYNTAX_GNU_AWK))
  141. {
  142. grub_free (buffer);
  143. return 1;
  144. }
  145. grub_free (buffer);
  146. return 0;
  147. }
  148. /* Split `str' into two parts: (1) dirname that is regexop free (2)
  149. dirname that has a regexop. */
  150. static void
  151. split_path (const char *str, const char **noregexop, const char **regexop)
  152. {
  153. char ch = 0;
  154. int regex = 0;
  155. const char *end;
  156. const char *split; /* points till the end of dirnaname that doesn't
  157. need expansion. */
  158. split = end = str;
  159. while ((ch = *end))
  160. {
  161. if (ch == '\\' && end[1])
  162. end++;
  163. else if (ch == '*' || ch == '?')
  164. regex = 1;
  165. else if (ch == '/' && ! regex)
  166. split = end + 1; /* forward to next regexop-free dirname */
  167. else if (ch == '/' && regex)
  168. break; /* stop at the first dirname with a regexop */
  169. end++;
  170. }
  171. *regexop = end;
  172. if (! regex)
  173. *noregexop = end;
  174. else
  175. *noregexop = split;
  176. }
  177. /* Context for match_devices. */
  178. struct match_devices_ctx
  179. {
  180. const regex_t *regexp;
  181. int noparts;
  182. int ndev;
  183. char **devs;
  184. };
  185. /* Helper for match_devices. */
  186. static int
  187. match_devices_iter (const char *name, void *data)
  188. {
  189. struct match_devices_ctx *ctx = data;
  190. char **t;
  191. char *buffer;
  192. /* skip partitions if asked to. */
  193. if (ctx->noparts && grub_strchr (name, ','))
  194. return 0;
  195. buffer = grub_xasprintf ("(%s)", name);
  196. if (! buffer)
  197. return 1;
  198. grub_dprintf ("expand", "matching: %s\n", buffer);
  199. if (regexec (ctx->regexp, buffer, 0, 0, 0))
  200. {
  201. grub_dprintf ("expand", "not matched\n");
  202. grub_free (buffer);
  203. return 0;
  204. }
  205. t = grub_realloc (ctx->devs, sizeof (char*) * (ctx->ndev + 2));
  206. if (! t)
  207. {
  208. grub_free (buffer);
  209. return 1;
  210. }
  211. ctx->devs = t;
  212. ctx->devs[ctx->ndev++] = buffer;
  213. ctx->devs[ctx->ndev] = 0;
  214. return 0;
  215. }
  216. static char **
  217. match_devices (const regex_t *regexp, int noparts)
  218. {
  219. struct match_devices_ctx ctx = {
  220. .regexp = regexp,
  221. .noparts = noparts,
  222. .ndev = 0,
  223. .devs = 0
  224. };
  225. int i;
  226. if (grub_device_iterate (match_devices_iter, &ctx))
  227. goto fail;
  228. return ctx.devs;
  229. fail:
  230. for (i = 0; ctx.devs && ctx.devs[i]; i++)
  231. grub_free (ctx.devs[i]);
  232. grub_free (ctx.devs);
  233. return 0;
  234. }
  235. /* Context for match_files. */
  236. struct match_files_ctx
  237. {
  238. const regex_t *regexp;
  239. char **files;
  240. unsigned nfile;
  241. char *dir;
  242. };
  243. /* Helper for match_files. */
  244. static int
  245. match_files_iter (const char *name,
  246. const struct grub_dirhook_info *info __attribute__((unused)),
  247. void *data)
  248. {
  249. struct match_files_ctx *ctx = data;
  250. char **t;
  251. char *buffer;
  252. /* skip . and .. names */
  253. if (grub_strcmp(".", name) == 0 || grub_strcmp("..", name) == 0)
  254. return 0;
  255. grub_dprintf ("expand", "matching: %s in %s\n", name, ctx->dir);
  256. if (regexec (ctx->regexp, name, 0, 0, 0))
  257. return 0;
  258. grub_dprintf ("expand", "matched\n");
  259. buffer = grub_xasprintf ("%s%s", ctx->dir, name);
  260. if (! buffer)
  261. return 1;
  262. t = grub_realloc (ctx->files, sizeof (char*) * (ctx->nfile + 2));
  263. if (! t)
  264. {
  265. grub_free (buffer);
  266. return 1;
  267. }
  268. ctx->files = t;
  269. ctx->files[ctx->nfile++] = buffer;
  270. ctx->files[ctx->nfile] = 0;
  271. return 0;
  272. }
  273. static char **
  274. match_files (const char *prefix, const char *suffix, const char *end,
  275. const regex_t *regexp)
  276. {
  277. struct match_files_ctx ctx = {
  278. .regexp = regexp,
  279. .nfile = 0,
  280. .files = 0
  281. };
  282. int i;
  283. const char *path;
  284. char *device_name;
  285. grub_fs_t fs;
  286. grub_device_t dev;
  287. dev = 0;
  288. device_name = 0;
  289. grub_error_push ();
  290. ctx.dir = make_dir (prefix, suffix, end);
  291. if (! ctx.dir)
  292. goto fail;
  293. device_name = grub_file_get_device_name (ctx.dir);
  294. dev = grub_device_open (device_name);
  295. if (! dev)
  296. goto fail;
  297. fs = grub_fs_probe (dev);
  298. if (! fs)
  299. goto fail;
  300. if (ctx.dir[0] == '(')
  301. {
  302. path = grub_strchr (ctx.dir, ')');
  303. if (!path)
  304. goto fail;
  305. path++;
  306. }
  307. else
  308. path = ctx.dir;
  309. if (fs->dir (dev, path, match_files_iter, &ctx))
  310. goto fail;
  311. grub_free (ctx.dir);
  312. grub_device_close (dev);
  313. grub_free (device_name);
  314. grub_error_pop ();
  315. return ctx.files;
  316. fail:
  317. grub_free (ctx.dir);
  318. for (i = 0; ctx.files && ctx.files[i]; i++)
  319. grub_free (ctx.files[i]);
  320. grub_free (ctx.files);
  321. if (dev)
  322. grub_device_close (dev);
  323. grub_free (device_name);
  324. grub_error_pop ();
  325. return 0;
  326. }
  327. /* Context for check_file. */
  328. struct check_file_ctx
  329. {
  330. const char *basename;
  331. int found;
  332. };
  333. /* Helper for check_file. */
  334. static int
  335. check_file_iter (const char *name, const struct grub_dirhook_info *info,
  336. void *data)
  337. {
  338. struct check_file_ctx *ctx = data;
  339. if (ctx->basename[0] == 0
  340. || (info->case_insensitive ? grub_strcasecmp (name, ctx->basename) == 0
  341. : grub_strcmp (name, ctx->basename) == 0))
  342. {
  343. ctx->found = 1;
  344. return 1;
  345. }
  346. return 0;
  347. }
  348. static int
  349. check_file (const char *dir, const char *basename)
  350. {
  351. struct check_file_ctx ctx = {
  352. .basename = basename,
  353. .found = 0
  354. };
  355. grub_fs_t fs;
  356. grub_device_t dev;
  357. const char *device_name, *path;
  358. device_name = grub_file_get_device_name (dir);
  359. dev = grub_device_open (device_name);
  360. if (! dev)
  361. goto fail;
  362. fs = grub_fs_probe (dev);
  363. if (! fs)
  364. goto fail;
  365. if (dir[0] == '(')
  366. {
  367. path = grub_strchr (dir, ')');
  368. if (!path)
  369. goto fail;
  370. path++;
  371. }
  372. else
  373. path = dir;
  374. fs->dir (dev, path[0] ? path : "/", check_file_iter, &ctx);
  375. if (grub_errno == 0 && basename[0] == 0)
  376. ctx.found = 1;
  377. fail:
  378. grub_errno = 0;
  379. return ctx.found;
  380. }
  381. static void
  382. unescape (char *out, const char *in, const char *end)
  383. {
  384. char *optr;
  385. const char *iptr;
  386. for (optr = out, iptr = in; iptr < end;)
  387. {
  388. if (*iptr == '\\' && iptr + 1 < end)
  389. {
  390. *optr++ = iptr[1];
  391. iptr += 2;
  392. continue;
  393. }
  394. if (*iptr == '\\')
  395. break;
  396. *optr++ = *iptr++;
  397. }
  398. *optr = 0;
  399. }
  400. static grub_err_t
  401. wildcard_expand (const char *s, char ***strs)
  402. {
  403. const char *start;
  404. const char *regexop;
  405. const char *noregexop;
  406. char **paths = 0;
  407. int had_regexp = 0;
  408. unsigned i;
  409. regex_t regexp;
  410. *strs = 0;
  411. if (s[0] != '/' && s[0] != '(' && s[0] != '*')
  412. return 0;
  413. start = s;
  414. while (*start)
  415. {
  416. split_path (start, &noregexop, &regexop);
  417. if (noregexop == regexop)
  418. {
  419. grub_dprintf ("expand", "no expansion needed\n");
  420. if (paths == 0)
  421. {
  422. paths = grub_malloc (sizeof (char *) * 2);
  423. if (!paths)
  424. goto fail;
  425. paths[0] = grub_malloc (regexop - start + 1);
  426. if (!paths[0])
  427. goto fail;
  428. unescape (paths[0], start, regexop);
  429. paths[1] = 0;
  430. }
  431. else
  432. {
  433. int j = 0;
  434. for (i = 0; paths[i]; i++)
  435. {
  436. char *o, *oend;
  437. char *n;
  438. char *p;
  439. o = paths[i];
  440. oend = o + grub_strlen (o);
  441. n = grub_malloc ((oend - o) + (regexop - start) + 1);
  442. if (!n)
  443. goto fail;
  444. grub_memcpy (n, o, oend - o);
  445. unescape (n + (oend - o), start, regexop);
  446. if (had_regexp)
  447. p = grub_strrchr (n, '/');
  448. else
  449. p = 0;
  450. if (!p)
  451. {
  452. grub_free (o);
  453. paths[j++] = n;
  454. continue;
  455. }
  456. *p = 0;
  457. if (!check_file (n, p + 1))
  458. {
  459. grub_dprintf ("expand", "file <%s> in <%s> not found\n",
  460. p + 1, n);
  461. grub_free (o);
  462. grub_free (n);
  463. continue;
  464. }
  465. *p = '/';
  466. grub_free (o);
  467. paths[j++] = n;
  468. }
  469. if (j == 0)
  470. {
  471. grub_free (paths);
  472. paths = 0;
  473. goto done;
  474. }
  475. paths[j] = 0;
  476. }
  477. grub_dprintf ("expand", "paths[0] = `%s'\n", paths[0]);
  478. start = regexop;
  479. continue;
  480. }
  481. if (make_regex (noregexop, regexop, &regexp))
  482. goto fail;
  483. had_regexp = 1;
  484. if (paths == 0)
  485. {
  486. if (start == noregexop) /* device part has regexop */
  487. paths = match_devices (&regexp, *start != '(');
  488. else /* device part explicit wo regexop */
  489. paths = match_files ("", start, noregexop, &regexp);
  490. }
  491. else
  492. {
  493. char **r = 0;
  494. for (i = 0; paths[i]; i++)
  495. {
  496. char **p;
  497. p = match_files (paths[i], start, noregexop, &regexp);
  498. grub_free (paths[i]);
  499. if (! p)
  500. continue;
  501. r = merge (r, p);
  502. if (! r)
  503. goto fail;
  504. }
  505. grub_free (paths);
  506. paths = r;
  507. }
  508. regfree (&regexp);
  509. if (! paths)
  510. goto done;
  511. start = regexop;
  512. }
  513. done:
  514. *strs = paths;
  515. return 0;
  516. fail:
  517. for (i = 0; paths && paths[i]; i++)
  518. grub_free (paths[i]);
  519. grub_free (paths);
  520. regfree (&regexp);
  521. return grub_errno;
  522. }