CHAT_OUTPUT_BEGIN
COMMAND_ID=READER-VM130-PUT-INTERFACE-AUDIT-331
STATUS=OK
RC=0
HOST=pve01
MODE=read-only
COMPONENT=cluster-audit
REFERENCE_REGISTER_CHECK=OK
REFERENCE_SHA256=5763f2f8edc75fcf6f3951f8c95896d1cc112ceccb6cf5b64a383f540a4deb8e
ERROR_REGISTER_CHECK=OK
ERROR_REGISTER_SHA256=08b3a769b8e38eaa1acec915352fb328180d70caa7ff6cd9d1ec0da426262752
COMMAND_SHA256=e8c9b036754a539b1282f1d1a4f3c2e6aacd2c36db93f7ab61f759039c7b9dc3
DUPLICATE_FAILED_COMMAND_BLOCKED=false
EXECUTION_STARTED=true
CHANGES_MADE=false
SANITIZED=yes
SECRETS_INCLUDED=no
PRIVATE_ADDRESSES_INCLUDED=no
RAW_EVIDENCE_SHA256=b8700f3f22291a9240833ee3553ab23c76aa5fb1d21c5659f330f36ffa8931f4
SANITIZED_OUTPUT_SHA256=b8700f3f22291a9240833ee3553ab23c76aa5fb1d21c5659f330f36ffa8931f4
OUTPUT_BEGIN
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/test
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/sha256sum
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/awk
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/sed
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/bash
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/stat
DETAIL=PUT_HELPER_SHA256=1166303044c104bcce68a5557b297ea034205ae8c497da7199e8a9f1cffc6bd8
DETAIL=PUT_HELPER_INTEGRITY=PASS
DETAIL=PUT_HELPER_METADATA=root:root|750|3227|regular file
DETAIL=PUT_HELPER_FIRST_LINE=#!/usr/bin/bash
DETAIL=PUT_HELPER_SYNTAX=PASS
DETAIL=PUT_HELPER_LINE_COUNT=83
DETAIL=PUT_HELPER_SOURCE_BEGIN
#!/usr/bin/bash
set -Eeuo pipefail

readonly VM_HELPER=/usr/local/sbin/homelab-vm130-exec
readonly VM_HELPER_SHA256=1959773393723b013af7c93cd29a7b833fd607c903d044e2fcef6accaa29c275
readonly SSH=/usr/bin/ssh
readonly JQ=/usr/bin/jq
readonly SHA256SUM=/usr/bin/sha256sum
readonly STAT=/usr/bin/stat
readonly MKTEMP=/usr/bin/mktemp
readonly RM=/usr/bin/rm
readonly CAT=/usr/bin/cat
readonly HOST=root@pve03
readonly VMID=130
readonly MAX_SIZE=1048576

fail() {
    printf 'VM130_PUT_ERROR=%s\n' "$1" >&2
    exit "${2:-1}"
}

(( $# == 2 )) || fail USAGE 64
source_file=$1
remote_name=$2

[[ "$remote_name" =~ ^homelab-[A-Za-z0-9._-]{1,96}$ ]] || fail REMOTE_NAME 65
remote_file="/tmp/$remote_name"

[[ -x "$VM_HELPER" ]] || fail VM_HELPER_MISSING 69
actual_vm_sha=$("$SHA256SUM" "$VM_HELPER" | /usr/bin/awk '{print $1}')
[[ "$actual_vm_sha" == "$VM_HELPER_SHA256" ]] || fail VM_HELPER_INTEGRITY 70

[[ -f "$source_file" && ! -L "$source_file" ]] || fail SOURCE_TYPE 66
source_meta=$("$STAT" -c '%U:%G|%a|%s|%F' "$source_file")
IFS='|' read -r source_owner source_mode source_size source_type <<<"$source_meta"
[[ "$source_owner" == root:root && "$source_mode" == 600 && "$source_type" == regular*file ]] || fail SOURCE_METADATA 66
[[ "$source_size" =~ ^[0-9]+$ ]] && (( source_size > 0 && source_size <= MAX_SIZE )) || fail SOURCE_SIZE 66
source_sha=$("$SHA256SUM" "$source_file" | /usr/bin/awk '{print $1}')

if "$VM_HELPER" /usr/bin/test -e "$remote_file"; then
    fail REMOTE_EXISTS 68
fi

tmpdir=$("$MKTEMP" -d /tmp/homelab-vm130-put.XXXXXX)
remote_created=NO
cleanup() {
    rc=$?
    if [[ "$remote_created" == YES && "$rc" -ne 0 ]]; then
        "$VM_HELPER" /usr/bin/rm -f -- "$remote_file" >/dev/null 2>&1 || true
    fi
    "$RM" -rf -- "$tmpdir"
}
trap cleanup EXIT

printf -v remote_cmd '%q ' /usr/sbin/qm guest exec "$VMID" --pass-stdin 1 -- /usr/bin/dd "of=$remote_file" status=none
if raw=$("$SSH" -o BatchMode=yes -o ConnectTimeout=8 "$HOST" "$remote_cmd" <"$source_file" 2>"$tmpdir/ssh.err"); then
    ssh_rc=0
else
    ssh_rc=$?
fi
(( ssh_rc == 0 )) || {
    "$CAT" "$tmpdir/ssh.err" >&2
    fail SSH_FAILURE 125
}
"$JQ" -e 'type=="object" and (.exited==1) and (.exitcode|type=="number")' <<<"$raw" >/dev/null 2>&1 || fail INVALID_QGA_JSON 126
guest_rc=$("$JQ" -r '.exitcode' <<<"$raw")
guest_out=$("$JQ" -r '."out-data" // empty' <<<"$raw")
guest_err=$("$JQ" -r '."err-data" // empty' <<<"$raw")
[[ "$guest_rc" == 0 && -z "$guest_out" && -z "$guest_err" ]] || fail GUEST_WRITE 74
remote_created=YES

"$VM_HELPER" /usr/bin/chown root:root "$remote_file"
"$VM_HELPER" /usr/bin/chmod 0600 "$remote_file"
remote_meta=$("$VM_HELPER" /usr/bin/stat -c '%U:%G|%a|%s|%F' "$remote_file")
IFS='|' read -r remote_owner remote_mode remote_size remote_type <<<"$remote_meta"
[[ "$remote_owner" == root:root && "$remote_mode" == 600 && "$remote_size" == "$source_size" && "$remote_type" == regular*file ]] || fail REMOTE_METADATA 74
remote_sha=$("$VM_HELPER" /usr/bin/sha256sum "$remote_file" | /usr/bin/awk '{print $1}')
[[ "$remote_sha" == "$source_sha" ]] || fail REMOTE_INTEGRITY 74

printf 'VM130_PUT=READY\n'
printf 'REMOTE_FILE=%s\n' "$remote_file"
printf 'SIZE=%s\n' "$source_size"
printf 'SHA256=%s\n' "$source_sha"
DETAIL=PUT_HELPER_SOURCE_END
DETAIL=PUT_HELPER_SOURCE_TRUNCATED=NO
CHANGES_MADE=NO

OUTPUT_END
CHAT_OUTPUT_END
