#include "ConsoleSession.h" #include "ConsoleServer.h" #include "TCPServer.h" #include "Log.h" namespace core { ConsoleSession::ConsoleSession(EPoll &ePoll, TCPServer *server) : TerminalSession(ePoll, server) {} ConsoleSession::~ConsoleSession() {} void ConsoleSession::protocol(coreutils::ZString &data) { coreutils::Log(coreutils::LOG_DEBUG_1) << data; coreutils::ZString blank(""); switch (status) { case WELCOME: status = LOGIN; protocol(blank); break; case LOGIN: out << "User: "; status = WAIT_USER_PROFILE; break; case WAIT_USER_PROFILE: // Previously discarded entirely - nothing captured what was // actually typed here, so a real credential check further // down could never have anything to compare against. enteredUser = data.str(); status = PASSWORD; protocol(blank); break; case PASSWORD: out << "Password: "; status = WAIT_PASSWORD; break; case WAIT_PASSWORD: // The actual check: previously this state unconditionally // advanced to PROMPT regardless of what (if anything) was // entered at either step - any username/password, including // blank, reached the command prompt. checkCredentials() // returns true unconditionally when the owning ConsoleServer // was built without a credentialChecker, preserving that // exact behavior for any other use of this class. if (((ConsoleServer *)server)->checkCredentials(enteredUser, data.str())) { status = PROMPT; protocol(blank); } else { out << "Login failed." << "\r\n"; status = DONE; protocol(blank); } break; case PROMPT: out << (": "); status = INPUT; break; case INPUT: command = data; status = PROCESS; protocol(blank); break; case PROCESS: doCommand(command); status = command.equals("exit") ? DONE: PROMPT; protocol(blank); break; case DONE: out << "Done." << std::endl; break; default: out << "Unrecognized status code: ERROR."; break; } } void ConsoleSession::onConnected() { coreutils::ZString blank(""); protocol(blank); } void ConsoleSession::writeLog(std::string data) { // saveCursor(); // setCursorLocation(16, 1); // restoreCursor(); } void ConsoleSession::doCommand(coreutils::ZString &request) { server->commands.processRequest(request, *this); } }