1.1 --- a/passwordreceiver.py Tue Nov 04 18:26:39 2014 +0100
1.2 +++ b/passwordreceiver.py Mon Nov 10 14:45:01 2014 +0100
1.3 @@ -41,14 +41,53 @@
1.4 import os
1.5 import sys
1.6 import base64
1.7 -#import logging
1.8 +import thread
1.9 +import urllib3
1.10 +import netaddr
1.11
1.12 opensecurity_urls = (
1.13 '/password', 'os_password',
1.14 '/init', 'os_init'
1.15 )
1.16
1.17 -#__LOG = logging.getLogger("passwordreceiver")
1.18 +STATUS_CODE_OK = 200
1.19 +
1.20 +
1.21 +def sendDataToRest (urlpath, data):
1.22 + netifaces.ifaddresses("eth0")[2][0]["addr"]
1.23 +
1.24 + # Get first address in network (0 = network ip -> 192.168.0.0)
1.25 + remote_ip = netaddr.IPNetwork("%s/%s" %(netifaces.ifaddresses("eth0")[2][0]["addr"], netifaces.ifaddresses("eth0")[2][0]["netmask"]))[1]
1.26 +
1.27 + url = ("http://%s:8090//%s" %(remote_ip, urlpath))
1.28 +
1.29 + try:
1.30 + response = httpPool.request_encode_body("POST", url, fields=data, retries=0)
1.31 + except Exception, e:
1.32 + return
1.33 +
1.34 + if response.status == STATUS_CODE_OK:
1.35 + return True
1.36 + else:
1.37 + return False
1.38 +
1.39 +
1.40 +def sendNotification (type, message):
1.41 + data = {"msgtype" : type, "text" : message}
1.42 +
1.43 + if (type == "information"):
1.44 + sendDataToRest ("message", data)
1.45 + else:
1.46 + sendDataToRest ("notification", data)
1.47 +
1.48 +def sendInitialisationFailedError():
1.49 + sendNotification("critical", "Initialisation of the stick failed.")
1.50 +
1.51 +
1.52 +
1.53 +
1.54 +
1.55 +
1.56
1.57 class os_password:
1.58
1.59 @@ -108,30 +147,25 @@
1.60 os.remove(keyfilepath)
1.61
1.62 def runPreInitScript(self, preinitscript, device):
1.63 - #__LOG.debug("Start preinit Script")
1.64
1.65 command = [preinitscript, device]
1.66 process = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
1.67 retval = process.wait()
1.68 ( stdout, stderr ) = process.communicate()
1.69
1.70 - #__LOG.debug("preinit done result: %s" %(retval,))
1.71 -
1.72 if (retval != 0):
1.73 raise web.badrequest(stderr)
1.74
1.75 def runPostInitScript(self, postinitscript):
1.76 - #__LOG.debug("Start postinit Script")
1.77 -
1.78 command = [postinitscript]
1.79 process = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
1.80 retval = process.wait()
1.81 ( stdout, stderr ) = process.communicate()
1.82
1.83 - #__LOG.debug("postinit done result: %s" %(retval,))
1.84 -
1.85 if (retval != 0):
1.86 - raise web.badrequest(stderr)
1.87 + return False
1.88 + else:
1.89 + return True
1.90
1.91 def GET(self, settings):
1.92 return self.POST(settings)
1.93 @@ -154,9 +188,12 @@
1.94 command = [settings["script"], settings["device"], settings["mountpoint"], args["password"], settings["keyfilepath"]]
1.95 else:
1.96 command = [settings["script"], settings["device"], settings["mountpoint"], args["password"]]
1.97 -
1.98 - #__LOG.debug("Start init script")
1.99
1.100 + thread.start_new_thread(self.initStick, (command,settings,args,))
1.101 +
1.102 + return "Success: Init started"
1.103 +
1.104 + def initStick(self, command, settings, args):
1.105 process = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
1.106 retval = process.wait()
1.107 ( stdout, stderr ) = process.communicate()
1.108 @@ -164,15 +201,12 @@
1.109 if "keyfile" in args:
1.110 self.deleteKeyfile(settings["keyfilepath"])
1.111
1.112 - #__LOG.debug("init done result: %s" %(retval,))
1.113 -
1.114 if (retval != 0):
1.115 - raise web.badrequest(stderr)
1.116 + sendInitialisationFailedError();
1.117
1.118 # Do the postinit stuff
1.119 - self.runPostInitScript(settings["postinitscript"])
1.120 -
1.121 - return "Success: Stick is initialized and mounted"
1.122 + if (self.runPostInitScript(settings["postinitscript"]) != True):
1.123 + sendInitialisationFailedError();
1.124
1.125 class MyRestListener(web.application):
1.126 def __init__(self, mapping=(), fvars={}, autoreload=None, script=None, device=None, mountpoint=None, tries=None, keyfilepath=None, preinitscript=None, postinitscript=None):