/* */ #include "Netrc.h" #include #include #include #include "DlAbortEx.h" #include "fmt.h" #include "A2STR.h" #include "util.h" #include "BufferedFile.h" #include "array_fun.h" namespace aria2 { Authenticator::Authenticator() {} Authenticator::Authenticator(std::string machine, std::string login, std::string password, std::string account) : machine_(std::move(machine)), login_(std::move(login)), password_(std::move(password)), account_(std::move(account)) { } Authenticator::~Authenticator() = default; bool Authenticator::match(const std::string& hostname) const { return util::noProxyDomainMatch(hostname, machine_); } void Authenticator::setMachine(std::string machine) { machine_ = std::move(machine); } void Authenticator::setLogin(std::string login) { login_ = std::move(login); } void Authenticator::setPassword(std::string password) { password_ = std::move(password); } void Authenticator::setAccount(std::string account) { account_ = std::move(account); } DefaultAuthenticator::DefaultAuthenticator() {} DefaultAuthenticator::DefaultAuthenticator(std::string login, std::string password, std::string account) : Authenticator("", std::move(login), std::move(password), std::move(account)) { } DefaultAuthenticator::~DefaultAuthenticator() = default; bool DefaultAuthenticator::match(const std::string& hostname) const { return true; } Netrc::Netrc() {} Netrc::~Netrc() = default; void Netrc::addAuthenticator(std::unique_ptr authenticator) { authenticators_.push_back(std::move(authenticator)); } namespace { void skipMacdef(BufferedFile& fp) { std::string s; while (1) { s = fp.getLine(); if (s.empty() || fp.eof()) { break; } if (!fp) { throw DL_ABORT_EX("Netrc:I/O error."); } if (s[0] == '\n' || s[0] == '\r') { break; } } } } // namespace void Netrc::parse(const std::string& path) { authenticators_.clear(); BufferedFile fp(path.c_str(), BufferedFile::READ); if (!fp) { throw DL_ABORT_EX(fmt("Cannot open file: %s", path.c_str())); } enum STATE { GET_TOKEN, SET_MACHINE, SET_LOGIN, SET_PASSWORD, SET_ACCOUNT, SET_MACDEF }; std::unique_ptr authenticator; STATE state = GET_TOKEN; while (1) { std::string line = fp.getLine(); if (line.empty()) { if (fp.eof()) { break; } else if (!fp) { throw DL_ABORT_EX("Netrc:I/O error."); } else { continue; } } if (line[0] == '#') { continue; } std::vector tokens; util::splitIterM(line.begin(), line.end(), std::back_inserter(tokens), " \t", true); for (std::vector::const_iterator iter = tokens.begin(), eoi = tokens.end(); iter != eoi; ++iter) { if (state == GET_TOKEN) { if (util::streq((*iter).first, (*iter).second, "machine")) { storeAuthenticator(std::move(authenticator)); authenticator = make_unique(); state = SET_MACHINE; } else if (util::streq((*iter).first, (*iter).second, "default")) { storeAuthenticator(std::move(authenticator)); authenticator = make_unique(); } else { if (!authenticator) { throw DL_ABORT_EX( fmt("Netrc:parse error. %s encounterd where 'machine'" " or 'default' expected.", std::string((*iter).first, (*iter).second).c_str())); } if (util::streq((*iter).first, (*iter).second, "login")) { state = SET_LOGIN; } else if (util::streq((*iter).first, (*iter).second, "password")) { state = SET_PASSWORD; } else if (util::streq((*iter).first, (*iter).second, "account")) { state = SET_ACCOUNT; } else if (util::streq((*iter).first, (*iter).second, "macdef")) { state = SET_MACDEF; } } } else { if (state == SET_MACHINE) { authenticator->setMachine({(*iter).first, (*iter).second}); } else if (state == SET_LOGIN) { authenticator->setLogin({(*iter).first, (*iter).second}); } else if (state == SET_PASSWORD) { authenticator->setPassword({(*iter).first, (*iter).second}); } else if (state == SET_ACCOUNT) { authenticator->setAccount({(*iter).first, (*iter).second}); } else if (state == SET_MACDEF) { skipMacdef(fp); } state = GET_TOKEN; } } } if (state != GET_TOKEN) { throw DL_ABORT_EX("Netrc:parse error. EOF reached where a token expected."); } storeAuthenticator(std::move(authenticator)); } void Netrc::storeAuthenticator(std::unique_ptr authenticator) { if (authenticator) { authenticators_.push_back(std::move(authenticator)); } } namespace { class AuthHostMatch { private: std::string hostname; public: AuthHostMatch(std::string hostname) : hostname(std::move(hostname)) {} bool operator()(const std::unique_ptr& authenticator) { return authenticator->match(hostname); } }; } // namespace const Authenticator* Netrc::findAuthenticator(const std::string& hostname) const { std::unique_ptr res; auto itr = std::find_if(std::begin(authenticators_), std::end(authenticators_), AuthHostMatch(hostname)); if (itr == std::end(authenticators_)) { return nullptr; } else { return (*itr).get(); } } const std::vector>& Netrc::getAuthenticators() const { return authenticators_; } } // namespace aria2