#!/usr/bin/env bash # Publish locally built MARS clients and the active gateway's address. No vendor binaries. set -euo pipefail export PATH=/usr/sbin:/usr/bin:/sbin:/bin umask 077 fail(){ echo "Windows clients: $*" >&2; exit 1; } [[ $EUID == 0 ]] || fail 'run as root' [[ $# == 1 ]] || fail 'usage: mars-publish-winclient --config-only OR /path/to/trusted/build-output' conf=${MARS_CONF:-/usr/local/etc/mars_nwe/nwserv.conf} gw=${MARS_GATEWAY_CONFIG:-/usr/local/etc/mars_nwe/ipxpkt.json} interfaces=${MARS_IPX_INTERFACES:-/run/mars-pcap/ipx_interface} command -v jq >/dev/null || fail 'install jq' [[ -f $conf && -f $gw && -f $interfaces ]] || fail 'configure/start the gateway first' mapfile -t roots < <(awk '$1==1 && toupper($2)=="SYS" {print $3}' "$conf") [[ ${#roots[@]} == 1 && ${roots[0]} == /* ]] || fail 'exactly one SYS volume required' sys=$(realpath -e -- "${roots[0]}") || fail 'missing SYS' secure(){ [[ -d $1 && ! -L $1 && $(stat -c %u -- "$1") == 0 ]] && (( (8#$(stat -c %a -- "$1") & 022) == 0 )); } [[ $sys != / ]] && secure "$sys" || fail 'unsafe SYS directory' public=''; for d in "$sys/PUBLIC" "$sys/public"; do if [[ -e $d || -L $d ]]; then [[ -z $public ]] || fail 'ambiguous PUBLIC case'; public=$d; fi; done [[ -n $public ]] && secure "$public" || fail 'initialize root-owned PUBLIC first' for d in "$public/CLIENTS" "$public/clients"; do [[ ! -e $d && ! -L $d ]] || fail 'CLIENTS already exists; back up and move it aside before republishing'; done net=$(jq -er 'select(.enabled == true) | .network | strings' "$gw") || fail 'gateway disabled or invalid configuration' [[ $net =~ ^0[xX]([0-9a-fA-F]{1,8})$ ]] || fail 'invalid IPX network' hex=${BASH_REMATCH[1]}; net=$(printf '%08X' "$((16#$hex))") ip=$(jq -er '.gateway | strings' "$gw") [[ $ip =~ ^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.1$ ]] || fail 'invalid gateway IPv4' for x in "${BASH_REMATCH[@]:1}"; do ((10#$x <= 255)) || fail 'invalid gateway IPv4'; done mapfile -t macs < <(awk -v n="$net" 'toupper($1)==n && $5~/^(Ethernet_II|802\.3|802\.2|SNAP)$/ {print toupper($2)}' "$interfaces") [[ ${#macs[@]} == 1 ]] || fail 'selected IPX network is not uniquely present in dispatcher state' mac=${macs[0]}; [[ $mac =~ ^[0-9A-F]{12}$ && $mac != 000000000000 ]] && (( (16#${mac:0:2} & 1) == 0 )) || fail 'invalid gateway MAC' files=(); if [[ $1 != --config-only ]]; then [[ -d $1 ]] || fail 'supply a trusted local build-output directory' for f in IPXDIAG.EXE MARSNET.EXE MARSTAP.EXE MARSIPX.EXE MARSNDIS.SYS; do if [[ -f $1/$f ]]; then [[ $(head -c2 "$1/$f") == MZ ]] || fail "$f is not a PE/DOS executable"; files+=("$f"); fi done (( ${#files[@]} > 0 )) || fail 'no compiled client binaries found' if [[ -f $1/MARSIPX.EXE || -f $1/MARSNDIS.SYS || -f $1/MARSNDIS.INF ]]; then [[ -f $1/MARSIPX.EXE && -f $1/MARSNDIS.SYS && -f $1/MARSNDIS.INF ]] || fail 'native client requires EXE, SYS and INF together' files+=(MARSNDIS.INF) fi if [[ -f $1/MARSNET3.SYS || -f $1/MARSNET.INF ]]; then [[ ${MARS_ALLOW_PREVIEW:-0} == 1 ]] || fail "Legacy driver is experimental; use MARS_ALLOW_PREVIEW=1 only for isolated VM testing" [[ -f $1/MARSNET.EXE && -f $1/MARSNET3.SYS && -f $1/MARSNET.INF ]] || fail "legacy client requires EXE, SYS and INF together" [[ $(head -c2 "$1/MARSNET3.SYS") == MZ ]] || fail "invalid legacy SYS" files+=(MARSNET3.SYS MARSNET.INF) fi fi stage=$(mktemp -d "$public/.clients-stage-XXXXXXXX"); trap 'rm -rf -- "$stage"' EXIT for os in WIN95 WIN98 WINME WINNT40 WIN2000 WINXP; do d="$stage/$os"; mkdir "$d" printf '[gateway]\r\nnetwork=%s\r\nserver=%s\r\nport=6181\r\nip=%s\r\n' "$net" "$mac" "$ip" > "$d/GATEWAY.INI" printf '%s\r\n' 'Experimental MARS Windows IPXPKT client.' 'Copy files to a local directory before installing or running.' 'No Windows driver compatibility has been validated.' 'External legacy drivers are not supplied.' 'Read WINDOWS.md in the source package. Do not run against a physical adapter.' > "$d/README.TXT" for f in "${files[@]}"; do case $f in MARSNET3.SYS|MARSNET.INF) [[ $os == WIN95 || $os == WIN98 || $os == WINME ]] || continue;; MARSNET.EXE) if [[ -f $1/MARSNET3.SYS ]]; then [[ $os == WIN95 || $os == WIN98 || $os == WINME ]] || continue; fi;; IPXDIAG.EXE) ;; *) [[ $os == WIN2000 || $os == WINXP ]] || continue;; esac install -m 0644 -- "$1/$f" "$d/$f" done done name=CLIENTS if [[ $public == */public ]]; then name=clients; while IFS= read -r -d '' p; do b=${p##*/}; mv -- "$p" "${p%/*}/${b,,}"; done < <(find "$stage" -mindepth 1 -depth -print0); fi find "$stage" -type f -exec chmod 644 {} +; find "$stage" -type d -exec chmod 755 {} + mv -T -- "$stage" "$public/$name"; trap - EXIT printf 'Published SYS:PUBLIC/CLIENTS (IPX %s, node %s, gateway %s). Verify read/file-scan trustees; deny writes.\n' "$net" "$mac" "$ip"