Exemplo n.º 1
0
 def scanForRemoved(self):
     conf = self.load_config()
     dbm = DBManager(conf)
     wHost = "host_name=\"%s\" AND " % conf["HOSTNAME"]
     wLoc = "file_location LIKE \"%s" % self.topDir
     wLoc += "%\""
     files = dbm.readFromDatabase("file_location,id", wHost + wLoc)
     missing_files = []
     for file_row in files:
         theF = re.sub(r'\\(.)', r'\1',file_row[0])
         if not os.path.isfile(theF):
             missing_files.append([file_row[1],theF])
     for fpack in missing_files:
         row_id = fpack[0]
         if self.disposition is "i":
             choice = self.handleMissingInteractive(theF)
         else:
             choice = self.disposition
             
         if choice is "d":
             print "removing from db"
             self.handleDeleteFromDB(row_id)
         elif choice is "r":
             print "restoring to %s" % theF
             self.handleRevertFromDb(row_id)
         else: 
             print "skipping"
             pass
             
     return missing_files
Exemplo n.º 2
0
 def handleRevertFromDb(self,row_id):
     dbm = DBManager(self.load_config())
     where_str = "id="+str(row_id)
     rows = dbm.readFromDatabase("file_fingerprint,bin_string,file_location",where_str)
     if rows[0][1] is None:
         print "%s was not backed up!!!" % rows[0][2]
         return False
     data = binascii.unhexlify(rows[0][1])
     local = re.sub(r'\\(.)', r'\1',rows[0][2])
     container = os.path.dirname(rows[0][2])
     if not os.path.isdir(container):
         call = "mkdir -p %s" % container
         print call
         subprocess.call(call)
     
     with open(local,"wb") as f:
         f.write(data)
         f.close()
     print "%s was restored" % rows[0][2]
Exemplo n.º 3
0
 def handleDeleteFromDB(self,row_id):
     where_str = "id="+str(row_id)
     dbm = DBManager(self.load_config())
     dbm.deleteFromDatabase(where_str)
Exemplo n.º 4
0
def hashFile(fileName, blocksize=65536,verbose=None,uploadFile=False,storeBin=False,disposistions=["i","i"],scanFile=True):
    hasher = hashlib.sha1()
    conf = load_config()
    dbm = DBManager(conf)
    
    with open(fileName, 'rb') as afile:
        buf = afile.read(blocksize)
        while len(buf) > 0:
            hasher.update(buf)
            
            buf = afile.read(blocksize)
            
    fingerprint = hasher.hexdigest()
    
    row = {"host_name":conf["HOSTNAME"],"file_name":os.path.basename(fileName),"file_location":fileName,"file_fingerprint":fingerprint}
    exists = (dbm.fingerprintRecordExists(fingerprint) or dbm.fileRecordExists(fileName)) 
    if storeBin:
        #hex Encode the Hex Data to store in the DB
        afile = open(fileName, 'rb')
        bytes = afile.read()
        hexadecimal = binascii.hexlify(bytes)
        #print len(hexadecimal)
        afile.close()
        
        if len(hexadecimal) > 4294967295:#Max size of TEXTLARGE in MySQL. Working on a fix for this
            print "Encoded Bin to Large!"
            raise Exception
        row["bin_string"] = hexadecimal
    is_new = "True"   
    has_changed = "False"
    num_tripped = 0
    
    if not exists:
        
        #Handle case where this is a new file
        print "%s is new. FINGERPRINT: %s." % (fileName,fingerprint)
        if scanFile:
            num_tripped = vscan_file(os.path.abspath(fileName),uploadFile)
        if dispos[0] is "i" or num_tripped > 0:
            c = handleNewInteractive(fileName)
        else:
            c = dispos[0]
            
        if c is "d":
            os.remove(os.path.abspath(fileName))
        elif c is "a":
            dbm.writeToDatabase(row)
        elif c is "p":
            print row
    else:
        #Handle cases Where files already exists in DB
        host_name = conf["HOSTNAME"]
        where_str = "(file_fingerprint=\"%s\" or file_location=\"%s\") AND host_name=\"%s\"" % (fingerprint,fileName,host_name)
        rows = dbm.readFromDatabase("id,file_fingerprint,bin_string",where_str)
        is_new = "False"
        for res in rows:
            if fingerprint != res[1]:
                if scanFile:
                    num_tripped = vscan_file(os.path.abspath(fileName),uploadFile)  
                #Handle case where fingerprint has changed
                if dispos[1] is "i" or num_tripped > 0:
                    c = handleChangedInteractive(fileName)
                else:
                    c = dispos[1]
                has_changed = "True"
                if c is "u":
                    dbm.updateRow(int(res[0]), row)
                elif c is "r":
                    where_str = "file_fingerprint=\"%s\" AND host_name=\"%s\"" % (fingerprint,conf["HOSTNAME"])
                    rows = dbm.readFromDatabase("file_fingerprint,bin_string",where_str)
                    data = binascii.unhexlify(b64decode(rows[0][1]))
                    with open(fileName,"wb") as f:
                        f.write(data)
                        f.close()
                elif c is "p":
                    print row
                    
            elif storeBin and res[2] != hexadecimal:
                print "Data changed for %s. Updating." % fileName
                #Handle Case where File is kown but we want to add the bin data
                dbm.updateRow(int(res[0]), {"bin_string":hexadecimal})
                
    if verbose is not None:
        try:
            is_backed_up = (row["bin_string"] != None)
        except:
            is_backed_up = False
        print 'File        :', os.path.basename(fileName)
        print 'Path        :', os.path.abspath(fileName)
        print 'New?        :', is_new
        print 'Changed?    :', has_changed
        print 'Fingerprint :', fingerprint
        print 'Backed Up?  :', is_backed_up
        print 'Absolute    :', os.path.isabs(fileName)
        print 'Is File?    :', os.path.isfile(fileName)
        print 'Is Dir?     :', os.path.isdir(fileName)
        print 'Is Link?    :', os.path.islink(fileName)
        print 'Mountpoint? :', os.path.ismount(fileName)
        print 'Exists?     :', os.path.exists(fileName)
        print 'Link Exists?:', os.path.lexists(fileName)
        print 
        
    return (row)