Netrc.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifndef _D_NETRC_H_
  36. #define _D_NETRC_H_
  37. #include "common.h"
  38. class Authenticatable {
  39. public:
  40. virtual ~Authenticatable() {}
  41. virtual bool match(const string& hostname) const = 0;
  42. };
  43. typedef SharedHandle<Authenticatable> AuthenticatableHandle;
  44. class Authenticator : public Authenticatable {
  45. private:
  46. string machine;
  47. string login;
  48. string password;
  49. string account;
  50. public:
  51. Authenticator() {}
  52. Authenticator(const string& machine,
  53. const string& login,
  54. const string& password,
  55. const string& account)
  56. :machine(machine),
  57. login(login),
  58. password(password),
  59. account(account) {}
  60. virtual ~Authenticator() {}
  61. virtual bool match(const string& hostname) const
  62. {
  63. return hostname == machine;
  64. }
  65. const string& getMachine() const
  66. {
  67. return machine;
  68. }
  69. void setMachine(const string& machine) { this->machine = machine; }
  70. const string& getLogin() const
  71. {
  72. return login;
  73. }
  74. void setLogin(const string& login) { this->login = login; }
  75. const string& getPassword() const
  76. {
  77. return password;
  78. }
  79. void setPassword(const string& password) { this->password = password; }
  80. const string& getAccount() const
  81. {
  82. return account;
  83. }
  84. void setAccount(const string& account) { this->account = account; }
  85. };
  86. typedef SharedHandle<Authenticator> AuthenticatorHandle;
  87. typedef deque<AuthenticatorHandle> Authenticators;
  88. class DefaultAuthenticator : public Authenticator {
  89. public:
  90. DefaultAuthenticator() {}
  91. DefaultAuthenticator(const string& login,
  92. const string& password,
  93. const string& account)
  94. :Authenticator("", login, password, account) {}
  95. virtual ~DefaultAuthenticator() {}
  96. virtual bool match(const string& hostname) const
  97. {
  98. return true;
  99. }
  100. };
  101. typedef SharedHandle<DefaultAuthenticator> DefaultAuthenticatorHandle;
  102. class Netrc {
  103. private:
  104. Authenticators authenticators;
  105. void storeAuthenticator(const AuthenticatorHandle& authenticator);
  106. string getRequiredNextToken(ifstream& f) const;
  107. void skipMacdef(ifstream& f) const;
  108. public:
  109. Netrc() {}
  110. void parse(const string& path);
  111. AuthenticatorHandle findAuthenticator(const string& hostname) const;
  112. const Authenticators& getAuthenticators() const
  113. {
  114. return authenticators;
  115. }
  116. void addAuthenticator(const AuthenticatorHandle& authenticator)
  117. {
  118. authenticators.push_back(authenticator);
  119. }
  120. };
  121. typedef SharedHandle<Netrc> NetrcHandle;
  122. //typedef SingletonHolder<NetrcHandle> NetrcSingletonHolder;
  123. #endif // _D_NETRC_H_