tls-names.sh (4174B)
1 #!/bin/bash 2 # This file is part of GNU Taler 3 # (C) 2026 Taler Systems S.A. 4 # 5 # GNU Taler is free software; you can redistribute it and/or modify it under the 6 # terms of the GNU General Public License as published by the Free Software 7 # Foundation; either version 3, or (at your option) any later version. 8 # 9 # GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 # A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 # 13 # You should have received a copy of the GNU General Public License along with 14 # GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 # 16 # Make one emulator resolve the hostnames the wallet hard-codes to the TLS 17 # front end on the host. 18 # 19 # The wallet hard-codes https://taldir.gnunet.org (see 20 # findings/alias-registration-hardcodes-the-directory.md) and defaults to 21 # https://mailbox.gnunet.org, so the only way to exercise those code paths as 22 # they ship is to answer for the names. The URLs under test are not touched. 23 # 24 # The simulator's CA is trusted because the "sim" build type bundles it as a 25 # trust anchor (wallet/src/sim/res/xml/network_security_config.xml), not by 26 # anything done here. Installing it into the device's own store instead is a 27 # dead end worth recording: both system stores have to be replaced by a tmpfs 28 # from inside init's mount namespace (an APEX mount made from adbd is invisible 29 # to apps, which shows up as "tls: unknown certificate" in the proxy log while 30 # everything else looks right), and the framework restart that is then needed 31 # to make zygote see them leaves the network at PARTIAL_CONNECTIVITY for a 32 # while -- during which the wallet refuses to do anything with "This action 33 # requires internet connection". 34 # 35 # Usage: tls-names.sh <worker> 36 # 37 # Needs the TLS front end running on the host (tls/tlsproxy, port 8443) and is 38 # undone by a `wipe`/reboot of the emulator, so it has to be re-run after one. 39 40 set -eu 41 42 SIM_ROOT=${SIM_ROOT:-/opt/taler-sim} 43 export ANDROID_SDK_ROOT=${ANDROID_SDK_ROOT:-/opt/android-sdk} 44 ADB=$ANDROID_SDK_ROOT/platform-tools/adb 45 WORKER=${1:?usage: tls-names.sh <worker>} 46 SERIAL=emulator-$((5554 + 2 * WORKER)) 47 HOSTS_ENTRY="127.0.0.1 taldir.gnunet.org mailbox.gnunet.org" 48 PROXY_PORT=${SIM_TLS_PORT:-8443} 49 50 say() { printf 'tls-names %s: %s\n' "$SERIAL" "$*" >&2; } 51 52 # 1. A privileged listening port on the device needs a root adbd. Restarting 53 # adbd takes every `adb reverse` mapping with it, so the stack's ports have 54 # to be put back afterwards or the wallet loses the exchange -- and a wallet 55 # that cannot reach the exchange reproduces unrelated findings very 56 # convincingly. 57 if [ "$("$ADB" -s "$SERIAL" shell id -u | tr -d '\r\n')" = 0 ]; then 58 say "adbd is already root" 59 else 60 "$ADB" -s "$SERIAL" root >/dev/null 2>&1 || true 61 sleep 4 62 "$ADB" -s "$SERIAL" wait-for-device 63 "$(dirname "$0")/emu.sh" reverse "$WORKER" >/dev/null 2>&1 || 64 say "WARNING: could not restore the stack's port mappings" 65 say "rooted, stack ports restored" 66 fi 67 68 # 2. The name has to resolve to the device's own loopback, where the reverse 69 # mapping is listening. /system is read-only on these images and `adb 70 # remount` does not help (no overlay), so the file is replaced by a bind 71 # mount rather than edited -- a mount needs no write access to /system. 72 if "$ADB" -s "$SERIAL" shell "grep -q taldir.gnunet.org /system/etc/hosts" 2>/dev/null; then 73 say "hosts entry already there" 74 else 75 "$ADB" -s "$SERIAL" shell " 76 set -e 77 cp /system/etc/hosts /data/local/tmp/hosts 78 echo '$HOSTS_ENTRY' >> /data/local/tmp/hosts 79 chmod 644 /data/local/tmp/hosts 80 chcon u:object_r:system_file:s0 /data/local/tmp/hosts 2>/dev/null || true 81 mount --bind /data/local/tmp/hosts /system/etc/hosts 82 grep taldir /system/etc/hosts 83 " >/dev/null || { say "could not bind a hosts file over /system/etc/hosts"; exit 1; } 84 say "hosts entry bind-mounted" 85 fi 86 87 # 3. Port 443 on the device forwards to the host's TLS front end. 88 "$ADB" -s "$SERIAL" reverse "tcp:443" "tcp:$PROXY_PORT" >/dev/null 89 say "device port 443 -> host $PROXY_PORT; done"