win_openssh.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (C) 2021 Lew Gordon <lew.gordon@genesys.com>
  2. # Copyright (C) 2022 Patrick Spendrin <ps_ml@gmx.de>
  3. #
  4. # This file is part of paramiko.
  5. #
  6. # Paramiko is free software; you can redistribute it and/or modify it under the
  7. # terms of the GNU Lesser General Public License as published by the Free
  8. # Software Foundation; either version 2.1 of the License, or (at your option)
  9. # any later version.
  10. #
  11. # Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY
  12. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  13. # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public License
  17. # along with Paramiko; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. import os.path
  20. import time
  21. PIPE_NAME = r"\\.\pipe\openssh-ssh-agent"
  22. def can_talk_to_agent():
  23. # use os.listdir() instead of os.path.exists(), because os.path.exists()
  24. # uses CreateFileW() API and the pipe cannot be reopen unless the server
  25. # calls DisconnectNamedPipe().
  26. dir_, name = os.path.split(PIPE_NAME)
  27. name = name.lower()
  28. return any(name == n.lower() for n in os.listdir(dir_))
  29. class OpenSSHAgentConnection:
  30. def __init__(self):
  31. while True:
  32. try:
  33. self._pipe = os.open(PIPE_NAME, os.O_RDWR | os.O_BINARY)
  34. except OSError as e:
  35. # retry when errno 22 which means that the server has not
  36. # called DisconnectNamedPipe() yet.
  37. if e.errno != 22:
  38. raise
  39. else:
  40. break
  41. time.sleep(0.1)
  42. def send(self, data):
  43. return os.write(self._pipe, data)
  44. def recv(self, n):
  45. return os.read(self._pipe, n)
  46. def close(self):
  47. return os.close(self._pipe)