void SMTPClientSession::loginUsingLogin(const std::string& username, const std::string& password) { std::string response; int status = sendCommand("AUTH LOGIN", response); if (!isPositiveIntermediate(status)) throw SMTPException("Cannot authenticate using LOGIN", response, status); std::ostringstream usernameBase64; Base64Encoder usernameEncoder(usernameBase64); usernameEncoder.rdbuf()->setLineLength(0); usernameEncoder << username; usernameEncoder.close(); std::ostringstream passwordBase64; Base64Encoder passwordEncoder(passwordBase64); passwordEncoder.rdbuf()->setLineLength(0); passwordEncoder << password; passwordEncoder.close(); //Server request for username/password not defined could be either //S: login: //C: user_login //S: password: //C: user_password //or //S: password: //C: user_password //S: login: //C: user_login std::string decodedResponse; std::istringstream responseStream(response.substr(4)); Base64Decoder responseDecoder(responseStream); StreamCopier::copyToString(responseDecoder, decodedResponse); if (Poco::icompare(decodedResponse, 0, 8, "username") == 0) // username first (md5("Username:"******"Login using LOGIN username failed", response, status); status = sendCommand(passwordBase64.str(), response); if (!isPositiveCompletion(status)) throw SMTPException("Login using LOGIN password failed", response, status); } else if (Poco::icompare(decodedResponse, 0, 8, "password") == 0) // password first (md5("Password:"******"Login using LOGIN password failed", response, status); status = sendCommand(usernameBase64.str(), response); if (!isPositiveCompletion(status)) throw SMTPException("Login using LOGIN username failed", response, status); } }
void SMTPClientSession::loginUsingCRAM(const std::string& username, const std::string& method, Poco::DigestEngine& hmac) { std::string response; int status = sendCommand(std::string("AUTH ") + method, response); if (!isPositiveIntermediate(status)) throw SMTPException(std::string("Cannot authenticate using ") + method, response, status); std::string challengeBase64 = response.substr(4); std::istringstream istr(challengeBase64); Base64Decoder decoder(istr); std::string challenge; StreamCopier::copyToString(decoder, challenge); hmac.update(challenge); const DigestEngine::Digest& digest = hmac.digest(); std::string digestString(DigestEngine::digestToHex(digest)); std::string challengeResponse = username + " " + digestString; std::ostringstream challengeResponseBase64; Base64Encoder encoder(challengeResponseBase64); encoder.rdbuf()->setLineLength(0); encoder << challengeResponse; encoder.close(); status = sendCommand(challengeResponseBase64.str(), response); if (!isPositiveCompletion(status)) throw SMTPException(std::string("Login using ") + method + " failed", response, status); }
void FTPClientSession::login(const std::string& username, const std::string& password) { if (_isLoggedIn) logout(); int status = FTP_POSITIVE_COMPLETION * 100; std::string response; if (!_pControlSocket) { _pControlSocket = new DialogSocket(SocketAddress(_host, _port)); _pControlSocket->setReceiveTimeout(_timeout); } if (!_serverReady) { status = _pControlSocket->receiveStatusMessage(response); if (!isPositiveCompletion(status)) throw FTPException("Cannot login to server", response, status); _serverReady = true; } status = sendCommand("USER", username, response); if (isPositiveIntermediate(status)) status = sendCommand("PASS", password, response); if (!isPositiveCompletion(status)) throw FTPException("Login denied", response, status); setFileType(_fileType); _isLoggedIn = true; }
void FTPClientSession::rename(const std::string& oldName, const std::string& newName) { std::string response; int status = sendCommand("RNFR", oldName, response); if (!isPositiveIntermediate(status)) throw FTPException(std::string("Cannot rename ") + oldName, response, status); status = sendCommand("RNTO", newName, response); if (!isPositiveCompletion(status)) throw FTPException(std::string("Cannot rename to ") + newName, response, status); }
void FTPClientSession::login(const std::string& username, const std::string& password) { std::string response; int status = _controlSocket.receiveStatusMessage(response); if (!isPositiveCompletion(status)) throw FTPException("Cannot login to server", response, status); status = sendCommand("USER", username, response); if (isPositiveIntermediate(status)) status = sendCommand("PASS", password, response); if (!isPositiveCompletion(status)) throw FTPException("Login denied", response, status); setFileType(_fileType); }
void SMTPClientSession::sendCommands(const MailMessage& message, const Recipients* pRecipients) { std::string response; int status = 0; const std::string& fromField = message.getSender(); std::string::size_type emailPos = fromField.find('<'); if (emailPos == std::string::npos) { std::string sender("<"); sender.append(fromField); sender.append(">"); status = sendCommand("MAIL FROM:", sender, response); } else { status = sendCommand("MAIL FROM:", fromField.substr(emailPos, fromField.size() - emailPos), response); } if (!isPositiveCompletion(status)) throw SMTPException("Cannot send message", response, status); std::ostringstream recipient; if (pRecipients) { for (Recipients::const_iterator it = pRecipients->begin(); it != pRecipients->end(); ++it) { recipient << '<' << *it << '>'; int status = sendCommand("RCPT TO:", recipient.str(), response); if (!isPositiveCompletion(status)) throw SMTPException(std::string("Recipient rejected: ") + recipient.str(), response, status); recipient.str(""); } } else { for (MailMessage::Recipients::const_iterator it = message.recipients().begin(); it != message.recipients().end(); ++it) { recipient << '<' << it->getAddress() << '>'; int status = sendCommand("RCPT TO:", recipient.str(), response); if (!isPositiveCompletion(status)) throw SMTPException(std::string("Recipient rejected: ") + recipient.str(), response, status); recipient.str(""); } } status = sendCommand("DATA", response); if (!isPositiveIntermediate(status)) throw SMTPException("Cannot send message data", response, status); }
void SMTPClientSession::sendData() { std::string response; int status = sendCommand("DATA", response); if (!isPositiveIntermediate(status)) throw SMTPException("Cannot send message data", response, status); }