Netrc.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "Netrc.h"
  36. #include <cstdio>
  37. #include <cstring>
  38. #include <algorithm>
  39. #include "DlAbortEx.h"
  40. #include "fmt.h"
  41. #include "A2STR.h"
  42. #include "util.h"
  43. #include "BufferedFile.h"
  44. #include "array_fun.h"
  45. namespace aria2 {
  46. Authenticator::Authenticator() {}
  47. Authenticator::Authenticator(std::string machine, std::string login,
  48. std::string password, std::string account)
  49. : machine_(std::move(machine)),
  50. login_(std::move(login)),
  51. password_(std::move(password)),
  52. account_(std::move(account))
  53. {
  54. }
  55. Authenticator::~Authenticator() = default;
  56. bool Authenticator::match(const std::string& hostname) const
  57. {
  58. return util::noProxyDomainMatch(hostname, machine_);
  59. }
  60. void Authenticator::setMachine(std::string machine)
  61. {
  62. machine_ = std::move(machine);
  63. }
  64. void Authenticator::setLogin(std::string login) { login_ = std::move(login); }
  65. void Authenticator::setPassword(std::string password)
  66. {
  67. password_ = std::move(password);
  68. }
  69. void Authenticator::setAccount(std::string account)
  70. {
  71. account_ = std::move(account);
  72. }
  73. DefaultAuthenticator::DefaultAuthenticator() {}
  74. DefaultAuthenticator::DefaultAuthenticator(std::string login,
  75. std::string password,
  76. std::string account)
  77. : Authenticator("", std::move(login), std::move(password),
  78. std::move(account))
  79. {
  80. }
  81. DefaultAuthenticator::~DefaultAuthenticator() = default;
  82. bool DefaultAuthenticator::match(const std::string& hostname) const
  83. {
  84. return true;
  85. }
  86. Netrc::Netrc() {}
  87. Netrc::~Netrc() = default;
  88. void Netrc::addAuthenticator(std::unique_ptr<Authenticator> authenticator)
  89. {
  90. authenticators_.push_back(std::move(authenticator));
  91. }
  92. namespace {
  93. void skipMacdef(BufferedFile& fp)
  94. {
  95. std::string s;
  96. while (1) {
  97. s = fp.getLine();
  98. if (s.empty() || fp.eof()) {
  99. break;
  100. }
  101. if (!fp) {
  102. throw DL_ABORT_EX("Netrc:I/O error.");
  103. }
  104. if (s[0] == '\n' || s[0] == '\r') {
  105. break;
  106. }
  107. }
  108. }
  109. } // namespace
  110. void Netrc::parse(const std::string& path)
  111. {
  112. authenticators_.clear();
  113. BufferedFile fp(path.c_str(), BufferedFile::READ);
  114. if (!fp) {
  115. throw DL_ABORT_EX(fmt("Cannot open file: %s", path.c_str()));
  116. }
  117. enum STATE {
  118. GET_TOKEN,
  119. SET_MACHINE,
  120. SET_LOGIN,
  121. SET_PASSWORD,
  122. SET_ACCOUNT,
  123. SET_MACDEF
  124. };
  125. std::unique_ptr<Authenticator> authenticator;
  126. STATE state = GET_TOKEN;
  127. while (1) {
  128. std::string line = fp.getLine();
  129. if (line.empty()) {
  130. if (fp.eof()) {
  131. break;
  132. }
  133. else if (!fp) {
  134. throw DL_ABORT_EX("Netrc:I/O error.");
  135. }
  136. else {
  137. continue;
  138. }
  139. }
  140. if (line[0] == '#') {
  141. continue;
  142. }
  143. std::vector<Scip> tokens;
  144. util::splitIterM(line.begin(), line.end(), std::back_inserter(tokens),
  145. " \t", true);
  146. for (std::vector<Scip>::const_iterator iter = tokens.begin(),
  147. eoi = tokens.end();
  148. iter != eoi; ++iter) {
  149. if (state == GET_TOKEN) {
  150. if (util::streq((*iter).first, (*iter).second, "machine")) {
  151. storeAuthenticator(std::move(authenticator));
  152. authenticator = make_unique<Authenticator>();
  153. state = SET_MACHINE;
  154. }
  155. else if (util::streq((*iter).first, (*iter).second, "default")) {
  156. storeAuthenticator(std::move(authenticator));
  157. authenticator = make_unique<DefaultAuthenticator>();
  158. }
  159. else {
  160. if (!authenticator) {
  161. throw DL_ABORT_EX(
  162. fmt("Netrc:parse error. %s encounterd where 'machine'"
  163. " or 'default' expected.",
  164. std::string((*iter).first, (*iter).second).c_str()));
  165. }
  166. if (util::streq((*iter).first, (*iter).second, "login")) {
  167. state = SET_LOGIN;
  168. }
  169. else if (util::streq((*iter).first, (*iter).second, "password")) {
  170. state = SET_PASSWORD;
  171. }
  172. else if (util::streq((*iter).first, (*iter).second, "account")) {
  173. state = SET_ACCOUNT;
  174. }
  175. else if (util::streq((*iter).first, (*iter).second, "macdef")) {
  176. state = SET_MACDEF;
  177. }
  178. }
  179. }
  180. else {
  181. if (state == SET_MACHINE) {
  182. authenticator->setMachine({(*iter).first, (*iter).second});
  183. }
  184. else if (state == SET_LOGIN) {
  185. authenticator->setLogin({(*iter).first, (*iter).second});
  186. }
  187. else if (state == SET_PASSWORD) {
  188. authenticator->setPassword({(*iter).first, (*iter).second});
  189. }
  190. else if (state == SET_ACCOUNT) {
  191. authenticator->setAccount({(*iter).first, (*iter).second});
  192. }
  193. else if (state == SET_MACDEF) {
  194. skipMacdef(fp);
  195. }
  196. state = GET_TOKEN;
  197. }
  198. }
  199. }
  200. if (state != GET_TOKEN) {
  201. throw DL_ABORT_EX("Netrc:parse error. EOF reached where a token expected.");
  202. }
  203. storeAuthenticator(std::move(authenticator));
  204. }
  205. void Netrc::storeAuthenticator(std::unique_ptr<Authenticator> authenticator)
  206. {
  207. if (authenticator) {
  208. authenticators_.push_back(std::move(authenticator));
  209. }
  210. }
  211. namespace {
  212. class AuthHostMatch {
  213. private:
  214. std::string hostname;
  215. public:
  216. AuthHostMatch(std::string hostname) : hostname(std::move(hostname)) {}
  217. bool operator()(const std::unique_ptr<Authenticator>& authenticator)
  218. {
  219. return authenticator->match(hostname);
  220. }
  221. };
  222. } // namespace
  223. const Authenticator* Netrc::findAuthenticator(const std::string& hostname) const
  224. {
  225. std::unique_ptr<Authenticator> res;
  226. auto itr = std::find_if(std::begin(authenticators_),
  227. std::end(authenticators_), AuthHostMatch(hostname));
  228. if (itr == std::end(authenticators_)) {
  229. return nullptr;
  230. }
  231. else {
  232. return (*itr).get();
  233. }
  234. }
  235. const std::vector<std::unique_ptr<Authenticator>>&
  236. Netrc::getAuthenticators() const
  237. {
  238. return authenticators_;
  239. }
  240. } // namespace aria2