Skip to content
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
45 changes: 24 additions & 21 deletions scapy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,18 @@
]


def _probe_config_file(*cf, default=None):
# type: (str, Optional[str]) -> Union[str, None]
def _probe_config_file(*cf):
# type: (str) -> Union[str, None]
path = pathlib.Path(os.path.expanduser("~"))
if not path.exists():
# ~ folder doesn't exist. Unsalvageable
return None
cf_path = path.joinpath(*cf)
if not cf_path.exists():
if default is not None:
# We have a default ! set it
cf_path.parent.mkdir(parents=True, exist_ok=True)
with cf_path.open("w") as fd:
fd.write(default)
return str(cf_path.resolve())
return None
return str(cf_path.resolve())
return str(path.joinpath(*cf).resolve())


def _read_config_file(cf, _globals=globals(), _locals=locals(),
interactive=True):
# type: (str, Dict[str, Any], Dict[str, Any], bool) -> None
interactive=True, default=None):
# type: (str, Dict[str, Any], Dict[str, Any], bool, Optional[str]) -> None
"""Read a config file: execute a python file while loading scapy, that
may contain some pre-configured values.

Expand All @@ -93,11 +84,13 @@ def _read_config_file(cf, _globals=globals(), _locals=locals(),
function. Otherwise, vars are only available from inside the scapy
console.

params:
- _globals: the globals() vars
- _locals: the locals() vars
- interactive: specified whether or not errors should be printed
Parameters:

:param _globals: the globals() vars
:param _locals: the locals() vars
:param interactive: specified whether or not errors should be printed
using the scapy console or raised.
:param default: if provided, set a default value for the config file

ex, content of a config.py file:
'conf.verb = 42\n'
Expand All @@ -107,6 +100,16 @@ def _read_config_file(cf, _globals=globals(), _locals=locals(),
2

"""
cf_path = pathlib.Path(cf)
if not cf_path.exists():
log_loading.debug("Config file [%s] does not exist.", cf)
if default is None:
return
# We have a default ! set it
cf_path.parent.mkdir(parents=True, exist_ok=True)
with cf_path.open("w") as fd:
fd.write(default)
log_loading.debug("Config file [%s] created with default.", cf)
log_loading.debug("Loading config file [%s]", cf)
try:
with open(cf) as cfgf:
Expand Down Expand Up @@ -151,8 +154,7 @@ def _validate_local(k):
# conf.use_pcap = True
""".strip()

DEFAULT_PRESTART_FILE = _probe_config_file(".config", "scapy", "prestart.py",
default=DEFAULT_PRESTART)
DEFAULT_PRESTART_FILE = _probe_config_file(".config", "scapy", "prestart.py")
DEFAULT_STARTUP_FILE = _probe_config_file(".config", "scapy", "startup.py")


Expand Down Expand Up @@ -718,7 +720,8 @@ def interact(mydict=None, argv=None, mybanner=None, loglevel=logging.INFO):
_read_config_file(
PRESTART_FILE,
interactive=True,
_locals=_scapy_prestart_builtins()
_locals=_scapy_prestart_builtins(),
default=DEFAULT_PRESTART,
)

SESSION = init_session(session_name, mydict=mydict, ret=True)
Expand Down
11 changes: 7 additions & 4 deletions test/regression.uts
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,8 @@ assert len(conf.temp_files) == 0
import mock, sys
from scapy.main import interact

from scapy.main import DEFAULT_PRESTART_FILE
from scapy.main import DEFAULT_PRESTART_FILE, DEFAULT_PRESTART, _read_config_file
_read_config_file(DEFAULT_PRESTART_FILE, _locals=globals(), default=DEFAULT_PRESTART)
# By now .config/scapy/startup.py should have been created
with open(DEFAULT_PRESTART_FILE, "r") as fd:
OLD_DEFAULT_PRESTART = fd.read()
Expand Down Expand Up @@ -691,7 +692,8 @@ interact_emulator(extra_args=["-d"]) # Extended
import sys
import mock

from scapy.main import DEFAULT_PRESTART_FILE
from scapy.main import DEFAULT_PRESTART_FILE, DEFAULT_PRESTART, _read_config_file
_read_config_file(DEFAULT_PRESTART_FILE, _locals=globals(), default=DEFAULT_PRESTART)
# By now .config/scapy/startup.py should have been created
with open(DEFAULT_PRESTART_FILE, "w+") as fd:
fd.write("conf.interactive_shell = 'ptpython'")
Expand Down Expand Up @@ -1093,6 +1095,7 @@ assert "NameError" in ret[0]

cmds = """log_runtime.info(hex_bytes("446166742050756e6b"))\n"""
ret = autorun_get_text_interactive_session(cmds)
ret
assert "Daft Punk" in ret[0]

= Test utility TEX functions
Expand Down Expand Up @@ -1122,8 +1125,8 @@ conf.verb = saved_conf_verb

= Test config file functions failures

from scapy.main import _probe_config_file
assert _probe_config_file("filethatdoesnotexistnorwillever.tsppajfsrdrr") is None
from scapy.main import _read_config_file, _probe_config_file
assert _read_config_file(_probe_config_file("filethatdoesnotexistnorwillever.tsppajfsrdrr")) is None

= Test CacheInstance repr

Expand Down