AuthConfigFactory.cc 8.0 KB

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