Netrc.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <fstream>
  37. #include <algorithm>
  38. #include "DlAbortEx.h"
  39. #include "fmt.h"
  40. #include "A2STR.h"
  41. #include "util.h"
  42. namespace aria2 {
  43. Authenticator::Authenticator() {}
  44. Authenticator::Authenticator
  45. (const std::string& machine,
  46. const std::string& login,
  47. const std::string& password,
  48. const std::string& account)
  49. : machine_(machine),
  50. login_(login),
  51. password_(password),
  52. account_(account)
  53. {}
  54. Authenticator::~Authenticator() {}
  55. bool Authenticator::match(const std::string& hostname) const
  56. {
  57. if(util::isNumericHost(hostname)) {
  58. return hostname == machine_;
  59. } else {
  60. if(util::startsWith(machine_, A2STR::DOT_C)) {
  61. return util::endsWith(A2STR::DOT_C+hostname, machine_);
  62. } else {
  63. return hostname == machine_;
  64. }
  65. }
  66. }
  67. void Authenticator::setMachine(const std::string& machine)
  68. {
  69. machine_ = machine;
  70. }
  71. void Authenticator::setLogin(const std::string& login)
  72. {
  73. login_ = login;
  74. }
  75. void Authenticator::setPassword(const std::string& password)
  76. {
  77. password_ = password;
  78. }
  79. void Authenticator::setAccount(const std::string& account)
  80. {
  81. account_ = account;
  82. }
  83. DefaultAuthenticator::DefaultAuthenticator() {}
  84. DefaultAuthenticator::DefaultAuthenticator
  85. (const std::string& login,
  86. const std::string& password,
  87. const std::string& account)
  88. : Authenticator(A2STR::NIL, login, password, account)
  89. {}
  90. DefaultAuthenticator::~DefaultAuthenticator() {}
  91. bool DefaultAuthenticator::match(const std::string& hostname) const
  92. {
  93. return true;
  94. }
  95. Netrc::Netrc() {}
  96. Netrc::~Netrc() {}
  97. void Netrc::addAuthenticator(const SharedHandle<Authenticator>& authenticator)
  98. {
  99. authenticators_.push_back(authenticator);
  100. }
  101. void Netrc::skipMacdef(std::ifstream& f) const
  102. {
  103. std::string line;
  104. while(getline(f, line)) {
  105. if(line == A2STR::CR_C || line.empty()) {
  106. break;
  107. }
  108. }
  109. }
  110. void Netrc::parse(const std::string& path)
  111. {
  112. authenticators_.clear();
  113. std::ifstream f(path.c_str(), std::ios::binary);
  114. if(!f) {
  115. throw DL_ABORT_EX(fmt("File not found: %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. SharedHandle<Authenticator> authenticator;
  126. std::string line;
  127. STATE state = GET_TOKEN;
  128. while(getline(f, line)) {
  129. if(util::startsWith(line, "#")) {
  130. continue;
  131. }
  132. std::vector<std::string> tokens;
  133. util::split(line, std::back_inserter(tokens), " \t", true);
  134. for(std::vector<std::string>::const_iterator iter = tokens.begin(),
  135. eoi = tokens.end(); iter != eoi; ++iter) {
  136. const std::string& token = *iter;
  137. if(state == GET_TOKEN) {
  138. if(token == "machine") {
  139. storeAuthenticator(authenticator);
  140. authenticator.reset(new Authenticator());
  141. state = SET_MACHINE;
  142. } else if(token == "default") {
  143. storeAuthenticator(authenticator);
  144. authenticator.reset(new DefaultAuthenticator());
  145. } else {
  146. if(!authenticator) {
  147. throw DL_ABORT_EX
  148. (fmt("Netrc:parse error. %s encounterd where 'machine'"
  149. " or 'default' expected.", token.c_str()));
  150. }
  151. if(token == "login") {
  152. state = SET_LOGIN;
  153. } else if(token == "password") {
  154. state = SET_PASSWORD;
  155. } else if(token == "account") {
  156. state = SET_ACCOUNT;
  157. } else if(token == "macdef") {
  158. state = SET_MACDEF;
  159. }
  160. }
  161. } else {
  162. if(state == SET_MACHINE) {
  163. authenticator->setMachine(token);
  164. } else if(state == SET_LOGIN) {
  165. authenticator->setLogin(token);
  166. } else if(state == SET_PASSWORD) {
  167. authenticator->setPassword(token);
  168. } else if(state == SET_ACCOUNT) {
  169. authenticator->setAccount(token);
  170. } else if(state == SET_MACDEF) {
  171. skipMacdef(f);
  172. }
  173. state = GET_TOKEN;
  174. }
  175. }
  176. }
  177. if(state != GET_TOKEN) {
  178. throw DL_ABORT_EX
  179. ("Netrc:parse error. EOF reached where a token expected.");
  180. }
  181. storeAuthenticator(authenticator);
  182. }
  183. void Netrc::storeAuthenticator(const SharedHandle<Authenticator>& authenticator)
  184. {
  185. if(authenticator) {
  186. authenticators_.push_back(authenticator);
  187. }
  188. }
  189. namespace {
  190. class AuthHostMatch {
  191. private:
  192. std::string hostname;
  193. public:
  194. AuthHostMatch(const std::string& hostname):hostname(hostname) {}
  195. bool operator()(const SharedHandle<Authenticator>& authenticator)
  196. {
  197. return authenticator->match(hostname);
  198. }
  199. };
  200. } // namespace
  201. SharedHandle<Authenticator>
  202. Netrc::findAuthenticator(const std::string& hostname) const
  203. {
  204. SharedHandle<Authenticator> res;
  205. std::vector<SharedHandle<Authenticator> >::const_iterator itr =
  206. std::find_if(authenticators_.begin(), authenticators_.end(),
  207. AuthHostMatch(hostname));
  208. if(itr != authenticators_.end()) {
  209. res = *itr;
  210. }
  211. return res;
  212. }
  213. } // namespace aria2