Skip to content

Commit b9aebbe

Browse files
authored
Chown when Scapy is run with sudo (#4566)
1 parent 014a86a commit b9aebbe

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

scapy/main.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,38 @@ def _read_config_file(cf, _globals=globals(), _locals=locals(),
129129
return
130130
# We have a default ! set it
131131
try:
132-
cf_path.parent.mkdir(parents=True, exist_ok=True)
132+
if not cf_path.parent.exists():
133+
cf_path.parent.mkdir(parents=True, exist_ok=True)
134+
if (
135+
not WINDOWS and
136+
"SUDO_UID" in os.environ and
137+
"SUDO_GID" in os.environ
138+
):
139+
# Was started with sudo. Still, chown to the user.
140+
try:
141+
os.chown(
142+
cf_path.parent,
143+
int(os.environ["SUDO_UID"]),
144+
int(os.environ["SUDO_GID"]),
145+
)
146+
except Exception:
147+
pass
133148
with cf_path.open("w") as fd:
134149
fd.write(default)
150+
if (
151+
not WINDOWS and
152+
"SUDO_UID" in os.environ and
153+
"SUDO_GID" in os.environ
154+
):
155+
# Was started with sudo. Still, chown to the user.
156+
try:
157+
os.chown(
158+
cf_path,
159+
int(os.environ["SUDO_UID"]),
160+
int(os.environ["SUDO_GID"]),
161+
)
162+
except Exception:
163+
pass
135164
log_loading.debug("Config file [%s] created with default.", cf)
136165
except OSError:
137166
log_loading.warning("Config file [%s] could not be created.", cf,

0 commit comments

Comments
 (0)