|
1 | | -#!/bin/sh |
2 | | -set -eu |
| 1 | +#!/usr/bin/python3 |
3 | 2 |
|
4 | | -# This is the startup script for cockpit-ws. |
| 3 | +# This is the startup script for the cockpit ws container. |
| 4 | + |
| 5 | +import argparse |
| 6 | +import contextlib |
| 7 | +import fcntl |
| 8 | +import os |
| 9 | +import shutil |
| 10 | +import socket |
| 11 | +import subprocess |
| 12 | +from collections.abc import Sequence |
| 13 | +from typing import Never |
| 14 | + |
| 15 | +SHA256_NIL = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' |
| 16 | + |
| 17 | + |
| 18 | +def exec_with_listener(cmd: Sequence[str], env: dict[str, str], sock: socket.socket) -> Never: |
| 19 | + # First dup to an fd > 3, then dup2(3). This ensures we always get a fresh |
| 20 | + # fd at 3 without CLOEXEC, even if the socket was already at fd 3 (where |
| 21 | + # dup2 would be a no-op). We're very fork/exec here because of the need to |
| 22 | + # set environment variables post-fork (to get the correct value for |
| 23 | + # LISTEN_PID). |
| 24 | + os.dup2(fcntl.fcntl(sock.fileno(), fcntl.F_DUPFD_CLOEXEC, 4), 3) |
| 25 | + env = {**env, 'LISTEN_FDS': "1", 'LISTEN_PID': f"{os.getpid()}"} |
| 26 | + |
| 27 | + os.chdir('/') |
| 28 | + os.execve(cmd[0], list(cmd), env) |
| 29 | + |
| 30 | + |
| 31 | +parser = argparse.ArgumentParser(description='Startup script for cockpit-ws') |
| 32 | +parser.add_argument('--no-tls', action='store_true', help='Run in plain HTTP mode (no TLS)') |
| 33 | +parser.add_argument('--port', '-p', type=int, default=9090, help='Port to bind to (default: 9090)') |
| 34 | +parser.add_argument('--address', default='', help='Address to bind to (default: all interfaces)') |
| 35 | +args = parser.parse_args() |
| 36 | + |
| 37 | +# survive `podman restart` |
| 38 | +shutil.rmtree("/run/cockpit", ignore_errors=True) |
| 39 | +os.makedirs("/run/cockpit/tls") |
| 40 | + |
| 41 | +# Build environment for child processes |
| 42 | +env = { |
| 43 | + **os.environ, |
| 44 | + 'PATH': '/bin:/sbin', |
| 45 | + 'RUNTIME_DIRECTORY': "/run/cockpit/tls", |
| 46 | + "COCKPIT_WS_PROCESS_IDLE": f"{(1 << 32) - 1}", # ~136 years |
| 47 | +} |
| 48 | + |
| 49 | +# Read the list of mountpoints so we can make some guesses about the container |
| 50 | +# configuration. Fields are whitespace-separated with whitespace within the |
| 51 | +# names octal-encoded. We are uninterested in paths with whitespace, so don't |
| 52 | +# bother decoding it. |
| 53 | +# See fstab(5) which is cited by proc_pid_mounts(5). |
| 54 | +with open('/proc/mounts') as f: |
| 55 | + mountpoints = frozenset(line.split()[1] for line in f) |
5 | 56 |
|
6 | | -cd / |
7 | | -PATH="/bin:/sbin" |
8 | 57 |
|
9 | 58 | # When run in a privileged container, the host file system must be mounted at /host. |
10 | | -if [ -d /host ]; then |
11 | | - # Run the install command just to be sure |
12 | | - /container/label-install || exit $? |
13 | | - |
14 | | - set +x |
15 | | - |
16 | | - /bin/mount --bind /host/usr/share/pixmaps /usr/share/pixmaps |
17 | | - /bin/mount --bind /host/usr/share/icons /usr/share/icons |
18 | | - /bin/mount --bind /host/var /var |
19 | | - /bin/mount --bind /host/etc/ssh /etc/ssh |
20 | | - |
21 | | - # Make the container think it's the host OS version |
22 | | - if ! mountpoint -q /etc/os-release; then |
23 | | - rm -f /etc/os-release /usr/lib/os-release |
24 | | - ln -sv /host/etc/os-release /etc/os-release |
25 | | - ln -sv /host/usr/lib/os-release /usr/lib/os-release |
26 | | - fi |
27 | | - |
28 | | - # And run cockpit-ws |
29 | | - TARGET_NS=/host/proc/1/ns |
30 | | - exec /usr/bin/nsenter --net=$TARGET_NS/net --uts=$TARGET_NS/uts -- /usr/libexec/cockpit-ws --local-ssh "$@" |
31 | | -else |
32 | | - # unprivileged mode |
| 59 | +if os.path.isdir('/host'): |
| 60 | + # Enter host namespaces so sockets bind to host network |
| 61 | + for ns in ['net', 'uts']: |
| 62 | + with open(f'/host/proc/1/ns/{ns}') as f: |
| 63 | + os.setns(f.fileno(), 0) |
| 64 | + |
| 65 | + subprocess.run(['/container/label-install'], check=True) |
| 66 | + |
| 67 | + for src, dest in ( |
| 68 | + ('/host/usr/share/pixmaps', '/usr/share/pixmaps'), |
| 69 | + ('/host/usr/share/icons', '/usr/share/icons'), |
| 70 | + ('/host/var', '/var'), |
| 71 | + ('/host/etc/ssh', '/etc/ssh'), |
| 72 | + ): |
| 73 | + subprocess.run(['/bin/mount', '--bind', src, dest], check=True) |
33 | 74 |
|
| 75 | + # Make the container think it's the host OS version (for branding purposes) |
| 76 | + if '/etc/os-release' not in mountpoints and '/usr/lib/os-release' not in mountpoints: |
| 77 | + for path in ['/etc/os-release', '/usr/lib/os-release']: |
| 78 | + with contextlib.suppress(FileNotFoundError): |
| 79 | + os.unlink(path) |
| 80 | + os.symlink('/host/etc/os-release', '/etc/os-release') |
| 81 | + os.symlink('/host/usr/lib/os-release', '/usr/lib/os-release') |
| 82 | + |
| 83 | +else: |
34 | 84 | # branding can be set from outside; if it's not, don't show any branding |
35 | | - if ! mountpoint --quiet /etc/os-release; then |
36 | | - rm /etc/os-release |
37 | | - printf 'NAME=default\nID=default\n' > /etc/os-release |
38 | | - fi |
| 85 | + if '/etc/os-release' not in mountpoints and '/usr/lib/os-release' not in mountpoints: |
| 86 | + os.unlink('/etc/os-release') |
| 87 | + with open('/etc/os-release', 'w') as f: |
| 88 | + f.write('NAME=default\nID=default\n') |
39 | 89 |
|
40 | 90 | # config can be customized, but provide a default suitable for a bastion host |
41 | | - mountpoint --quiet /etc/cockpit || mountpoint --quiet /etc/cockpit/cockpit.conf || ln -s /container/default-bastion.conf /etc/cockpit/cockpit.conf |
42 | | - |
43 | | - /usr/libexec/cockpit-certificate-ensure |
| 91 | + if '/etc/cockpit' not in mountpoints and '/etc/cockpit/cockpit.conf' not in mountpoints: |
| 92 | + with contextlib.suppress(FileExistsError): |
| 93 | + os.symlink('/container/default-bastion.conf', '/etc/cockpit/cockpit.conf') |
44 | 94 |
|
45 | 95 | # start SSH agent, unless we already got pointed to one |
46 | | - [ -n "${SSH_AUTH_SOCK:-}" ] || eval "$(ssh-agent)" |
| 96 | + if 'SSH_AUTH_SOCK' not in env: |
| 97 | + result = subprocess.run(['ssh-agent'], capture_output=True, text=True, check=True) |
| 98 | + for line in result.stdout.splitlines(): |
| 99 | + if '=' in line and ';' in line: |
| 100 | + var, value = line.split(';')[0].split('=', 1) |
| 101 | + env[var] = value |
| 102 | + |
| 103 | + |
| 104 | +# Create our main TCP listener socket |
| 105 | +if args.address == '' or ':' in args.address: |
| 106 | + tcp_listener = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) |
| 107 | + tcp_listener.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) |
| 108 | +else: |
| 109 | + tcp_listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 110 | + |
| 111 | + |
| 112 | +with tcp_listener: |
| 113 | + tcp_listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 114 | + tcp_listener.bind((args.address, args.port)) |
| 115 | + tcp_listener.listen() |
| 116 | + |
| 117 | + if args.no_tls: |
| 118 | + exec_with_listener(['/usr/libexec/cockpit-ws', '--no-tls', '--local-ssh'], env, tcp_listener) |
| 119 | + |
| 120 | + else: |
| 121 | + subprocess.run( |
| 122 | + ['/usr/libexec/cockpit-certificate-ensure', '--for-cockpit-tls'], env=env, cwd='/', check=True |
| 123 | + ) |
| 124 | + |
| 125 | + os.mkdir(wsinstance_dir := "/run/cockpit/wsinstance") |
| 126 | + |
| 127 | + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as unix_listener: |
| 128 | + unix_listener.bind(f"{wsinstance_dir}/http.sock") |
| 129 | + os.symlink("http.sock", f"{wsinstance_dir}/https@{SHA256_NIL}.sock") |
| 130 | + unix_listener.listen() |
| 131 | + |
| 132 | + if os.fork() == 0: # child |
| 133 | + exec_with_listener(['/usr/libexec/cockpit-ws', '--for-tls-proxy', '--local-ssh'], env, unix_listener) |
47 | 134 |
|
48 | | - exec /usr/libexec/cockpit-ws --local-ssh "$@" |
49 | | -fi |
| 135 | + exec_with_listener(['/usr/libexec/cockpit-tls', '--idle-timeout=0'], env, tcp_listener) |
0 commit comments