OpenSH for WAPT
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

setup.py 4.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # -*- coding: utf-8 -*-
  2. from setuphelpers import *
  3. import requests,json
  4. uninstallkey = []
  5. def install():
  6. print('Installing OpenSSH')
  7. mkdirs(makepath(programfiles,'OpenSSH'))
  8. print(' Stoping the service')
  9. for service in ['sshd','ssh-agent']:
  10. if service_installed(service) and service_is_running(service):
  11. service_stop(service)
  12. killalltasks('%s.exe' % service)
  13. print(' Extract OpenSSH archive')
  14. run('"%s" e -y -o"%s" OpenSSH-Win%s.zip' % (makepath(programfiles,'7-Zip','7z.exe'),makepath(programfiles,'OpenSSH'),'64' if iswin64() else '32'))
  15. print(' Installing the services')
  16. run(r'sc.exe create sshd binPath= "%s" DisplayName= "OpenSSH Server" start= auto' % makepath(programfiles,'OpenSSH','sshd.exe'), accept_returncodes=[0,1073])
  17. run(r'sc.exe privs sshd SeAssignPrimaryTokenPrivilege/SeTcbPrivilege/SeBackupPrivilege/SeRestorePrivilege/SeImpersonatePrivilege')
  18. run(r'sc.exe create ssh-agent binPath= "%s" DisplayName= "OpenSSH Authentication Agent" start= auto' % makepath(programfiles,'OpenSSH','ssh-agent.exe'), accept_returncodes=[0,1073])
  19. run(r'sc.exe sdset ssh-agent "D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)(A;;RP;;;AU)"')
  20. run(r'sc.exe privs ssh-agent SeImpersonatePrivilege')
  21. run(r'wevtutil.exe um "%s"' % makepath(programfiles,'OpenSSH','openssh-events.man'))
  22. filecopyto('openssh-events.man',makepath(programfiles,'OpenSSH'))
  23. run(r'wevtutil.exe im "%s"' % makepath(programfiles,'OpenSSH','openssh-events.man'))
  24. print(' Opening port 22 in the firewall')
  25. run_notfatal(r'netsh advfirewall firewall del rule name="OpenSSH Server"')
  26. run(r'netsh advfirewall firewall add rule name="OpenSSH Server" dir=in action=allow protocol=TCP localport=22 enable=yes')
  27. print(' Starting the service')
  28. service_start('sshd')
  29. print(r' Enabling sshd service on boot')
  30. run('sc config sshd start= auto')
  31. def uninstall():
  32. print('Removing OpenSSH')
  33. for service in ['sshd','ssh-agent']:
  34. print(' Stoping the services %s' % service)
  35. if service_is_running(service):
  36. service_stop(service)
  37. killalltasks('%s.exe' % service)
  38. print(' Uninstalling service %s' % service)
  39. run(r'sc.exe delete %s' % service)
  40. print(' Unregister events handler')
  41. run(r'wevtutil um "%s"' % makepath(programfiles,'OpenSSH','openssh-events.man'))
  42. print(' Removing files')
  43. remove_tree(makepath(programfiles,'OpenSSH'))
  44. def update_package():
  45. print('Updating OpenSSH Package')
  46. latest = json.loads(requests.get('https://api.github.com/repos/PowerShell/Win32-OpenSSH/releases/latest').text.encode('utf-8'))
  47. version = latest['tag_name'].split('-',1)[0].lstrip('v').rstrip('p1')
  48. pe = PackageEntry();
  49. control = pe.load_control_from_wapt('.')
  50. if Version(version) > Version(control['version'].split('-',1)[0]):
  51. for arch in ['32','64']:
  52. remove_file('OpenSSH-Win%s.zip' % arch)
  53. for asset in latest['assets']:
  54. if asset['name'] == 'OpenSSH-Win%s.zip' % arch and not isfile('OpenSSH-Win%s.zip' % arch):
  55. url = asset['browser_download_url']
  56. print('Downloading OpenSSH %s for win%s from %s' % (version,arch,url))
  57. wget(url,'OpenSSH-Win%s.zip' % arch)
  58. print('Updating control file with new version %s' % version)
  59. pe.version = version + '-0'
  60. pe.save_control_to_wapt('.')
  61. def audit():
  62. version = control['version'].split('-',1)[0]
  63. installed_version = get_file_properties(makepath(programfiles,'OpenSSH','sshd.exe'))['FileVersion']
  64. if Version(version) != Version(installed_version):
  65. print('Installed version %s does not match version %s' % (version, installed_version))
  66. return "ERROR"
  67. return "OK"
  68. if __name__ == '__main__':
  69. update_package()