legacycfg.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2000, 2001, 2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB 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. * GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/types.h>
  19. #include <grub/misc.h>
  20. #include <grub/command.h>
  21. #include <grub/mm.h>
  22. #include <grub/err.h>
  23. #include <grub/dl.h>
  24. #include <grub/file.h>
  25. #include <grub/normal.h>
  26. #include <grub/script_sh.h>
  27. #include <grub/i18n.h>
  28. #include <grub/term.h>
  29. #include <grub/legacy_parse.h>
  30. #include <grub/crypto.h>
  31. #include <grub/auth.h>
  32. #include <grub/disk.h>
  33. #include <grub/partition.h>
  34. #include <grub/safemath.h>
  35. GRUB_MOD_LICENSE ("GPLv3+");
  36. /* Helper for legacy_file. */
  37. static grub_err_t
  38. legacy_file_getline (char **line, int cont __attribute__ ((unused)),
  39. void *data __attribute__ ((unused)))
  40. {
  41. *line = 0;
  42. return GRUB_ERR_NONE;
  43. }
  44. static grub_err_t
  45. legacy_file (const char *filename)
  46. {
  47. grub_file_t file;
  48. char *entryname = NULL, *entrysrc = NULL;
  49. grub_menu_t menu;
  50. char *suffix = grub_strdup ("");
  51. if (!suffix)
  52. return grub_errno;
  53. file = grub_file_open (filename, GRUB_FILE_TYPE_CONFIG);
  54. if (! file)
  55. {
  56. grub_free (suffix);
  57. return grub_errno;
  58. }
  59. menu = grub_env_get_menu ();
  60. if (! menu)
  61. {
  62. menu = grub_zalloc (sizeof (*menu));
  63. if (! menu)
  64. {
  65. grub_free (suffix);
  66. return grub_errno;
  67. }
  68. grub_env_set_menu (menu);
  69. }
  70. while (1)
  71. {
  72. char *buf = grub_file_getline (file);
  73. char *parsed = NULL;
  74. if (!buf && grub_errno)
  75. {
  76. grub_file_close (file);
  77. grub_free (suffix);
  78. return grub_errno;
  79. }
  80. if (!buf)
  81. break;
  82. {
  83. char *oldname = NULL;
  84. char *newsuffix;
  85. char *ptr;
  86. for (ptr = buf; *ptr && grub_isspace (*ptr); ptr++);
  87. oldname = entryname;
  88. parsed = grub_legacy_parse (ptr, &entryname, &newsuffix);
  89. grub_free (buf);
  90. buf = NULL;
  91. if (newsuffix)
  92. {
  93. char *t;
  94. grub_size_t sz;
  95. if (grub_add (grub_strlen (suffix), grub_strlen (newsuffix), &sz) ||
  96. grub_add (sz, 1, &sz))
  97. {
  98. grub_errno = GRUB_ERR_OUT_OF_RANGE;
  99. goto fail_0;
  100. }
  101. t = suffix;
  102. suffix = grub_realloc (suffix, sz);
  103. if (!suffix)
  104. {
  105. grub_free (t);
  106. fail_0:
  107. grub_free (entrysrc);
  108. grub_free (parsed);
  109. grub_free (newsuffix);
  110. grub_free (suffix);
  111. return grub_errno;
  112. }
  113. grub_memcpy (suffix + grub_strlen (suffix), newsuffix,
  114. grub_strlen (newsuffix) + 1);
  115. grub_free (newsuffix);
  116. newsuffix = NULL;
  117. }
  118. if (oldname != entryname && oldname)
  119. {
  120. const char **args = grub_malloc (sizeof (args[0]));
  121. if (!args)
  122. {
  123. grub_file_close (file);
  124. return grub_errno;
  125. }
  126. args[0] = oldname;
  127. grub_normal_add_menu_entry (1, args, NULL, NULL, "legacy",
  128. NULL, NULL,
  129. entrysrc, 0);
  130. grub_free (args);
  131. entrysrc[0] = 0;
  132. grub_free (oldname);
  133. }
  134. }
  135. if (parsed && !entryname)
  136. {
  137. grub_normal_parse_line (parsed, legacy_file_getline, NULL);
  138. grub_print_error ();
  139. grub_free (parsed);
  140. parsed = NULL;
  141. }
  142. else if (parsed)
  143. {
  144. if (!entrysrc)
  145. entrysrc = parsed;
  146. else
  147. {
  148. char *t;
  149. grub_size_t sz;
  150. if (grub_add (grub_strlen (entrysrc), grub_strlen (parsed), &sz) ||
  151. grub_add (sz, 1, &sz))
  152. {
  153. grub_errno = GRUB_ERR_OUT_OF_RANGE;
  154. goto fail_1;
  155. }
  156. t = entrysrc;
  157. entrysrc = grub_realloc (entrysrc, sz);
  158. if (!entrysrc)
  159. {
  160. grub_free (t);
  161. fail_1:
  162. grub_free (parsed);
  163. grub_free (suffix);
  164. return grub_errno;
  165. }
  166. grub_memcpy (entrysrc + grub_strlen (entrysrc), parsed,
  167. grub_strlen (parsed) + 1);
  168. grub_free (parsed);
  169. parsed = NULL;
  170. }
  171. }
  172. }
  173. grub_file_close (file);
  174. if (entryname)
  175. {
  176. const char **args = grub_malloc (sizeof (args[0]));
  177. if (!args)
  178. {
  179. grub_free (suffix);
  180. grub_free (entrysrc);
  181. return grub_errno;
  182. }
  183. args[0] = entryname;
  184. grub_normal_add_menu_entry (1, args, NULL, NULL, NULL,
  185. NULL, NULL, entrysrc, 0);
  186. grub_free (args);
  187. }
  188. grub_normal_parse_line (suffix, legacy_file_getline, NULL);
  189. grub_print_error ();
  190. grub_free (suffix);
  191. grub_free (entrysrc);
  192. return GRUB_ERR_NONE;
  193. }
  194. static grub_err_t
  195. grub_cmd_legacy_source (struct grub_command *cmd,
  196. int argc, char **args)
  197. {
  198. int new_env, extractor;
  199. grub_err_t ret;
  200. if (argc != 1)
  201. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  202. extractor = (cmd->name[0] == 'e');
  203. new_env = (cmd->name[extractor ? (sizeof ("extract_legacy_entries_") - 1)
  204. : (sizeof ("legacy_") - 1)] == 'c');
  205. if (new_env)
  206. grub_cls ();
  207. if (new_env && !extractor)
  208. grub_env_context_open ();
  209. if (extractor)
  210. grub_env_extractor_open (!new_env);
  211. ret = legacy_file (args[0]);
  212. if (new_env)
  213. {
  214. grub_menu_t menu;
  215. menu = grub_env_get_menu ();
  216. if (menu && menu->size)
  217. grub_show_menu (menu, 1, 0);
  218. if (!extractor)
  219. grub_env_context_close ();
  220. }
  221. if (extractor)
  222. grub_env_extractor_close (!new_env);
  223. return ret;
  224. }
  225. static enum
  226. {
  227. GUESS_IT, LINUX, MULTIBOOT, KFREEBSD, KNETBSD, KOPENBSD
  228. } kernel_type;
  229. static grub_err_t
  230. grub_cmd_legacy_kernel (struct grub_command *mycmd __attribute__ ((unused)),
  231. int argc, char **args)
  232. {
  233. int i;
  234. #ifdef TODO
  235. int no_mem_option = 0;
  236. #endif
  237. struct grub_command *cmd;
  238. char **cutargs;
  239. int cutargc;
  240. grub_err_t err = GRUB_ERR_NONE;
  241. for (i = 0; i < 2; i++)
  242. {
  243. /* FIXME: really support this. */
  244. if (argc >= 1 && grub_strcmp (args[0], "--no-mem-option") == 0)
  245. {
  246. #ifdef TODO
  247. no_mem_option = 1;
  248. #endif
  249. argc--;
  250. args++;
  251. continue;
  252. }
  253. /* linux16 handles both zImages and bzImages. */
  254. if (argc >= 1 && (grub_strcmp (args[0], "--type=linux") == 0
  255. || grub_strcmp (args[0], "--type=biglinux") == 0))
  256. {
  257. kernel_type = LINUX;
  258. argc--;
  259. args++;
  260. continue;
  261. }
  262. if (argc >= 1 && grub_strcmp (args[0], "--type=multiboot") == 0)
  263. {
  264. kernel_type = MULTIBOOT;
  265. argc--;
  266. args++;
  267. continue;
  268. }
  269. if (argc >= 1 && grub_strcmp (args[0], "--type=freebsd") == 0)
  270. {
  271. kernel_type = KFREEBSD;
  272. argc--;
  273. args++;
  274. continue;
  275. }
  276. if (argc >= 1 && grub_strcmp (args[0], "--type=openbsd") == 0)
  277. {
  278. kernel_type = KOPENBSD;
  279. argc--;
  280. args++;
  281. continue;
  282. }
  283. if (argc >= 1 && grub_strcmp (args[0], "--type=netbsd") == 0)
  284. {
  285. kernel_type = KNETBSD;
  286. argc--;
  287. args++;
  288. continue;
  289. }
  290. }
  291. if (argc < 2)
  292. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  293. cutargs = grub_calloc (argc - 1, sizeof (cutargs[0]));
  294. if (!cutargs)
  295. return grub_errno;
  296. cutargc = argc - 1;
  297. grub_memcpy (cutargs + 1, args + 2, sizeof (cutargs[0]) * (argc - 2));
  298. cutargs[0] = args[0];
  299. do
  300. {
  301. /* First try Linux. */
  302. if (kernel_type == GUESS_IT || kernel_type == LINUX)
  303. {
  304. #ifdef GRUB_MACHINE_PCBIOS
  305. cmd = grub_command_find ("linux16");
  306. #else
  307. cmd = grub_command_find ("linux");
  308. #endif
  309. if (cmd)
  310. {
  311. if (!(cmd->func) (cmd, cutargc, cutargs))
  312. {
  313. kernel_type = LINUX;
  314. goto out;
  315. }
  316. }
  317. grub_errno = GRUB_ERR_NONE;
  318. }
  319. /* Then multiboot. */
  320. if (kernel_type == GUESS_IT || kernel_type == MULTIBOOT)
  321. {
  322. cmd = grub_command_find ("multiboot");
  323. if (cmd)
  324. {
  325. if (!(cmd->func) (cmd, argc, args))
  326. {
  327. kernel_type = MULTIBOOT;
  328. goto out;
  329. }
  330. }
  331. grub_errno = GRUB_ERR_NONE;
  332. }
  333. {
  334. int bsd_device = -1;
  335. int bsd_slice = -1;
  336. int bsd_part = -1;
  337. {
  338. grub_device_t dev;
  339. const char *hdbiasstr;
  340. int hdbias = 0;
  341. hdbiasstr = grub_env_get ("legacy_hdbias");
  342. if (hdbiasstr)
  343. {
  344. hdbias = grub_strtoul (hdbiasstr, 0, 0);
  345. grub_errno = GRUB_ERR_NONE;
  346. }
  347. dev = grub_device_open (0);
  348. if (dev && dev->disk
  349. && dev->disk->dev->id == GRUB_DISK_DEVICE_BIOSDISK_ID
  350. && dev->disk->id >= 0x80 && dev->disk->id <= 0x90)
  351. {
  352. struct grub_partition *part = dev->disk->partition;
  353. bsd_device = dev->disk->id - 0x80 - hdbias;
  354. if (part && (grub_strcmp (part->partmap->name, "netbsd") == 0
  355. || grub_strcmp (part->partmap->name, "openbsd") == 0
  356. || grub_strcmp (part->partmap->name, "bsd") == 0))
  357. {
  358. bsd_part = part->number;
  359. part = part->parent;
  360. }
  361. if (part && grub_strcmp (part->partmap->name, "msdos") == 0)
  362. bsd_slice = part->number;
  363. }
  364. if (dev)
  365. grub_device_close (dev);
  366. }
  367. /* k*BSD didn't really work well with grub-legacy. */
  368. if (kernel_type == GUESS_IT || kernel_type == KFREEBSD)
  369. {
  370. char buf[sizeof("adXXXXXXXXXXXXsXXXXXXXXXXXXYYY")];
  371. if (bsd_device != -1)
  372. {
  373. if (bsd_slice != -1 && bsd_part != -1)
  374. grub_snprintf(buf, sizeof(buf), "ad%ds%d%c", bsd_device,
  375. bsd_slice, 'a' + bsd_part);
  376. else if (bsd_slice != -1)
  377. grub_snprintf(buf, sizeof(buf), "ad%ds%d", bsd_device,
  378. bsd_slice);
  379. else
  380. grub_snprintf(buf, sizeof(buf), "ad%d", bsd_device);
  381. grub_env_set ("kFreeBSD.vfs.root.mountfrom", buf);
  382. }
  383. else
  384. grub_env_unset ("kFreeBSD.vfs.root.mountfrom");
  385. cmd = grub_command_find ("kfreebsd");
  386. if (cmd)
  387. {
  388. if (!(cmd->func) (cmd, cutargc, cutargs))
  389. {
  390. kernel_type = KFREEBSD;
  391. goto out;
  392. }
  393. }
  394. grub_errno = GRUB_ERR_NONE;
  395. }
  396. {
  397. char **bsdargs;
  398. int bsdargc;
  399. char bsddevname[sizeof ("wdXXXXXXXXXXXXY")];
  400. int found = 0;
  401. if (bsd_device == -1)
  402. {
  403. bsdargs = cutargs;
  404. bsdargc = cutargc;
  405. }
  406. else
  407. {
  408. char rbuf[3] = "-r";
  409. bsdargc = cutargc + 2;
  410. bsdargs = grub_calloc (bsdargc, sizeof (bsdargs[0]));
  411. if (!bsdargs)
  412. {
  413. err = grub_errno;
  414. goto out;
  415. }
  416. grub_memcpy (bsdargs, args, argc * sizeof (bsdargs[0]));
  417. bsdargs[argc] = rbuf;
  418. bsdargs[argc + 1] = bsddevname;
  419. grub_snprintf (bsddevname, sizeof (bsddevname),
  420. "wd%d%c", bsd_device,
  421. bsd_part != -1 ? bsd_part + 'a' : 'c');
  422. }
  423. if (kernel_type == GUESS_IT || kernel_type == KNETBSD)
  424. {
  425. cmd = grub_command_find ("knetbsd");
  426. if (cmd)
  427. {
  428. if (!(cmd->func) (cmd, bsdargc, bsdargs))
  429. {
  430. kernel_type = KNETBSD;
  431. found = 1;
  432. goto free_bsdargs;
  433. }
  434. }
  435. grub_errno = GRUB_ERR_NONE;
  436. }
  437. if (kernel_type == GUESS_IT || kernel_type == KOPENBSD)
  438. {
  439. cmd = grub_command_find ("kopenbsd");
  440. if (cmd)
  441. {
  442. if (!(cmd->func) (cmd, bsdargc, bsdargs))
  443. {
  444. kernel_type = KOPENBSD;
  445. found = 1;
  446. goto free_bsdargs;
  447. }
  448. }
  449. grub_errno = GRUB_ERR_NONE;
  450. }
  451. free_bsdargs:
  452. if (bsdargs != cutargs)
  453. grub_free (bsdargs);
  454. if (found)
  455. goto out;
  456. }
  457. }
  458. }
  459. while (0);
  460. err = grub_error (GRUB_ERR_BAD_OS, "couldn't load file %s",
  461. args[0]);
  462. out:
  463. grub_free (cutargs);
  464. return err;
  465. }
  466. static grub_err_t
  467. grub_cmd_legacy_initrd (struct grub_command *mycmd __attribute__ ((unused)),
  468. int argc, char **args)
  469. {
  470. struct grub_command *cmd;
  471. if (kernel_type == LINUX)
  472. {
  473. #ifdef GRUB_MACHINE_PCBIOS
  474. cmd = grub_command_find ("initrd16");
  475. #else
  476. cmd = grub_command_find ("initrd");
  477. #endif
  478. if (!cmd)
  479. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("can't find command `%s'"),
  480. #ifdef GRUB_MACHINE_PCBIOS
  481. "initrd16"
  482. #else
  483. "initrd"
  484. #endif
  485. );
  486. return cmd->func (cmd, argc ? 1 : 0, args);
  487. }
  488. if (kernel_type == MULTIBOOT)
  489. {
  490. cmd = grub_command_find ("module");
  491. if (!cmd)
  492. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("can't find command `%s'"),
  493. "module");
  494. return cmd->func (cmd, argc, args);
  495. }
  496. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  497. N_("you need to load the kernel first"));
  498. }
  499. static grub_err_t
  500. grub_cmd_legacy_initrdnounzip (struct grub_command *mycmd __attribute__ ((unused)),
  501. int argc, char **args)
  502. {
  503. struct grub_command *cmd;
  504. if (kernel_type == LINUX)
  505. {
  506. cmd = grub_command_find ("initrd16");
  507. if (!cmd)
  508. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("can't find command `%s'"),
  509. "initrd16");
  510. return cmd->func (cmd, argc, args);
  511. }
  512. if (kernel_type == MULTIBOOT)
  513. {
  514. char **newargs;
  515. grub_err_t err;
  516. char nounzipbuf[10] = "--nounzip";
  517. cmd = grub_command_find ("module");
  518. if (!cmd)
  519. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("can't find command `%s'"),
  520. "module");
  521. newargs = grub_calloc (argc + 1, sizeof (newargs[0]));
  522. if (!newargs)
  523. return grub_errno;
  524. grub_memcpy (newargs + 1, args, argc * sizeof (newargs[0]));
  525. newargs[0] = nounzipbuf;
  526. err = cmd->func (cmd, argc + 1, newargs);
  527. grub_free (newargs);
  528. return err;
  529. }
  530. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  531. N_("you need to load the kernel first"));
  532. }
  533. static grub_err_t
  534. check_password_deny (const char *user __attribute__ ((unused)),
  535. const char *entered __attribute__ ((unused)),
  536. void *password __attribute__ ((unused)))
  537. {
  538. return GRUB_ACCESS_DENIED;
  539. }
  540. #define MD5_HASHLEN 16
  541. struct legacy_md5_password
  542. {
  543. grub_uint8_t *salt;
  544. int saltlen;
  545. grub_uint8_t hash[MD5_HASHLEN];
  546. };
  547. static int
  548. check_password_md5_real (const char *entered,
  549. struct legacy_md5_password *pw)
  550. {
  551. grub_size_t enteredlen = grub_strlen (entered);
  552. unsigned char alt_result[MD5_HASHLEN];
  553. unsigned char *digest;
  554. grub_uint8_t *ctx;
  555. grub_size_t i;
  556. int ret;
  557. ctx = grub_zalloc (GRUB_MD_MD5->contextsize);
  558. if (!ctx)
  559. return 0;
  560. GRUB_MD_MD5->init (ctx);
  561. GRUB_MD_MD5->write (ctx, entered, enteredlen);
  562. GRUB_MD_MD5->write (ctx, pw->salt + 3, pw->saltlen - 3);
  563. GRUB_MD_MD5->write (ctx, entered, enteredlen);
  564. digest = GRUB_MD_MD5->read (ctx);
  565. GRUB_MD_MD5->final (ctx);
  566. grub_memcpy (alt_result, digest, MD5_HASHLEN);
  567. GRUB_MD_MD5->init (ctx);
  568. GRUB_MD_MD5->write (ctx, entered, enteredlen);
  569. GRUB_MD_MD5->write (ctx, pw->salt, pw->saltlen); /* include the $1$ header */
  570. for (i = enteredlen; i > 16; i -= 16)
  571. GRUB_MD_MD5->write (ctx, alt_result, 16);
  572. GRUB_MD_MD5->write (ctx, alt_result, i);
  573. for (i = enteredlen; i > 0; i >>= 1)
  574. GRUB_MD_MD5->write (ctx, entered + ((i & 1) ? enteredlen : 0), 1);
  575. digest = GRUB_MD_MD5->read (ctx);
  576. GRUB_MD_MD5->final (ctx);
  577. for (i = 0; i < 1000; i++)
  578. {
  579. grub_memcpy (alt_result, digest, 16);
  580. GRUB_MD_MD5->init (ctx);
  581. if ((i & 1) != 0)
  582. GRUB_MD_MD5->write (ctx, entered, enteredlen);
  583. else
  584. GRUB_MD_MD5->write (ctx, alt_result, 16);
  585. if (i % 3 != 0)
  586. GRUB_MD_MD5->write (ctx, pw->salt + 3, pw->saltlen - 3);
  587. if (i % 7 != 0)
  588. GRUB_MD_MD5->write (ctx, entered, enteredlen);
  589. if ((i & 1) != 0)
  590. GRUB_MD_MD5->write (ctx, alt_result, 16);
  591. else
  592. GRUB_MD_MD5->write (ctx, entered, enteredlen);
  593. digest = GRUB_MD_MD5->read (ctx);
  594. GRUB_MD_MD5->final (ctx);
  595. }
  596. ret = (grub_crypto_memcmp (digest, pw->hash, MD5_HASHLEN) == 0);
  597. grub_free (ctx);
  598. return ret;
  599. }
  600. static grub_err_t
  601. check_password_md5 (const char *user,
  602. const char *entered,
  603. void *password)
  604. {
  605. if (!check_password_md5_real (entered, password))
  606. return GRUB_ACCESS_DENIED;
  607. grub_auth_authenticate (user);
  608. return GRUB_ERR_NONE;
  609. }
  610. static inline int
  611. ib64t (char c)
  612. {
  613. if (c == '.')
  614. return 0;
  615. if (c == '/')
  616. return 1;
  617. if (c >= '0' && c <= '9')
  618. return c - '0' + 2;
  619. if (c >= 'A' && c <= 'Z')
  620. return c - 'A' + 12;
  621. if (c >= 'a' && c <= 'z')
  622. return c - 'a' + 38;
  623. return -1;
  624. }
  625. static struct legacy_md5_password *
  626. parse_legacy_md5 (int argc, char **args)
  627. {
  628. const char *salt, *saltend;
  629. struct legacy_md5_password *pw = NULL;
  630. int i;
  631. const char *p;
  632. if (grub_memcmp (args[0], "--md5", sizeof ("--md5")) != 0)
  633. goto fail;
  634. if (argc == 1)
  635. goto fail;
  636. if (grub_strlen(args[1]) <= 3)
  637. goto fail;
  638. salt = args[1];
  639. saltend = grub_strchr (salt + 3, '$');
  640. if (!saltend)
  641. goto fail;
  642. pw = grub_malloc (sizeof (*pw));
  643. if (!pw)
  644. goto fail;
  645. p = saltend + 1;
  646. for (i = 0; i < 5; i++)
  647. {
  648. int n;
  649. grub_uint32_t w = 0;
  650. for (n = 0; n < 4; n++)
  651. {
  652. int ww = ib64t(*p++);
  653. if (ww == -1)
  654. goto fail;
  655. w |= ww << (n * 6);
  656. }
  657. pw->hash[i == 4 ? 5 : 12+i] = w & 0xff;
  658. pw->hash[6+i] = (w >> 8) & 0xff;
  659. pw->hash[i] = (w >> 16) & 0xff;
  660. }
  661. {
  662. int n;
  663. grub_uint32_t w = 0;
  664. for (n = 0; n < 2; n++)
  665. {
  666. int ww = ib64t(*p++);
  667. if (ww == -1)
  668. goto fail;
  669. w |= ww << (6 * n);
  670. }
  671. if (w >= 0x100)
  672. goto fail;
  673. pw->hash[11] = w;
  674. }
  675. pw->saltlen = saltend - salt;
  676. pw->salt = (grub_uint8_t *) grub_strndup (salt, pw->saltlen);
  677. if (!pw->salt)
  678. goto fail;
  679. return pw;
  680. fail:
  681. grub_free (pw);
  682. return NULL;
  683. }
  684. static grub_err_t
  685. grub_cmd_legacy_password (struct grub_command *mycmd __attribute__ ((unused)),
  686. int argc, char **args)
  687. {
  688. struct legacy_md5_password *pw = NULL;
  689. if (argc == 0)
  690. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
  691. if (args[0][0] != '-' || args[0][1] != '-')
  692. return grub_normal_set_password ("legacy", args[0]);
  693. pw = parse_legacy_md5 (argc, args);
  694. if (pw)
  695. return grub_auth_register_authentication ("legacy", check_password_md5, pw);
  696. else
  697. /* This is to imitate minor difference between grub-legacy in GRUB2.
  698. If 2 password commands are executed in a row and second one fails
  699. on GRUB2 the password of first one is used, whereas in grub-legacy
  700. authenthication is denied. In case of no password command was executed
  701. early both versions deny any access. */
  702. return grub_auth_register_authentication ("legacy", check_password_deny,
  703. NULL);
  704. }
  705. int
  706. grub_legacy_check_md5_password (int argc, char **args,
  707. char *entered)
  708. {
  709. struct legacy_md5_password *pw = NULL;
  710. int ret;
  711. if (args[0][0] != '-' || args[0][1] != '-')
  712. {
  713. char correct[GRUB_AUTH_MAX_PASSLEN];
  714. grub_memset (correct, 0, sizeof (correct));
  715. grub_strncpy (correct, args[0], sizeof (correct));
  716. return grub_crypto_memcmp (entered, correct, GRUB_AUTH_MAX_PASSLEN) == 0;
  717. }
  718. pw = parse_legacy_md5 (argc, args);
  719. if (!pw)
  720. return 0;
  721. ret = check_password_md5_real (entered, pw);
  722. grub_free (pw);
  723. return ret;
  724. }
  725. static grub_err_t
  726. grub_cmd_legacy_check_password (struct grub_command *mycmd __attribute__ ((unused)),
  727. int argc, char **args)
  728. {
  729. char entered[GRUB_AUTH_MAX_PASSLEN];
  730. if (argc == 0)
  731. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
  732. grub_puts_ (N_("Enter password: "));
  733. if (!grub_password_get (entered, GRUB_AUTH_MAX_PASSLEN))
  734. return GRUB_ACCESS_DENIED;
  735. if (!grub_legacy_check_md5_password (argc, args,
  736. entered))
  737. return GRUB_ACCESS_DENIED;
  738. return GRUB_ERR_NONE;
  739. }
  740. static grub_command_t cmd_source, cmd_configfile;
  741. static grub_command_t cmd_source_extract, cmd_configfile_extract;
  742. static grub_command_t cmd_kernel, cmd_initrd, cmd_initrdnounzip;
  743. static grub_command_t cmd_password, cmd_check_password;
  744. GRUB_MOD_INIT(legacycfg)
  745. {
  746. cmd_source
  747. = grub_register_command ("legacy_source",
  748. grub_cmd_legacy_source,
  749. N_("FILE"),
  750. /* TRANSLATORS: "legacy config" means
  751. "config as used by grub-legacy". */
  752. N_("Parse legacy config in same context"));
  753. cmd_configfile
  754. = grub_register_command ("legacy_configfile",
  755. grub_cmd_legacy_source,
  756. N_("FILE"),
  757. N_("Parse legacy config in new context"));
  758. cmd_source_extract
  759. = grub_register_command ("extract_legacy_entries_source",
  760. grub_cmd_legacy_source,
  761. N_("FILE"),
  762. N_("Parse legacy config in same context taking only menu entries"));
  763. cmd_configfile_extract
  764. = grub_register_command ("extract_legacy_entries_configfile",
  765. grub_cmd_legacy_source,
  766. N_("FILE"),
  767. N_("Parse legacy config in new context taking only menu entries"));
  768. cmd_kernel = grub_register_command ("legacy_kernel",
  769. grub_cmd_legacy_kernel,
  770. N_("[--no-mem-option] [--type=TYPE] FILE [ARG ...]"),
  771. N_("Simulate grub-legacy `kernel' command"));
  772. cmd_initrd = grub_register_command ("legacy_initrd",
  773. grub_cmd_legacy_initrd,
  774. N_("FILE [ARG ...]"),
  775. N_("Simulate grub-legacy `initrd' command"));
  776. cmd_initrdnounzip = grub_register_command ("legacy_initrd_nounzip",
  777. grub_cmd_legacy_initrdnounzip,
  778. N_("FILE [ARG ...]"),
  779. N_("Simulate grub-legacy `modulenounzip' command"));
  780. cmd_password = grub_register_command ("legacy_password",
  781. grub_cmd_legacy_password,
  782. N_("[--md5] PASSWD [FILE]"),
  783. N_("Simulate grub-legacy `password' command"));
  784. cmd_check_password = grub_register_command ("legacy_check_password",
  785. grub_cmd_legacy_check_password,
  786. N_("[--md5] PASSWD [FILE]"),
  787. N_("Simulate grub-legacy `password' command in menu entry mode"));
  788. }
  789. GRUB_MOD_FINI(legacycfg)
  790. {
  791. grub_unregister_command (cmd_source);
  792. grub_unregister_command (cmd_configfile);
  793. grub_unregister_command (cmd_source_extract);
  794. grub_unregister_command (cmd_configfile_extract);
  795. grub_unregister_command (cmd_kernel);
  796. grub_unregister_command (cmd_initrd);
  797. grub_unregister_command (cmd_initrdnounzip);
  798. grub_unregister_command (cmd_password);
  799. grub_unregister_command (cmd_check_password);
  800. }