# cpanel-importer — sanitization sandbox for cPanel cpmove tarballs.
#
# See cpanel-import-container-spec.md §1 for the full design.
#
# Build:  docker build -t cpanel-importer:dev .
# Run:    see README.md for the docker run invocation the WHP panel uses.

# Base image. CI passes BASE_IMAGE=almalinux@sha256:<digest>, resolved
# DIRECTLY from Docker Hub in the same run (.gitea/scripts/resolve-base-image.sh),
# so the pkg-cache registry mirror can only serve content-addressed blobs.
# Local builds use the tag. Canonical docs: cloud-apache-container docs/pkg-cache.md.
ARG BASE_IMAGE=almalinux:10-minimal

# --- whp-scan-scope: the scan-scope tool of the WHP panel repo -------------
# Decides which files of the extract clamscan sees (code and risky files; the
# mail tree and clean bulk media are skipped) and writes the scope attestation
# the panel's privd checks. Built from a PINNED commit of the (private) WHP
# repo, stdlib only, CGO off, static. Source: panel/cmd/whp-scan-scope and
# panel/internal/cpimport/scanscope; policy docs: docs/runbooks/account-transfer-scan-scope.md
# in that repo.
#
# WHP_COMMIT MUST be a full 40-hex commit sha and MUST be bumped when the
# scan policy changes. It is the merge of WHP PR #334 (limit 2000 MiB; refuses
# unreadable, control-character, >= 1023-byte paths and web-root links into the
# skipped mail tree).
# CI passes a READ-ONLY token for cloud-hosting-platform/whp as the BuildKit
# secret `whp_token` (repo secret WHP_READ_TOKEN: never the registry-push
# CI_TOKEN). The token is handed to git by a credential helper that reads the
# secret file at fetch time: it is never on a command line, in an ENV/ARG, in
# .git/config or in a layer. For a local build without network access to the
# repo, override the whole source stage with a checkout:
#   docker buildx build --build-context whp-src=/path/to/whp-checkout ...
#
# GO_IMAGE is pinned by DIGEST (the multi-arch index of golang:1.27-alpine,
# resolved 2026-11-22 from registry-1.docker.io). Bump tag and digest together.
ARG GO_IMAGE=golang:1.27-alpine@sha256:70bd7ddea8b6cbe5d6943cde279d6b67feb10cc924fb9100d1d8757c1575b280
ARG WHP_REPO=https://repo.anhonesthost.net/cloud-hosting-platform/whp.git
ARG WHP_COMMIT=8a350449e5e89703ad1b0ad1c1a446f001d178b5

FROM ${GO_IMAGE} AS whp-fetch
ARG WHP_REPO
ARG WHP_COMMIT
RUN apk add --no-cache git
RUN --mount=type=secret,id=whp_token,required=false \
    set -eu; export GIT_TERMINAL_PROMPT=0; \
    case "${WHP_COMMIT}" in *[!0-9a-f]*|"") echo "WHP_COMMIT must be a full lowercase hex sha" >&2; exit 1;; esac; \
    [ "${#WHP_COMMIT}" -eq 40 ] || { echo "WHP_COMMIT must be 40 hex chars" >&2; exit 1; }; \
    git init -q /whp && cd /whp; \
    if [ -s /run/secrets/whp_token ]; then \
        git -c credential.helper= \
            -c 'credential.helper=!f() { [ "$1" = get ] || exit 0; echo username=whp-read; printf "password=%s\n" "$(cat /run/secrets/whp_token)"; }; f' \
            fetch -q --depth 1 "${WHP_REPO}" "${WHP_COMMIT}"; \
    else \
        git fetch -q --depth 1 "${WHP_REPO}" "${WHP_COMMIT}"; \
    fi; \
    git checkout -q FETCH_HEAD; \
    [ "$(git rev-parse HEAD)" = "${WHP_COMMIT}" ] || { echo "fetched commit is not the pinned one" >&2; exit 1; }
# Named context `whp-src` (see above) replaces this stage; its root is a repo root.
FROM scratch AS whp-src
COPY --from=whp-fetch /whp/panel /panel

FROM ${GO_IMAGE} AS whp-scan-scope-build
COPY --from=whp-src /panel /src/panel
WORKDIR /src/panel
# stdlib only: no module download may be needed (GOFLAGS=-mod=mod and GOPROXY=off prove it).
ENV CGO_ENABLED=0 GOFLAGS=-trimpath GOPROXY=off GOTOOLCHAIN=local
RUN go build -ldflags="-s -w" -o /out/whp-scan-scope ./cmd/whp-scan-scope
# smoke: no arguments is a usage error (exit 2)
RUN set +e; /out/whp-scan-scope 2>/dev/null; test $? -eq 2

FROM ${BASE_IMAGE}

LABEL org.opencontainers.image.title="cpanel-importer"
LABEL org.opencontainers.image.description="cPanel cpmove sanitization sandbox (ClamAV + SaneSecurity + WP content scan)"
LABEL org.opencontainers.image.source="https://repo.anhonesthost.net/cloud-hosting-platform/cpanel-importer"
LABEL org.opencontainers.image.licenses="MIT"

ARG TARGETARCH=amd64
# UID/GID of the unprivileged worker. Matches the spec — panel calls
# `docker run --user 999:999`, so this UID must actually exist inside the
# image (the EPEL `clamav` and `php` user accounts collide with low UIDs;
# 999 is well clear of them).
ARG WHP_UID=999
ARG WHP_GID=999

ENV LANG=C.UTF-8 \
    LC_ALL=C.UTF-8 \
    PHP_INI_DIR=/etc/php.d

# Single RUN to minimize layers and image size. Cleans dnf cache and
# the SaneSecurity rsync temp files at the end of the layer.
#
# Pinning strategy:
#   - PHP 8.5: AlmaLinux 10 stock ships PHP 8.3 only; the spec asks for
#     a current Remi stream (8.5; the 8.4 stream had a known exploit). We
#     add Remi's modular repo and enable the `php:remi-8.5` stream. We DO NOT pin to a specific 8.5.X because
#     Remi rolls security patches into the same minor and an exact pin
#     would block updates.
#   - clamav / clamav-update: track the AL10 EPEL stream. CI builds
#     monthly so signature DB age is bounded.
#   - SaneSecurity: rsync at build time, then again at container start
#     via `freshclam` (with the SaneSecurity third-party DBs configured).
#
# Ordering note: clamav-filesystem's RPM scripts auto-create a
# `virusgroup` system group at the next free GID. If we let dnf install
# clamav first, that lands at GID 999 — which then collides with the
# UID/GID we want for whp-import. We pre-create our user FIRST so
# virusgroup ends up at 998.
#
# OPTIONAL internal package cache, e.g. --build-arg PKG_CACHE=https://192.168.1.40
# (CT130 pkg-cache, https only). Unset = upstream repositories. epel-release
# and remi-release are GPG-verified against keys/pkg-cache/ either way
# (pkg-cache-repos.sh release-install). Repo files are restored at the end of
# the RUN, so no layer keeps the cache address. Same script as
# cloud-apache-container (scripts/pkg-cache-repos.sh, docs/pkg-cache.md there).
ARG PKG_CACHE=
COPY scripts/pkg-cache-repos.sh /usr/local/sbin/pkg-cache-repos.sh
COPY keys/pkg-cache/ /usr/local/share/pkg-cache/
RUN set -eux; \
    /usr/local/sbin/pkg-cache-repos.sh rpm ; \
    # microdnf is what almalinux:10-minimal ships with by default.
    microdnf -y install --setopt=install_weak_deps=0 \
        dnf \
        shadow-utils \
        ; \
    # EPEL + Remi's repo for PHP 8.5 (AL10 stock has 8.3 only); release
    # RPMs signature-checked against the committed keys, then the new repo
    # files are pointed at the cache too (no-op without PKG_CACHE).
    /usr/local/sbin/pkg-cache-repos.sh release-install epel remi ; \
    /usr/local/sbin/pkg-cache-repos.sh rpm ; \
    dnf -y --setopt=install_weak_deps=0 module reset php ; \
    dnf -y --setopt=install_weak_deps=0 module enable php:remi-8.5 ; \
    # Pre-create the worker BEFORE installing clamav so virusgroup
    # doesn't claim our GID.
    groupadd --system --gid ${WHP_GID} whp-import ; \
    useradd  --system --uid ${WHP_UID} --gid ${WHP_GID} \
             --home-dir /opt/whp --no-create-home \
             --shell /sbin/nologin whp-import ; \
    dnf -y --setopt=install_weak_deps=0 install \
        php-cli \
        php-json \
        php-mbstring \
        php-pdo \
        php-mysqlnd \
        php-xml \
        php-zip \
        php-process \
        clamav \
        clamav-update \
        tar \
        gzip \
        bzip2 \
        xz \
        mariadb \
        rsync \
        ca-certificates \
        coreutils-single \
        findutils \
        which \
        ; \
    mkdir -p /opt/whp /scripts /host/backup /host/quarantine /host/sanitized \
             /var/lib/clamav /var/log/clamav ; \
    # /opt/whp + /var/log/clamav owned by worker now. /var/lib/clamav
    # ownership is set AFTER the freshclam build-time pull below — root
    # has to be able to write there during the build.
    chown -R whp-import:whp-import /opt/whp /var/log/clamav ; \
    # /host/quarantine and /host/sanitized are the bind-mount RW
    # targets. The panel chowns the HOST paths to UID 999 before
    # invocation (see README.md). When the host path is empty Docker
    # copies the IMAGE-side dir's ownership onto the new volume; we
    # need that ownership to be whp-import so an empty bind mount on
    # those paths still results in a writable volume. (Bind mounts to
    # an EXISTING host dir keep host ownership and are independent of
    # this — the panel sets up its own dirs with mode 750 owner 999.)
    chown whp-import:whp-import /host/quarantine /host/sanitized ; \
    /usr/local/sbin/pkg-cache-repos.sh restore ; \
    # Strip dnf cache.
    dnf -y clean all ; \
    rm -rf /var/cache/dnf /var/cache/yum /var/cache/ldconfig/* \
           /usr/share/doc /usr/share/man /usr/share/info

# Pre-seed ClamAV signature databases at build time so the first
# container run isn't dependent on freshclam succeeding before the scan.
#
# We do two passes:
#   1. freshclam (mainline ClamAV signatures: main.cvd, daily.cvd, bytecode.cvd).
#   2. rsync the SaneSecurity Foxhole.PHP DB — PHP-malware-focused, this
#      is the high-value addition for our use case. Junkemailfilter rules
#      are deliberately skipped (we don't scan email here).
#
# Both runs are wrapped in `|| true` so a transient network failure
# during build does not break the image build; the container also runs
# `freshclam` on start so a stale baseline gets refreshed at runtime.
COPY configs/freshclam.conf /etc/freshclam.conf
COPY configs/sanesecurity-mirror.txt /opt/whp/sanesecurity-mirror.txt

# Pre-seed signatures as root, then chown the result. We don't ship the
# privilege-switching tools (runuser/su are in util-linux full, ~2MB we
# don't need at runtime) — the worker only needs to READ /var/lib/clamav
# and the runtime freshclam refresh runs as the same UID 999 anyway, so
# ownership matters there.
RUN set -eux; \
    chown whp-import:whp-import /etc/freshclam.conf ; \
    # Mainline ClamAV DB pull at build time so we have something to scan
    # against even if the runtime freshclam refresh fails (e.g., no net).
    # freshclam has a compile-time default --user=clamupdate (UID 997)
    # and tries to setuid() to it; the build-time dir is whp-import-owned
    # so we tell it explicitly to stay as root for this one-shot pull.
    freshclam --no-warnings --user=root || \
        echo "WARN: freshclam failed during build; runtime refresh will retry" ; \
    # SaneSecurity Foxhole.PHP rules. The project rotates mirrors; the
    # file we COPYed lists the working rsync mirror used at build time.
    SANE_MIRROR="$(cat /opt/whp/sanesecurity-mirror.txt)" ; \
    rsync -av --no-motd --contimeout=30 \
        --include='foxhole_filename.cdb' \
        --include='foxhole_filename.cdb.sig' \
        --include='foxhole_generic.cdb' \
        --include='foxhole_generic.cdb.sig' \
        --include='foxhole_js.cdb' \
        --include='foxhole_js.cdb.sig' \
        --include='foxhole_js.ndb' \
        --include='foxhole_js.ndb.sig' \
        --include='foxhole_mail.cdb' \
        --include='foxhole_mail.cdb.sig' \
        --include='foxhole_all.ndb' \
        --include='foxhole_all.ndb.sig' \
        --exclude='*' \
        "rsync://${SANE_MIRROR}/sanesecurity/" /var/lib/clamav/ \
        || echo "WARN: SaneSecurity rsync failed during build; runtime freshclam will retry" ; \
    chown -R whp-import:whp-import /var/lib/clamav ; \
    chmod -R u=rwX,g=rX,o= /var/lib/clamav ; \
    ls -la /var/lib/clamav/

COPY --from=whp-scan-scope-build /out/whp-scan-scope /usr/local/bin/whp-scan-scope
COPY --chown=whp-import:whp-import scripts/ /scripts/
RUN chmod 0755 /scripts/entrypoint.sh /scripts/extract.sh \
              /scripts/scan-files.php /scripts/scan-dbs.php

WORKDIR /opt/whp
USER whp-import

# stdin is closed — the container reads its inputs from env + bind mounts.
ENTRYPOINT ["/scripts/entrypoint.sh"]
