MXS-1683: Allow MaxAdmin use without controlling tty

When MaxAdmin would be used without a controlling terminal, it would
refuse to accept passwords from stdin as it could not set the terminal
attributes. This means that executing MaxAdmin commands from other
programs would fail if the process had no controlling terminal.

Turning the error into a warning will allow users to know that terminal
echo is still enabled before they type their passwords.
This commit is contained in:
Markus Mäkelä
2018-02-25 04:24:05 +02:00
parent 8bfb4f231a
commit 73de112ae4
2 changed files with 41 additions and 23 deletions

View File

@ -937,8 +937,7 @@ read_inifile(char **socket,
*/
bool getPassword(char *passwd, size_t len)
{
bool gotten = false;
bool err = false;
struct termios tty_attr;
tcflag_t c_lflag;
@ -948,36 +947,48 @@ bool getPassword(char *passwd, size_t len)
tty_attr.c_lflag &= ~ICANON;
tty_attr.c_lflag &= ~ECHO;
if (tcsetattr(STDIN_FILENO, 0, &tty_attr) == 0)
if (tcsetattr(STDIN_FILENO, 0, &tty_attr) != 0)
{
printf("Password: ");
if (fgets(passwd, len, stdin) == NULL)
{
printf("Failed to read password\n");
}
err = true;
}
}
else
{
err = true;
}
tty_attr.c_lflag = c_lflag;
if (err)
{
fprintf(stderr,
"Warning: Could not configure terminal. Terminal echo is still enabled. This\n"
"means that the password will be visible on the controlling terminal when\n"
"it is written!\n");
}
if (tcsetattr(STDIN_FILENO, 0, &tty_attr) == 0)
{
int i = strlen(passwd);
printf("Password: ");
if (fgets(passwd, len, stdin) == NULL)
{
printf("Failed to read password\n");
}
if (i > 1)
{
passwd[i - 1] = '\0';
}
if (!err)
{
tty_attr.c_lflag = c_lflag;
printf("\n");
gotten = true;
}
if (tcsetattr(STDIN_FILENO, 0, &tty_attr) != 0)
{
err = true;
}
}
if (!gotten)
int i = strlen(passwd);
if (i > 0)
{
fprintf(stderr, "Could not configure terminal.\n");
passwd[i - 1] = '\0';
}
return gotten;
printf("\n");
return *passwd;
}