legacycfg.c 21 KB

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