Netrc.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "StringFormat.h"
  40. #include "A2STR.h"
  41. #include "util.h"
  42. namespace aria2 {
  43. const std::string Netrc::MACHINE("machine");
  44. const std::string Netrc::DEFAULT("default");
  45. const std::string Netrc::LOGIN("login");
  46. const std::string Netrc::PASSWORD("password");
  47. const std::string Netrc::ACCOUNT("account");
  48. const std::string Netrc::MACDEF("macdef");
  49. void Netrc::skipMacdef(std::ifstream& f) const
  50. {
  51. std::string line;
  52. while(getline(f, line)) {
  53. if(line == A2STR::CR_C || line.empty()) {
  54. break;
  55. }
  56. }
  57. }
  58. void Netrc::parse(const std::string& path)
  59. {
  60. _authenticators.clear();
  61. std::ifstream f(path.c_str(), std::ios::binary);
  62. if(!f) {
  63. throw DL_ABORT_EX
  64. (StringFormat("File not found: %s", path.c_str()).str());
  65. }
  66. enum STATE {
  67. GET_TOKEN,
  68. SET_MACHINE,
  69. SET_LOGIN,
  70. SET_PASSWORD,
  71. SET_ACCOUNT,
  72. SET_MACDEF
  73. };
  74. SharedHandle<Authenticator> authenticator;
  75. std::string line;
  76. STATE state = GET_TOKEN;
  77. while(getline(f, line)) {
  78. if(util::startsWith(line, "#")) {
  79. continue;
  80. }
  81. std::vector<std::string> tokens;
  82. util::split(line, std::back_inserter(tokens), " \t", true);
  83. for(std::vector<std::string>::const_iterator iter = tokens.begin(),
  84. eoi = tokens.end(); iter != eoi; ++iter) {
  85. const std::string& token = *iter;
  86. if(state == GET_TOKEN) {
  87. if(token == Netrc::MACHINE) {
  88. storeAuthenticator(authenticator);
  89. authenticator.reset(new Authenticator());
  90. state = SET_MACHINE;
  91. } else if(token == Netrc::DEFAULT) {
  92. storeAuthenticator(authenticator);
  93. authenticator.reset(new DefaultAuthenticator());
  94. } else {
  95. if(authenticator.isNull()) {
  96. throw DL_ABORT_EX
  97. (StringFormat("Netrc:parse error. %s encounterd where 'machine'"
  98. " or 'default' expected.", token.c_str()).str());
  99. }
  100. if(token == Netrc::LOGIN) {
  101. state = SET_LOGIN;
  102. } else if(token == Netrc::PASSWORD) {
  103. state = SET_PASSWORD;
  104. } else if(token == Netrc::ACCOUNT) {
  105. state = SET_ACCOUNT;
  106. } else if(token == Netrc::MACDEF) {
  107. state = SET_MACDEF;
  108. }
  109. }
  110. } else {
  111. if(state == SET_MACHINE) {
  112. authenticator->setMachine(token);
  113. } else if(state == SET_LOGIN) {
  114. authenticator->setLogin(token);
  115. } else if(state == SET_PASSWORD) {
  116. authenticator->setPassword(token);
  117. } else if(state == SET_ACCOUNT) {
  118. authenticator->setAccount(token);
  119. } else if(state == SET_MACDEF) {
  120. skipMacdef(f);
  121. }
  122. state = GET_TOKEN;
  123. }
  124. }
  125. }
  126. if(state != GET_TOKEN) {
  127. throw DL_ABORT_EX
  128. ("Netrc:parse error. EOF reached where a token expected.");
  129. }
  130. storeAuthenticator(authenticator);
  131. }
  132. void Netrc::storeAuthenticator(const SharedHandle<Authenticator>& authenticator)
  133. {
  134. if(!authenticator.isNull()) {
  135. _authenticators.push_back(authenticator);
  136. }
  137. }
  138. class AuthHostMatch {
  139. private:
  140. std::string hostname;
  141. public:
  142. AuthHostMatch(const std::string& hostname):hostname(hostname) {}
  143. bool operator()(const SharedHandle<Authenticator>& authenticator)
  144. {
  145. return authenticator->match(hostname);
  146. }
  147. };
  148. SharedHandle<Authenticator>
  149. Netrc::findAuthenticator(const std::string& hostname) const
  150. {
  151. SharedHandle<Authenticator> res;
  152. std::vector<SharedHandle<Authenticator> >::const_iterator itr =
  153. std::find_if(_authenticators.begin(), _authenticators.end(),
  154. AuthHostMatch(hostname));
  155. if(itr != _authenticators.end()) {
  156. res = *itr;
  157. }
  158. return res;
  159. }
  160. } // namespace aria2