|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# run-shellcheck |
| 4 | +# |
| 5 | +# CIS Debian Hardening |
| 6 | +# |
| 7 | + |
| 8 | +# |
| 9 | +# Ensure chrony is configured with authorized timeserver (Automated) |
| 10 | +# |
| 11 | + |
| 12 | +set -e # One error, it's over |
| 13 | +set -u # One variable unset, it's over |
| 14 | + |
| 15 | +# shellcheck disable=2034 |
| 16 | +HARDENING_LEVEL=2 |
| 17 | +# shellcheck disable=2034 |
| 18 | +DESCRIPTION="Ensure chrony is configured with authorized timeserver." |
| 19 | + |
| 20 | +PACKAGE='chrony' |
| 21 | +SOURCES_DIR='/etc/chrony/sources.d' |
| 22 | +SOURCES_FILE="$SOURCES_DIR/authorized.sources" |
| 23 | +MAIN_CONF='/etc/chrony/chrony.conf' |
| 24 | + |
| 25 | +# Configurable via create_config |
| 26 | +CHRONY_TIME_SOURCES='' |
| 27 | + |
| 28 | +# Global state (0=success, 1=failure) |
| 29 | +CHRONY_AUTH_PKG_INSTALLED=1 |
| 30 | +CHRONY_AUTH_CONFIG_OK=1 |
| 31 | + |
| 32 | +# Check function to populate state |
| 33 | +chrony_auth_check() { |
| 34 | + CHRONY_AUTH_PKG_INSTALLED=1 |
| 35 | + CHRONY_AUTH_CONFIG_OK=1 |
| 36 | + |
| 37 | + is_pkg_installed "$PACKAGE" |
| 38 | + if [ "$FNRET" != 0 ]; then |
| 39 | + # Package not installed (1=not installed/failure) |
| 40 | + CHRONY_AUTH_PKG_INSTALLED=1 |
| 41 | + return |
| 42 | + fi |
| 43 | + # Package is installed (0=installed/success) |
| 44 | + CHRONY_AUTH_PKG_INSTALLED=0 |
| 45 | + |
| 46 | + # Check if sources.d directory is included in main config |
| 47 | + if [ -f "$MAIN_CONF" ]; then |
| 48 | + does_pattern_exist_in_file "$MAIN_CONF" "^sourcedir.*$SOURCES_DIR" |
| 49 | + if [ "$FNRET" != 0 ]; then |
| 50 | + # sourcedir not configured (1=not OK/failure) |
| 51 | + CHRONY_AUTH_CONFIG_OK=1 |
| 52 | + return |
| 53 | + fi |
| 54 | + else |
| 55 | + # Main config not found (1=not OK/failure) |
| 56 | + CHRONY_AUTH_CONFIG_OK=1 |
| 57 | + return |
| 58 | + fi |
| 59 | + |
| 60 | + # Check sources file |
| 61 | + if [ ! -f "$SOURCES_FILE" ]; then |
| 62 | + # Sources file doesn't exist (1=not OK/failure) |
| 63 | + CHRONY_AUTH_CONFIG_OK=1 |
| 64 | + return |
| 65 | + fi |
| 66 | + |
| 67 | + if [ -z "$CHRONY_TIME_SOURCES" ]; then |
| 68 | + # Cannot verify without configured sources (1=not OK/failure) |
| 69 | + CHRONY_AUTH_CONFIG_OK=1 |
| 70 | + return |
| 71 | + fi |
| 72 | + |
| 73 | + # Check if configured sources are present |
| 74 | + does_pattern_exist_in_file "$SOURCES_FILE" "$CHRONY_TIME_SOURCES" |
| 75 | + if [ "$FNRET" != 0 ]; then |
| 76 | + # Sources not found (1=not OK/failure) |
| 77 | + CHRONY_AUTH_CONFIG_OK=1 |
| 78 | + return |
| 79 | + fi |
| 80 | + |
| 81 | + # All checks passed (0=OK/success) |
| 82 | + CHRONY_AUTH_CONFIG_OK=0 |
| 83 | +} |
| 84 | + |
| 85 | +# This function will be called if the script status is on enabled / audit mode |
| 86 | +audit() { |
| 87 | + chrony_auth_check |
| 88 | + |
| 89 | + if [ "$CHRONY_AUTH_PKG_INSTALLED" -ne 0 ]; then |
| 90 | + crit "$PACKAGE is not installed" |
| 91 | + return |
| 92 | + fi |
| 93 | + ok "$PACKAGE is installed" |
| 94 | + |
| 95 | + if [ "$CHRONY_AUTH_CONFIG_OK" -ne 0 ]; then |
| 96 | + crit "Chrony configuration is not correct" |
| 97 | + else |
| 98 | + ok "Time sources correctly configured" |
| 99 | + fi |
| 100 | +} |
| 101 | + |
| 102 | +# This function will be called if the script status is on enabled mode |
| 103 | +apply() { |
| 104 | + if [ "$CHRONY_AUTH_PKG_INSTALLED" -ne 0 ]; then |
| 105 | + crit "$PACKAGE is not installed, cannot apply" |
| 106 | + return |
| 107 | + fi |
| 108 | + |
| 109 | + if [ "$CHRONY_AUTH_CONFIG_OK" -ne 0 ]; then |
| 110 | + # Ensure sourcedir directive exists in main config |
| 111 | + info "Ensuring sourcedir is configured in $MAIN_CONF" |
| 112 | + if [ -f "$MAIN_CONF" ]; then |
| 113 | + does_pattern_exist_in_file "$MAIN_CONF" "^sourcedir.*$SOURCES_DIR" |
| 114 | + if [ "$FNRET" != 0 ]; then |
| 115 | + backup_file "$MAIN_CONF" |
| 116 | + add_end_of_file "$MAIN_CONF" "sourcedir $SOURCES_DIR" |
| 117 | + fi |
| 118 | + fi |
| 119 | + |
| 120 | + # Create sources directory and file |
| 121 | + info "Creating chrony sources configuration" |
| 122 | + mkdir -p "$SOURCES_DIR" |
| 123 | + |
| 124 | + if [ -n "$CHRONY_TIME_SOURCES" ]; then |
| 125 | + echo "$CHRONY_TIME_SOURCES" >"$SOURCES_FILE" |
| 126 | + fi |
| 127 | + |
| 128 | + # Restart chronyd service |
| 129 | + info "Restarting chronyd service" |
| 130 | + is_systemctl_running |
| 131 | + if [ "$FNRET" = 0 ]; then |
| 132 | + systemctl restart chronyd |
| 133 | + else |
| 134 | + info "Systemd is not running, skipping service restart" |
| 135 | + fi |
| 136 | + else |
| 137 | + ok "Chrony configuration already correct" |
| 138 | + fi |
| 139 | +} |
| 140 | + |
| 141 | +# This function will check config parameters required |
| 142 | +check_config() { |
| 143 | + if [ -z "$CHRONY_TIME_SOURCES" ]; then |
| 144 | + crit "CHRONY_TIME_SOURCES is not configured" |
| 145 | + exit 128 |
| 146 | + fi |
| 147 | +} |
| 148 | + |
| 149 | +# This function will create the config file for this check with default values |
| 150 | +create_config() { |
| 151 | + cat <<EOF |
| 152 | +status=audit |
| 153 | +# Configuration for script: $SCRIPT_NAME |
| 154 | +# Put your authorized NTP time servers here in chrony.sources format |
| 155 | +# Example: pool 2.debian.pool.ntp.org iburst |
| 156 | +CHRONY_TIME_SOURCES='pool 2.debian.pool.ntp.org iburst' |
| 157 | +EOF |
| 158 | +} |
| 159 | + |
| 160 | +# Source Root Dir Parameter |
| 161 | +if [ -r /etc/default/cis-hardening ]; then |
| 162 | + # shellcheck source=../../debian/default |
| 163 | + . /etc/default/cis-hardening |
| 164 | +fi |
| 165 | +if [ -z "${CIS_LIB_DIR}" ]; then |
| 166 | + echo "There is no /etc/default/cis-hardening file nor cis-hardening directory in current environment." |
| 167 | + echo "Cannot source CIS_LIB_DIR variable, aborting." |
| 168 | + exit 128 |
| 169 | +fi |
| 170 | + |
| 171 | +# Main function, will call the proper functions given the configuration (audit, enabled, disabled) |
| 172 | +if [ -r "${CIS_LIB_DIR}"/main.sh ]; then |
| 173 | + # shellcheck source=../../lib/main.sh |
| 174 | + . "${CIS_LIB_DIR}"/main.sh |
| 175 | +else |
| 176 | + echo "Cannot find main.sh, have you correctly defined your root directory? Current value is ${CIS_LIB_DIR} in /etc/default/cis-hardening" |
| 177 | + exit 128 |
| 178 | +fi |
0 commit comments