Commit 8fd98939 by devttys0

Updated settings.py to be more pythonic.

parent fdbcf419
...@@ -13,6 +13,12 @@ export BUILD_FUZZY=@BUILD_FUZZY@ ...@@ -13,6 +13,12 @@ export BUILD_FUZZY=@BUILD_FUZZY@
BUILD_C_LIBS=@BUILD_C_LIBS@ BUILD_C_LIBS=@BUILD_C_LIBS@
PYTHON=@PYTHON@ PYTHON=@PYTHON@
SRC_C_DIR="./src/C" SRC_C_DIR="./src/C"
ifeq ($(strip $(prefix)),)
PREFIX=""
else
PREFIX="--prefix=$(prefix)"
endif
START_SCRIPT="binwalk-start"
.PHONY: all install build deps clean uninstall .PHONY: all install build deps clean uninstall
...@@ -20,25 +26,32 @@ all: build ...@@ -20,25 +26,32 @@ all: build
install: build install: build
if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR) install; fi if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR) install; fi
ifeq ($(strip $(prefix)),) $(PYTHON) ./setup.py install $(PREFIX)
$(PYTHON) ./setup.py install
else
$(PYTHON) ./setup.py install --prefix=$(prefix)
endif
if [ "$(BUILD_C_LIBS)" -eq "1" ] && [ $(findstring "CYGWIN", $(PLATFORM)) ]; then ldconfig || true; fi if [ "$(BUILD_C_LIBS)" -eq "1" ] && [ $(findstring "CYGWIN", $(PLATFORM)) ]; then ldconfig || true; fi
build: build:
if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR); fi if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR); fi
echo "$(prefix)" > src/binwalk/config/prefix.conf
$(PYTHON) ./setup.py build $(PYTHON) ./setup.py build
start-script:
echo '#!/bin/sh' > $(START_SCRIPT)
echo "PREFIX=\"$(prefix)\"" >> $(START_SCRIPT)
echo 'PYVERSION=`python --version 2>&1 | cut -d' ' -f2 | cut -d'.' -f1,2`' >> $(START_SCRIPT)
echo 'export PYTHONPATH="\$PREFIX/lib/python$PYVERSION/site-packages"' >> $(START_SCRIPT)
echo '\$PREFIX/bin/binwalk "\$@"' >> $(START_SCRIPT)
chmod +x $(START_SCRIPT)
clean: clean:
if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR) clean; fi if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR) clean; fi
$(PYTHON) ./setup.py clean $(PYTHON) ./setup.py clean
distclean: clean distclean: clean
if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR) distclean; fi if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR) distclean; fi
rm -rf Makefile config.* *.cache rm -rf Makefile config.* *.cache $(START_SCRIPT)
uninstall: uninstall:
if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR) uninstall; fi if [ "$(BUILD_C_LIBS)" -eq "1" ]; then make -C $(SRC_C_DIR) uninstall; fi
$(PYTHON) ./setup.py uninstall $(PYTHON) ./setup.py uninstall
...@@ -3333,6 +3333,15 @@ fi ...@@ -3333,6 +3333,15 @@ fi
CFLAGS="-Wall -fPIC $CFLAGS" CFLAGS="-Wall -fPIC $CFLAGS"
INSTALL_OPTIONS="-m644" INSTALL_OPTIONS="-m644"
if test "$prefix" != "NONE"
then
CONFIGURE_PREFIX="--prefix=$prefix"
else
CONFIGURE_PREFIX=""
fi
echo "prefix: $prefix"
if test "$(uname)" == "Darwin" if test "$(uname)" == "Darwin"
then then
SONAME="-install_name" SONAME="-install_name"
...@@ -3346,12 +3355,12 @@ if test "$BUILD_C_LIBS" != "0" ...@@ -3346,12 +3355,12 @@ if test "$BUILD_C_LIBS" != "0"
then then
if test "$BUILD_MAGIC" != "0" if test "$BUILD_MAGIC" != "0"
then then
(cd ./src/C/file-*/ && autoreconf -v && ./configure) || exit 1 (cd ./src/C/file-*/ && autoreconf -v && ./configure $CONFIGURE_PREFIX) || exit 1
fi fi
if test "$BUILD_FUZZY" != "0" if test "$BUILD_FUZZY" != "0"
then then
(cd ./src/C/ssdeep-*/ && autoreconf -v && ./configure) || exit 1 (cd ./src/C/ssdeep-*/ && autoreconf -v && ./configure $CONFIGURE_PREFIX) || exit 1
fi fi
fi fi
......
...@@ -30,6 +30,15 @@ AC_ARG_ENABLE([libfuzzy], ...@@ -30,6 +30,15 @@ AC_ARG_ENABLE([libfuzzy],
CFLAGS="-Wall -fPIC $CFLAGS" CFLAGS="-Wall -fPIC $CFLAGS"
INSTALL_OPTIONS="-m644" INSTALL_OPTIONS="-m644"
if test "$prefix" != "NONE"
then
CONFIGURE_PREFIX="--prefix=$prefix"
else
CONFIGURE_PREFIX=""
fi
echo "prefix: $prefix"
if test "$(uname)" == "Darwin" if test "$(uname)" == "Darwin"
then then
SONAME="-install_name" SONAME="-install_name"
...@@ -43,12 +52,12 @@ if test "$BUILD_C_LIBS" != "0" ...@@ -43,12 +52,12 @@ if test "$BUILD_C_LIBS" != "0"
then then
if test "$BUILD_MAGIC" != "0" if test "$BUILD_MAGIC" != "0"
then then
(cd ./src/C/file-*/ && autoreconf -v && ./configure) || exit 1 (cd ./src/C/file-*/ && autoreconf -v && ./configure $CONFIGURE_PREFIX) || exit 1
fi fi
if test "$BUILD_FUZZY" != "0" if test "$BUILD_FUZZY" != "0"
then then
(cd ./src/C/ssdeep-*/ && autoreconf -v && ./configure) || exit 1 (cd ./src/C/ssdeep-*/ && autoreconf -v && ./configure $CONFIGURE_PREFIX) || exit 1
fi fi
fi fi
......
...@@ -96,6 +96,7 @@ class Library(object): ...@@ -96,6 +96,7 @@ class Library(object):
Returns None. Returns None.
''' '''
self.settings = binwalk.core.settings.Settings()
self.library = ctypes.cdll.LoadLibrary(self.find_library(library)) self.library = ctypes.cdll.LoadLibrary(self.find_library(library))
if not self.library: if not self.library:
raise Exception("Failed to load library '%s'" % library) raise Exception("Failed to load library '%s'" % library)
...@@ -114,20 +115,25 @@ class Library(object): ...@@ -114,20 +115,25 @@ class Library(object):
''' '''
lib_path = None lib_path = None
try:
prefix = open(self.settings.system.prefix, 'r').read().strip()
except:
prefix = ''
if isinstance(libraries, str): if isinstance(libraries, str):
libraries = [libraries] libraries = [libraries]
for library in libraries: for library in libraries:
system_paths = { system_paths = {
'linux' : ['/usr/local/lib/lib%s.so' % library], 'linux' : [os.path.join(prefix, 'lib', 'lib%s.so' % library), '/usr/local/lib/lib%s.so' % library],
'linux2' : ['/usr/local/lib/lib%s.so' % library], 'linux2' : [os.path.join(prefix, 'lib', 'lib%s.so' % library), '/usr/local/lib/lib%s.so' % library],
'linux3' : ['/usr/local/lib/lib%s.so' % library], 'linux3' : [os.path.join(prefix, 'lib', 'lib%s.so' % library), '/usr/local/lib/lib%s.so' % library],
'darwin' : ['/opt/local/lib/lib%s.dylib' % library, 'darwin' : [os.path.join(prefix, 'lib', 'lib%s.dylib' % library), '/opt/local/lib/lib%s.dylib' % library,
'/usr/local/lib/lib%s.dylib' % library, '/usr/local/lib/lib%s.dylib' % library,
] + glob.glob('/usr/local/Cellar/lib%s/*/lib/lib%s.dylib' % (library, library)), ] + glob.glob('/usr/local/Cellar/lib%s/*/lib/lib%s.dylib' % (library, library)),
'cygwin' : ['/usr/local/lib/lib%s.so' % library], 'cygwin' : [os.path.join(prefix, 'lib', 'lib%s.so' % library), '/usr/local/lib/lib%s.so' % library],
'win32' : ['%s.dll' % library] 'win32' : [os.path.join(prefix, 'lib%s.dll' % library), '%s.dll' % library]
} }
# Search the common install directories first; these are usually not in the library search path # Search the common install directories first; these are usually not in the library search path
......
...@@ -173,7 +173,10 @@ class Plugins(object): ...@@ -173,7 +173,10 @@ class Plugins(object):
} }
for key in plugins.keys(): for key in plugins.keys():
plugins[key]['path'] = self.settings.get_file_path(key, self.settings.PLUGINS) if key == 'user':
plugins[key]['path'] = self.settings.user.plugins
else:
plugins[key]['path'] = self.settings.system.plugins
if plugins[key]['path']: if plugins[key]['path']:
for file_name in os.listdir(plugins[key]['path']): for file_name in os.listdir(plugins[key]['path']):
......
...@@ -4,6 +4,12 @@ import os ...@@ -4,6 +4,12 @@ import os
import binwalk.core.common as common import binwalk.core.common as common
from binwalk.core.compat import * from binwalk.core.compat import *
class Container(object):
def __init__(self, **kwargs):
for (k,v) in iterator(kwargs):
setattr(self, k, v)
class Settings: class Settings:
''' '''
Binwalk settings class, used for accessing user and system file paths and general configuration settings. Binwalk settings class, used for accessing user and system file paths and general configuration settings.
...@@ -27,6 +33,7 @@ class Settings: ...@@ -27,6 +33,7 @@ class Settings:
# File names # File names
PLUGINS = "plugins" PLUGINS = "plugins"
PREFIX_FILE = "prefix.conf"
EXTRACT_FILE = "extract.conf" EXTRACT_FILE = "extract.conf"
BINWALK_MAGIC_FILE = "binwalk" BINWALK_MAGIC_FILE = "binwalk"
BINARCH_MAGIC_FILE = "binarch" BINARCH_MAGIC_FILE = "binarch"
...@@ -41,38 +48,21 @@ class Settings: ...@@ -41,38 +48,21 @@ class Settings:
# Path to the system wide binwalk directory # Path to the system wide binwalk directory
self.system_dir = self._get_system_dir() self.system_dir = self._get_system_dir()
# Dictionary of all absolute user/system file paths
self.paths = {
'user' : {},
'system' : {},
}
# Build the paths to all user-specific files # Build the paths to all user-specific files
self.paths['user'][self.BINWALK_MAGIC_FILE] = self._user_path(self.BINWALK_MAGIC_DIR, self.BINWALK_MAGIC_FILE) self.user = Container(binwalk=self._user_path(self.BINWALK_MAGIC_DIR, self.BINWALK_MAGIC_FILE),
self.paths['user'][self.BINARCH_MAGIC_FILE] = self._user_path(self.BINWALK_MAGIC_DIR, self.BINARCH_MAGIC_FILE) binarch=self._user_path(self.BINWALK_MAGIC_DIR, self.BINARCH_MAGIC_FILE),
self.paths['user'][self.BINCAST_MAGIC_FILE] = self._user_path(self.BINWALK_MAGIC_DIR, self.BINCAST_MAGIC_FILE) bincast=self._user_path(self.BINWALK_MAGIC_DIR, self.BINCAST_MAGIC_FILE),
self.paths['user'][self.EXTRACT_FILE] = self._user_path(self.BINWALK_CONFIG_DIR, self.EXTRACT_FILE) extract=self._user_path(self.BINWALK_CONFIG_DIR, self.EXTRACT_FILE),
self.paths['user'][self.PLUGINS] = self._user_path(self.BINWALK_PLUGINS_DIR) plugins=self._user_path(self.BINWALK_PLUGINS_DIR))
# Build the paths to all system-wide files
self.paths['system'][self.BINWALK_MAGIC_FILE] = self._system_path(self.BINWALK_MAGIC_DIR, self.BINWALK_MAGIC_FILE)
self.paths['system'][self.BINARCH_MAGIC_FILE] = self._system_path(self.BINWALK_MAGIC_DIR, self.BINARCH_MAGIC_FILE)
self.paths['system'][self.BINCAST_MAGIC_FILE] = self._system_path(self.BINWALK_MAGIC_DIR, self.BINCAST_MAGIC_FILE)
self.paths['system'][self.EXTRACT_FILE] = self._system_path(self.BINWALK_CONFIG_DIR, self.EXTRACT_FILE)
self.paths['system'][self.PLUGINS] = self._system_path(self.BINWALK_PLUGINS_DIR)
def get_file_path(self, usersys, fname):
'''
Retrieves the specified file path from self.paths.
@usersys - One of: 'user', 'system'.
@fname - The file name (e.g., self.BINWALK_MAGIC_FILE, self.PLUGINS, etc)
Returns the path, if it exists; returns None otherwise. # Build the paths to all system-wide files
''' self.system = Container(binwalk=self._system_path(self.BINWALK_MAGIC_DIR, self.BINWALK_MAGIC_FILE),
if has_key(self.paths, usersys) and has_key(self.paths[usersys], fname) and self.paths[usersys][fname]: binarch=self._system_path(self.BINWALK_MAGIC_DIR, self.BINARCH_MAGIC_FILE),
return self.paths[usersys][fname] bincast=self._system_path(self.BINWALK_MAGIC_DIR, self.BINCAST_MAGIC_FILE),
return None extract=self._system_path(self.BINWALK_CONFIG_DIR, self.EXTRACT_FILE),
prefix=self._system_path(self.BINWALK_CONFIG_DIR, self.PREFIX_FILE),
plugins=self._system_path(self.BINWALK_PLUGINS_DIR))
def find_magic_file(self, fname, system_only=False, user_only=False): def find_magic_file(self, fname, system_only=False, user_only=False):
''' '''
......
...@@ -859,7 +859,7 @@ ...@@ -859,7 +859,7 @@
0 string bFLT BFLT executable 0 string bFLT BFLT executable
>4 belong <1 invalid >4 belong <1 invalid
>4 belong >4 invalid >4 belong >4 invalid
>4 belong x version %ld, >4 belong x version %d,
>4 belong 4 >4 belong 4
>8 belong x code offset: 0x%.8X, >8 belong x code offset: 0x%.8X,
>12 belong x data segment starts at: 0x%.8X, >12 belong x data segment starts at: 0x%.8X,
...@@ -1191,34 +1191,34 @@ ...@@ -1191,34 +1191,34 @@
0 lelong 0x28cd3d45 CramFS filesystem, little endian 0 lelong 0x28cd3d45 CramFS filesystem, little endian
>4 lelong <0 invalid >4 lelong <0 invalid
>4 lelong >1073741824 invalid >4 lelong >1073741824 invalid
>4 lelong x size %lu >4 lelong x size %u
>8 lelong &1 version #2 >8 lelong &1 version #2
>8 lelong &2 sorted_dirs >8 lelong &2 sorted_dirs
>8 lelong &4 hole_support >8 lelong &4 hole_support
>32 lelong x CRC 0x%x, >32 lelong x CRC 0x%x,
>36 lelong x edition %lu, >36 lelong x edition %u,
>40 lelong <0 invalid >40 lelong <0 invalid
>40 lelong x %lu blocks, >40 lelong x %u blocks,
>44 lelong <0 invalid >44 lelong <0 invalid
>44 lelong x %lu files >44 lelong x %u files
>4 lelong x {jump-to-offset:%lu} >4 lelong x {jump-to-offset:%u}
>4 lelong x {file-size:%lu} >4 lelong x {file-size:%u}
0 belong 0x28cd3d45 CramFS filesystem, big endian 0 belong 0x28cd3d45 CramFS filesystem, big endian
>4 belong <0 invalid >4 belong <0 invalid
>4 belong >1073741824 invalid >4 belong >1073741824 invalid
>4 belong x size %lu >4 belong x size %u
>8 belong &1 version #2 >8 belong &1 version #2
>8 belong &2 sorted_dirs >8 belong &2 sorted_dirs
>8 belong &4 hole_support >8 belong &4 hole_support
>32 belong x CRC 0x%x, >32 belong x CRC 0x%x,
>36 belong x edition %lu, >36 belong x edition %u,
>40 belong <0 invalid >40 belong <0 invalid
>40 belong x %lu blocks, >40 belong x %u blocks,
>44 belong <0 invalid >44 belong <0 invalid
>44 belong x %lu files >44 belong x %u files
>4 belong x {jump-to-offset:%lu} >4 belong x {jump-to-offset:%u}
>4 belong x {file-size:%lu} >4 belong x {file-size:%u}
...@@ -2251,8 +2251,8 @@ ...@@ -2251,8 +2251,8 @@
>16 belong >10000 invalid >16 belong >10000 invalid
>20 belong <1 invalid >20 belong <1 invalid
>20 belong >10000 invalid >20 belong >10000 invalid
>16 belong x \b, %ld x >16 belong x \b, %d x
>20 belong x %ld, >20 belong x %d,
>24 byte x %d-bit >24 byte x %d-bit
>25 byte 0 grayscale, >25 byte 0 grayscale,
>25 byte 2 \b/color RGB, >25 byte 2 \b/color RGB,
...@@ -2267,8 +2267,8 @@ ...@@ -2267,8 +2267,8 @@
0 string GIF8 GIF image data 0 string GIF8 GIF image data
>4 string 7a \b, version "8%s", >4 string 7a \b, version "8%s",
>4 string 9a \b, version "8%s", >4 string 9a \b, version "8%s",
>6 leshort >0 %hd x >6 leshort >0 %d x
>8 leshort >0 %hd >8 leshort >0 %d
#>10 byte &0x80 color mapped, #>10 byte &0x80 color mapped,
#>10 byte&0x07 =0x00 2 colors #>10 byte&0x07 =0x00 2 colors
#>10 byte&0x07 =0x01 4 colors #>10 byte&0x07 =0x01 4 colors
......
...@@ -270,8 +270,8 @@ class Extractor(Module): ...@@ -270,8 +270,8 @@ class Extractor(Module):
''' '''
# Load the user extract file first to ensure its rules take precedence. # Load the user extract file first to ensure its rules take precedence.
extract_files = [ extract_files = [
self.config.settings.get_file_path('user', self.config.settings.EXTRACT_FILE), self.config.settings.user.extract,
self.config.settings.get_file_path('system', self.config.settings.EXTRACT_FILE), self.config.settings.system.extract,
] ]
for extract_file in extract_files: for extract_file in extract_files:
......
...@@ -64,20 +64,20 @@ class Signature(Module): ...@@ -64,20 +64,20 @@ class Signature(Module):
# Append the user's magic file first so that those signatures take precedence # Append the user's magic file first so that those signatures take precedence
elif self.search_for_opcodes: elif self.search_for_opcodes:
self.magic_files = [ self.magic_files = [
self.config.settings.get_file_path('user', self.config.settings.BINARCH_MAGIC_FILE), self.config.settings.user.binarch,
self.config.settings.get_file_path('system', self.config.settings.BINARCH_MAGIC_FILE), self.config.settings.system.binarch,
] ]
elif self.cast_data_types: elif self.cast_data_types:
self.magic_files = [ self.magic_files = [
self.config.settings.get_file_path('user', self.config.settings.BINCAST_MAGIC_FILE), self.config.settings.user.bincast,
self.config.settings.get_file_path('system', self.config.settings.BINCAST_MAGIC_FILE), self.config.settings.system.bincast,
] ]
# Use the system default magic file if no other was specified, or if -B was explicitly specified # Use the system default magic file if no other was specified, or if -B was explicitly specified
if (not self.magic_files) or (self.explicit_signature_scan and not self.cast_data_types): if (not self.magic_files) or (self.explicit_signature_scan and not self.cast_data_types):
self.magic_files.append(self.config.settings.get_file_path('user', self.config.settings.BINWALK_MAGIC_FILE)) self.magic_files.append(self.config.settings.user.binwalk)
self.magic_files.append(self.config.settings.get_file_path('system', self.config.settings.BINWALK_MAGIC_FILE)) self.magic_files.append(self.config.settings.system.binwalk)
# Parse the magic file(s) and initialize libmagic # Parse the magic file(s) and initialize libmagic
binwalk.core.common.debug("Loading magic files: %s" % str(self.magic_files)) binwalk.core.common.debug("Loading magic files: %s" % str(self.magic_files))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment