Skip to content

Commit 2f3f5dd

Browse files
authored
Chown the history file (#4656)
* Chown the history file * Fix
1 parent d2c6055 commit 2f3f5dd

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

scapy/main.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,26 @@ def _probe_cache_folder(*cf):
101101
)
102102

103103

104+
def _check_perms(file: Union[pathlib.Path, str]) -> None:
105+
"""
106+
Checks that the permissions of a file are properly user-specific, if sudo is used.
107+
"""
108+
if (
109+
not WINDOWS and
110+
"SUDO_UID" in os.environ and
111+
"SUDO_GID" in os.environ
112+
):
113+
# Was started with sudo. Still, chown to the user.
114+
try:
115+
os.chown(
116+
file,
117+
int(os.environ["SUDO_UID"]),
118+
int(os.environ["SUDO_GID"]),
119+
)
120+
except Exception:
121+
pass
122+
123+
104124
def _read_config_file(cf, _globals=globals(), _locals=locals(),
105125
interactive=True, default=None):
106126
# type: (str, Dict[str, Any], Dict[str, Any], bool, Optional[str]) -> None
@@ -137,36 +157,12 @@ def _read_config_file(cf, _globals=globals(), _locals=locals(),
137157
try:
138158
if not cf_path.parent.exists():
139159
cf_path.parent.mkdir(parents=True, exist_ok=True)
140-
if (
141-
not WINDOWS and
142-
"SUDO_UID" in os.environ and
143-
"SUDO_GID" in os.environ
144-
):
145-
# Was started with sudo. Still, chown to the user.
146-
try:
147-
os.chown(
148-
cf_path.parent,
149-
int(os.environ["SUDO_UID"]),
150-
int(os.environ["SUDO_GID"]),
151-
)
152-
except Exception:
153-
pass
160+
_check_perms(cf_path.parent)
161+
154162
with cf_path.open("w") as fd:
155163
fd.write(default)
156-
if (
157-
not WINDOWS and
158-
"SUDO_UID" in os.environ and
159-
"SUDO_GID" in os.environ
160-
):
161-
# Was started with sudo. Still, chown to the user.
162-
try:
163-
os.chown(
164-
cf_path,
165-
int(os.environ["SUDO_UID"]),
166-
int(os.environ["SUDO_GID"]),
167-
)
168-
except Exception:
169-
pass
164+
165+
_check_perms(cf_path)
170166
log_loading.debug("Config file [%s] created with default.", cf)
171167
except OSError:
172168
log_loading.warning("Config file [%s] could not be created.", cf,
@@ -816,6 +812,14 @@ def interact(mydict=None, argv=None, mybanner=None, loglevel=logging.INFO):
816812
banner_text += "\n"
817813
banner_text += mybanner
818814

815+
# Make sure the history file has proper permissions
816+
try:
817+
if not pathlib.Path(conf.histfile).exists():
818+
pathlib.Path(conf.histfile).touch()
819+
_check_perms(conf.histfile)
820+
except OSError:
821+
pass
822+
819823
# Configure interactive terminal
820824

821825
if conf.interactive_shell not in [

0 commit comments

Comments
 (0)