rmtpass.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jliball.hpp"
  14. #include "platform.h"
  15. #include "jlib.hpp"
  16. #include "jsem.hpp"
  17. #include "jthread.hpp"
  18. #include "portlist.h"
  19. #include "jsocket.hpp"
  20. #include "rmtpass.hpp"
  21. void CachedPasswordProvider::addPasswordForFilename(const char * filename)
  22. {
  23. RemoteFilename remote;
  24. remote.setRemotePath(filename);
  25. addPasswordForIp(remote.queryIP());
  26. }
  27. void CachedPasswordProvider::addPasswordForFilename(RemoteFilename & filename)
  28. {
  29. addPasswordForIp(filename.queryIP());
  30. }
  31. void CachedPasswordProvider::addPasswordForIp(const IpAddress & ip)
  32. {
  33. if (!hasPassword(ip))
  34. {
  35. IPasswordProvider * provider = queryPasswordProvider();
  36. StringBuffer username, password;
  37. if (provider && provider->getPassword(ip, username, password))
  38. {
  39. CachedPassword & cur = * new CachedPassword;
  40. cur.ip.ipset(ip);
  41. cur.password.set(password.str());
  42. cur.username.set(username.str());
  43. passwords.append(cur);
  44. }
  45. }
  46. }
  47. void CachedPasswordProvider::clear()
  48. {
  49. passwords.kill();
  50. }
  51. void CachedPasswordProvider::deserialize(MemoryBuffer & in)
  52. {
  53. unsigned num;
  54. in.read(num);
  55. while (num--)
  56. {
  57. CachedPassword & cur = * new CachedPassword;
  58. cur.deserialize(in);
  59. passwords.append(cur);
  60. }
  61. }
  62. void CachedPasswordProvider::serialize(MemoryBuffer & out)
  63. {
  64. unsigned num = passwords.ordinality();
  65. out.append(num);
  66. ForEachItemIn(idx, passwords)
  67. passwords.item(idx).serialize(out);
  68. }
  69. bool CachedPasswordProvider::getPassword(const IpAddress & ip, StringBuffer & username, StringBuffer & password)
  70. {
  71. ForEachItemIn(idx, passwords)
  72. {
  73. CachedPassword & cur = passwords.item(idx);
  74. if (cur.ip.ipequals(ip))
  75. {
  76. username.append(cur.username);
  77. password.append(cur.password);
  78. return true;
  79. }
  80. }
  81. StringBuffer iptext;
  82. LOG(MCdebugInfo, unknownJob, "cached:getPassword(%s) failed", ip.getIpText(iptext).str());
  83. return false;
  84. }
  85. bool CachedPasswordProvider::hasPassword(const IpAddress & ip) const
  86. {
  87. ForEachItemIn(idx, passwords)
  88. {
  89. CachedPassword & cur = passwords.item(idx);
  90. if (cur.ip.ipequals(ip))
  91. return true;
  92. }
  93. return false;
  94. }