diff options
Diffstat (limited to 'src/Client/UserCommands.cpp')
-rw-r--r-- | src/Client/UserCommands.cpp | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/src/Client/UserCommands.cpp b/src/Client/UserCommands.cpp index b93de94..21259d0 100644 --- a/src/Client/UserCommands.cpp +++ b/src/Client/UserCommands.cpp @@ -22,10 +22,10 @@ #include "CommandParser.h" #include <Common/RequestManager.h> - #include <Common/UserManager.h> #include <iostream> +#include <termios.h> namespace Mad { @@ -491,5 +491,67 @@ void UserCommands::deleteUserFromGroupCommand(CommandParser *commandParser, cons } } +void UserCommands::setPasswordCommand(CommandParser *commandParser, const std::vector<std::string> &args) { + if(args.size() < 2) { + std::cerr << args[0] << ": Too few arguments." << std::endl; + commandParser->printUsage("set_password"); + return; + } + if(args.size() > 2) { + std::cerr << args[0] << ": Too many arguments." << std::endl; + commandParser->printUsage("set_password"); + return; + } + + char *endptr; + unsigned long uid = std::strtoul(args[1].c_str(), &endptr, 10); + if(args[1].empty() || *endptr != '\0') { + std::cerr << args[0] << ": Unable to parse user id." << std::endl; + commandParser->printUsage("set_password"); + return; + } + + // TODO Extract this as a method + struct termios termold, termnew; + + if(tcgetattr(STDIN_FILENO, &termold) != 0) { + std::cerr << "Unable to set up terminal for password entry." << std::endl; + return; + } + + termnew = termold; + termnew.c_lflag &= ~ECHO; + if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &termnew) != 0) { + std::cerr << "Unable to set up terminal for password entry." << std::endl; + return; + } + + std::string password, password2; + + std::cout << "Password: " << std::flush; + std::getline(std::cin, password); + std::cout << std::endl; + + std::cout << "Verify password: " << std::flush; + std::getline(std::cin, password2); + std::cout << std::endl; + + tcsetattr(STDIN_FILENO, TCSAFLUSH, &termold); + + if(password != password2) { + std::cerr << "Passwords do not match." << std::endl; + return; + } + + try { + commandParser->application->getUserManager()->setPassword(uid, password); + + std::cout << "Password set." << std::endl; + } + catch(Core::Exception e) { + std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + } +} + } } |