This commit is contained in:
randogoth 2025-10-18 20:34:07 +03:00
parent daae8bd74e
commit eca967a494
3 changed files with 162 additions and 138 deletions

View file

@ -16,7 +16,6 @@ RUN apt-get update && \
unzip \ unzip \
file \ file \
&& add-apt-repository -y ppa:dosemu2/ppa && \ && add-apt-repository -y ppa:dosemu2/ppa && \
apt-get update && \
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
dosemu2 \ dosemu2 \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*

View file

@ -1,57 +1,128 @@
#!/bin/bash #!/bin/bash
# /usr/local/bin/dos-shell # /usr/local/bin/dos-shell
# 1) reset C: drive contents to only the allowed files # Prepare the SvarDOS environment and launch dosemu.
# 2) launch dosemu with /cdrive mounted as C:
set -euo pipefail set -euo pipefail
ALLOWED_LIST="/etc/dos_allowed" ALLOWED_LIST="/etc/dos_allowed"
ALLOWED_REPO="/opt/allowed_repo" # repository of allowed DOS files stored in container ALLOWED_REPO="/opt/allowed_repo"
C_DRIVE="/cdrive"
ENV_DIR="/etc/dos_env" ENV_DIR="/etc/dos_env"
AUTOEXEC_TEMPLATE="${ENV_DIR}/AUTOEXEC.BAT" AUTOEXEC_TEMPLATE="${ENV_DIR}/AUTOEXEC.BAT"
CONFIG_TEMPLATE="${ENV_DIR}/CONFIG.SYS" CONFIG_TEMPLATE="${ENV_DIR}/CONFIG.SYS"
SVARDOS_BASE="/opt/svardos/base" SVARDOS_ROOT="${SVARDOS_ROOT:-/opt/svardos}"
ALLOW_MODE="${DOS_ALLOW_MODE:-all}" # default to exposing everything from the repo SVARDOS_BASE="${SVARDOS_BASE:-${SVARDOS_ROOT}/base}"
ALLOW_MODE="${DOS_ALLOW_MODE:-all}"
DEFAULT_DOS_USER="dosuser"
INSTALL_SENTINEL=".svardos_installed"
if [ "$(id -u)" -eq 0 ]; then
DOS_USER="${DEFAULT_DOS_USER}"
DOS_HOME="$(getent passwd "${DOS_USER}" | cut -d: -f6)"
else
DOS_USER="$(id -un)"
DOS_HOME="${HOME}"
fi
if [ -z "${DOS_HOME}" ]; then
echo "Unable to determine home directory for ${DOS_USER}" >&2
exit 1
fi
DOSEMU_DIR="${DOS_HOME}/.dosemu"
C_DRIVE="${DOSEMU_DIR}/drive_c"
DOSEMURC_PATH="${DOS_HOME}/.dosemurc"
INSTALL_MARKER="${DOSEMU_DIR}/${INSTALL_SENTINEL}"
DOSEMU_ARGS=(-quiet -t -K "${C_DRIVE}")
allowed_entries=()
mkdir -p "${DOSEMU_DIR}" "${C_DRIVE}"
if [ "$(id -u)" -eq 0 ] && [ ! -d "${ENV_DIR}" ]; then if [ "$(id -u)" -eq 0 ] && [ ! -d "${ENV_DIR}" ]; then
mkdir -p "${ENV_DIR}" mkdir -p "${ENV_DIR}"
fi fi
# clear C: drive to ensure only allowed programs are present set_install_marker() {
rm -rf "${C_DRIVE:?}/"* touch "${INSTALL_MARKER}"
mkdir -p "${C_DRIVE}" if [ "$(id -u)" -eq 0 ]; then
chown "${DOS_USER}:${DOS_USER}" "${INSTALL_MARKER}"
fi
}
has_svardos_installation() {
if [ -f "${C_DRIVE}/KERNEL.SYS" ] || [ -f "${C_DRIVE}/kernel.sys" ]; then
return 0
fi
if [ -f "${C_DRIVE}/BIN/PKG.EXE" ] || [ -f "${C_DRIVE}/bin/pkg.exe" ]; then
return 0
fi
if [ -d "${C_DRIVE}/SVARDOS" ] || [ -d "${C_DRIVE}/svardos" ]; then
return 0
fi
return 1
}
needs_install() {
if [ "${DOS_FORCE_INSTALL:-0}" = "1" ]; then
return 0
fi
if [ -f "${INSTALL_MARKER}" ]; then
return 1
fi
if has_svardos_installation; then
set_install_marker
return 1
fi
return 0
}
stage_initial_install() {
if [ ! -d "${SVARDOS_BASE}" ] || [ -z "$(ls -A "${SVARDOS_BASE}")" ]; then
echo "SvarDOS base directory ${SVARDOS_BASE} is empty. Aborting." >&2
exit 1
fi
rm -rf "${C_DRIVE:?}/"*
# Seed the C: drive with the SvarDOS base image
if [ -e "${SVARDOS_BASE}/COMMAND.COM" ] || [ -e "${SVARDOS_BASE}/command.com" ]; then
cp -a "${SVARDOS_BASE}/." "${C_DRIVE}/" cp -a "${SVARDOS_BASE}/." "${C_DRIVE}/"
else set_install_marker
echo "SvarDOS base not found in ${SVARDOS_BASE}. Aborting." >&2 echo "SvarDOS base staged to ${C_DRIVE}"
exit 1 }
fi
# Sync additional DOS files into the C: drive generate_dosemurc() {
allowed_entries=() local boot_target="$1"
case "${ALLOW_MODE}" in {
all) printf "%s\n" "\$_hdimage = \"${C_DRIVE}\""
if [ -d "${ALLOWED_REPO}" ] && [ "$(ls -A "${ALLOWED_REPO}")" ]; then printf "%s\n" "\$_boot = \"${boot_target}\""
printf "%s\n" "\$_emusys = \"drdos\""
printf "%s\n" "\$_fdpp = (off)"
} > "${DOSEMURC_PATH}"
if [ -f "${ENV_DIR}/dosemurc" ]; then
cat "${ENV_DIR}/dosemurc" >> "${DOSEMURC_PATH}"
fi
if [ "$(id -u)" -eq 0 ]; then
chown "${DOS_USER}:${DOS_USER}" "${DOSEMURC_PATH}"
fi
}
sync_allowed_content() {
allowed_entries=()
local mode="${ALLOW_MODE}"
if [ "${mode}" = "all" ]; then
if [ -d "${ALLOWED_REPO}" ] && [ "$(ls -A "${ALLOWED_REPO}" 2>/dev/null)" ]; then
cp -a "${ALLOWED_REPO}/." "${C_DRIVE}/" cp -a "${ALLOWED_REPO}/." "${C_DRIVE}/"
allowed_entries+=("ALL")
fi fi
;; return
list|allowlist) fi
;;
*) if [ "${mode}" != "list" ] && [ "${mode}" != "allowlist" ]; then
echo "Unknown DOS_ALLOW_MODE '${ALLOW_MODE}', falling back to allowlist mode." >&2 echo "Unknown DOS_ALLOW_MODE '${mode}', defaulting to allowlist mode." >&2
ALLOW_MODE="list" mode="list"
;; ALLOW_MODE="${mode}"
esac fi
if [ "${ALLOW_MODE}" != "all" ]; then
if [ -f "${ALLOWED_LIST}" ]; then if [ -f "${ALLOWED_LIST}" ]; then
while IFS= read -r line || [ -n "$line" ]; do while IFS= read -r line || [ -n "$line" ]; do
# trim whitespace
line="$(printf '%s' "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" line="$(printf '%s' "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
# skip blank/comment lines
case "$line" in case "$line" in
''|\#*) continue ;; ''|\#*) continue ;;
esac esac
@ -69,47 +140,41 @@ if [ "${ALLOW_MODE}" != "all" ]; then
echo "Warning: allowed file not found: $src" >&2 echo "Warning: allowed file not found: $src" >&2
fi fi
done done
}
apply_templates() {
if [ -f "${AUTOEXEC_TEMPLATE}" ]; then
cp "${AUTOEXEC_TEMPLATE}" "${C_DRIVE}/AUTOEXEC.BAT"
fi
if [ -f "${CONFIG_TEMPLATE}" ]; then
cp "${CONFIG_TEMPLATE}" "${C_DRIVE}/CONFIG.SYS"
fi
}
if needs_install; then
stage_initial_install
fi fi
# Optional: create an AUTOEXEC.BAT and CONFIG.SYS or other DOS boot files generate_dosemurc "c"
autoexec_path="${C_DRIVE}/AUTOEXEC.BAT"
config_path="${C_DRIVE}/CONFIG.SYS"
if [ -f "${AUTOEXEC_TEMPLATE}" ]; then sync_allowed_content
cp "${AUTOEXEC_TEMPLATE}" "${autoexec_path}" apply_templates
else
cat > "${autoexec_path}" <<'EOF'
@echo off
prompt [DOS]$P$G
echo Welcome to the containerized DOS environment.
EOF
fi
if [ "${ALLOW_MODE}" = "all" ]; then
printf 'echo All repository programs are currently enabled.\r\n' >> "${autoexec_path}"
elif [ ${#allowed_entries[@]} -gt 0 ]; then
{
printf 'echo Allowed programs:\r\n'
for entry in "${allowed_entries[@]}"; do
dos_entry="${entry//\//\\}"
printf 'echo %s\r\n' "${dos_entry}"
done
} >> "${autoexec_path}"
else
{
printf 'echo No extra programs are currently enabled.\r\n'
} >> "${autoexec_path}"
fi
if [ -f "${CONFIG_TEMPLATE}" ]; then
cp "${CONFIG_TEMPLATE}" "${config_path}"
fi
# Drop privileges to dosuser if we were run as root (ssh will run as the user)
# but in case sshd runs ForceCommand as root, we switch.
if [ "$(id -u)" -eq 0 ]; then if [ "$(id -u)" -eq 0 ]; then
exec runuser -u dosuser -- dosemu -quiet -K "${C_DRIVE}" chown -R "${DOS_USER}:${DOS_USER}" "${DOSEMU_DIR}"
else fi
# running as the user already
exec dosemu -quiet -K "${C_DRIVE}" if [ "${ALLOW_MODE}" = "all" ] && [ "${#allowed_entries[@]}" -gt 0 ]; then
echo "All files in ${ALLOWED_REPO} are available on drive C:."
elif [ "${#allowed_entries[@]}" -gt 0 ]; then
echo "Allowlisted files copied to C: from ${ALLOWED_REPO}:"
for entry in "${allowed_entries[@]}"; do
echo " ${entry}"
done
fi
if [ "$(id -u)" -eq 0 ]; then
exec runuser -u "${DOS_USER}" -- dosemu "${DOSEMU_ARGS[@]}"
else
exec dosemu "${DOSEMU_ARGS[@]}"
fi fi

View file

@ -1,79 +1,39 @@
#!/bin/bash #!/bin/bash
set -euo pipefail set -euo pipefail
SVARDOS_BASE_DIR="/opt/svardos/base" SVARDOS_ROOT="${SVARDOS_ROOT:-/opt/svardos}"
SVARDOS_CACHE="/opt/svardos/cache" SVARDOS_BASE_DIR="${SVARDOS_BASE_DIR:-${SVARDOS_ROOT}/base}"
SVARDOS_IMG="${SVARDOS_CACHE}/svardos.img" DEFAULT_URL="http://svardos.org/download/20250427/svardos-20250427-dosemu.zip"
DEFAULT_REL="download/20250427/svardos-20250427-floppy-1.44M.zip" ARCHIVE_URL="${SVARDOS_IMG_URL:-$DEFAULT_URL}"
DEFAULT_BASE="http://svardos.org"
if [ -z "${SVARDOS_IMG_URL:-}" ]; then # Skip work if the base tree already exists (unless SVARDOS_REFRESH is set)
homepage="$( if [ -z "${SVARDOS_REFRESH:-}" ] && [ -e "${SVARDOS_BASE_DIR}/COMMAND.COM" ]; then
curl -fsSL "${DEFAULT_BASE}/" 2>/dev/null || true
)"
discovered_rel=""
if [ -n "$homepage" ]; then
for pattern in 'download/[0-9]+/svardos-[0-9]+-floppy-1\.44M\.zip' 'download/[0-9]+/svardos-[0-9]+-usb\.zip'; do
candidate="$(printf '%s' "$homepage" | grep -Eo "$pattern" | head -n1)"
if [ -n "$candidate" ]; then
discovered_rel="$candidate"
break
fi
done
fi
if [ -n "$discovered_rel" ]; then
SVARDOS_IMG_URL="${DEFAULT_BASE}/${discovered_rel}"
else
SVARDOS_IMG_URL="${DEFAULT_BASE}/${DEFAULT_REL}"
fi
fi
if [ -e "${SVARDOS_BASE_DIR}/COMMAND.COM" ] || [ -e "${SVARDOS_BASE_DIR}/command.com" ]; then
exit 0 exit 0
fi fi
mkdir -p "${SVARDOS_BASE_DIR}" "${SVARDOS_CACHE}"
tmp_archive="${SVARDOS_CACHE}/svardos_download"
rm -f "${tmp_archive}" "${SVARDOS_IMG}"
echo "Fetching SvarDOS base from ${SVARDOS_IMG_URL}"
curl -fsSL "${SVARDOS_IMG_URL}" -o "${tmp_archive}"
mime_type="$(file -b --mime-type "${tmp_archive}")"
case "${mime_type}" in
application/zip)
unzip -o "${tmp_archive}" -d "${SVARDOS_CACHE}" >/dev/null
;;
application/x-bzip2|application/x-gzip|application/x-xz)
tar -xf "${tmp_archive}" -C "${SVARDOS_CACHE}"
;;
application/octet-stream)
# assume this is already a raw image
cp "${tmp_archive}" "${SVARDOS_IMG}"
;;
*)
echo "Unsupported SvarDOS payload type: ${mime_type}" >&2
exit 1
;;
esac
if [ ! -f "${SVARDOS_IMG}" ]; then
# try to locate the image file
candidate="$(find "${SVARDOS_CACHE}" -maxdepth 1 -type f \( -iname '*svardos*.img' -o -iname '*.ima' -o -iname '*.img' \) | head -n1)"
if [ -z "${candidate}" ]; then
echo "Could not locate a SvarDOS disk image after extraction." >&2
exit 1
fi
mv "${candidate}" "${SVARDOS_IMG}"
fi
rm -rf "${SVARDOS_BASE_DIR}"/*
mkdir -p "${SVARDOS_BASE_DIR}" mkdir -p "${SVARDOS_BASE_DIR}"
# Copy contents of FAT image into the base directory tmpdir="$(mktemp -d)"
mcopy -s -i "${SVARDOS_IMG}" ::* "${SVARDOS_BASE_DIR}/" cleanup() {
rm -rf "${tmpdir}"
}
trap cleanup EXIT
archive_path="${tmpdir}/svardos.zip"
unpack_dir="${tmpdir}/unpacked"
echo "Fetching SvarDOS base from ${ARCHIVE_URL}"
curl -fsSL "${ARCHIVE_URL}" -o "${archive_path}"
unzip -q "${archive_path}" -d "${unpack_dir}"
# Some archives might unpack into a single top-level directory; flatten it if so.
if [ "$(find "${unpack_dir}" -mindepth 1 -maxdepth 1 -type d | wc -l)" -eq 1 ] && \
[ "$(find "${unpack_dir}" -mindepth 1 -maxdepth 1 | wc -l)" -eq 1 ]; then
unpack_dir="$(find "${unpack_dir}" -mindepth 1 -maxdepth 1 -type d)"
fi
rm -rf "${SVARDOS_BASE_DIR:?}/"*
cp -a "${unpack_dir}/." "${SVARDOS_BASE_DIR}/"
rm -f "${SVARDOS_IMG}" "${tmp_archive}"
rm -rf "${SVARDOS_CACHE}"
echo "SvarDOS base copied to ${SVARDOS_BASE_DIR}" echo "SvarDOS base copied to ${SVARDOS_BASE_DIR}"