#!/usr/bin/env bash # Publish trusted FreeDOS mTCP and Crynwr ZIPs, preserving their original archives. # No client-supplied paths, no ZIP extraction into live SYS, no overwrite. set -euo pipefail export PATH=/usr/sbin:/usr/bin:/sbin:/bin umask 077 fail(){ echo "mTCP: $*" >&2; exit 1; } [[ $EUID == 0 ]] || fail 'run as root' conf=${MARS_CONF:-/usr/local/etc/mars_nwe/nwserv.conf} [[ -f $conf ]] || fail 'nwserv.conf missing' mapfile -t roots < <(awk '$1==1 && toupper($2)=="SYS" {print $3}' "$conf") [[ ${#roots[@]} == 1 && ${roots[0]} == /* ]] || fail 'exactly one SYS with absolute path required' sys=$(realpath -e -- "${roots[0]}") || fail 'SYS path missing' [[ $sys != / ]] || fail 'SYS cannot be /' secure_dir(){ [[ -d $1 && ! -L $1 && $(stat -c %u -- "$1") == 0 ]] && (( (8#$(stat -c %a -- "$1") & 022) == 0 )); } secure_dir "$sys" || fail 'SYS must be root-owned and not group/world writable' 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_dir "$public" || fail 'initialize root-owned SYS:PUBLIC first' [[ ! -e $public/MTCP && ! -L $public/MTCP && ! -e $public/mtcp && ! -L $public/mtcp ]] || fail 'MTCP already exists; preserve existing installation, move it aside deliberately before updating' command -v unzip >/dev/null || fail 'install unzip' work=$(mktemp -d); stage=''; trap 'rm -rf -- "$work"; if [[ -n $stage ]]; then rm -rf -- "$stage"; fi' EXIT if [[ $# == 1 && $1 == --download ]]; then command -v curl >/dev/null || fail 'install curl' base=https://ibiblio.org/pub/micro/pc-stuff/freedos/files/repositories/latest/net curl --fail --silent --show-error --location --proto '=https' --proto-redir '=https' --max-time 120 "$base/mtcp/20250604.0/mtcp.zip" -o "$work/MTCP.ZIP" curl --fail --silent --show-error --location --proto '=https' --proto-redir '=https' --max-time 120 "$base/crynwr.zip" -o "$work/CRYNWR.ZIP" # Published FreeDOS checksums (integrity/version pin, not a digital signature). (cd "$work"; printf '%s\n' 'bb747d0163519a946c5808bd38a49a6680db39c1 MTCP.ZIP' 'c72d97e1cdc05ca6bf9910d01bda54ba661a34c6 CRYNWR.ZIP' | sha1sum -c -) || fail 'archive hash changed; review upstream before manual import' elif [[ $# == 2 ]]; then [[ -f $1 && -f $2 ]] || fail 'supply two trusted ZIP files' cp -- "$1" "$work/MTCP.ZIP"; cp -- "$2" "$work/CRYNWR.ZIP" else fail 'usage: sudo ./publish-mtcp.sh --download OR mtcp.zip crynwr.zip'; fi stage=$(mktemp -d "$public/.mtcp-stage-XXXXXXXX") # Select unique basename, extract only exact safe paths. Archive never unpacked wholesale. extract(){ local zip=$1 name=$2 required=$3; local -a matches; mapfile -t matches < <(unzip -Z1 "$zip" | awk -F/ -v name="$name" 'toupper($NF)==name {print}'); if (( ${#matches[@]} == 0 )) && [[ $required == optional ]]; then return; fi; (( ${#matches[@]} == 1 )) || fail "missing or ambiguous $name"; local ent=${matches[0]}; [[ $ent =~ ^[A-Za-z0-9_./+-]+$ && $ent != /* && $ent != *..* ]] || fail "unsafe ZIP member $ent"; unzip -p "$zip" "$ent" > "$stage/$name" || fail "cannot extract $name"; [[ -s $stage/$name ]] || fail "empty $name"; } extract "$work/CRYNWR.ZIP" IPXPKT.COM required for name in DHCP.EXE PING.EXE; do extract "$work/MTCP.ZIP" "$name" required; done for name in FTP.EXE FTPSRV.EXE TELNET.EXE HTGET.EXE SNTP.EXE NC.EXE IRCJR.EXE NETDRIVE.EXE; do extract "$work/MTCP.ZIP" "$name" optional; done cp "$work/MTCP.ZIP" "$work/CRYNWR.ZIP" "$stage/" printf 'PACKETINT 0x60\r\nHOSTNAME DOSCLIENT\r\n' > "$stage/MTCP.CFG" # Run in the actual DOS command interpreter after LOGIN; environment survives CALL. printf '%s\r\n' '@ECHO OFF' 'IF EXIST C:\MTCP\MTCP.CFG GOTO READY' 'IF NOT EXIST C:\MTCP\NUL MD C:\MTCP' 'COPY T:\MTCP.CFG C:\MTCP\MTCP.CFG' ':READY' 'SET MTCPCFG=C:\MTCP\MTCP.CFG' 'T:\IPXPKT.COM -i 0x60' 'ECHO Run T:\DHCP.EXE after the server approves this MAC.' > "$stage/SETUP.BAT" printf '%s\r\n' '@ECHO OFF' 'CALL T:\SETUP.BAT' 'T:\DHCP.EXE' > "$stage/START.BAT" printf '%s\r\n' 'MAP ROOT T:=SYS:PUBLIC\MTCP' > "$stage/MAP.TXT" printf '%s\r\n' 'MARS NetWare mTCP distribution' 'Map T: to SYS:PUBLIC\MTCP in the NetWare login script.' 'Run CALL T:\SETUP.BAT once per boot, then T:\DHCP.EXE.' 'Change PACKETINT if 0x60 is already in use.' 'Keep C:\MTCP\MTCP.CFG local, never share writable configuration.' 'Original FreeDOS archives preserved. See their licensing and sources.' 'mTCP: https://github.com/mbbrutman/mTCP' 'Crynwr: https://github.com/fragglet/crynwr_mirror' > "$stage/README.TXT" # Match volume case convention used by the original server. name=MTCP if [[ $public == */public ]]; then name=mtcp; for f in "$stage"/*; do b=${f##*/}; mv -- "$f" "$stage/${b,,}"; done; fi chmod 644 "$stage"/*; chmod 755 "$stage"; mv -T -- "$stage" "$public/$name"; stage='' printf 'Published %s (SYS:PUBLIC/MTCP). Add MAP ROOT T:=SYS:PUBLIC\\MTCP to login script. Verify NetWare read/file-scan trustees and deny write.\n' "$public/$name"