legacycfg.c 20 KB

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