Skip to content

SERVER-46007 ignore SIGINT when input password in stdin #1348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions src/mongo/util/password.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@

namespace mongo {

#ifndef _WIN32
char getachar(void)
{
struct termios tm, tm_temp;
char chr;
if (isatty(STDIN_FILENO)) {
if(tcgetattr(STDIN_FILENO, &tm) < 0)
return -1;
tm_temp = tm;
cfmakeraw(&tm);
if(tcsetattr(STDIN_FILENO, TCSANOW, &tm) < 0)
return -1;
}
chr = getchar();
if (isatty(STDIN_FILENO)) {
if(tcsetattr(STDIN_FILENO, TCSANOW, &tm_temp) < 0)
return -1;
}
return chr;
}
#endif

std::string askPassword() {
std::string password;
std::cerr << "Enter password: ";
Expand All @@ -53,33 +75,11 @@ std::string askPassword() {
}

#ifndef _WIN32
const int stdinfd = 0;
termios termio;
tcflag_t old = 0;
if (isatty(stdinfd)) {
int i = tcgetattr(stdinfd, &termio);
if (i == -1) {
std::cerr << "Cannot get terminal attributes " << errnoWithDescription() << std::endl;
return std::string();
}
old = termio.c_lflag;
termio.c_lflag &= ~ECHO;
i = tcsetattr(stdinfd, TCSANOW, &termio);
if (i == -1) {
std::cerr << "Cannot set terminal attributes " << errnoWithDescription() << std::endl;
return std::string();
}
}

getline(std::cin, password);

if (isatty(stdinfd)) {
termio.c_lflag = old;
int i = tcsetattr(stdinfd, TCSANOW, &termio);
if (i == -1) {
std::cerr << "Cannot set terminal attributes " << errnoWithDescription() << std::endl;
return std::string();
}
char chr;
while ((chr = getachar()) > 0) {
if (chr == '\r' || chr == '\n')
break;
password.append(&chr);
}
#else
HANDLE stdinh = GetStdHandle(STD_INPUT_HANDLE);
Expand Down