From 2c23f765f970c1b201a2777075f5b23797bcff8e Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Thu, 25 Jan 2024 22:01:04 +1300 Subject: [PATCH] fix: Ensure parsed configs have CRLF to LF corrected (where possible) Likewise, this runtime fix was only covering two config files. It now applies to all callers of this method. --- target/scripts/helpers/accounts.sh | 6 ------ target/scripts/helpers/utils.sh | 19 +++++++++++++++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/target/scripts/helpers/accounts.sh b/target/scripts/helpers/accounts.sh index 16a066a7..31ded04a 100644 --- a/target/scripts/helpers/accounts.sh +++ b/target/scripts/helpers/accounts.sh @@ -19,9 +19,6 @@ function _create_accounts() { _create_masters if [[ -f ${DATABASE_ACCOUNTS} ]]; then - _log 'trace' "Checking file line endings" - sed -i 's|\r||g' "${DATABASE_ACCOUNTS}" - _log 'trace' "Regenerating postfix user list" echo "# WARNING: this file is auto-generated. Modify ${DATABASE_ACCOUNTS} to edit the user list." > /etc/postfix/vmailbox @@ -154,9 +151,6 @@ function _create_masters() { local DATABASE_DOVECOT_MASTERS='/tmp/docker-mailserver/dovecot-masters.cf' if [[ -f ${DATABASE_DOVECOT_MASTERS} ]]; then - _log 'trace' "Checking file line endings" - sed -i 's|\r||g' "${DATABASE_DOVECOT_MASTERS}" - _log 'trace' "Regenerating dovecot masters list" chown dovecot:dovecot "${DOVECOT_MASTERDB_FILE}" diff --git a/target/scripts/helpers/utils.sh b/target/scripts/helpers/utils.sh index 239e3b34..d5109c1e 100644 --- a/target/scripts/helpers/utils.sh +++ b/target/scripts/helpers/utils.sh @@ -17,18 +17,33 @@ function _escape_for_sed() { # Returns input after filtering out lines that are: # empty, white-space, comments (`#` as the first non-whitespace character) function _get_valid_lines_from_file() { + _convert_crlf_to_lf_if_necessary "${1}" _append_final_newline_if_missing "${1}" + grep --extended-regexp --invert-match "^\s*$|^\s*#" "${1}" || true } +# This is to sanitize configs from users that unknowingly introduced CRLF: +function _convert_crlf_to_lf_if_necessary() { + if [[ $(file "${1}") =~ 'CRLF' ]]; then + _log 'warn' "${1} contains CRLF line-endings." + + if [[ -w "${1}" ]]; then + _log 'debug' 'Converting CRLF to LF' + sed -i 's|\r||g' "${1}" + fi + fi +} + +# This is to sanitize configs from users that unknowingly removed the end-of-file LF: function _append_final_newline_if_missing() { # Correctly detect a missing final newline and fix it: # https://stackoverflow.com/questions/38746/how-to-detect-file-ends-in-newline#comment82380232_25749716 # https://unix.stackexchange.com/questions/31947/how-to-add-a-newline-to-the-end-of-a-file/441200#441200 # https://unix.stackexchange.com/questions/159557/how-to-non-invasively-test-for-write-access-to-a-file - if [[ $(tail -c1 "${1}" | wc -l) -gt 0 ]]; then + if [[ $(tail -c1 "${1}" | wc -l) -eq 0 ]]; then # Avoid fixing when the destination is read-only: - if [[ test -w "${1}" ]]; then + if [[ -w "${1}" ]]; then printf '\n' >> "${1}" _log 'warn' "${1} was missing a final newline. This has been fixed."