src/encryptionprovider.py
changeset 1 ad15a8882cac
parent 0 35acc83f4749
child 5 21d27339c811
     1.1 --- a/src/encryptionprovider.py	Tue Feb 25 08:08:37 2014 +0100
     1.2 +++ b/src/encryptionprovider.py	Tue Apr 08 11:39:52 2014 +0200
     1.3 @@ -10,10 +10,9 @@
     1.4  import sys
     1.5  import ConfigParser
     1.6  import logging
     1.7 +from passwordreceiver import *
     1.8  
     1.9 -
    1.10 -
    1.11 -MINOPTS = { "Main" : ["LogFile", "LogLevel", "MountScript", "UmountScript", "InitScript", "GetDevicesScript"]}
    1.12 +MINOPTS = { "Main" : ["LogFile", "LogLevel", "MountScript", "UmountScript", "InitScript", "GetDevicesScript", "Keyfile"]}
    1.13  
    1.14  #CONFIG_FILE="/etc/enryptionprovider/encryptionprovider.cfg"
    1.15  CONFIG_FILE="/home/spawn/workspace_python/encryptionprovider/config/encryptionprovider.cfg"
    1.16 @@ -68,9 +67,73 @@
    1.17      
    1.18      
    1.19      
    1.20 +def runExternalScripts (command):
    1.21 +    LOG.debug ("Run external Script: %s" %(command,))
    1.22      
    1.23 +    if (os.path.isfile (command[0]) == False):
    1.24 +        LOG.error ("File does not exist: %s" %((command[0]),))
    1.25 +        sys.stderr.write("File does not exist: %s\n" %((command[0]),))
    1.26 +        exit (1)
    1.27      
    1.28 +    process = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
    1.29 +    retcode = process.wait()
    1.30 +    ( stdout, stderr ) = process.communicate()
    1.31      
    1.32 +    return { "retcode" : retcode, "stdout" : stdout, "stderr" : stderr }
    1.33 +    
    1.34 +    
    1.35 +def getDevices (script):
    1.36 +    command = [script];
    1.37 +    result = runExternalScripts (command);
    1.38 +    
    1.39 +    if (result["retcode"] != 0):
    1.40 +        LOG.error ("Retcode: %s" %(result["retcode"],))
    1.41 +        LOG.error ("stdout: %s" %(result["stdout"],))
    1.42 +        LOG.error ("stderr: %s" %(result["stderr"],))
    1.43 +        sys.stderr.write("%s" %(result["stderr"],))
    1.44 +        exit (1)
    1.45 +    
    1.46 +    #print ("%s" %(result["stdout"],))
    1.47 +    # don't use print here, because of the extra newline
    1.48 +    sys.stdout.write ("%s" %(result["stdout"],))
    1.49 +
    1.50 +
    1.51 +def umountDevice (script, device):
    1.52 +    command = [script, device];
    1.53 +    result = runExternalScripts (command);
    1.54 +    
    1.55 +    if (result["retcode"] != 0):
    1.56 +        LOG.error ("Retcode: %s" %(result["retcode"],))
    1.57 +        LOG.error ("stdout: %s" %(result["stdout"],))
    1.58 +        LOG.error ("stderr: %s" %(result["stderr"],))
    1.59 +        sys.stderr.write("%s" %(result["stderr"],))
    1.60 +        exit (1)
    1.61 +        
    1.62 +    #print ("%s" %(result["stdout"],))
    1.63 +    # don't use print here, because of the extra newline
    1.64 +    sys.stdout.write ("%s" %(result["stdout"],))
    1.65 +    
    1.66 +
    1.67 +def mountDevice (script, interface, port, device, mountpoint, keyfilepath):    
    1.68 +    listener = MyRestListener (opensecurity_urls, globals(), script = script, device = device, mountpoint = mountpoint, tries = 3, keyfilepath = keyfilepath)
    1.69 +    thread.start_new_thread(listener.run, (interface, port,))
    1.70 +    
    1.71 +    #command = [script, device, mountpoint, password];
    1.72 +    #result = runExternalScripts (command);
    1.73 +    
    1.74 +    close = False
    1.75 +    while (close == False):
    1.76 +        time.sleep(1)
    1.77 +        if (os.path.ismount(mountpoint) == True):
    1.78 +            close = True
    1.79 +            LOG.info ("Stick \"%s\" was mounted sucessfully to \"%s\"" %(device, mountpoint,))
    1.80 +            sys.exit(0)
    1.81 +            
    1.82 +        if (os.path.exists(device) == False):
    1.83 +            close = True
    1.84 +            LOG.error ("Stick \"%s\" removed -> exit" %(device,))
    1.85 +            sys.exit(1)
    1.86 +
    1.87  
    1.88  if __name__ == "__main__":
    1.89      
    1.90 @@ -79,22 +142,21 @@
    1.91      group.add_argument('-m', '--mount', action='store', nargs=4, dest='mount', help='Mounts an encrypted device.', metavar=("interface", "port", "device", "mountpoint"))
    1.92      group.add_argument('-u', '--umount', action='store', nargs=1, dest='umount', help='Unmounts an encrypted device', metavar="device")
    1.93      group.add_argument('-i', '--initialize', action='store', nargs=4, dest='initialize', help='Initialize an device.', metavar=("interface", "port", "device", "mountpoint"))
    1.94 -    group.add_argument('-g', '--getdevices', action='store_true', dest="getdevices", help='Returns a list of all encrypted mounted devices')
    1.95 +    group.add_argument('-g', '--getdevices', action='store_true', dest="getdevices", help='Returns a list of all mounted encrypted devices')
    1.96      arguments = parser.parse_args()
    1.97      
    1.98      
    1.99      config = loadConfig ()
   1.100      initLog (config)
   1.101      
   1.102 -    
   1.103      if (arguments.getdevices):
   1.104 -        print ("%s" %(arguments.getdevices,))
   1.105 +        getDevices (config.get ("Main", "GetDevicesScript"))
   1.106          
   1.107      if (arguments.umount):
   1.108 -        print ("%s" %(arguments.umount,))
   1.109 +        umountDevice (config.get ("Main", "UmountScript"), arguments.umount[0])
   1.110      
   1.111      if (arguments.mount):
   1.112 -        print ("%s" %(arguments.mount,))
   1.113 +        mountDevice (config.get ("Main", "MountScript"), arguments.mount[0], int(arguments.mount[1]), arguments.mount[2], arguments.mount[3], config.get ("Main", "Keyfile"))
   1.114      
   1.115      if (arguments.initialize):
   1.116 -        print ("%s" %(arguments.initialize,))
   1.117 +        print ("Init: %s" %(arguments.initialize,))