legacycfg.c 21 KB

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