wildcard.c 12 KB

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