#!/bin/bash # # Purpose : The Matrix server Hermes' gateway talks to, and whether you've let # its sandbox reach it. Source this file; used by configure agents, # configure hermes and sandbox/agent. Needs CONNECTOR. # # Hermes' settings (~/.hermes/.env) name the server, but Hermes can change them # from inside its sandbox, so they say only what Hermes wants to reach. The # server it may reach is in your own hermes.json, which no sandbox can write # (or read); configure hermes asks you before adding it there. HERMES_ENV="$HOME/.hermes/.env" HERMES_SANDBOX_CONF="$HOME/.config/federated-connector/sandbox/hermes.json" HERMES_GATEWAY_UNIT="$HOME/.config/systemd/user/hermes-gateway.service" HERMES_GATEWAY_DROPIN="$HERMES_GATEWAY_UNIT.d/federated-connector-sandbox.conf" # A plain host name or IPv4 address, so it can't be a wildcard or carry # characters that would change what a terminal shows when it's asked about hermes_valid_host() { [[ "$1" =~ ^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$ ]] } # The host in MATRIX_HOMESERVER, e.g. matrix.example.org, or nothing hermes_homeserver() { local host host="$(sed -nE "s#^MATRIX_HOMESERVER=[\"']?https?://([^/:\"']+).*#\1#p" "$HERMES_ENV" 2> /dev/null | head -n 1)" hermes_valid_host "$host" && echo "$host" } # hermes_homeserver_allowed HOST: true if Hermes' sandbox may reach HOST hermes_homeserver_allowed() { [ -n "$1" ] && jq -e --arg host "$1" '(.network.allowedDomains // []) | index($host) != null' \ "$HERMES_SANDBOX_CONF" > /dev/null 2>&1 } # hermes_allow_homeserver HOST: let Hermes' sandbox reach HOST hermes_allow_homeserver() { source "$CONNECTOR/configure.d/lib/json.sh" edit_json "$HERMES_SANDBOX_CONF" '{}' --arg host "$1" \ '.network.allowedDomains = ((.network.allowedDomains // []) - [$host] + [$host])' } # The gateway's drop-in used to allow it whatever server Hermes' settings # named, through FEDERATED_SANDBOX_DOMAINS. Carry over the server an old # drop-in allows, so a gateway that works keeps working until configure agents # rewrites the drop-in without it. The drop-in is read, not the variable: the # sandbox can't write the drop-in, but an agent's children inherit the variable. hermes_carry_over_homeserver() { local host [ -f "$HERMES_GATEWAY_DROPIN" ] || return 0 host="$(sed -nE 's#^Environment="?FEDERATED_SANDBOX_DOMAINS=([^" ]*)"?$#\1#p' "$HERMES_GATEWAY_DROPIN" | head -n 1)" hermes_valid_host "$host" || return 0 hermes_homeserver_allowed "$host" && return 0 hermes_allow_homeserver "$host" }