now the password is hidden

This commit is contained in:
Ghostie 2025-02-06 02:38:19 +00:00
parent d037a43136
commit 5e363aed5e
2 changed files with 31 additions and 4 deletions

View File

@ -89,7 +89,7 @@ In that code now we have two users: ~lain~ with the settings we mentioned before
and a new user ~alyssa~ that will execute commands as ~dbuser~ and ~PERM_PASSWD~ will and a new user ~alyssa~ that will execute commands as ~dbuser~ and ~PERM_PASSWD~ will
make that she has to enter her password every time she runs ~beroot~. make that she has to enter her password every time she runs ~beroot~.
** TODO ** TO-DO List
I don't want to make this a big program, I want to keep everything as simple as I don't want to make this a big program, I want to keep everything as simple as
possible, but there are some things that I want to do: possible, but there are some things that I want to do:
@ -97,6 +97,5 @@ possible, but there are some things that I want to do:
standard library. standard library.
- Some kind of persistency (there can be a ~PERM_PERSIST~ flag) so the password - Some kind of persistency (there can be a ~PERM_PERSIST~ flag) so the password
doesn't need to be entered every time. doesn't need to be entered every time.
- Hide the password while it's being typed.
Those are the only things I have planned adding to beroot. Those are the only things I have planned adding to beroot.

View File

@ -7,6 +7,8 @@
#include <shadow.h> #include <shadow.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h>
#include "config.h" #include "config.h"
void void
@ -104,6 +106,30 @@ execute (char **command)
commands = NULL; commands = NULL;
} }
void
get_password (char *password)
{
if (!password)
return;
static struct termios old_t = { 0 }, new_t = { 0 };
tcgetattr (STDIN_FILENO, &old_t);
new_t = old_t;
new_t.c_lflag &= ~(ECHO);
tcsetattr (STDIN_FILENO, TCSANOW, &new_t);
int i = 0;
char c = '\0';
while ((c = getchar ()) != '\n' && c != EOF && i < LOGIN_PASSWD_MAX)
{
password[i++] = c;
}
password[i] = '\0';
tcsetattr (STDIN_FILENO, TCSANOW, &old_t);
puts (""); // the \n character is not printed anymore
}
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
@ -150,10 +176,12 @@ main (int argc, char *argv[])
else if (user->permissions & PERM_PASSWD) else if (user->permissions & PERM_PASSWD)
{ {
printf ("Enter the %s password: ", user->target_user); printf ("Enter the %s password: ", user->target_user);
char entered_password[LOGIN_PASSWD_MAX] = { 0 }; char *entered_password = calloc (sizeof (char), LOGIN_PASSWD_MAX);
fgets (entered_password, LOGIN_PASSWD_MAX - 1, stdin); get_password (entered_password);
is_authenticated is_authenticated
= authenticate_user (target_swpd->sp_pwdp, entered_password); = authenticate_user (target_swpd->sp_pwdp, entered_password);
free (entered_password);
} }
else else
{ {