| 1 |
#!/usr/bin/python3 |
| 2 |
import contextlib |
| 3 |
import fcntl |
| 4 |
import glob |
| 5 |
import os |
| 6 |
import re |
| 7 |
import shlex |
| 8 |
import subprocess |
| 9 |
import sys |
| 10 |
import syslog |
| 11 |
|
| 12 |
from mini_buildd import changes, cli, util |
| 13 |
|
| 14 |
#: Needed for man page hack in setup.py |
| 15 |
DESCRIPTION = "Script to use as a 'command' in an authorized_key file to allow authorization to mini-buildd uploads via SSH" |
| 16 |
EPILOG = """ |
| 17 |
See ``mini-buildd-ssh-setup --help`` for further explanations. |
| 18 |
""" |
| 19 |
|
| 20 |
# Use syslog, facility user, to log access and key |
| 21 |
syslog.openlog(facility=syslog.LOG_USER) |
| 22 |
|
| 23 |
|
| 24 |
def log(msg): |
| 25 |
print(msg, file=sys.stderr) |
| 26 |
syslog.syslog(syslog.LOG_NOTICE, msg) |
| 27 |
|
| 28 |
|
| 29 |
@contextlib.contextmanager |
| 30 |
def filelock(lockfile_path): |
| 31 |
with open(lockfile_path, "w", encoding="UTF-8") as lockfile: |
| 32 |
fcntl.lockf(lockfile, fcntl.LOCK_EX) |
| 33 |
yield |
| 34 |
fcntl.lockf(lockfile, fcntl.LOCK_UN) |
| 35 |
|
| 36 |
|
| 37 |
class CLI(cli.CLI): |
| 38 |
""" |
| 39 |
>>> any(re.match(regex_str, "/usr/lib/openssh/sftp-server") for regex_str in CLI.ALLOWED_COMMANDS) # dput (bookworm and higher) |
| 40 |
True |
| 41 |
>>> any(re.match(regex_str, "scp -p -d -t /home/mini-buildd-uploader/incoming/") for regex_str in CLI.ALLOWED_COMMANDS) # dput (bullseye and lower) |
| 42 |
True |
| 43 |
>>> any(re.match(regex_str, "scp -p -t /home/mini-buildd-uploader/incoming/pkg_1.2.3-1.dsc") for regex_str in CLI.ALLOWED_COMMANDS) # dput-ng (paramiko) |
| 44 |
True |
| 45 |
>>> any(re.match(regex_str, "scp -p -d -t /tmp/") for regex_str in CLI.ALLOWED_COMMANDS) |
| 46 |
False |
| 47 |
""" |
| 48 |
ALLOWED_COMMANDS = [ |
| 49 |
r"/usr/lib/openssh/sftp-server", # dput (bookworm and higher) |
| 50 |
r"scp.*\-t[ ]*/home/mini-buildd-uploader/incoming.*", # dput-ng, dput (bullseye and lower) |
| 51 |
] |
| 52 |
|
| 53 |
def __init__(self): |
| 54 |
super().__init__("mini-buildd-ssh-uploader-command", DESCRIPTION, epilog=EPILOG) |
| 55 |
self.incoming = os.path.expanduser("~") + "/incoming/" |
| 56 |
self.lockfile = os.path.expanduser("~") + "/lock" |
| 57 |
|
| 58 |
def _gpg_key_id(self): |
| 59 |
return subprocess.check_output("gpg --list-secret-keys --with-colons | grep --max-count=1 '^sec' | cut -d: -f5", shell=True).strip().decode(encoding="UTF-8") |
| 60 |
|
| 61 |
def _upload(self): |
| 62 |
for c_path in glob.glob(self.incoming + "/*.changes"): |
| 63 |
try: |
| 64 |
subprocess.check_output(["debsign", "--re-sign", "-k", self._gpg_key_id(), c_path], stderr=subprocess.STDOUT) |
| 65 |
subprocess.check_output(["dput", "-U", cli.DputCf().first_target(), c_path], stderr=subprocess.STDOUT) |
| 66 |
log(f"{c_path} uploaded from {os.environ.get('SSH_CONNECTION')}") |
| 67 |
except Exception as e: |
| 68 |
log(f"Upload error: {e}") |
| 69 |
if isinstance(e, subprocess.CalledProcessError): |
| 70 |
for line in e.output.split(b"\n"): |
| 71 |
log(line.decode(encoding="UTF-8")) |
| 72 |
finally: |
| 73 |
for f in changes.Base(c_path).get_files(): |
| 74 |
f_path = os.path.join(self.incoming, f["name"]) |
| 75 |
util.attempt(os.remove, f_path) |
| 76 |
util.attempt(os.remove, c_path) |
| 77 |
|
| 78 |
def _runcli(self): |
| 79 |
original_command = os.environ.get("SSH_ORIGINAL_COMMAND") |
| 80 |
if not any(re.match(regex_str, original_command) for regex_str in CLI.ALLOWED_COMMANDS): |
| 81 |
raise Exception(f"Command not allowed: '{original_command}'") # pylint: disable=broad-exception-raised |
| 82 |
subprocess.check_call(shlex.split(original_command)) |
| 83 |
|
| 84 |
self._upload() |
| 85 |
|
| 86 |
def runcli(self): |
| 87 |
with filelock(self.lockfile): |
| 88 |
self._runcli() |
| 89 |
|
| 90 |
|
| 91 |
if __name__ == "__main__": |
| 92 |
CLI().run() |