update mail-status

Path Validation (Directory Traversal): Added a strict check for the --log-dir argument. The script now only accepts /var/log or /var/log/mail to prevent it from processing malicious files if a user tries to pass an arbitrary path.

Secure Temp Files: Replaced the hardcoded /tmp/ directory with the environment-aware ${TMPDIR:-/tmp}/ everywhere mktemp is used, so the script respects the host operating system's settings.

LOCK File Security: Appended the user identifier (${UID}) to the LOCK file name (e.g., postfix_summary_${UID}.lock). This prevents a scenario where one user locks out execution for all other users on the system.
This commit is contained in:
2026-09-07 17:15:16 +02:00
parent cb3678ed5e
commit fdacdf8dae
+11 -5
View File
@@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# Postfix Mail Summary Tool - PRODUCTION READY EDITION (v4.0.7) # Postfix Mail Summary Tool - PRODUCTION READY EDITION (v4.0.8)
set -euo pipefail set -euo pipefail
IFS=$'\n\t' IFS=$'\n\t'
@@ -134,14 +134,20 @@ if [[ "$WATCH_MODE" -eq 1 && "$REGEX_MODE" -eq 1 ]]; then
exit 1 exit 1
fi fi
# SECURITY SAFEGUARD: Path Validation for --log-dir (Directory Traversal Protection)
if [[ "$LOG_DIR" != "/var/log" && "$LOG_DIR" != "/var/log/mail" ]]; then
echo "Error: Unsafe log directory specified ($LOG_DIR). Only /var/log or /var/log/mail are allowed." >&2
exit 1
fi
# ============================================================================== # ==============================================================================
# 3. RESOURCE LIMITS & LOCKFILES (PRODUCTION SAFEGUARDS) # 3. RESOURCE LIMITS & LOCKFILES (PRODUCTION SAFEGUARDS)
# ============================================================================== # ==============================================================================
if [[ "$WATCH_MODE" -eq 1 ]]; then if [[ "$WATCH_MODE" -eq 1 ]]; then
LOCKFILE="/tmp/postfix_watch.lock" LOCKFILE="${TMPDIR:-/tmp}/postfix_watch_${UID}.lock"
ulimit -v 500000 ulimit -v 500000
else else
LOCKFILE="/tmp/postfix_summary.lock" LOCKFILE="${TMPDIR:-/tmp}/postfix_summary_${UID}.lock"
ulimit -t 15 ulimit -t 15
ulimit -v 500000 ulimit -v 500000
ulimit -f 102400 ulimit -f 102400
@@ -153,7 +159,7 @@ if ! flock -n 9; then
exit 1 exit 1
fi fi
TMP_DATA=$(mktemp /tmp/postfix_log.XXXXXX) TMP_DATA=$(mktemp "${TMPDIR:-/tmp}/postfix_log.XXXXXX")
trap 'rm -f "$TMP_DATA" 2>/dev/null' EXIT trap 'rm -f "$TMP_DATA" 2>/dev/null' EXIT
# ============================================================================== # ==============================================================================
@@ -181,7 +187,7 @@ get_log_stream() {
done < <(find "$LOG_DIR" -maxdepth 1 \( -name 'maillog*' -o -name 'mail.log*' \) -type f -print0 2>/dev/null | sort -z -r) done < <(find "$LOG_DIR" -maxdepth 1 \( -name 'maillog*' -o -name 'mail.log*' \) -type f -print0 2>/dev/null | sort -z -r)
if [ ${#LOG_FILES[@]} -eq 0 ]; then if [ ${#LOG_FILES[@]} -eq 0 ]; then
echo "Error: No Postfix logs found." >&2; exit 1 echo "Error: No Postfix logs found in $LOG_DIR." >&2; exit 1
fi fi
for log in "${LOG_FILES[@]}"; do for log in "${LOG_FILES[@]}"; do