Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check access rights for OUTPUT_PATH #486

Merged
merged 1 commit into from
Sep 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions framework/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
from framework.config import health_check
from framework.lib.general import cprint
from framework.db import models, target_manager
from framework.utils import NetworkOperations, FileOperations
from framework.utils import NetworkOperations, directory_access, FileOperations


REPLACEMENT_DELIMITER = "@@@"
Expand Down Expand Up @@ -520,13 +520,11 @@ def FrameworkConfigGetLogsDir(self):
config file
"""
logs_dir = self.FrameworkConfigGet("LOGS_DIR")
if (os.path.isabs(logs_dir)):
# Check access for logsdir parent directory because logsdir may not be created.
if os.path.isabs(logs_dir) and directory_access(os.path.dirname(logs_dir), "w+"):
return logs_dir
else:
return os.path.join(
self.FrameworkConfigGet("OUTPUT_PATH"),
logs_dir
)
return os.path.join(self.GetOutputDir(), logs_dir)

def FrameworkConfigGetLogPath(self, process_name):
"""
Expand Down Expand Up @@ -582,7 +580,14 @@ def Show(self):
cprint(str(k) + " => " + str(v))

def GetOutputDir(self):
return os.path.expanduser(self.FrameworkConfigGet("OUTPUT_PATH"))
output_dir = os.path.expanduser(self.FrameworkConfigGet("OUTPUT_PATH"))
if not os.path.isabs(output_dir) and directory_access(os.getcwd(), "w+"):
return output_dir
else:
# The output_dir may not be created yet, so check its parent.
if directory_access(os.path.dirname(output_dir), "w+"):
return output_dir
return os.path.expanduser(os.path.join(self.FrameworkConfigGet("SETTINGS_DIR"), output_dir))

def GetOutputDirForTargets(self):
return os.path.join(
Expand Down
2 changes: 2 additions & 0 deletions framework/config/framework_config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# + '#' can be used for comments, please add them explaining any setting if necessary
# + Seperate any two sections by two empty lines

#------------------------- Settings Directory ----------------------->
SETTINGS_DIR: ~/.owtf

# ----------------------- Version Information ----------------------- #
VERSION: 1.0.1
Expand Down
18 changes: 18 additions & 0 deletions framework/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shutil
import codecs
import logging
import tempfile
from framework.dependency_management.dependency_resolver import ServiceLocator
from framework.lib.general import WipeBadCharsForFilename

Expand Down Expand Up @@ -59,6 +60,23 @@ def io_error(*args, **kwargs):
return io_error


def directory_access(path, mode):
"""Check if a directory can be accessed in the specified mode by the current user.

:param str path: Directory path.
:param str mode: Access type.

:return: Valid access rights
:rtype:`str`
"""

try:
temp_file = tempfile.NamedTemporaryFile(mode=mode, dir=path, delete=True)
except (IOError, OSError):
return False
return True


class FileOperations(object):

@staticmethod
Expand Down