From 77b51dae571d675643c213fd0d9279da879d4f1e Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Sat, 8 Jan 2011 09:30:10 +0000 Subject: [PATCH] adding openFile method with an exception block around file opening part --- lib/core/common.py | 39 ++++++++++++++++++++++++++++++++++++++- lib/core/dump.py | 3 ++- lib/core/option.py | 7 ++++--- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/lib/core/common.py b/lib/core/common.py index 903663717..7b1285424 100644 --- a/lib/core/common.py +++ b/lib/core/common.py @@ -1670,6 +1670,10 @@ def runningAsAdmin(): return isAdmin def logHTTPTraffic(requestLogMsg, responseLogMsg): + """ + Logs HTTP traffic to the output file + """ + kb.locks.logLock.acquire() dataToTrafficFile("%s\n" % requestLogMsg) @@ -1686,6 +1690,7 @@ def getPublicTypeMembers(type_, onlyValues=False): """ Useful for getting members from types (e.g. in enums) """ + retVal = [] for name, value in getmembers(type_): @@ -1698,6 +1703,10 @@ def getPublicTypeMembers(type_, onlyValues=False): return retVal def enumValueToNameLookup(type_, value_): + """ + Returns name of a enum member with a given value + """ + retVal = None for name, value in getPublicTypeMembers(type_): @@ -1708,6 +1717,11 @@ def enumValueToNameLookup(type_, value_): return retVal def extractRegexResult(regex, content, flags=0): + """ + Returns 'result' group value from a possible match with regex on a given + content + """ + retVal = None if regex and content and '?P' in regex: @@ -1722,6 +1736,7 @@ def trimAlphaNum(value): """ Trims alpha numeric characters from start and ending of a given value """ + while value and value[-1].isalnum(): value = value[:-1] @@ -1731,9 +1746,17 @@ def trimAlphaNum(value): return value def isNumPosStrValue(value): + """ + Returns True if value is a string with a positive integer representation + """ + return value and isinstance(value, basestring) and value.isdigit() and value != "0" def aliasToDbmsEnum(value): + """ + Returns major DBMS name from a given alias + """ + retVal = None for key, item in dbmsDict.items(): @@ -1746,7 +1769,7 @@ def aliasToDbmsEnum(value): def findDynamicContent(firstPage, secondPage): """ This function checks if the provided pages have dynamic content. If they - are dynamic, proper markings will be made. + are dynamic, proper markings will be made """ infoMsg = "searching for dynamic content" @@ -2019,3 +2042,17 @@ def getComparePageRatio(firstPage, secondPage, filtered=False): conf.seqMatcher.set_seq2(secondPage) return conf.seqMatcher.quick_ratio() + +def openFile(filename, mode='r'): + """ + Returns file handle of a given filename + """ + + try: + return codecs.open(filename, mode, conf.dataEncoding) + except IOError, e: + errMsg = "there has been a file opening error for filename '%s'. " % filename + errMsg += "Please check %s permissions on a file " % ("write" if mode and\ + ('w' in mode or 'a' in mode or '+' in mode) else "read") + errMsg += "and that it's not locked by another process." + raise sqlmapFilePathException, errMsg diff --git a/lib/core/dump.py b/lib/core/dump.py index bcb520cb9..ce0ed9b6c 100644 --- a/lib/core/dump.py +++ b/lib/core/dump.py @@ -14,6 +14,7 @@ import os from lib.core.common import dataToDumpFile from lib.core.common import dataToStdout from lib.core.common import getUnicode +from lib.core.common import openFile from lib.core.common import restoreDumpMarkedChars from lib.core.data import conf from lib.core.data import kb @@ -259,7 +260,7 @@ class Dump: os.makedirs(dumpDbPath, 0755) dumpFileName = "%s%s%s.csv" % (dumpDbPath, os.sep, table) - dumpFP = codecs.open(dumpFileName, "wb", conf.dataEncoding) + dumpFP = openFile(dumpFileName, "wb") count = int(tableValues["__infos__"]["count"]) separator = str() diff --git a/lib/core/option.py b/lib/core/option.py index 5389f7c58..3812cc5e7 100644 --- a/lib/core/option.py +++ b/lib/core/option.py @@ -29,6 +29,7 @@ from lib.core.common import getFileItems from lib.core.common import getFileType from lib.core.common import normalizePath from lib.core.common import ntToPosixSlashes +from lib.core.common import openFile from lib.core.common import parseTargetDirect from lib.core.common import parseTargetUrl from lib.core.common import paths @@ -130,7 +131,7 @@ def __urllib2Opener(): urllib2.install_opener(opener) def __feedTargetsDict(reqFile, addedTargetUrls): - fp = codecs.open(reqFile, "rb") + fp = openFile(reqFile, "rb") fread = fp.read() fread = fread.replace("\r", "") @@ -1262,7 +1263,7 @@ def __saveCmdline(): config.set(family, option, value) - confFP = codecs.open(paths.SQLMAP_CONFIG, "wb", conf.dataEncoding) + confFP = openFile(paths.SQLMAP_CONFIG, "wb") config.write(confFP) infoMsg = "saved command line options on '%s' configuration file" % paths.SQLMAP_CONFIG @@ -1317,7 +1318,7 @@ def __mergeOptions(inputOptions): def __setTrafficOutputFP(): if conf.trafficFile: - conf.trafficFP = codecs.open(conf.trafficFile, "w+", conf.dataEncoding) + conf.trafficFP = openFile(conf.trafficFile, "w+") def __basicOptionValidation(): if conf.limitStart is not None and not (isinstance(conf.limitStart, int) and conf.limitStart > 0):