From ee363c67e0ce5f58cee8ede6c604c34640e2c759 Mon Sep 17 00:00:00 2001 From: Richard T Bonhomme Date: Sun, 22 Dec 2024 18:24:36 +0000 Subject: [PATCH] write_legacy_file_v2(): Correct over-write logic If 'overwrite' is not enabled AND the target exists then skip this target, instead of erroring out. Include EASYRSA_DEBUG output. Signed-off-by: Richard T Bonhomme --- easyrsa3/easyrsa | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/easyrsa3/easyrsa b/easyrsa3/easyrsa index 66f97308..57f99479 100755 --- a/easyrsa3/easyrsa +++ b/easyrsa3/easyrsa @@ -4990,7 +4990,7 @@ write_legacy_file_v2() { write_type="$1" write_file="$2" write_over= - [ "$3" = overwrite ] && write_over="$3" + [ "$3" = overwrite ] && write_over=1 # Select by type case "$write_type" in @@ -5022,29 +5022,40 @@ write_legacy_file_v2() { if [ -f "$write_file" ]; then # if this is a temp file then enable auto-overwrite path="${write_file%%/temp.*}" - if [ "${secured_session}" = "$path" ]; then + if [ "$path" = "${secured_session}" ]; then verbose ": write_legacy_file_v2 - temp-file ACCEPTED" - write_over=overwrite + write_over=1 else # target is not a temp-file, overwrite not changed verbose ": Target is not a temp-file: $write_file" fi else - # enable overwrite, "there is no file" to over write - verbose ": Missing input file: $write_file" - write_over=overwrite + verbose ": Create new file: $write_file" fi + else + verbose ": No target file - output to stdout" fi # write legacy data stream to stdout or file - if [ "$write_file" ]; then + if [ -f "$write_file" ]; then if [ "$write_over" ]; then verbose ": write_legacy_file_v2 - over-write ENABLED" create_legacy_stream "$write_type" > "$write_file" || \ die "write failed" + [ "$EASYRSA_DEBUG" ] && print \ + "### write OVERWRITE: $write_type to $write_file" else - user_error "write: Over-write refused for existing file!" + # Preserve existing file and continue + verbose "write_legacy_file_v2 - over-write DISABLED " + [ "$EASYRSA_DEBUG" ] && print \ + "### write PRESERVE existing: $write_file" fi + elif [ "$write_file" ]; then + verbose ": write_legacy_file_v2 - over-write DISABLED" + create_legacy_stream "$write_type" > "$write_file" || \ + die "write failed" + [ "$EASYRSA_DEBUG" ] && print \ + "### write NEWFILE: $write_type to $write_file" else # write stream to stdout ONLY create_legacy_stream "$write_type"