OpenSecurity/bin/opensecurity_client_restful_server.py
changeset 151 d0f24f265331
parent 144 dd472ede7a9f
child 154 651bf8fd169e
     1.1 --- a/OpenSecurity/bin/opensecurity_client_restful_server.py	Mon May 12 18:00:05 2014 +0200
     1.2 +++ b/OpenSecurity/bin/opensecurity_client_restful_server.py	Wed May 14 18:13:39 2014 +0100
     1.3 @@ -45,6 +45,11 @@
     1.4  import web
     1.5  import threading
     1.6  import time
     1.7 +import string
     1.8 +
     1.9 +from opensecurity_util import logger, setupLogger, OpenSecurityException
    1.10 +if sys.platform == 'win32' or sys.platform == 'cygwin':
    1.11 +    from cygwin import Cygwin
    1.12  
    1.13  # local
    1.14  import __init__ as opensecurity
    1.15 @@ -61,6 +66,8 @@
    1.16      '/log',                     'os_log',
    1.17      '/notification',            'os_notification',
    1.18      '/password',                'os_password',
    1.19 +    '/netmount',                'os_netmount',
    1.20 +    '/netumount',               'os_netumount',
    1.21      '/',                        'os_root'
    1.22  )
    1.23  
    1.24 @@ -243,6 +250,107 @@
    1.25          
    1.26          return 'user queried for password'
    1.27  
    1.28 +# handles netumount request                    
    1.29 +class MountNetworkDriveHandler(threading.Thread): 
    1.30 +    drive = None
    1.31 +    resource = None
    1.32 +    
    1.33 +    def __init__(self, drv, net_path):
    1.34 +        threading.Thread.__init__(self)
    1.35 +        self.drive = drv
    1.36 +        self.networkPath = net_path
    1.37 +    
    1.38 +    def run(self):
    1.39 +        #Check for drive availability
    1.40 +        if os.path.exists(self.drive):
    1.41 +            logger.error("Drive letter is already in use: " + self.drive)
    1.42 +            return 1
    1.43 +        
    1.44 +        #Check for network resource availability
    1.45 +        retry = 5
    1.46 +        while not os.path.exists(self.networkPath):
    1.47 +            time.sleep(1)
    1.48 +            if retry == 0:
    1.49 +                return 1
    1.50 +            logger.info("Path not accessible: " + self.networkPath + " retrying")
    1.51 +            retry-=1
    1.52 +    
    1.53 +        command = 'USE ' + self.drive + ' ' + self.networkPath + ' /PERSISTENT:NO'
    1.54 +    
    1.55 +        result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\NET', command))
    1.56 +        if string.find(result[1], 'successfully',) == -1:
    1.57 +            logger.error("Failed: NET " + command)
    1.58 +            return 1
    1.59 +        return 0
    1.60 +
    1.61 +class os_netmount:
    1.62 +    
    1.63 +    """OpenSecurity '/netmount' handler"""
    1.64 +    
    1.65 +    def GET(self):
    1.66 +        # pick the arguments
    1.67 +        args = web.input()
    1.68 +        
    1.69 +        # we _need_ a net_resource
    1.70 +        if not "net_resource" in args:
    1.71 +            raise web.badrequest('no net_resource given')
    1.72 +        
    1.73 +        # we _need_ a drive_letter
    1.74 +        if not "drive_letter" in args:
    1.75 +            raise web.badrequest('no drive_letter given')
    1.76 +
    1.77 +        driveHandler = MountNetworkDriveHandler(args['drive_letter'], args['net_resource'])
    1.78 +        driveHandler.start()
    1.79 +        return 'Ok'
    1.80 +
    1.81 +         
    1.82 +        
    1.83 +# handles netumount request                    
    1.84 +class UmountNetworkDriveHandler(threading.Thread): 
    1.85 +    drive = None
    1.86 +    running = True
    1.87 +    
    1.88 +    def __init__(self, drv):
    1.89 +        threading.Thread.__init__(self)
    1.90 +        self.drive = drv
    1.91 +
    1.92 +    def run(self):
    1.93 +        while self.running:
    1.94 +            result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
    1.95 +            mappedDrives = list()
    1.96 +            for line in result[1].splitlines():
    1.97 +                if 'USB' in line or 'Download' in line:
    1.98 +                    parts = line.split()
    1.99 +                    mappedDrives.append(parts[1])
   1.100 +            
   1.101 +            logger.info(mappedDrives)
   1.102 +            logger.info(self.drive)
   1.103 +            if self.drive not in mappedDrives:
   1.104 +                self.running = False
   1.105 +            else:
   1.106 +                command = 'USE ' + self.drive + ' /DELETE /YES' 
   1.107 +                result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', command)) 
   1.108 +                if string.find(str(result[1]), 'successfully',) == -1:
   1.109 +                    logger.error(result[2])
   1.110 +                    continue
   1.111 +                        
   1.112 +
   1.113 +class os_netumount:
   1.114 +    
   1.115 +    """OpenSecurity '/netumount' handler"""
   1.116 +    
   1.117 +    def GET(self):
   1.118 +        # pick the arguments
   1.119 +        args = web.input()
   1.120 +        
   1.121 +        # we _need_ a drive_letter
   1.122 +        if not "drive_letter" in args:
   1.123 +            raise web.badrequest('no drive_letter given')
   1.124 +        
   1.125 +        driveHandler = UmountNetworkDriveHandler(args['drive_letter'])
   1.126 +        driveHandler.start()
   1.127 +        return 'Ok'
   1.128 +    
   1.129  
   1.130  class os_root:
   1.131