AuthConfigFactory.cc 7.9 KB

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