make the user and nick and password more secure

This commit is contained in:
mochaoui
2024-04-28 18:55:00 +01:00
parent e9c1d60f46
commit beb4db3d02
2 changed files with 34 additions and 36 deletions

View File

@@ -334,16 +334,6 @@ bool Server::dontputthesameusername(const std::string& username) {
return false;
}
int Server::findUserFdforkickregulars(const std::string& username) {
std::map<int, std::string>::iterator it;
for (it = usernamesregulars.begin(); it != usernamesregulars.end(); ++it) {
if (it->second == username) {
return it->first;
}
}
return -1;
}
// brodcasting msg to all nicks in the channel
void Server::broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg) {
@@ -517,15 +507,6 @@ void Server::smallbroadcastMOOD(std::string nicknamesender, const std::string& c
}
}
void Server::kickUser(int fd) {
close(fd);
std::map<int, std::string>::iterator it = usernamesregulars.find(fd);
if (it != usernamesregulars.end()) {
usernamesregulars.erase(it);
}
}
int stringToInt(const std::string& str) {
std::stringstream ss(str);
int result;
@@ -564,12 +545,26 @@ void Server::handleClientData(int fd)
if ((startsWith(command, "pass ") || startsWith(command, "PASS ")) && auth == 0)
{
std::string cmd, password;
std::istringstream iss(command);
iss >> cmd >> password;
password = trim(password); // Remove leading/trailing whitespace
std::string passwordLine = command.substr(command.find(" ") + 1);
passwordLine = trim(passwordLine);
// Check if the password starts and ends with a quote
if (!passwordLine.empty() && ((passwordLine[0] == '"' && passwordLine.size() > 1 && passwordLine[passwordLine.size() - 1] == '"') ||
(passwordLine[0] == '\'' && passwordLine.size() > 1 && passwordLine[passwordLine.size() - 1] == '\'')))
{
// Remove the quotes
passwordLine = passwordLine.substr(1, passwordLine.size() - 2);
}
// Validate the password
if (passwordLine.empty())
{
std::string errorMessage = "Error: Password cannot be empty\n";
send(fd, errorMessage.c_str(), errorMessage.length(), 0);
return;
}
std::string passwordoftheserver = getPassowrd();
if (passwordoftheserver != password)
if (passwordoftheserver != passwordLine)
{
std::string errorMessage = "Error: Incorrect Password\n";
send(fd, errorMessage.c_str(), errorMessage.length(), 0);
@@ -623,13 +618,18 @@ void Server::handleClientData(int fd)
std::istringstream iss(command);
iss >> cmd >> nick;
nick = trim(nick);
if (iss.fail())
// Check if there are more tokens after the nickname
std::string remaining;
if (iss >> remaining)
{
std::string errorMessage = "Error: Command requires 1 parameters\n";
std::string errorMessage = "Error: Command requires only 1 parameter\n";
send(fd, errorMessage.c_str(), errorMessage.length(), 0);
return;
}
if (dontputthesamenick(nick) == true)
// Validate the nickname
if (dontputthesamenick(nick))
{
std::string confirmation = "Please Use a Different Nickname : \n";
send(fd, confirmation.c_str(), confirmation.length(), 0);
@@ -642,7 +642,7 @@ void Server::handleClientData(int fd)
if (_clients[i].getFd() == fd)
{
_clients[i].setNick(nick);
std::cout << "Password set for client " << fd << ": " << nick << std::endl;
std::cout << "Nickname set for client " << fd << ": " << nick << std::endl;
break;
}
}
@@ -663,7 +663,8 @@ void Server::handleClientData(int fd)
dontworry1 = trim(dontworry1);
realname = trim(realname);
if (iss.fail())
std::string reme;
if (iss.fail() || iss >> reme)
{
std::string errorMessage = "Error: Command requires at least four parameters\n";
send(fd, errorMessage.c_str(), errorMessage.length(), 0);

View File

@@ -39,10 +39,7 @@ class Server {
std::vector<Client> _clients;
// THAT'S THA DATA OF TOOOP GGG START FROM THERE .
std::map<int, std::string> nicknames; // Replace unordered_map with map
std::map<int, std::string> usernamesoperators; // Replace unordered_map with map
std::map<int, std::string> usernames; // Replace unordered_map with map
std::map<int, std::string> usernamesregulars;
// std::map<std::string, std::vector<std::string> > channels; //here a chanel name and list of client in every chanel
std::map<std::string, Channel> channels;