tis-cwrsync 3.1.1-5

  • package : tis-cwrsync
  • version : 3.1.1-5
  • description : install of a cwRsync daemon manages by nssm. rsyncd.conf must be customized to suit your needs. meant to be used with tisbackup for example.
  • maintainer : Hubert TOUVET
  • date : 2017-07-20 11:19:41
  • signer : TRANQUIL IT SYSTEMS
  • signature_date : 20170720-111941
  • min_os_version :
  • min_wapt_version :

setup.py

# -*- coding: utf-8 -*-
from setuphelpers import *
from iniparse import RawConfigParser
import string,random


uninstallkey = []

service_name = 'cwRsync'
portnumber = 873

install_path = makepath(programfiles32,service_name)
nssm_path = makepath(install_path,'nssm.exe')
rsync_conf = makepath(install_path,'rsyncd.conf')
rsync_secrets = makepath(install_path,'rsyncd.secrets')
rsync_log = makepath(install_path,'rsyncd.log')

def install():
    print('installing tis-cwrsync')

    if service_installed(service_name) and  not service_is_stopped(service_name):
        print('Stopping rsync service')
        service_stop(service_name)

    print('Copy of cwRsync binaries')
    copytree2('cwRsync',install_path)
    if not isfile(nssm_path):
        print('Copy of nssm binary')
        if iswin64():
            filecopyto(makepath('win64','nssm.exe'),nssm_path)
        else:
            filecopyto(makepath('win32','nssm.exe'),nssm_path)


    if not isfile(rsync_conf):
        print('Adding a rsyncd.conf file')
        filecopyto('rsyncd.conf',rsync_conf)
    else:
        print('Keeping existing rsyncd.conf file')


    if not isfile(rsync_secrets):
        print('Create a secrets file')
        password = ''.join(random.choice(string.ascii_letters + string.digits) for x in range(20))
        open(rsync_secrets,'w').write('root:%s'%password)
        print('root password is %s'% password)
    else:
        print('Keep existing secrets file')

    print('Set secrets readable only by administrators')
    print run('echo O| cacls "%s" /S:"D:PAI(A;;FA;;;SY)(A;;FA;;;BA)"' % rsync_secrets)
    print run('echo Y| cacls "%s" /S:"D:PAI(A;;FA;;;SY)(A;;FA;;;BA)"' % rsync_secrets)

    print('Open firewall')
    check_open_port()

    print('Install service with nssm manager')
    install_service()
    # or using cygrunsrv instead...
    #run(r'"{pf32}\cwRsync\cygrunsrv.exe" --install cwRsync -p "{pf32}\cwRsync\rsync.exe" -a "--config rsyncd.conf --daemon --no-detach" -c "{pf32}\cwRsync"'.format(pf32=programfiles32))

    print('Start rsync service')
    service_start(service_name)


def check_open_port():
    """Configure local firewall to accept incoming request to specified tcp port
    >>> check_open_port(8088)
    """
    import win32serviceutil
    import platform
    import win32service
    win_major_version = int(platform.win32_ver()[1].split('.')[0])
    try:
        if win_major_version<6:
            #check if firewall is running
            if win32serviceutil.QueryServiceStatus( 'SharedAccess', None)[1]==win32service.SERVICE_RUNNING:
                print(u"Firewall started, checking for port openning...")
                #WinXP 2003
                if service_name not in run_notfatal('netsh firewall show portopening'):
                    print(u"Adding a firewall rule to open port %s"%portnumber)
                    print run_notfatal("""netsh.exe firewall add portopening name="%s %s" port=%s protocol=TCP profile=ALL"""%(service_name,portnumber,portnumber))
                else:
                    print(u"port %s already opened, skipping firewall configuration"%(portnumber,))
        else:
            if win32serviceutil.QueryServiceStatus('MpsSvc', None)[1]==win32service.SERVICE_RUNNING:
                print(u"Firewall started, checking for port openning...")
                if service_name not in run_notfatal('netsh advfirewall firewall show rule name="%s %s"'%(service_name,portnumber,)):
                    print(u"No port opened for %s, opening port %s"%(service_name,portnumber))
                    #win Vista and higher
                    print run_notfatal("""netsh advfirewall firewall add rule name="%s %s" dir=in action=allow protocol=TCP localport=%s """%(service_name,portnumber,portnumber))
                else:
                    print(u"port %s already opened, skipping firewall configuration"%(portnumber,))
    except Exception as e:
        print('Unable to check if port %s is opened : %s' % (portnumber,e))


def install_service(nssm_path=nssm_path,exe_path=None,exe_params=None):
    """Setup cwRsync as a windows Service managed by nssm
    >>> install_service()
    """
    datatypes = {
        'dword':REG_DWORD,
        'sz':REG_SZ,
        'expand_sz':REG_EXPAND_SZ,
        'multi_sz':REG_MULTI_SZ,
    }

    if service_installed(service_name):
        if not service_is_stopped(service_name):
            print(u'Stop running "%s"' % service_name)
            run('net stop "%s"' % service_name)
            while not service_is_stopped(service_name):
                print(u'Waiting for "%s" to terminate' % service_name)
                time.sleep(2)
        print(u'Unregister existing "%s"' % service_name)
        run('sc delete "%s"' % service_name)

    if not exe_path:
        exe_path = makepath(install_path,'rsync.exe')

    if not exe_params:
        exe_params = '--daemon --no-detach --config rsyncd.conf'

    print(u'Register new %s with nssm' % service_name)
    run('"{nssm_path}" install {service_name} "{exe_path}" "{exe_params}"'.format(
        service_name = service_name,
        exe_path = exe_path,
        nssm_path = nssm_path,
        exe_params = exe_params,
     ))

    # fix some parameters (quotes for path with spaces...
    params = {
        "Description": "sz:%s" % service_name,
        "DisplayName" : "sz:%s" % service_name,
        "AppStdout" : r"expand_sz:{}".format(rsync_log),
        "Parameters\\AppStderr" : r"expand_sz:{}".format(rsync_log),
        "Parameters\\AppParameters" : r'expand_sz:{}'.format(exe_params),
        }

    print('Set service parameters...')
    root = HKEY_LOCAL_MACHINE
    base = r'SYSTEM\CurrentControlSet\services\%s' % service_name
    for key in params:
        if isinstance(params[key],int):
            (valuetype,value) = ('dword',params[key])
        elif ':' in params[key]:
            (valuetype,value) = params[key].split(':',1)
            if valuetype == 'dword':
                value = int(value)
        else:
            (valuetype,value) = ('sz',params[key])
        fullpath = base+'\\'+key
        (path,keyname) = fullpath.rsplit('\\',1)
        if keyname == '@' or keyname =='':
            keyname = None
        registry_set(root,path,keyname,value,type = datatypes[valuetype])
        print(r'HKLM\%s : %s = %s' % (path,keyname,value))

def uninstall():
    if service_installed(service_name):
        if not service_is_stopped(service_name):
            print(u'Stop running "%s"' % service_name)
            run('net stop "%s"' % service_name)
            while not service_is_stopped(service_name):
                print(u'Waiting for "%s" to terminate' % service_name)
                time.sleep(2)
        print(u'Unregister service "%s"' % service_name)
        run('sc delete "%s"' % service_name)
    print('Remove service files')
    remove_tree(install_path)

    print('Remove firewall exception')
    # winxp
    run_notfatal("""netsh.exe firewall add portopening name="%s %s" port=%s protocol=TCP profile=ALL"""%(service_name,portnumber,portnumber))
    # win 7
    run_notfatal("""netsh advfirewall firewall add rule name="%s %s" dir=in action=allow protocol=TCP localport=%s"""%(service_name,portnumber,portnumber,))

    

manifest

[["WAPT/certificate.crt", "0db563dc9077b268ca07ba834322d0fc5e21f8e5"], ["win32/nssm.exe", "399696b365dd9d0e0586512e36e0e65f12bd4f79"], ["rsyncd.conf", "5f9ae74d20d3253872e06d67522559b9600ab31d"], ["cwRsync/README.rsync.txt", "57cf8b83b614ace5be9a56e9ad94dbda19a8d227"], ["cwRsync/ssh.exe", "49627490977906cd8e54234535495db73a423de5"], ["cwRsync/rsyncd.conf.html", "230a74eb33f776f6821fe700d17bb52e36e44aa4"], ["cwRsync/rsync.log", "c88aef6b24de1f452abc48e8056171c6b282157e"], ["WAPT/control", "d7e302165f714184504e2bab069c8dff31eb7c4e"], ["cwRsync/cygz.dll", "b0ec48205b57192a3c052b304293dea5a7ba495d"], ["cwRsync/cygssp-0.dll", "7c8f8f9e5fa2482f99106511e7e7389e787cbf15"], ["WAPT/wapt.psproj", "eb115e137a2d5424de6d413322ab5a30895ece92"], ["cwRsync/cygcrypto-1.0.0.dll", "cc1e3bdc3c655dc888c0e4a788dd9c1ceafcd576"], ["cwRsync/cygrunsrv.exe", "bd651ebea1fc63b60ea90f28c766a4a62098ffe6"], ["cwRsync/rsync.html", "da39a3ee5e6b4b0d3255bfef95601890afd80709"], ["win64/nssm.exe", "21938992290c9608c11539e6f2b18c75db33ffaa"], ["cwRsync/cygwin1.dll", "6917b01a69fbb84ad27aabcce7694d6a9862e997"], ["cwRsync/cwrsync.cmd", "8066439e18ef3b8db015cc48eea40acb1ceb4737"], ["cwRsync/cyggcc_s-1.dll", "5535e94a67809dbbe5923eb1cde95d3214ce1b82"], ["setup.py", "1375e49b58e756e04403f3e6c85a90adb5b40ebb"], ["cwRsync/cygiconv-2.dll", "d0b0a93295c3145d94b10362927a0eb7df16ee7e"], ["cwRsync/rsync.exe", "49535e0942e48baf24db8e00df9a3ddebe4e01b2"], ["cwRsync/ssh-keygen.exe", "22999b92d7cfc06b9a4d1ba942d46cf918b1a9e3"], ["cwRsync/README.cwrsync.txt", "4f2f812a4f856d5e2c7b1cd167a78ced9adafcf1"]]