Edouard@2321: import os Edouard@2328: from binascii import b2a_hqx Edouard@2339: try: Edouard@2339: from runtime.spawn_subprocess import call Edouard@2339: except ImportError: Edouard@2339: from subprocess import call Edouard@2321: Edouard@2321: restart_stunnel_cmdline = ["/etc/init.d/S50stunnel","restart"] Edouard@2321: Edouard@2324: _PSKpath = None Edouard@2324: Edouard@2324: def PSKgen(ID, PSKpath): Edouard@2323: Edouard@2328: # b2a_hqx output len is 4/3 input len Edouard@2328: secret = os.urandom(192) # int(256/1.3333) Edouard@2328: secretstring = b2a_hqx(secret) Edouard@2323: Edouard@2324: PSKstring = ID+":"+secretstring Edouard@2324: with open(PSKpath, 'w') as f: Edouard@2324: f.write(PSKstring) Edouard@2321: call(restart_stunnel_cmdline) Edouard@2321: Edouard@2324: def ensurePSK(ID, PSKpath): Edouard@2324: global _PSKpath Edouard@2324: _PSKpath = PSKpath Edouard@2321: # check if already there Edouard@2324: if not os.path.exists(PSKpath): Edouard@2321: # create if needed Edouard@2324: PSKgen(ID, PSKpath) Edouard@2321: Edouard@2324: def getPSKID(): Edouard@2324: if _PSKpath is not None : Edouard@2324: if not os.path.exists(_PSKpath): Edouard@2324: confnodesroot.logger.write_error( Edouard@2324: 'Error: Pre-Shared-Key Secret in %s is missing!\n' % _PSKpath) Edouard@2324: return None Edouard@2324: ID,_sep,PSK = open(_PSKpath).read().partition(':') Edouard@2324: PSK = PSK.rstrip('\n\r') Edouard@2324: return (ID,PSK) Edouard@2324: return None Edouard@2324: