Hello all,

Seeing that GRUB's password feature is somewhat flawed (i.e. it is not a
difficult matter to try every key until one works and repeat the process
to obtain the password), I have reimplemented the password entry code so
that it acts more like a normal entry, where the user enters the whole
password before it is checked against the real one.

The attached patch does three things: adding an implementation of strcmp,
adding a "hidden" flag to get_cmdline, so that the characters echoed back
are asterisks, and reimplementing the password feature as indicated. I
wrote it for GRUB 0.5.91 but the patch applies cleanly to the most recent
CVS tree (as of this afternoon).

-Bradford Hovinen

"Thou art a soul in bliss, but I am bound
 Upon a wheel of fire, that mine own tears
 Do scald like molten lead."
 
         -Shakespeare, "King Lear", 4.7.52-53
diff -x *.mk -x *stage1_5 -x *.o -x stage1 -x stage2 -x config.* -x Makefile -p -r -C 
2 grub-0.5.91/shared_src/char_io.c grub/shared_src/char_io.c
*** grub-0.5.91/shared_src/char_io.c    Sun Mar 14 21:08:05 1999
--- grub/shared_src/char_io.c   Wed Mar 24 22:54:58 1999
*************** init_page (void)
*** 140,144 ****
     or zero-length). */
  int
! get_cmdline (char *prompt, char *commands, char *cmdline, int maxlen)
  {
    int ystart, yend, xend, lpos, c;
--- 140,144 ----
     or zero-length). */
  int
! get_cmdline (char *prompt, char *commands, char *cmdline, int maxlen, int hidden)
  {
    int ystart, yend, xend, lpos, c;
*************** get_cmdline (char *prompt, char *command
*** 147,155 ****
  
    /* nested function definition for code simplicity */
!   static void cl_print (char *str)
    {
      while (*str != 0)
        {
!       putchar (*(str++));
        if (++xend > 78)
          {
--- 147,164 ----
  
    /* nested function definition for code simplicity */
!   static void cl_print (char *str, int hidden)
    {
      while (*str != 0)
        {
!         if (hidden)
!           {
!             putchar ('*');
!             str++;
!           }
!         else
!           {
!           putchar (*(str++));
!           }
!         
        if (++xend > 78)
          {
*************** get_cmdline (char *prompt, char *command
*** 181,186 ****
      yend = ystart;
      xend = 0;
!     cl_print (prompt);
!     cl_print (cmdline);
      cl_setcpos ();
    }
--- 190,195 ----
      yend = ystart;
      xend = 0;
!     cl_print (prompt, 0);
!     cl_print (cmdline, hidden);
      cl_setcpos ();
    }
*************** get_cmdline (char *prompt, char *command
*** 325,329 ****
              if (lpos != llen)
                {
!                 cl_print (cmdline + lpos);
                  cl_setcpos ();
                }
--- 334,338 ----
              if (lpos != llen)
                {
!                 cl_print (cmdline + lpos, hidden);
                  cl_setcpos ();
                }
*************** get_cmdline (char *prompt, char *command
*** 349,353 ****
                  lpos = 0;
                  cl_setcpos ();
!                 cl_print (cmdline);
                  cl_setcpos ();
                }
--- 358,362 ----
                  lpos = 0;
                  cl_setcpos ();
!                 cl_print (cmdline, hidden);
                  cl_setcpos ();
                }
*************** get_cmdline (char *prompt, char *command
*** 361,365 ****
                  cmdline[lpos] = c;
                  cmdline[lpos + 1] = 0;
!                 cl_print (cmdline + lpos);
                  lpos++;
                }
--- 370,374 ----
                  cmdline[lpos] = c;
                  cmdline[lpos + 1] = 0;
!                 cl_print (cmdline + lpos, hidden);
                  lpos++;
                }
*************** get_cmdline (char *prompt, char *command
*** 371,375 ****
                  cmdline[lpos] = c;
                  cl_setcpos ();
!                 cl_print (cmdline + lpos);
                  lpos++;
                  cl_setcpos ();
--- 380,384 ----
                  cmdline[lpos] = c;
                  cl_setcpos ();
!                 cl_print (cmdline + lpos, hidden);
                  lpos++;
                  cl_setcpos ();
*************** strstr (char *s1, char *s2)
*** 559,562 ****
--- 568,583 ----
  }
  
+ int 
+ strcmp (char *s1, char *s2)
+ {
+   while (*s1 || *s2)
+     {
+       if (*s1 != *s2)
+         return *s1 - *s2;
+       s1++; s2++;
+     }
+   
+   return 0;
+ }
  
  int
diff -x *.mk -x *stage1_5 -x *.o -x stage1 -x stage2 -x config.* -x Makefile -p -r -C 
2 grub-0.5.91/shared_src/cmdline.c grub/shared_src/cmdline.c
*** grub-0.5.91/shared_src/cmdline.c    Sun Mar 14 21:08:06 1999
--- grub/shared_src/cmdline.c   Wed Mar 24 22:28:38 1999
*************** returnit:
*** 225,229 ****
      }
  
!   if (run_cmdline && get_cmdline(PACKAGE "> ", commands, cur_heap, 2048))
      return 1;
  
--- 225,229 ----
      }
  
!   if (run_cmdline && get_cmdline(PACKAGE "> ", commands, cur_heap, 2048, 0))
      return 1;
  
diff -x *.mk -x *stage1_5 -x *.o -x stage1 -x stage2 -x config.* -x Makefile -p -r -C 
2 grub-0.5.91/shared_src/shared.h grub/shared_src/shared.h
*** grub-0.5.91/shared_src/shared.h     Sun Mar 14 21:08:11 1999
--- grub/shared_src/shared.h    Wed Mar 24 22:52:37 1999
*************** extern char *grub_scratch_mem;
*** 212,216 ****
  #define strstr grub_strstr
  #define tolower grub_tolower
! 
  
  #ifndef ASM_FILE
--- 212,216 ----
  #define strstr grub_strstr
  #define tolower grub_tolower
! #define strcmp grub_strcmp
  
  #ifndef ASM_FILE
*************** int grub_bcopy (char *from, char *to, in
*** 439,442 ****
--- 439,443 ----
  int grub_bzero (char *start, int len);
  char *grub_strstr (char *s1, char *s2);
+ int grub_strcmp (char *s1, char *s2);
  
  /* misc */
*************** void init_page (void);
*** 444,448 ****
  void print_error (void);
  char *convert_to_ascii (char *buf, int c,...);
! int get_cmdline (char *prompt, char *commands, char *cmdline, int maxlen);
  int substring (char *s1, char *s2);
  int get_based_digit (int c, int base);
--- 445,449 ----
  void print_error (void);
  char *convert_to_ascii (char *buf, int c,...);
! int get_cmdline (char *prompt, char *commands, char *cmdline, int maxlen, int 
hidden);
  int substring (char *s1, char *s2);
  int get_based_digit (int c, int base);
diff -x *.mk -x *stage1_5 -x *.o -x stage1 -x stage2 -x config.* -x Makefile -p -r -C 
2 grub-0.5.91/shared_src/stage2.c grub/shared_src/stage2.c
*** grub-0.5.91/shared_src/stage2.c     Sun Mar 14 21:08:13 1999
--- grub/shared_src/stage2.c    Wed Mar 24 23:02:14 1999
*************** restart:
*** 315,336 ****
                {
                  /* Do password check here! */
!                 char *ptr = password;
!                 gotoxy(2, 22);
!                 printf("Entering password...  ");
!                 do
!                   {
!                     if (isspace(*ptr))
!                       {
!                         char *new_file = config_file;
!                         while (isspace(*ptr)) ptr++;
!                         while ((*(new_file++) = *(ptr++)) != 0);
!                         return;
!                       }
!                     c = ASCII_CHAR(getkey());
!                   }
!                 while (*(ptr++) == c);
!                 printf("Failed!\n      Press any key to continue...");
!                 getkey();
!                 goto restart;
                }
            }
--- 315,344 ----
                {
                  /* Do password check here! */
!                   char entered[32];
!                   char *pptr = password, *passwd_end;
!                   
!                 gotoxy(1, 21);
!                 get_cmdline(" Password: ", commands, entered, 31, 1);
!                   
!                   while (!isspace (*pptr)) pptr++;
!                   passwd_end = pptr; *pptr = '\0'; pptr++;
!                   
!                   if (!strcmp (password, entered))
!                     {
!                     char *new_file = config_file;
!                       bzero (entered, 32);
!                     while (isspace(*pptr)) pptr++;
!                     while ((*(new_file++) = *(pptr++)));
!                       *passwd_end = ' ';
!                     return;
!                     }
!                   else
!                     {
!                       bzero (entered, 32);
!                     printf("Failed!\n      Press any key to continue...");
!                       *passwd_end = ' ';
!                     getkey();
!                     goto restart;
!                     }
                }
            }
*************** restart:
*** 381,385 ****
  
                      if (!get_cmdline(PACKAGE " edit> ", commands, new_heap,
!                                      NEW_HEAPSIZE + 1))
                        {
                          int j = 0;
--- 389,393 ----
  
                      if (!get_cmdline(PACKAGE " edit> ", commands, new_heap,
!                                      NEW_HEAPSIZE + 1, 0))
                        {
                          int j = 0;

Reply via email to