runtime/Stunnel.py
changeset 2542 a3ec35ee94e7
parent 2492 7dd551ac2fa0
child 3750 f62625418bff
equal deleted inserted replaced
2541:92f0980888e8 2542:a3ec35ee94e7
     1 from __future__ import absolute_import
     1 from __future__ import absolute_import
       
     2 from __future__ import print_function
     2 import os
     3 import os
     3 from binascii import b2a_hqx
     4 from binascii import b2a_hqx
     4 try:
     5 try:
     5     from runtime.spawn_subprocess import call
     6     from runtime.spawn_subprocess import call
     6 except ImportError:
     7 except ImportError:
     9 restart_stunnel_cmdline = ["/etc/init.d/S50stunnel", "restart"]
    10 restart_stunnel_cmdline = ["/etc/init.d/S50stunnel", "restart"]
    10 
    11 
    11 _PSKpath = None
    12 _PSKpath = None
    12 
    13 
    13 
    14 
       
    15 def restartStunnel():
       
    16     """
       
    17     Restart stunnel service using SysV init stript
       
    18     to apply new generated credentials
       
    19     """
       
    20     try:
       
    21         call(restart_stunnel_cmdline)
       
    22     except OSError:
       
    23         print(_("Couldn't restart stunnel service"))
       
    24 
       
    25 
    14 def PSKgen(ID, PSKpath):
    26 def PSKgen(ID, PSKpath):
    15 
    27 
    16     # b2a_hqx output len is 4/3 input len
    28     # b2a_hqx output len is 4/3 input len
    17     secret = os.urandom(192)  # int(256/1.3333)
    29     secret = os.urandom(192)  # int(256/1.3333)
    18     secretstring = b2a_hqx(secret)
    30     secretstring = b2a_hqx(secret)
    19 
    31 
    20     PSKstring = ID+":"+secretstring
    32     PSKstring = ID+":"+secretstring
    21     with open(PSKpath, 'w') as f:
    33     with open(PSKpath, 'w') as f:
    22         f.write(PSKstring)
    34         f.write(PSKstring)
    23     call(restart_stunnel_cmdline)
    35     restartStunnel()
    24 
    36 
    25 
    37 
    26 def ensurePSK(ID, PSKpath):
    38 def ensurePSK(ID, PSKpath):
    27     global _PSKpath
    39     global _PSKpath
    28     _PSKpath = PSKpath
    40     _PSKpath = PSKpath