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