runtime/Stunnel.py
author Edouard Tisserant
Tue, 20 Nov 2018 11:32:42 +0100
changeset 2339 48b4eba13064
parent 2328 7eb6cb70bf5b
child 2492 7dd551ac2fa0
permissions -rw-r--r--
IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
import os
from binascii import b2a_hqx
try:
    from runtime.spawn_subprocess import call
except ImportError:
    from subprocess import call

restart_stunnel_cmdline = ["/etc/init.d/S50stunnel","restart"]

_PSKpath = None

def PSKgen(ID, PSKpath):

    # b2a_hqx output len is 4/3 input len
    secret = os.urandom(192) # int(256/1.3333)
    secretstring = b2a_hqx(secret)

    PSKstring = ID+":"+secretstring
    with open(PSKpath, 'w') as f:
        f.write(PSKstring)
    call(restart_stunnel_cmdline)

def ensurePSK(ID, PSKpath):
    global _PSKpath
    _PSKpath = PSKpath
    # check if already there
    if not os.path.exists(PSKpath):
        # create if needed
        PSKgen(ID, PSKpath)

def getPSKID():
    if _PSKpath is not None :
        if not os.path.exists(_PSKpath):
            confnodesroot.logger.write_error(
                'Error: Pre-Shared-Key Secret in %s is missing!\n' % _PSKpath)
            return None
        ID,_sep,PSK = open(_PSKpath).read().partition(':')
        PSK = PSK.rstrip('\n\r')
        return (ID,PSK)
    return None