Skip to content

Commit

Permalink
sfx: support ubuntu and openrc:
Browse files Browse the repository at this point in the history
-- ubuntu does not let root follow symlinks created by other users
-- openrc expects copyparty to die if you kill the sfx parent
  • Loading branch information
9001 committed Aug 23, 2020
1 parent 82e568d commit f550a81
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions scripts/sfx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# coding: utf-8
from __future__ import print_function, unicode_literals

import re, os, sys, stat, time, shutil, tarfile, hashlib, platform, tempfile
import re, os, sys, time, shutil, signal, tarfile, hashlib, platform, tempfile
import subprocess as sp

"""
Expand All @@ -29,6 +29,7 @@
PY2 = sys.version_info[0] == 2
sys.dont_write_bytecode = True
me = os.path.abspath(os.path.realpath(__file__))
cpp = None


def eprint(*args, **kwargs):
Expand Down Expand Up @@ -305,17 +306,19 @@ def hashfile(fn):
def unpack():
"""unpacks the tar yielded by `data`"""
name = "pe-copyparty"
tag = "v" + str(STAMP)
withpid = "{}.{}".format(name, os.getpid())
top = tempfile.gettempdir()
final = os.path.join(top, name)
mine = os.path.join(top, withpid)
tar = os.path.join(mine, "tar")
tag_mine = os.path.join(mine, "v" + str(STAMP))
tag_final = os.path.join(final, "v" + str(STAMP))

if os.path.exists(tag_final):
msg("found early")
return final
try:
if tag in os.listdir(final):
msg("found early")
return final
except:
pass

nwrite = 0
os.mkdir(mine)
Expand All @@ -338,12 +341,15 @@ def unpack():

os.remove(tar)

with open(tag_mine, "wb") as f:
with open(os.path.join(mine, tag), "wb") as f:
f.write(b"h\n")

if os.path.exists(tag_final):
msg("found late")
return final
try:
if tag in os.listdir(final):
msg("found late")
return final
except:
pass

try:
if os.path.islink(final):
Expand Down Expand Up @@ -428,10 +434,15 @@ def get_payload():
def confirm():
msg()
msg("*** hit enter to exit ***")
raw_input() if PY2 else input()
try:
raw_input() if PY2 else input()
except:
pass


def run(tmp, py):
global cpp

msg("OK")
msg("will use:", py)
msg("bound to:", tmp)
Expand All @@ -447,8 +458,11 @@ def run(tmp, py):
pass

fp_py = os.path.join(tmp, "py")
with open(fp_py, "wb") as f:
f.write(py.encode("utf-8") + b"\n")
try:
with open(fp_py, "wb") as f:
f.write(py.encode("utf-8") + b"\n")
except:
pass

# avoid loading ./copyparty.py
cmd = [
Expand All @@ -460,16 +474,21 @@ def run(tmp, py):
] + list(sys.argv[1:])

msg("\n", cmd, "\n")
p = sp.Popen(str(x) for x in cmd)
cpp = sp.Popen(str(x) for x in cmd)
try:
p.wait()
cpp.wait()
except:
p.wait()
cpp.wait()

if p.returncode != 0:
if cpp.returncode != 0:
confirm()

sys.exit(p.returncode)
sys.exit(cpp.returncode)


def bye(sig, frame):
if cpp is not None:
cpp.terminate()


def main():
Expand Down Expand Up @@ -504,6 +523,8 @@ def main():

# skip 0

signal.signal(signal.SIGTERM, bye)

tmp = unpack()
fp_py = os.path.join(tmp, "py")
if os.path.exists(fp_py):
Expand Down

0 comments on commit f550a81

Please sign in to comment.