|
| 1 | +#!/usr/bin/bash |
| 2 | +# |
| 3 | +# Copyright (c) [2026] SUSE LLC |
| 4 | +# |
| 5 | +# All Rights Reserved. |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or modify it |
| 8 | +# under the terms of the GNU General Public License as published by the Free |
| 9 | +# Software Foundation; either version 2 of the License, or (at your option) |
| 10 | +# any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 | +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 | +# more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License along |
| 18 | +# with this program; if not, contact SUSE LLC. |
| 19 | +# |
| 20 | +# To contact SUSE LLC about this file by physical or electronic mail, you may |
| 21 | +# find current contact information at www.suse.com. |
| 22 | + |
| 23 | +# This script is a wrapper for the Agama web server, it evaluates to which |
| 24 | +# addresses the server should listen to. |
| 25 | + |
| 26 | +if [[ "$1" == "-h" || "$1" == "--help" ]]; then |
| 27 | + echo "Usage: $0" |
| 28 | + echo |
| 29 | + echo " This is a wrapper script for the Agama web server (agama-web-server)." |
| 30 | + echo |
| 31 | + echo " It configures the listening addresses for the web server based on" |
| 32 | + echo " the \"inst.listen_on\" boot option." |
| 33 | + exit 0 |
| 34 | +fi |
| 35 | + |
| 36 | +# the default options: listen on all interfaces for both HTTP and HTTPS ports, |
| 37 | +# the IPv4 addresses are fallbacks when IPv6 is disabled with the |
| 38 | +# "ipv6.disable=1" kernel boot option |
| 39 | +DEFAULT_OPTIONS=(--address ":::80,0.0.0.0:80" --address ":::443,0.0.0.0:443") |
| 40 | +# options for localhost access only |
| 41 | +LOCAL_OPTIONS=(--address "::1:80,127.0.0.1:80" --address "::1:443,127.0.0.1:443") |
| 42 | + |
| 43 | +# check if the "inst.listen_on=" boot option was used |
| 44 | +if grep -q "\binst.listen_on=.\+" /run/agama/cmdline.d/agama.conf; then |
| 45 | + LISTEN_ON=$(grep "\binst.listen_on=.\+" /run/agama/cmdline.d/agama.conf | sed 's/.*\binst.listen_on=\([^[:space:]]\+\)/\1/') |
| 46 | + |
| 47 | + if [ "$LISTEN_ON" = "localhost" ]; then |
| 48 | + OPTIONS=("${LOCAL_OPTIONS[@]}") |
| 49 | + elif [ "$LISTEN_ON" = "all" ]; then |
| 50 | + OPTIONS=("${DEFAULT_OPTIONS[@]}") |
| 51 | + else |
| 52 | + # always run on the localhost |
| 53 | + OPTIONS=("${LOCAL_OPTIONS[@]}") |
| 54 | + |
| 55 | + # the string can contain multiple addresses separated by comma, |
| 56 | + # replace commas with spaces and iterate over items |
| 57 | + ADDRESSES=${LISTEN_ON//,/ } |
| 58 | + for ADDRESS in $ADDRESSES; do |
| 59 | + # check if the value is an IP address (IPv6, IPv6 link local or IPv4) |
| 60 | + if echo "$ADDRESS" | grep -qE '^[0-9a-fA-F:]+$|^[fF][eE]80|^([0-9]{1,3}\.){3}[0-9]{1,3}$'; then |
| 61 | + echo "<5>Listening on IP address ${ADDRESS}" |
| 62 | + OPTIONS+=(--address "${ADDRESS}:80" --address "${ADDRESS}:443") |
| 63 | + else |
| 64 | + # otherwise assume it is as an interface name |
| 65 | + if ip addr show dev "${ADDRESS}" >/dev/null 2>&1; then |
| 66 | + # find the IP address for the specified interface |
| 67 | + IP_ADDRS=$(ip -o addr show dev "${ADDRESS}" | awk '{print $4}' | cut -d/ -f1) |
| 68 | + if [ -n "${IP_ADDRS}" ]; then |
| 69 | + for IP in $IP_ADDRS; do |
| 70 | + # append the %device for link local IPv6 addresses |
| 71 | + if [[ "$IP" == fe80* ]]; then |
| 72 | + IP="${IP}%${ADDRESS}" |
| 73 | + fi |
| 74 | + echo "<5>Listening on interface ${ADDRESS} with IP address ${IP}" |
| 75 | + OPTIONS+=(--address "${IP}:80" --address "${IP}:443") |
| 76 | + done |
| 77 | + else |
| 78 | + echo "<3>IP address for interface ${ADDRESS} not found" |
| 79 | + fi |
| 80 | + else |
| 81 | + echo "<3>Network Interface ${ADDRESS} not found" |
| 82 | + fi |
| 83 | + fi |
| 84 | + done |
| 85 | + fi |
| 86 | +else |
| 87 | + OPTIONS=("${DEFAULT_OPTIONS[@]}") |
| 88 | +fi |
| 89 | + |
| 90 | +if [ "${OPTIONS[*]}" = "${DEFAULT_OPTIONS[*]}" ]; then |
| 91 | + echo "<5>Listening on all network interfaces" |
| 92 | +elif [ "${OPTIONS[*]}" = "${LOCAL_OPTIONS[*]}" ]; then |
| 93 | + echo "<5>Disabling remote access to the Agama web server" |
| 94 | +fi |
| 95 | + |
| 96 | +echo "<5>Starting Agama web server with options: ${OPTIONS[*]}" |
| 97 | +exec /usr/bin/agama-web-server serve "${OPTIONS[@]}" |
0 commit comments