AuthConfigFactory.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. return createAuthConfig(request->getUsername(), request->getPassword());
  81. } else {
  82. return
  83. createFtpAuthResolver(op)->resolveAuthConfig(request->getHost());
  84. }
  85. } else {
  86. return SharedHandle<AuthConfig>();
  87. }
  88. }
  89. AuthConfigHandle
  90. AuthConfigFactory::createAuthConfig(const std::string& user, const std::string& password) const
  91. {
  92. SharedHandle<AuthConfig> ac;
  93. if(!user.empty()) {
  94. ac.reset(new AuthConfig(user, password));
  95. }
  96. return ac;
  97. }
  98. AuthResolverHandle AuthConfigFactory::createHttpAuthResolver
  99. (const Option* op) const
  100. {
  101. AbstractAuthResolverHandle resolver;
  102. if(op->getAsBool(PREF_NO_NETRC)) {
  103. resolver.reset(new DefaultAuthResolver());
  104. } else {
  105. NetrcAuthResolverHandle authResolver(new NetrcAuthResolver());
  106. authResolver->setNetrc(_netrc);
  107. authResolver->ignoreDefault();
  108. resolver = authResolver;
  109. }
  110. resolver->setUserDefinedAuthConfig
  111. (createAuthConfig(op->get(PREF_HTTP_USER), op->get(PREF_HTTP_PASSWD)));
  112. return resolver;
  113. }
  114. AuthResolverHandle AuthConfigFactory::createFtpAuthResolver
  115. (const Option* op) const
  116. {
  117. AbstractAuthResolverHandle resolver;
  118. if(op->getAsBool(PREF_NO_NETRC)) {
  119. resolver.reset(new DefaultAuthResolver());
  120. } else {
  121. NetrcAuthResolverHandle authResolver(new NetrcAuthResolver());
  122. authResolver->setNetrc(_netrc);
  123. resolver = authResolver;
  124. }
  125. resolver->setUserDefinedAuthConfig
  126. (createAuthConfig(op->get(PREF_FTP_USER), op->get(PREF_FTP_PASSWD)));
  127. SharedHandle<AuthConfig> defaultAuthConfig
  128. (new AuthConfig(AuthConfigFactory::ANONYMOUS,
  129. AuthConfigFactory::ARIA2USER_AT));
  130. resolver->setDefaultAuthConfig(defaultAuthConfig);
  131. return resolver;
  132. }
  133. void AuthConfigFactory::setNetrc(const NetrcHandle& netrc)
  134. {
  135. _netrc = netrc;
  136. }
  137. void AuthConfigFactory::updateBasicCred(const BasicCred& basicCred)
  138. {
  139. std::deque<BasicCred>::iterator i =
  140. std::lower_bound(_basicCreds.begin(), _basicCreds.end(), basicCred);
  141. if(i != _basicCreds.end() && (*i) == basicCred) {
  142. (*i) = basicCred;
  143. } else {
  144. _basicCreds.insert(i, basicCred);
  145. }
  146. }
  147. bool AuthConfigFactory::activateBasicCred
  148. (const std::string& host, const std::string& path, const Option* op)
  149. {
  150. std::deque<BasicCred>::iterator i = findBasicCred(host, path);
  151. if(i == _basicCreds.end()) {
  152. SharedHandle<AuthConfig> authConfig =
  153. createHttpAuthResolver(op)->resolveAuthConfig(host);
  154. if(authConfig.isNull()) {
  155. return false;
  156. } else {
  157. BasicCred bc(authConfig->getUser(), authConfig->getPassword(),
  158. host, path, true);
  159. i = std::lower_bound(_basicCreds.begin(), _basicCreds.end(), bc);
  160. _basicCreds.insert(i, bc);
  161. return true;
  162. }
  163. } else {
  164. (*i).activate();
  165. return true;
  166. }
  167. }
  168. AuthConfigFactory::BasicCred::BasicCred
  169. (const std::string& user, const std::string& password,
  170. const std::string& host, const std::string& path,
  171. bool activated):
  172. _user(user), _password(password),
  173. _host(host), _path(path), _activated(activated)
  174. {
  175. if(!Util::endsWith(_path, "/")) {
  176. _path += "/";
  177. }
  178. }
  179. void AuthConfigFactory::BasicCred::activate()
  180. {
  181. _activated = true;
  182. }
  183. bool AuthConfigFactory::BasicCred::isActivated() const
  184. {
  185. return _activated;
  186. }
  187. bool AuthConfigFactory::BasicCred::operator==(const BasicCred& cred) const
  188. {
  189. return _host == cred._host && _path == cred._path;
  190. }
  191. bool AuthConfigFactory::BasicCred::operator<(const BasicCred& cred) const
  192. {
  193. int c = _host.compare(cred._host);
  194. if(c == 0) {
  195. return _path > cred._path;
  196. } else {
  197. return c < 0;
  198. }
  199. }
  200. std::deque<AuthConfigFactory::BasicCred>::iterator
  201. AuthConfigFactory::findBasicCred(const std::string& host,
  202. const std::string& path)
  203. {
  204. BasicCred bc("", "", host, path);
  205. std::deque<BasicCred>::iterator i =
  206. std::lower_bound(_basicCreds.begin(), _basicCreds.end(), bc);
  207. for(; i != _basicCreds.end() && (*i)._host == host; ++i) {
  208. if(Util::startsWith(bc._path, (*i)._path)) {
  209. return i;
  210. }
  211. }
  212. return _basicCreds.end();
  213. }
  214. } // namespace aria2