#!/bin/bash # SPDX-FileCopyrightText: 2026 Coffey Labs LLC # SPDX-License-Identifier: AGPL-3.0-or-later # # Joining from the interface: `inbuxa` with no arguments in a terminal (a # pseudo-terminal here), the mail server skipped, and the Server screen # filled in with a server and an API key, as a person would. Two machines are # played by one, as in join.sh, and the result is held to what join.sh checks # of the command: the server lists the front ends, the console may call it, # and a person signs in to the joined webmail. # # What only the interface can get wrong is checked too: skipping the server # leads to the Server screen rather than a refusal, the key is never on # screen, the server is asked before the plan, the plan shows `inbuxa join` # with `--key -`, nothing happens before the "y", and the summary says where # it joined and that the key wasn't kept. # # Run from the host with: e2e/vm/run.sh e2e/cases/interface-join.sh set -uo pipefail pass=0; fail=0 ok() { echo " ok $*"; pass=$((pass+1)); } bad() { echo " FAIL $*"; fail=$((fail+1)); } SERVER_DIR=/var/lib/inbuxa DIR=/var/lib/inbuxa-joined SERVER=http://127.0.0.1:8081 if [ -x /tmp/inbuxa-server-test ]; then export INBUXA_SERVER_BINARY=/tmp/inbuxa-server-test # A debug build needs a deeper stack than a release one, as the server's # own test suites do (RUST_MIN_STACK in its CI). A drop-in made before the # unit exists applies when it does. mkdir -p /etc/systemd/system/inbuxa-server.service.d printf '[Service]\nEnvironment=RUST_MIN_STACK=16777216\n' > /etc/systemd/system/inbuxa-server.service.d/test-build.conf fi rt() { command -v docker >/dev/null 2>&1 && echo docker || echo podman; } compose() { if [ "$(rt)" = docker ]; then docker compose -f "$DIR/compose.yaml" "$@" else DOCKER_HOST=unix:///run/podman/podman.sock \ /usr/local/lib/docker/cli-plugins/docker-compose -f "$DIR/compose.yaml" "$@" fi } jmap() { # jmap TOKEN-or-user:pass CAPABILITY JSON-CALLS local auth case "$1" in *:*) auth=(-u "$1") ;; *) auth=(-H "Authorization: Bearer $1") ;; esac curl -s "${auth[@]}" -H 'Content-Type: application/json' \ -d "{\"using\":[\"urn:ietf:params:jmap:core\",\"$2\"],\"methodCalls\":$3}" "$SERVER/jmap/" } wait_server() { for _ in $(seq 1 60); do [ "$(curl -s -o /dev/null -w '%{http_code}' $SERVER/.well-known/jmap)" != 000 ] && return 0; sleep 2; done; return 1; } echo "==> the server, on its own" OUT="$(/tmp/inbuxa install --local --domain example.test --server host --console skip --webmail skip --yes 2>&1)"; rc=$? if [ $rc -eq 0 ]; then ok "installed the server alone"; else bad "server install exit $rc"; echo "$OUT" | tail -15 | sed 's/^/ /' journalctl -u inbuxa-server -n 30 --no-pager -o cat | sed 's/^/ | /' echo; echo "==> $pass passed, $fail failed (stopped: no server to join)"; exit 1 fi [ -n "${INBUXA_SERVER_BINARY:-}" ] && echo " (server: $(/usr/local/bin/inbuxa-server --version 2>&1 | head -1), from the build given)" echo echo "==> an API key, as an administrator makes one in the console" # The console is a browser; here the server takes the administrator's # password for one restart, the way the installer's own setup does. AUSER=$(awk '/^administrator/ {print $2}' "$SERVER_DIR/credentials.txt") APASS=$(awk '/^administrator/{getline; print $2}' "$SERVER_DIR/credentials.txt") echo "INBUXA_HTTP_BASIC_AUTH=all" >> "$SERVER_DIR/server.env" systemctl restart inbuxa-server; wait_server EXPIRES=$(date -u -d '+1 hour' +%Y-%m-%dT%H:%M:%SZ) ANSWER=$(jmap "$AUSER:$APASS" urn:inbuxa:jmap:registry "[[\"x:ApiKey/set\",{\"create\":{\"k\":{\"description\":\"join test\",\"expiresAt\":\"$EXPIRES\",\"permissions\":{\"@type\":\"Inherit\"}}}},\"0\"]]") KEY=$(sed -n 's/.*"secret":"\([^"]*\)".*/\1/p' <<<"$ANSWER") sed -i '/^INBUXA_HTTP_BASIC_AUTH=/d' "$SERVER_DIR/server.env" systemctl restart inbuxa-server; wait_server [ -n "$KEY" ] && ok "an API key for the administrator" || { bad "no API key"; head -c 300 <<<"$ANSWER" | sed 's/^/ /'; } ACCOUNT=$(curl -s -H "Authorization: Bearer $KEY" $SERVER/jmap/session | grep -o '"primaryAccounts":{[^}]*}' | sed -n 's/^"primaryAccounts":{"[^"]*":"\([^"]*\)".*/\1/p') [ -n "$ACCOUNT" ] || bad "no account id in the key's session" # From here this is "another machine": no install recorded on it. mv /etc/inbuxa/install.json /etc/inbuxa/install.server.json # From here this is "another machine": no install recorded on it. command -v python3 >/dev/null || { bad "no python3 on this machine to drive a terminal with"; echo "==> $pass passed, $fail failed"; exit 1; } cat > /tmp/drive-join.py <<'PY' import os, pty, re, select, struct, sys, time, fcntl, termios KEY = os.environ.pop("KEY") pid, fd = pty.fork() if pid == 0: os.environ["TERM"] = "xterm-256color" os.execv("/tmp/inbuxa", ["/tmp/inbuxa"]) fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack("HHHH", 32, 110, 0, 0)) seen = "" def plain(s): return re.sub(r"\x1b\[[0-9;?]*[A-Za-z]", "", s) def pump(t): global seen end = time.time() + t while time.time() < end: r, _, _ = select.select([fd], [], [], 0.1) if r: try: seen += os.read(fd, 65536).decode("utf-8", "replace") except OSError: return False return True def frame(): return plain(seen.split("\x1b[H")[-1]) def wait_for(text, timeout, label): end = time.time() + timeout while time.time() < end: if text in " ".join(frame().replace("|", " ").split()): print("OK", label); return True if not pump(0.5): break print("FAIL", label, "-- never saw:", text) print("\n".join(" | " + l for l in frame().split("\r\n")[:30])) return False def wait_done(timeout, label): # Done, or Stopped with the reason: either way the screen has its answer. end = time.time() + timeout while time.time() < end: words = " ".join(frame().replace("|", " ").split()) if "Done." in words: print("OK", label); return True if "Stopped:" in words: break if not pump(0.5): break print("FAIL", label, "-- the screen says:") print("\n".join(" | " + l for l in frame().split("\r\n")[:30])) return False def keys(s): os.write(fd, s.encode()); pump(0.4) ENTER, DOWN, CTRL_U = "\r", "\x1b[B", "\x15" wait_for("What this machine allows", 30, "screen 1: what it found") keys(ENTER) wait_for("Where it will be reached", 10, "screen 2: names") keys(DOWN * 5 + CTRL_U + "/var/lib/inbuxa-joined" + DOWN + " ") wait_for("example.test", 5, "loopback, in a directory of its own") keys(ENTER) wait_for("What goes on this machine", 10, "screen 3: components") keys("s") keys(ENTER) wait_for("The server these front ends are for", 10, "skipping the server leads to the Server screen") wait_for("Server › Reach", 5, "and the steps say so") keys("http://127.0.0.1:8081" + DOWN + KEY) print("FAIL the key is on screen" if KEY in plain(seen) else "OK the key is typed, not shown") keys(DOWN + ENTER) wait_for("The key works, and the server can take these front ends", 30, "the server is asked before the plan") wait_for("publishes nothing", 10, "then reach, on loopback") keys(ENTER) wait_for("Nothing has happened yet", 10, "the plan") wait_for("Register these front ends with the server", 5, "the plan registers them with the server") keys("\x1b[6~" * 4) wait_for("inbuxa join http://127.0.0.1:8081 --key - --local --domain example.test --dir /var/lib/inbuxa-joined --install-deps --yes", 5, "the plan shows the join command, with --key -") keys(ENTER) wait_for("Join now?", 5, "a confirmation before anything happens") print("OK nothing joined before the y" if not os.path.exists("/var/lib/inbuxa-joined/compose.yaml") else "FAIL something was written before the y") keys("y") wait_done(1500, "the join finishes on screen") keys("q") pump(2) after = plain(seen.split("\x1b[?1049l")[-1]) if "\x1b[?1049l" in seen else "" print("OK the terminal is given back" if "\x1b[?1049l" in seen and "\x1b[?25h" in seen else "FAIL the terminal was not restored") print("OK the summary says where it joined" if "joined to" in after and "127.0.0.1:8081" in after else "FAIL no join in the summary: " + repr(after[-300:])) print("OK and that the key wasn't kept" if "not kept here" in after else "FAIL the summary doesn't say the key wasn't kept") print("FAIL the key appeared on screen at some point" if KEY in plain(seen) else "OK the key never appeared on screen") _, status = os.waitpid(pid, 0) code = os.waitstatus_to_exitcode(status) print(("OK" if code == 0 else "FAIL") + f" exit status {code}") PY echo echo "==> joining from the interface" OUT="$(KEY="$KEY" python3 /tmp/drive-join.py 2>&1)" while IFS= read -r line; do case "$line" in OK\ *) ok "${line#OK }" ;; FAIL\ *) bad "${line#FAIL }" ;; *) echo "$line" ;; esac done <<<"$OUT" echo echo "==> what it joined" grep -q "$KEY" <<<"$(cat $DIR/* 2>/dev/null)" && bad "the API key was written down here" || ok "the API key was not kept" STATE="$(compose ps --format '{{.Service}} {{.State}}')" for s in console webmail; do grep -q "^$s running" <<<"$STATE" && ok "$s is running" || { bad "$s is not running"; echo "$STATE" | sed 's/^/ /'; } done grep -q "^ server:" "$DIR/compose.yaml" && bad "a server was put here too" || ok "no server here" grep -q '"serverUrl": "http://127.0.0.1:8081"' /etc/inbuxa/install.json && ok "the install record names the server it joined" || bad "no serverUrl in the install record" echo "==> the server knows the front ends" FE=$(jmap "$KEY" urn:inbuxa:jmap '[["inbuxa:FrontEnds/get",{"accountId":"'"$ACCOUNT"'"},"0"]]') grep -q '"adminUrl":"http://127.0.0.1:8082"' <<<"$FE" && ok "the console's address" || bad "adminUrl: $FE" grep -q '"webmailUrl":"http://127.0.0.1:8080"' <<<"$FE" && ok "the webmail's address" || bad "webmailUrl: $FE" grep -q '"webmailClientSecretSet":true' <<<"$FE" && ok "and a webmail secret, which it does not show" || bad "no webmail secret: $FE" ALLOW=$(curl -s -o /dev/null -D - -X OPTIONS $SERVER/jmap/session -H 'Origin: http://127.0.0.1:8082' \ -H 'Access-Control-Request-Method: GET' -H 'Access-Control-Request-Headers: authorization' | tr -d '\r' | awk -F': ' 'tolower($1)=="access-control-allow-origin"{print $2}') [ "$ALLOW" = "http://127.0.0.1:8082" ] && ok "the console's origin may call the server" || bad "access-control-allow-origin: '$ALLOW'" curl -fsS http://127.0.0.1:8082/ 2>/dev/null | grep -q "content=\"$SERVER\"" && ok "the console points at the server" || bad "the console's api-base-url is not $SERVER" echo echo "==> a person signs in to the joined webmail" USER=$(awk '/^first mailbox/ {print $3}' "$SERVER_DIR/credentials.txt") PASS=$(awk '/^first mailbox/{getline; print $2}' "$SERVER_DIR/credentials.txt") rm -f /tmp/jar LOC=$(curl -s -o /dev/null -c /tmp/jar -w '%{redirect_url}' "http://127.0.0.1:8080/api/auth/oauth/start?username=$USER&remember=1") grep -q "^$SERVER/" <<<"$LOC" && ok "the webmail sends sign-in to the server" || bad "sign-in went to '${LOC:-nowhere}'" q() { sed -n "s/.*[?&]$1=\([^&]*\).*/\1/p" <<<"$LOC"; } urldecode() { printf '%b' "${1//%/\\x}"; } STATE=$(q state); CHALLENGE=$(q code_challenge); REDIRECT=$(urldecode "$(q redirect_uri)") ANSWER=$(curl -s -X POST $SERVER/api/auth -H 'Content-Type: application/json' -H 'Accept: application/json' \ -d "{\"type\":\"authCode\",\"accountName\":\"$USER\",\"accountSecret\":\"$PASS\",\"clientId\":\"ihasmail-inbuxa\",\"redirectUri\":\"$REDIRECT\",\"codeChallenge\":\"$CHALLENGE\",\"codeChallengeMethod\":\"S256\"}") AUTHCODE=$(sed -n 's/.*"client_\{0,1\}[cC]ode":"\([^"]*\)".*/\1/p' <<<"$ANSWER") [ -n "$AUTHCODE" ] && ok "the server takes the joined webmail's redirect" || { bad "the server's sign-in answered"; head -c 200 <<<"$ANSWER" | sed 's/^/ /'; } BACK=$(curl -s -o /dev/null -b /tmp/jar -c /tmp/jar -w '%{redirect_url}' "$REDIRECT?code=$AUTHCODE&state=$STATE") grep -q "signin_error" <<<"$BACK" && bad "the webmail's code exchange failed (its secret?): $BACK" || ok "the webmail exchanges the code with the secret made here" CODE=$(curl -s -o /tmp/login.json -b /tmp/jar -w '%{http_code}' -H 'X-Requested-With: ihasmail' http://127.0.0.1:8080/api/auth/session) [ "$CODE" = 200 ] && grep -q "urn:ietf:params:jmap:mail" /tmp/login.json && ok "signed in, with mail" || { bad "the session answered $CODE"; head -c 200 /tmp/login.json | sed 's/^/ /'; } echo echo "==> $pass passed, $fail failed" [ "$fail" -eq 0 ]