Skip to content

change "-c -" to pull password input from stdin instead of tty #169

@EeyoreTheDonkee

Description

@EeyoreTheDonkee

e.g. something like the following (i.e. get_password) in fsarchiver.c instead of <unistd.h> getpass and removal of password verification would also be helpful in scripts and databases.

#include <termios.h>

void hide_terminal_input() {
    struct termios tty;
    tcgetattr(STDIN_FILENO, &tty);
    tty.c_lflag &= ~ECHO; // Disable echo
    tcsetattr(STDIN_FILENO, TCSANOW, &tty);
}

void show_terminal_input() {
    struct termios tty;
    tcgetattr(STDIN_FILENO, &tty);
    tty.c_lflag |= ECHO; // Enable echo
    tcsetattr(STDIN_FILENO, TCSANOW, &tty);
}

void get_password(char *password, size_t size) {
    printf("Enter password: ");
    hide_terminal_input();
    fgets(password, size, stdin);
    show_terminal_input();

    // null terminator for strlen
    size_t len = strlen(password);
    if (len > 0 && password[len - 1] == '\n') {
        password[len - 1] = '\0'; 
    }
    printf("\n");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions