mirror of
https://github.com/mongodb/mongo.git
synced 2024-11-30 09:06:21 +01:00
All tools (including shell) take -p should allow it to be blank and do password input SERVER-764
forgot to add 2 files
This commit is contained in:
parent
a8aab6f98a
commit
e3811ab624
77
util/password.cpp
Normal file
77
util/password.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
|
||||
#include "../pch.h"
|
||||
#include "password.h"
|
||||
#include <iostream>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <termios.h>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace mongo {
|
||||
|
||||
string askPassword() {
|
||||
|
||||
std::string password;
|
||||
cout << "Enter password: ";
|
||||
#ifndef _WIN32
|
||||
const int stdinfd = 0;
|
||||
termios termio;
|
||||
tcflag_t old = 0;
|
||||
if ( isatty( stdinfd ) ) {
|
||||
int i = tcgetattr( stdinfd, &termio );
|
||||
if( i == -1 ) {
|
||||
cerr << "Cannot get terminal attributes " << errnoWithDescription() << endl;
|
||||
return string();
|
||||
}
|
||||
old = termio.c_lflag;
|
||||
termio.c_lflag &= ~ECHO;
|
||||
i = tcsetattr( stdinfd, TCSANOW, &termio );
|
||||
if( i == -1 ) {
|
||||
cerr << "Cannot set terminal attributes " << errnoWithDescription() << endl;
|
||||
return string();
|
||||
}
|
||||
}
|
||||
|
||||
cin >> password;
|
||||
|
||||
if ( isatty( stdinfd ) ) {
|
||||
termio.c_lflag = old;
|
||||
int i = tcsetattr( stdinfd, TCSANOW, &termio );
|
||||
if( i == -1 ) {
|
||||
cerr << "Cannot set terminal attributes " << errnoWithDescription() << endl;
|
||||
return string();
|
||||
}
|
||||
}
|
||||
#else
|
||||
HANDLE stdinh = GetStdHandle( STD_INPUT_HANDLE );
|
||||
if ( stdinh == INVALID_HANDLE_VALUE) {
|
||||
cerr << "Cannot get stdin handle " << GetLastError() << "\n";
|
||||
return string();
|
||||
}
|
||||
|
||||
DWORD old;
|
||||
if ( !GetConsoleMode( stdinh, &old ) ) {
|
||||
cerr << "Cannot get console mode " << GetLastError() << "\n";
|
||||
return string();
|
||||
}
|
||||
|
||||
DWORD noecho = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
|
||||
if ( !SetConsoleMode( stdinh, noecho ) ) {
|
||||
cerr << "Cannot set console mode " << GetLastError() << "\n";
|
||||
return string();
|
||||
}
|
||||
|
||||
cin >> password;
|
||||
|
||||
if ( !SetConsoleMode( stdinh, old ) ) {
|
||||
cerr << "Cannot set console mode " << GetLastError() << "\n";
|
||||
return string();
|
||||
}
|
||||
cin.get();
|
||||
#endif
|
||||
cout << "\n";
|
||||
return password;
|
||||
}
|
||||
}
|
45
util/password.h
Normal file
45
util/password.h
Normal file
@ -0,0 +1,45 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace mongo {
|
||||
|
||||
struct PasswordValue : public boost::program_options::typed_value<std::string> {
|
||||
|
||||
PasswordValue( std::string* val )
|
||||
: boost::program_options::typed_value<std::string>( val ) { }
|
||||
|
||||
unsigned min_tokens() const {
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned max_tokens() const {
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool is_required() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
void xparse( boost::any& value_store,
|
||||
const std::vector<std::string>& new_tokens ) const {
|
||||
if ( !value_store.empty() )
|
||||
#if BOOST_VERSION >= 104200
|
||||
boost::throw_exception( boost::program_options::validation_error( boost::program_options::validation_error::multiple_values_not_allowed ) );
|
||||
#else
|
||||
boost::throw_exception( boost::program_options::validation_error( "multiple values not allowed" ) );
|
||||
#endif
|
||||
else if ( !new_tokens.empty() )
|
||||
boost::program_options::typed_value<std::string>::xparse
|
||||
(value_store, new_tokens);
|
||||
else
|
||||
value_store = std::string();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
std::string askPassword();
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user