OpenSecurity/bin/opensecurity_client_restful_server.py
changeset 178 e0c11d73a10c
parent 176 32c895509a2a
child 179 04f1f06faaf0
     1.1 --- a/OpenSecurity/bin/opensecurity_client_restful_server.py	Wed Jun 04 16:42:37 2014 +0100
     1.2 +++ b/OpenSecurity/bin/opensecurity_client_restful_server.py	Thu Jun 05 14:49:21 2014 +0200
     1.3 @@ -53,6 +53,7 @@
     1.4  import win32api
     1.5  import win32con
     1.6  import win32wnet
     1.7 +import win32netcon
     1.8  import itertools
     1.9  import ctypes
    1.10  
    1.11 @@ -205,6 +206,9 @@
    1.12      This is called on GET /notification?msgtype=TYPE&text=TEXT.
    1.13      This will pop up an OpenSecurity notifcation window
    1.14      """
    1.15 +
    1.16 +    def POST(self):
    1.17 +        return self.GET()
    1.18      
    1.19      def GET(self):
    1.20          
    1.21 @@ -290,7 +294,23 @@
    1.22              if path in network_path:
    1.23                  return drive
    1.24      return None
    1.25 -
    1.26 +	
    1.27 +def mapDrive(drive, networkPath, user, password):
    1.28 +    print networkPath
    1.29 +    if (os.path.exists(networkPath)):
    1.30 +        print networkPath, " is found..."
    1.31 +        print "Trying to map ", networkPath, " on to ", drive, " ....."
    1.32 +        try:
    1.33 +            win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_DISK, drive, networkPath, None, user, password)
    1.34 +        except:
    1.35 +            print "Unexpected error..."
    1.36 +            return 1
    1.37 +        print "Mapping successful"
    1.38 +        return 0
    1.39 +    else:
    1.40 +        print "Network path unreachable..."
    1.41 +        return 1    
    1.42 +		
    1.43  # handles netumount request                    
    1.44  class MountNetworkDriveHandler(threading.Thread): 
    1.45      networkPath = None
    1.46 @@ -313,21 +333,15 @@
    1.47              return 1
    1.48          
    1.49          #Check for network resource availability
    1.50 -        retry = 5
    1.51 +        retry = 20
    1.52          while not os.path.exists(self.networkPath):
    1.53 +            if retry == 0:
    1.54 +                break
    1.55 +            logger.info("Path not accessible: " + self.networkPath + " retrying")
    1.56              time.sleep(1)
    1.57 -            if retry == 0:
    1.58 -                return 1
    1.59 -            logger.info("Path not accessible: " + self.networkPath + " retrying")
    1.60              retry-=1
    1.61 -    
    1.62 -        command = 'USE ' + drive + ' ' + self.networkPath + ' /PERSISTENT:NO'
    1.63 -    
    1.64 -        result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\NET', command))
    1.65 -        if string.find(result[1], 'successfully',) == -1:
    1.66 -            logger.error("Failed: NET " + command)
    1.67 -            return 1
    1.68 -        return 0
    1.69 +
    1.70 +        return mapDrive(drive, self.networkPath, "", "")
    1.71  
    1.72  class os_netmount:
    1.73      
    1.74 @@ -345,7 +359,20 @@
    1.75          driveHandler.start()
    1.76          driveHandler.join(None)
    1.77          return 'Ok'
    1.78 -        
    1.79 +
    1.80 +def unmapDrive(drive, force=0):
    1.81 +    print "drive in use, trying to unmap..."
    1.82 +    if force == 0:
    1.83 +        print "Executing un-forced call..."
    1.84 +    
    1.85 +    try:
    1.86 +        win32wnet.WNetCancelConnection2(drive, 1, force)
    1.87 +        print drive, "successfully unmapped..."
    1.88 +        return 0
    1.89 +    except:
    1.90 +        print "Unmap failed, try again..."
    1.91 +        return 1
    1.92 +
    1.93  # handles netumount request                    
    1.94  class UmountNetworkDriveHandler(threading.Thread): 
    1.95      networkPath = None
    1.96 @@ -364,10 +391,8 @@
    1.97              else:
    1.98                  drive = drive+':'
    1.99                  logger.info("Unmounting drive " + drive + " for " + self.networkPath)
   1.100 -                command = 'USE ' + drive + ' /DELETE /YES' 
   1.101 -                result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', command)) 
   1.102 -                if string.find(str(result[1]), 'successfully',) == -1:
   1.103 -                    logger.error(result[2])
   1.104 +                result = unmapDrive(drive, force=1) 
   1.105 +                if result != 0:
   1.106                      continue
   1.107                          
   1.108