opal: Set terminal attribute for secure input

Previously we use getpass. But it looks like this
is obsolete, this patch is to fix this.

Change-Id: If3b667cea8e09aab170bfdb75b8d51e6855bb0b0
Signed-off-by: Chunyang Hui <chunyang.hui@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/461151
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
This commit is contained in:
Chunyang Hui 2019-07-10 18:05:48 +08:00 committed by Changpeng Liu
parent e27421b344
commit 63133871d2

View File

@ -286,16 +286,39 @@ display_controller_list(void)
} }
static char * static char *
get_line(char *buf, int buf_size, FILE *f) get_line(char *buf, int buf_size, FILE *f, bool secret)
{ {
char *ret; char *ch;
size_t len; size_t len;
struct termios default_attr = {}, new_attr = {};
int ret;
ret = fgets(buf, buf_size, f); if (secret) {
if (ret == NULL) { ret = tcgetattr(STDIN_FILENO, &default_attr);
if (ret) {
return NULL; return NULL;
} }
new_attr = default_attr;
new_attr.c_lflag &= ~ECHO; /* disable echo */
ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, &new_attr);
if (ret) {
return NULL;
}
}
ch = fgets(buf, buf_size, f);
if (ch == NULL) {
return NULL;
}
if (secret) {
ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, &default_attr); /* restore default confing */
if (ret) {
return NULL;
}
}
len = strlen(buf); len = strlen(buf);
if (len > 0 && buf[len - 1] == '\n') { if (len > 0 && buf[len - 1] == '\n') {
buf[len - 1] = '\0'; buf[len - 1] = '\0';
@ -321,7 +344,7 @@ get_controller(void)
printf("Please Input PCI Address(domain:bus:dev.func):\n"); printf("Please Input PCI Address(domain:bus:dev.func):\n");
while ((ch = getchar()) != '\n' && ch != EOF); while ((ch = getchar()) != '\n' && ch != EOF);
p = get_line(address, 64, stdin); p = get_line(address, 64, stdin, false);
if (p == NULL) { if (p == NULL) {
return NULL; return NULL;
} }
@ -794,7 +817,7 @@ update_firmware_image(void)
printf("Please Input The Path Of Firmware Image\n"); printf("Please Input The Path Of Firmware Image\n");
if (get_line(path, sizeof(path), stdin) == NULL) { if (get_line(path, sizeof(path), stdin, false) == NULL) {
printf("Invalid path setting\n"); printf("Invalid path setting\n");
while (getchar() != '\n'); while (getchar() != '\n');
return; return;
@ -985,7 +1008,7 @@ opal_init(struct dev *iter)
if (spdk_opal_supported(iter->opal_dev)) { if (spdk_opal_supported(iter->opal_dev)) {
printf("Please input the new password for ownership:\n"); printf("Please input the new password for ownership:\n");
while ((ch = getchar()) != '\n' && ch != EOF); while ((ch = getchar()) != '\n' && ch != EOF);
passwd_p = getpass(new_passwd); passwd_p = get_line(new_passwd, MAX_PASSWORD_SIZE, stdin, true);
if (passwd_p) { if (passwd_p) {
ret = spdk_opal_cmd_take_ownership(iter->opal_dev, passwd_p); ret = spdk_opal_cmd_take_ownership(iter->opal_dev, passwd_p);
if (ret) { if (ret) {
@ -1043,7 +1066,7 @@ opal_setup_lockingrange(struct dev *iter)
if (spdk_opal_supported(iter->opal_dev)) { if (spdk_opal_supported(iter->opal_dev)) {
printf("Please input the password for setting up locking range:\n"); printf("Please input the password for setting up locking range:\n");
while ((ch = getchar()) != '\n' && ch != EOF); while ((ch = getchar()) != '\n' && ch != EOF);
passwd_p = getpass(passwd); passwd_p = get_line(passwd, MAX_PASSWORD_SIZE, stdin, true);
if (passwd_p) { if (passwd_p) {
ret = spdk_opal_cmd_lock_unlock(iter->opal_dev, OPAL_ADMIN1, OPAL_READWRITE, ret = spdk_opal_cmd_lock_unlock(iter->opal_dev, OPAL_ADMIN1, OPAL_READWRITE,
OPAL_LOCKING_RANGE_GLOBAL, passwd_p); OPAL_LOCKING_RANGE_GLOBAL, passwd_p);
@ -1147,7 +1170,7 @@ opal_list_locking_ranges(struct dev *iter)
if (spdk_opal_supported(iter->opal_dev)) { if (spdk_opal_supported(iter->opal_dev)) {
printf("Please input password:\n"); printf("Please input password:\n");
while ((ch = getchar()) != '\n' && ch != EOF); while ((ch = getchar()) != '\n' && ch != EOF);
passwd_p = getpass(passwd); passwd_p = get_line(passwd, MAX_PASSWORD_SIZE, stdin, true);
if (passwd_p) { if (passwd_p) {
ret = spdk_opal_cmd_get_max_ranges(iter->opal_dev, passwd_p); ret = spdk_opal_cmd_get_max_ranges(iter->opal_dev, passwd_p);
if (ret) { if (ret) {
@ -1208,7 +1231,7 @@ opal_revert_tper(struct dev *iter)
printf("Please be noted this operation will erase ALL DATA on this drive\n"); printf("Please be noted this operation will erase ALL DATA on this drive\n");
printf("Please input password for revert TPer:\n"); printf("Please input password for revert TPer:\n");
while ((ch = getchar()) != '\n' && ch != EOF); while ((ch = getchar()) != '\n' && ch != EOF);
passwd_p = getpass(passwd); passwd_p = get_line(passwd, MAX_PASSWORD_SIZE, stdin, true);
if (passwd_p) { if (passwd_p) {
ret = spdk_opal_cmd_revert_tper(iter->opal_dev, passwd_p); ret = spdk_opal_cmd_revert_tper(iter->opal_dev, passwd_p);
if (ret) { if (ret) {