ismaelvazquezjr icon

Pointer Mutation

ismaelvazquezjr | PRO | 09/27/20 04:41:29 AM UTC | 0 ⭐ | 1201 👁️ | Never ⏰ | []
C++ |

1.37 KB

|

None

|

0 👍

/

0 👎

#include <string>
#include <iostream>
 
std::string getInput(std::string prompt);
 
enum UserType : int {
    Admin = 0,
    Normal = 1
};
 
void runServer(
    size_t numberOfUsers,
    std::string* passwords,
    std::string* users,
    int* failedLogins, int* userTypes
) {
    while (true) {
        auto name = getInput("Username");
        auto password = getInput("Password");
 
        // lookup user
        size_t userId = 0;
        for (; userId < numberOfUsers; userId++)
            if (users[userId] == name)
                break;
 
        // check password 
        if (userId == numberOfUsers || passwords[userId] != password) {
            failedLogins++;
            std::cout << "Invalid username or password!\n";
            continue;
        }
        else
            failedLogins[userId] = 0;
 
        // execute command 
        auto command = getInput("Command");
        if (command == "shell") {
            if (userTypes[userId] == UserType::Admin) system("cmd.exe");
            else std::cout << "Only an admin can execute this command!\n";
        }
        
    }
}
 
void main() {
    size_t numberOfUsers = 2;
    std::string passwords[] = { "passw0rd", "12345" };
    std::string users[] = { "admin", "guest" };
    int failedLogins[] = { 0, 0 };
    int userTypes[] = { UserType::Admin, UserType::Normal };
 
    runServer(numberOfUsers, passwords, users, failedLogins, userTypes);
}
 
std::string getInput(std::string prompt) {
    std::string input;
    std::cout << prompt << ": ";
    std::cin >> input;
    return input;
}

Comments