rmtpass.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #include "jliball.hpp"
  15. #include "platform.h"
  16. #include "jlib.hpp"
  17. #include "jsem.hpp"
  18. #include "jthread.hpp"
  19. #include "portlist.h"
  20. #include "jsocket.hpp"
  21. #include "rmtfile.hpp"
  22. #include "rmtpass.hpp"
  23. static CBuildVersion _bv("$HeadURL: https://svn.br.seisint.com/ecl/trunk/common/remote/rmtpass.cpp $ $Id: rmtpass.cpp 62376 2011-02-04 21:59:58Z sort $");
  24. void CachedPasswordProvider::addPasswordForFilename(const char * filename)
  25. {
  26. RemoteFilename remote;
  27. remote.setRemotePath(filename);
  28. addPasswordForIp(remote.queryIP());
  29. }
  30. void CachedPasswordProvider::addPasswordForFilename(RemoteFilename & filename)
  31. {
  32. addPasswordForIp(filename.queryIP());
  33. }
  34. void CachedPasswordProvider::addPasswordForIp(const IpAddress & ip)
  35. {
  36. if (!hasPassword(ip))
  37. {
  38. IPasswordProvider * provider = queryPasswordProvider();
  39. StringBuffer username, password;
  40. if (provider && provider->getPassword(ip, username, password))
  41. {
  42. CachedPassword & cur = * new CachedPassword;
  43. cur.ip.ipset(ip);
  44. cur.password.set(password.str());
  45. cur.username.set(username.str());
  46. passwords.append(cur);
  47. }
  48. }
  49. }
  50. void CachedPasswordProvider::clear()
  51. {
  52. passwords.kill();
  53. }
  54. void CachedPasswordProvider::deserialize(MemoryBuffer & in)
  55. {
  56. unsigned num;
  57. in.read(num);
  58. while (num--)
  59. {
  60. CachedPassword & cur = * new CachedPassword;
  61. cur.deserialize(in);
  62. passwords.append(cur);
  63. }
  64. }
  65. void CachedPasswordProvider::serialize(MemoryBuffer & out)
  66. {
  67. unsigned num = passwords.ordinality();
  68. out.append(num);
  69. ForEachItemIn(idx, passwords)
  70. passwords.item(idx).serialize(out);
  71. }
  72. bool CachedPasswordProvider::getPassword(const IpAddress & ip, StringBuffer & username, StringBuffer & password)
  73. {
  74. ForEachItemIn(idx, passwords)
  75. {
  76. CachedPassword & cur = passwords.item(idx);
  77. if (cur.ip.ipequals(ip))
  78. {
  79. username.append(cur.username);
  80. password.append(cur.password);
  81. return true;
  82. }
  83. }
  84. StringBuffer iptext;
  85. LOG(MCdebugInfo, unknownJob, "cached:getPassword(%s) failed", ip.getIpText(iptext).str());
  86. return false;
  87. }
  88. bool CachedPasswordProvider::hasPassword(const IpAddress & ip) const
  89. {
  90. ForEachItemIn(idx, passwords)
  91. {
  92. CachedPassword & cur = passwords.item(idx);
  93. if (cur.ip.ipequals(ip))
  94. return true;
  95. }
  96. return false;
  97. }