Files
gniza4cp/lib/pkgacct.sh
shuki 1f68ea1058 Security hardening, static analysis fixes, and expanded test coverage
- Fix CRITICAL: safe config parser replacing shell source, sshpass -e,
  CSRF with /dev/urandom, symlink-safe file I/O
- Fix HIGH: input validation for timestamps/accounts, path traversal
  prevention in Runner.pm, AJAX CSRF on all endpoints
- Fix MEDIUM: umask 077, chmod 700 on config dirs, Config.pm TOCTOU lock,
  rsync exit code capture bug, RSYNC_EXTRA_OPTS character validation
- ShellCheck: fix word-splitting in notify.sh, safe rm in pkgacct.sh,
  suppress cross-file SC2034 false positives
- Perl::Critic: return undef→bare return, return (sort), unpack @_,
  explicit return on void subs, rename Config::write→save
- Remove dead code: enforce_retention_all(), rsync_dry_run()
- Add require_cmd checks for rsync/ssh/hostname/gzip at startup
- Escape $hint/$tip in CGI helper functions for defense-in-depth
- Expand tests from 17→40: validate_timestamp, validate_account_name,
  _safe_source_config (including malicious input), numeric validation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-04 23:57:26 +02:00

75 lines
2.0 KiB
Bash

#!/usr/bin/env bash
# gniza/lib/pkgacct.sh — pkgacct execution, .sql gzipping, temp cleanup
run_pkgacct() {
local user="$1"
local temp_dir="${TEMP_DIR:-$DEFAULT_TEMP_DIR}"
local output_dir="$temp_dir/$user"
mkdir -p "$temp_dir" || {
log_error "Failed to create temp directory: $temp_dir"
return 1
}
# Clean any previous attempt
[[ -d "$output_dir" ]] && rm -rf "$output_dir"
log_info "Running pkgacct for $user..."
log_debug "CMD: /usr/local/cpanel/bin/pkgacct --incremental --nocompress --backup --skiphomedir $user $temp_dir"
if ! /usr/local/cpanel/bin/pkgacct --incremental --nocompress --backup --skiphomedir "$user" "$temp_dir"; then
log_error "pkgacct failed for $user"
return 1
fi
if [[ ! -d "$output_dir" ]]; then
log_error "pkgacct output directory not found: $output_dir"
return 1
fi
log_info "pkgacct completed for $user"
return 0
}
gzip_sql_files() {
local user="$1"
local temp_dir="${TEMP_DIR:-$DEFAULT_TEMP_DIR}"
local mysql_dir="$temp_dir/$user/mysql"
if [[ ! -d "$mysql_dir" ]]; then
log_debug "No mysql directory for $user, skipping gzip"
return 0
fi
local count=0
while IFS= read -r -d '' sql_file; do
log_debug "Compressing: $sql_file"
if gzip -f "$sql_file"; then
((count++)) || true
else
log_warn "Failed to gzip: $sql_file"
fi
done < <(find "$mysql_dir" -name "*.sql" -print0)
log_info "Compressed $count SQL file(s) for $user"
return 0
}
cleanup_pkgacct() {
local user="$1"
local temp_dir="${TEMP_DIR:-$DEFAULT_TEMP_DIR}"
local output_dir="$temp_dir/$user"
if [[ -d "$output_dir" ]]; then
rm -rf "$output_dir"
log_debug "Cleaned up pkgacct temp for $user"
fi
}
cleanup_all_temp() {
local temp_dir="${TEMP_DIR:-$DEFAULT_TEMP_DIR}"
if [[ -d "$temp_dir" ]]; then
rm -rf "${temp_dir:?}"/*
log_debug "Cleaned up all temp contents: $temp_dir"
fi
}