From 2fd063fee431e1af6fe5168a73674eb27d2bd184 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Sat, 18 Apr 2020 10:26:08 +0200 Subject: [PATCH 1/6] gitignore build directory --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 9183973..8868926 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.swp .vagrant +build From 14c4b63e699811ba8c808f166841c29f1c1f4c31 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Sat, 18 Apr 2020 10:26:51 +0200 Subject: [PATCH 2/6] error() function is not available at this stage --- bkctld | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bkctld b/bkctld index d5d68ca..4032bf9 100755 --- a/bkctld +++ b/bkctld @@ -14,7 +14,10 @@ set -u -[ "$(id -u)" -ne 0 ] && error "You need to be root to run ${0} !" +if [ "$(id -u)" -ne 0 ]; then + echo "You need to be root to run ${0} !" >&2 + exit 1 +fi basedir=$(dirname "$0") if [ "${basedir}" = "/usr/local/sbin" ] && [ -d "/usr/local/lib/bkctld" ]; then @@ -24,7 +27,8 @@ elif [ "${basedir}" = "/usr/sbin" ] && [ -d "/usr/lib/bkctld" ]; then elif [ -d './lib' ]; then LIBDIR='lib' else - error "Failed to find a suitable lib directory for bkctld." + echo "Failed to find a suitable lib directory for bkctld." >&2 + exit 1 fi # shellcheck source=lib/includes From 4c9cbf976e714733b782ced546c9748739bf14fa Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Sat, 18 Apr 2020 10:28:06 +0200 Subject: [PATCH 3/6] subcommand options are examined in their respective context --- bkctld | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/bkctld b/bkctld index 4032bf9..67e74b1 100755 --- a/bkctld +++ b/bkctld @@ -35,24 +35,21 @@ fi . "${LIBDIR}/includes" subcommand="${1:-}" -jail_name="${2:-}" -option="${3:-}" - -if [ ! -x "${LIBDIR}/bkctld-${subcommand}" ]; then - "${LIBDIR}/bkctld-help" && exit 1 -fi - case "${subcommand}" in "inc" | "rm" | "check" | "stats" | "help" | "list") "${LIBDIR}/bkctld-${subcommand}" ;; "init" | "is-on") + jail_name="${2:-}" "${LIBDIR}/bkctld-${subcommand}" "${jail_name}" ;; "key" | "port" | "ip") + jail_name="${2:-}" + option="${3:-}" "${LIBDIR}/bkctld-${subcommand}" "${jail_name}" "${option}" ;; "start" | "stop" | "reload" | "restart" | "sync" | "update" | "remove" | "firewall") + jail_name="${2:-}" if [ "${jail_name}" = "all" ]; then "${LIBDIR}/bkctld-list" | xargs --no-run-if-empty --max-args=1 --max-procs=0 "${LIBDIR}/bkctld-${subcommand}" else @@ -60,10 +57,15 @@ case "${subcommand}" in fi ;; "status") + jail_name="${2:-}" if [ "${jail_name}" = "all" ] || [ -z "${jail_name}" ]; then "${LIBDIR}/bkctld-list" | xargs --no-run-if-empty --max-args=1 "${LIBDIR}/bkctld-${subcommand}" else "${LIBDIR}/bkctld-${subcommand}" "${jail_name}" fi ;; + *) + "${LIBDIR}/bkctld-help" + exit 1 + ;; esac From 0dcd72d3f2edc1beac42dd9455413107865077a2 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Sat, 18 Apr 2020 10:28:44 +0200 Subject: [PATCH 4/6] move relative_date() function to includes files --- lib/bkctld-rm | 10 ---------- lib/includes | 10 ++++++++++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/bkctld-rm b/lib/bkctld-rm index 05e7cf9..5e4f8fe 100755 --- a/lib/bkctld-rm +++ b/lib/bkctld-rm @@ -7,16 +7,6 @@ # shellcheck source=./includes LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes" -relative_date() { - format=$(echo $1 | cut -d'.' -f1) - time_jump=$(echo $1 | cut -d'.' -f2) - - reference_date=$(date "${format}") - past_date=$(date --date "${reference_date} ${time_jump}" +"%Y-%m-%d") - - echo ${past_date} -} - delete_inc_btrfs() { jail_name=$1 inc_name=$2 diff --git a/lib/includes b/lib/includes index 86608c2..a33fed6 100755 --- a/lib/includes +++ b/lib/includes @@ -158,6 +158,16 @@ current_jail_check_policy_file() { echo "" fi } +# relative_date "+%Y-%m-%d.-2day" +relative_date() { + format=$(echo $1 | cut -d'.' -f1) + time_jump=$(echo $1 | cut -d'.' -f2) + + reference_date=$(date "${format}") + past_date=$(date --date "${reference_date} ${time_jump}" +"%Y-%m-%d") + + echo ${past_date} +} setup_jail_chroot() { jail_name=${1:?} From f7e8324ba57ed0ec13c41206c35f295aa1f19991 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Sat, 18 Apr 2020 10:29:21 +0200 Subject: [PATCH 5/6] Embed check-incs and check-last-incs in bkctld --- CHANGELOG.md | 24 ++++++++++----- bkctld | 11 +++++++ check-incs.sh | 61 -------------------------------------- check-last-incs.sh | 20 ------------- lib/bkctld-check-incs | 53 +++++++++++++++++++++++++++++++++ lib/bkctld-check-last-incs | 27 +++++++++++++++++ lib/includes | 9 ++++++ test/checks.bats | 21 +++++++++++++ 8 files changed, 137 insertions(+), 89 deletions(-) delete mode 100644 check-incs.sh delete mode 100644 check-last-incs.sh create mode 100755 lib/bkctld-check-incs create mode 100755 lib/bkctld-check-last-incs diff --git a/CHANGELOG.md b/CHANGELOG.md index 98eae12..ad2cc73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +### Changed + +* check-incs.sh and check-last-incs.sh are embedded in bkctld + +### Deprecated + +### Removed + +### Fixed + +### Security + +## [2.2.0] - 2020-04-17 + +### Added + * Shellcheck directives to have 0 warnings and errors * Ability to override critical/warning thresholds per jail for bkctld-check * Support new location for jail configuration (/etc/evobackup/.d/) @@ -28,11 +44,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Split BATS tests file and use helper functions * Improve "lib" detection * Revamp the README - -### Deprecated - -### Removed - -### Fixed - -### Security diff --git a/bkctld b/bkctld index 67e74b1..080bd29 100755 --- a/bkctld +++ b/bkctld @@ -39,6 +39,17 @@ case "${subcommand}" in "inc" | "rm" | "check" | "stats" | "help" | "list") "${LIBDIR}/bkctld-${subcommand}" ;; + "check-incs") + option="${2:-}" + if [ "${option}" = "all" ] || [ -z "${option}" ]; then + "${LIBDIR}/bkctld-check-incs" + elif [ "${option}" = "last" ]; then + "${LIBDIR}/bkctld-check-last-incs" + else + "${LIBDIR}/bkctld-help" + exit 1 + fi + ;; "init" | "is-on") jail_name="${2:-}" "${LIBDIR}/bkctld-${subcommand}" "${jail_name}" diff --git a/check-incs.sh b/check-incs.sh deleted file mode 100644 index e9ca85a..0000000 --- a/check-incs.sh +++ /dev/null @@ -1,61 +0,0 @@ -#!/bin/sh - -EVOBACKUP_CONFIGS="/etc/evobackup/*" - -relative_date() { - format=$(echo $1 | cut -d'.' -f1) - time_jump=$(echo $1 | cut -d'.' -f2) - - reference_date=$(date "${format}") - past_date=$(date --date "${reference_date} ${time_jump}" +"%Y-%m-%d") - - echo ${past_date} -} -inc_exists() { - ls -d /backup/incs/$1 > /dev/null 2>&1 -} -jail_exists() { - ls -d /backup/jails/$1 > /dev/null 2>&1 -} -# default return value is 0 (succes) -rc=0 -# loop for each configured jail -for file in ${EVOBACKUP_CONFIGS}; do - jail_name=$(basename ${file}) - - # check if jail is present - if jail_exists ${jail_name}; then - today=$(date +"%s") - # get jail last configuration date - jail_config_age=$(date --date "$(stat -c %y ${file})" +"%s") - - # loop for each line in jail configuration - for line in $(cat ${file}); do - # inc date in ISO format - inc_date=$(relative_date ${line}) - # inc date in seconds from epoch - inc_age=$(date --date "${inc_date}" +"%s") - - # skip line if date is inthe future - if [ "${inc_age}" -gt "${today}" ]; then - echo "INFO: no inc expected for ${inc_date} \`${jail_name}'" - else - # check if the configuration changed after the inc date - if [ "${jail_config_age}" -lt "${inc_age}" ]; then - # Error if inc is not found - if ! inc_exists ${jail_name}/${inc_date}*; then - echo "ERROR: inc is missing \`${jail_name}/${inc_date}'" >&2 - rc=1 - fi - else - echo "INFO: no inc expected for ${inc_date} \`${jail_name}'" - fi - fi - done - else - echo "ERROR: jail is missing \`${jail_name}'" >&2 - rc=1 - fi -done - -exit $rc diff --git a/check-last-incs.sh b/check-last-incs.sh deleted file mode 100644 index cefcb89..0000000 --- a/check-last-incs.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh - -inc_exists() { - ls -d /backup/incs/$1 > /dev/null 2>&1 -} -# default return value is 0 (succes) -rc=0 -# loop for each found jail -for file in /backup/jails/*; do - jail_name=$(basename ${file}) - # inc date in seconds from epoch - inc_date=$(date --date "yesterday" +"%Y-%m-%d") - # Error if inc is not found - if ! inc_exists ${jail_name}/${inc_date}*; then - echo "ERROR: inc is missing \`${jail_name}/${inc_date}'" >&2 - rc=1 - fi -done - -exit $rc diff --git a/lib/bkctld-check-incs b/lib/bkctld-check-incs new file mode 100755 index 0000000..22cf1a8 --- /dev/null +++ b/lib/bkctld-check-incs @@ -0,0 +1,53 @@ +#!/bin/sh + +set -u + +# shellcheck source=./includes +LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes" + +# default return value is 0 (succes) +rc=0 +# loop for each configured jail +for jail_name in $(bkctld list); do + incs_policy_file=$(current_jail_incs_policy_file "${jail_name}") + + # Today in seconds from epoch + today_epoch=$(date +"%s") + # Today in ISO format + today_iso=$(date +"%Y-%m-%d") + + # get jail last configuration date + jail_config_epoch=$(date --date "$(stat -c %y ${incs_policy_file})" +"%s") + + if [ -n "${incs_policy_file}" ]; then + # loop for each line in jail configuration + for line in $(cat ${incs_policy_file}); do + # inc date in ISO format + inc_iso=$(relative_date ${line}) + # inc date in seconds from epoch + inc_epoch=$(date --date "${inc_iso}" +"%s") + + # skip line if date is in the future + if [ "${inc_epoch}" -gt "${today_epoch}" ]; then + echo "INFO: ${jail_name} : no inc expected for ${inc_iso}" + else + # check if the configuration changed after the inc date + # or if it's today's inc + if [ "${jail_config_epoch}" -lt "${inc_epoch}" ] \ + || [ "${today_iso}" = "${inc_iso}" ]; then + # Error if inc is not found + if ! inc_exists "${jail_name}" "${inc_iso}*"; then + echo "ERROR: ${jail_name} : missing inc for ${inc_iso}" >&2 + rc=1 + fi + else + echo "INFO: ${jail_name} : no inc expected for ${inc_iso}" + fi + fi + done + else + echo "INFO: ${jail_name} : no inc expected at all" + fi +done + +exit $rc diff --git a/lib/bkctld-check-last-incs b/lib/bkctld-check-last-incs new file mode 100755 index 0000000..87218a2 --- /dev/null +++ b/lib/bkctld-check-last-incs @@ -0,0 +1,27 @@ +#!/bin/sh + +set -u + +# shellcheck source=./includes +LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes" + +# default return value is 0 (succes) +rc=0 +# loop for each found jail +for jail_name in $(bkctld list); do + incs_policy_file=$(current_jail_incs_policy_file "${jail_name}") + + if [ -n "${incs_policy_file}" ]; then + # inc date in seconds from epoch + inc_date=$(date +"%Y-%m-%d") + # Error if inc is not found + if ! inc_exists "${jail_name}" "${inc_date}*"; then + echo "ERROR: ${jail_name} : missing inc for ${inc_date}" >&2 + rc=1 + fi + else + echo "INFO: ${jail_name} : no inc expected at all" + fi +done + +exit $rc diff --git a/lib/includes b/lib/includes index a33fed6..ebf7b37 100755 --- a/lib/includes +++ b/lib/includes @@ -111,6 +111,15 @@ inc_path() { echo "${INCDIR}/${jail_name}/${inc_name}" } +# Test the existence of an inc pattern for a jail +inc_exists() { + jail_name=${1-?} + inc_pattern=${2-?} + inc_path=$(inc_path "${jail_name}" "${inc_pattern}") + + # inc_path must not be quoted because it can contain globs + ls -d ${inc_path} > /dev/null 2>&1 +} jail_config_dir() { jail_name=${1:?} diff --git a/test/checks.bats b/test/checks.bats index 079d1d7..77a7b4a 100644 --- a/test/checks.bats +++ b/test/checks.bats @@ -165,3 +165,24 @@ OUT run /usr/lib/bkctld/bkctld-check assert_equal "0" "$status" } + +@test "Check-last-incs OK if jail is present" { + /usr/lib/bkctld/bkctld-inc + + run /usr/lib/bkctld/bkctld-check-last-incs + assert_equal "0" "$status" +} + +@test "Check-last-incs Error if jail is missing" { + + run /usr/lib/bkctld/bkctld-check-last-incs + assert_equal "1" "$status" +} + +@test "Check-incs OK" { + /usr/lib/bkctld/bkctld-inc + + run /usr/lib/bkctld/bkctld-check-incs + assert_equal "0" "$status" +} +# TODO: write many more tests for bkctld-check-incs From 7d9043acc97fa58dbc6dbad6cf687999ae8042ec Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Sat, 18 Apr 2020 10:34:50 +0200 Subject: [PATCH 6/6] Release 2.2.1 --- CHANGELOG.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad2cc73..c75b813 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,8 +10,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -* check-incs.sh and check-last-incs.sh are embedded in bkctld - ### Deprecated ### Removed @@ -20,6 +18,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Security +## [2.2.1] - 2020-04-18 + +### Changed + +* check-incs.sh and check-last-incs.sh are embedded in bkctld + ## [2.2.0] - 2020-04-17 ### Added