From 73de112ae4e5e44523d0da33a02de602ab3f232e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Sun, 25 Feb 2018 04:24:05 +0200 Subject: [PATCH] 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. --- .../MaxScale-2.2.3-Release-Notes.md | 7 +++ client/maxadmin.c | 57 +++++++++++-------- 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/Documentation/Release-Notes/MaxScale-2.2.3-Release-Notes.md b/Documentation/Release-Notes/MaxScale-2.2.3-Release-Notes.md index cf1f85e50..c7c1d4f58 100644 --- a/Documentation/Release-Notes/MaxScale-2.2.3-Release-Notes.md +++ b/Documentation/Release-Notes/MaxScale-2.2.3-Release-Notes.md @@ -17,6 +17,13 @@ being monitored by the mariadbmon monitor, the current GTID position will be displayed in the newly added column. If no GTID is available, an empty value is returned. +### MaxAdmin input from scripts + +The failure to set terminal attributes for MaxScale is no longer considered an +error as scripts most often do not have an actual terminal that control the +process. This means that passwords and other commands can be passed to MaxAdmin +without a controlling terminal. + ## Dropped Features ## New Features diff --git a/client/maxadmin.c b/client/maxadmin.c index 9ef3a33ce..486f3bbce 100644 --- a/client/maxadmin.c +++ b/client/maxadmin.c @@ -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; }