#!/bin/sh # conf_replace.sh -- Create modified ISO copy with replaced config files # # Syntax: HIPERISO_CONF_REPLACE="org=/iso/path;new=data/path" # # org= Absolute ISO-internal path (must start with /), e.g. /boot/grub.cfg # new= Relative path on the data partition (no leading /), e.g. overlay/grub.cfg # # If set, creates /tmp/modified.iso and prints its path on stdout. # All diagnostics go to stderr (captured by init's 2> redirect). # Called by init: _modified=$(sh /conf_replace.sh 2>"$LOG_DIR/conf_replace.log") # POSIX sh compatible . /hiperiso-lib.sh # Redirect stdout → stderr so all hiperiso_log output and xorriso noise # go to the diagnostic channel. Preserve the original stdout as fd 3 # for the single path-print at the end. exec 3>&1 1>&2 if [ -z "${HIPERISO_CONF_REPLACE:-}" ]; then exit 0 fi # Validate the whole config value: semicolon-separated org=/new= entries # using only safe characters (including / for ISO-internal paths). if printf '%s' "$HIPERISO_CONF_REPLACE" | grep -qE '[^-a-zA-Z0-9._/;=]|\.\.'; then hiperiso_log "conf_replace: config value contains unsafe characters or traversal" exit 0 fi _org="" _new="" OLDIFS="$IFS" IFS=';' _hiso_glob_state=$- set -f for _pair in $HIPERISO_CONF_REPLACE; do case "$_pair" in org=*) _org="${_pair#org=}" ;; new=*) _new="${_pair#new=}" ;; esac done case "$_hiso_glob_state" in *f*) : ;; *) set +f ;; esac IFS="$OLDIFS" if [ -z "$_org" ] || [ -z "$_new" ]; then hiperiso_log "conf_replace: malformed config, skipping" exit 0 fi # _new: relative path on data partition (no leading /, no traversal) if ! hiperiso_validate_path "$_new"; then hiperiso_log "conf_replace: replacement path is unsafe: $_new" exit 0 fi # _org: absolute ISO-internal path (must start with /, no traversal, # only path-safe characters including /) case "$_org" in /*) : ;; *) hiperiso_log "conf_replace: original path must be absolute (start with /): $_org" exit 0 ;; esac case "$_org" in *..*) hiperiso_log "conf_replace: original path contains traversal: $_org" exit 0 ;; esac case "$_org" in *[!/a-zA-Z0-9._~-]*) hiperiso_log "conf_replace: original path has unsafe characters: $_org" exit 0 ;; esac _new_abs=$(hiperiso_join_path "${DATA_MOUNT:-}" "$_new") || exit 0 if [ ! -f "$_new_abs" ]; then hiperiso_log "conf_replace: replacement file not found: $_new_abs" exit 0 fi XORRISO_BIN="" for _cand in /usr/bin/xorriso /mnt/runtime/usr/bin/xorriso; do if [ -x "$_cand" ]; then XORRISO_BIN="$_cand"; break; fi done if [ -z "$XORRISO_BIN" ]; then hiperiso_log "conf_replace: xorriso not available — ISO modification skipped" exit 0 fi _modified_iso="/tmp/modified.iso" hiperiso_log "conf_replace: creating modified ISO copy" hiperiso_log "conf_replace: org=$_org new=$_new_abs" if "$XORRISO_BIN" -indev "$ISO_PATH" \ -outdev "$_modified_iso" \ -boot_image any keep \ -map "$_new_abs" "$_org" \ 2>&1; then if [ -f "$_modified_iso" ]; then hiperiso_log "conf_replace: modified ISO at $_modified_iso" printf '%s' "$_modified_iso" >&3 exit 0 fi fi hiperiso_log "conf_replace: xorriso failed, using original ISO" rm -f "$_modified_iso" 2>/dev/null || true exit 0