From 4576619ae713b8c221f1bdd643a31ab58527c514 Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Sat, 6 Jun 2020 20:49:13 +0100 Subject: [PATCH] Version 0.2.0 - major refactoring of code, and new features. --- TODO.md | 3 - lumberjack | 658 +++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 468 insertions(+), 193 deletions(-) diff --git a/TODO.md b/TODO.md index b49ebdc..6507569 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,2 @@ * Write a man page. * Add a regex filter (read from a file) to decide what to log and what to drop. -* Instead of requiring a fifo already exist, if the file doesn't already exist create the fifo. Would need to modify the trap for SIGTERM in order to clean up the file. -* Figure out a way to check if the program is respawning too offten - this would indicate an error in the calling process and we don't want to just keep looping forever. -* Have an option to change UID and/or GID when running. Alternatively, use setpriv to drop capabilities. diff --git a/lumberjack b/lumberjack index fc75e71..bc77523 100755 --- a/lumberjack +++ b/lumberjack @@ -3,83 +3,167 @@ # Darren 'Tadgy' Austin # Licensed under the terms of the GNU General Public License version 3. # -#........1.........2.........3.........4.........5.........6.........7.........8.........9.........0.........1.........2.........3.....:...4 +#........1.........2.........3.........4.........5.........6.........7.........8.........9.........0.........1..:......2.........3.........4.........5.........: # Script details. -LJ_NAME="${0##*/}" -LJ_VERSION="0.1.7" +NAME="${0##*/}" +VERSION="0.2.0" +# Functions. +check_leading_dirs() { + # $1 The virtual host being processed. + # $2 The path of which to validate the leading directories. + + [[ -z "$1" || -z "$2" ]] && return 1 + + if ! is_dir "$(remove_expansions "$2")"; then + (( FLAGS[${1}-template_prefix] == 0 )) && { + syslog "warn" "prefix directories of template do not exist: $(remove_expansions "$2")" + FLAGS[${1}-template_prefix]=1 + } + return 1 + else + (( FLAGS[${1}-template_prefix] == 1 )) && { + syslog "info" "prefix directories of template reappeared: $(remove_expansions "$2")" + FLAGS[${1}-template_prefix]=0 + } + fi + return 0 +} + +close_fd() { + # $1 The site of which to close the file descriptor. + + [[ -z "$1" ]] && return 1 + + # shellcheck disable=SC1083 + { exec {FDS[$1]}>&-; } 2>/dev/null || syslog "warn" "failed to close FD ${FDS[$1]} for $1" + unset "FDS[$1]" "FLAGS[${1}-template_prefix]" "FLAGS[${1}-make_dir_fail]" "FLAGS[${1}-fix_link]" +} + +create_missing_dirs() { + # $1 The virtual host being processed. + # $2 The directory to insure exists. + + [[ -z "$1" || -z "$2" ]] && return 1 + + if ! make_dir "$2"; then + (( FLAGS[${1}-make_dir_fail] == 0 )) && { + syslog "warn" "error creating log file's directory: $2" + FLAGS[${1}-make_dir_fail]=1 + } + return 1 + else + (( FLAGS[${1}-make_dir_fail] == 1 )) && { + syslog "info" "created log file's directory: $2" + FLAGS[${1}-make_dir_fail]=0 + } + fi + return 0 +} + die() { # $1 The text of the error message to display on stderr. - printf "%s: %s\n" "$LJ_NAME" "$1" >&2 + + printf "%s: %s\\n" "$NAME" "$1" >&2 exit 1 } display_help() { # |........1.........2.........3.........4.........5.........6.........7.........8 cat <<-EOF - Usage: $LJ_NAME [options]