AuthConfigFactory.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "AuthConfigFactory.h"
  36. #include <algorithm>
  37. #include "Option.h"
  38. #include "AuthConfig.h"
  39. #include "Netrc.h"
  40. #include "DefaultAuthResolver.h"
  41. #include "NetrcAuthResolver.h"
  42. #include "prefs.h"
  43. #include "Request.h"
  44. #include "util.h"
  45. namespace aria2 {
  46. namespace {
  47. const std::string AUTH_DEFAULT_USER("anonymous");
  48. const std::string AUTH_DEFAULT_PASSWD("ARIA2USER@");
  49. } // namespace
  50. AuthConfigFactory::AuthConfigFactory() {}
  51. AuthConfigFactory::~AuthConfigFactory() {}
  52. std::unique_ptr<AuthConfig>
  53. AuthConfigFactory::createAuthConfig
  54. (const std::shared_ptr<Request>& request, const Option* op)
  55. {
  56. if(request->getProtocol() == "http" || request->getProtocol() == "https") {
  57. if(op->getAsBool(PREF_HTTP_AUTH_CHALLENGE)) {
  58. if(!request->getUsername().empty()) {
  59. updateBasicCred(make_unique<BasicCred>(request->getUsername(),
  60. request->getPassword(),
  61. request->getHost(),
  62. request->getPort(),
  63. request->getDir(), true));
  64. return AuthConfig::create(request->getUsername(),
  65. request->getPassword());
  66. }
  67. auto i = findBasicCred(request->getHost(), request->getPort(),
  68. request->getDir());
  69. if(i == std::end(basicCreds_)) {
  70. return nullptr;
  71. } else {
  72. return AuthConfig::create((*i)->user_, (*i)->password_);
  73. }
  74. } else {
  75. if(!request->getUsername().empty()) {
  76. return AuthConfig::create(request->getUsername(),
  77. request->getPassword());
  78. } else {
  79. return
  80. createHttpAuthResolver(op)->resolveAuthConfig(request->getHost());
  81. }
  82. }
  83. } else if(request->getProtocol() == "ftp" ||
  84. request->getProtocol() == "sftp") {
  85. if(!request->getUsername().empty()) {
  86. if(request->hasPassword()) {
  87. return AuthConfig::create(request->getUsername(),
  88. request->getPassword());
  89. } else {
  90. if(!op->getAsBool(PREF_NO_NETRC)) {
  91. // First, check we have password corresponding to host and
  92. // username
  93. NetrcAuthResolver authResolver;
  94. authResolver.setNetrc(netrc_.get());
  95. auto ac = authResolver.resolveAuthConfig(request->getHost());
  96. if(ac && ac->getUser() == request->getUsername()) {
  97. return ac;
  98. }
  99. }
  100. // We don't have password for host and username. Return
  101. // password specified by --ftp-passwd
  102. return AuthConfig::create(request->getUsername(),
  103. op->get(PREF_FTP_PASSWD));
  104. }
  105. } else {
  106. return
  107. createFtpAuthResolver(op)->resolveAuthConfig(request->getHost());
  108. }
  109. } else {
  110. return nullptr;
  111. }
  112. }
  113. std::unique_ptr<AuthResolver> AuthConfigFactory::createHttpAuthResolver
  114. (const Option* op) const
  115. {
  116. std::unique_ptr<AbstractAuthResolver> resolver;
  117. if(op->getAsBool(PREF_NO_NETRC)) {
  118. resolver = make_unique<DefaultAuthResolver>();
  119. } else {
  120. auto authResolver = make_unique<NetrcAuthResolver>();
  121. authResolver->setNetrc(netrc_.get());
  122. authResolver->ignoreDefault();
  123. resolver = std::move(authResolver);
  124. }
  125. resolver->setUserDefinedCred(op->get(PREF_HTTP_USER),
  126. op->get(PREF_HTTP_PASSWD));
  127. return std::move(resolver);
  128. }
  129. std::unique_ptr<AuthResolver> AuthConfigFactory::createFtpAuthResolver
  130. (const Option* op) const
  131. {
  132. std::unique_ptr<AbstractAuthResolver> resolver;
  133. if(op->getAsBool(PREF_NO_NETRC)) {
  134. resolver = make_unique<DefaultAuthResolver>();
  135. } else {
  136. auto authResolver = make_unique<NetrcAuthResolver>();
  137. authResolver->setNetrc(netrc_.get());
  138. resolver = std::move(authResolver);
  139. }
  140. resolver->setUserDefinedCred(op->get(PREF_FTP_USER),
  141. op->get(PREF_FTP_PASSWD));
  142. resolver->setDefaultCred(AUTH_DEFAULT_USER, AUTH_DEFAULT_PASSWD);
  143. return std::move(resolver);
  144. }
  145. void AuthConfigFactory::setNetrc(std::unique_ptr<Netrc> netrc)
  146. {
  147. netrc_ = std::move(netrc);
  148. }
  149. void AuthConfigFactory::updateBasicCred(std::unique_ptr<BasicCred> basicCred)
  150. {
  151. auto i = basicCreds_.lower_bound(basicCred);
  152. if(i != std::end(basicCreds_) && *i == basicCred) {
  153. *(*i) = std::move(*basicCred);
  154. } else {
  155. basicCreds_.insert(i, std::move(basicCred));
  156. }
  157. }
  158. bool AuthConfigFactory::activateBasicCred
  159. (const std::string& host,
  160. uint16_t port,
  161. const std::string& path,
  162. const Option* op)
  163. {
  164. auto i = findBasicCred(host, port, path);
  165. if(i == std::end(basicCreds_)) {
  166. auto authConfig = createHttpAuthResolver(op)->resolveAuthConfig(host);
  167. if(!authConfig) {
  168. return false;
  169. } else {
  170. basicCreds_.insert(make_unique<BasicCred>(authConfig->getUser(),
  171. authConfig->getPassword(),
  172. host, port, path, true));
  173. return true;
  174. }
  175. } else {
  176. (*i)->activate();
  177. return true;
  178. }
  179. }
  180. BasicCred::BasicCred
  181. (std::string user, std::string password,
  182. std::string host, uint16_t port, std::string path,
  183. bool activated)
  184. : user_(std::move(user)),
  185. password_(std::move(password)),
  186. host_(std::move(host)),
  187. port_(port),
  188. path_(std::move(path)),
  189. activated_(activated)
  190. {
  191. if(path_.empty() || path_[path_.size()-1] != '/') {
  192. path_ += "/";
  193. }
  194. }
  195. void BasicCred::activate()
  196. {
  197. activated_ = true;
  198. }
  199. bool BasicCred::isActivated() const
  200. {
  201. return activated_;
  202. }
  203. bool BasicCred::operator==(const BasicCred& cred) const
  204. {
  205. return host_ == cred.host_ && port_ == cred.port_ && path_ == cred.path_;
  206. }
  207. bool BasicCred::operator<(const BasicCred& cred) const
  208. {
  209. return host_ < cred.host_ ||
  210. (!(cred.host_ < host_) && (port_ < cred.port_ ||
  211. (!(cred.port_ < port_) && path_ > cred.path_)));
  212. }
  213. AuthConfigFactory::BasicCredSet::iterator AuthConfigFactory::findBasicCred
  214. (const std::string& host,
  215. uint16_t port,
  216. const std::string& path)
  217. {
  218. auto bc = make_unique<BasicCred>("", "", host, port, path);
  219. auto i = basicCreds_.lower_bound(bc);
  220. for(; i != std::end(basicCreds_) &&
  221. (*i)->host_ == host &&
  222. (*i)->port_ == port; ++i) {
  223. if(util::startsWith(bc->path_, (*i)->path_)) {
  224. return i;
  225. }
  226. }
  227. return std::end(basicCreds_);
  228. }
  229. } // namespace aria2