Advertisement
ismaelvazquezjr

Pointer Mutation

Sep 26th, 2020
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3.  
  4. std::string getInput(std::string prompt);
  5.  
  6. enum UserType : int {
  7.     Admin = 0,
  8.     Normal = 1
  9. };
  10.  
  11. void runServer(
  12.     size_t numberOfUsers,
  13.     std::string* passwords,
  14.     std::string* users,
  15.     int* failedLogins, int* userTypes
  16. ) {
  17.     while (true) {
  18.         auto name = getInput("Username");
  19.         auto password = getInput("Password");
  20.  
  21.         // lookup user
  22.         size_t userId = 0;
  23.         for (; userId < numberOfUsers; userId++)
  24.             if (users[userId] == name)
  25.                 break;
  26.  
  27.         // check password
  28.         if (userId == numberOfUsers || passwords[userId] != password) {
  29.             failedLogins++;
  30.             std::cout << "Invalid username or password!\n";
  31.             continue;
  32.         }
  33.         else
  34.             failedLogins[userId] = 0;
  35.  
  36.         // execute command
  37.         auto command = getInput("Command");
  38.         if (command == "shell") {
  39.             if (userTypes[userId] == UserType::Admin) system("cmd.exe");
  40.             else std::cout << "Only an admin can execute this command!\n";
  41.         }
  42.        
  43.     }
  44. }
  45.  
  46. void main() {
  47.     size_t numberOfUsers = 2;
  48.     std::string passwords[] = { "passw0rd", "12345" };
  49.     std::string users[] = { "admin", "guest" };
  50.     int failedLogins[] = { 0, 0 };
  51.     int userTypes[] = { UserType::Admin, UserType::Normal };
  52.  
  53.     runServer(numberOfUsers, passwords, users, failedLogins, userTypes);
  54. }
  55.  
  56. std::string getInput(std::string prompt) {
  57.     std::string input;
  58.     std::cout << prompt << ": ";
  59.     std::cin >> input;
  60.     return input;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement