From b6f7d36a544a7a8c18c815ef4158cf684108d06d Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 16 Aug 2009 03:01:12 +0200 Subject: =?UTF-8?q?Passw=C3=B6rter=20k=C3=B6nnen=20gesetzt=20werden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Client/UserCommands.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) (limited to 'src/Client/UserCommands.cpp') 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 - #include #include +#include namespace Mad { @@ -491,5 +491,67 @@ void UserCommands::deleteUserFromGroupCommand(CommandParser *commandParser, cons } } +void UserCommands::setPasswordCommand(CommandParser *commandParser, const std::vector &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; + } +} + } } -- cgit v1.2.3