fix(rpcd): Remove local keyword from backup/mailserver handlers

POSIX shell doesn't allow 'local' outside functions.
Fixed for busybox/ash compatibility.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
CyberMind-FR 2026-02-05 10:48:25 +01:00
parent ded107e408
commit 04d61baa6f
2 changed files with 61 additions and 66 deletions

View File

@ -27,8 +27,8 @@ case "$1" in
json_init json_init
# Get storage info # Get storage info
local storage=$($BACKUP_CMD status 2>/dev/null | grep "Storage Path" | cut -d: -f2 | tr -d ' ') storage=$($BACKUP_CMD status 2>/dev/null | grep "Storage Path" | cut -d: -f2 | tr -d ' ')
local used=$($BACKUP_CMD status 2>/dev/null | grep "Storage Used" | cut -d: -f2 | tr -d ' ') used=$($BACKUP_CMD status 2>/dev/null | grep "Storage Used" | cut -d: -f2 | tr -d ' ')
json_add_string "storage_path" "${storage:-/srv/backups}" json_add_string "storage_path" "${storage:-/srv/backups}"
json_add_string "storage_used" "${used:-0}" json_add_string "storage_used" "${used:-0}"
@ -36,10 +36,10 @@ case "$1" in
# Last backup times # Last backup times
json_add_object "last_backup" json_add_object "last_backup"
for type in config containers services; do for type in config containers services; do
local latest=$(ls -t "${storage:-/srv/backups}/$type/"*.tar* 2>/dev/null | head -1) latest=$(ls -t "${storage:-/srv/backups}/$type/"*.tar* 2>/dev/null | head -1)
if [ -n "$latest" ]; then if [ -n "$latest" ]; then
local date=$(stat -c %Y "$latest" 2>/dev/null) bdate=$(stat -c %Y "$latest" 2>/dev/null)
json_add_int "$type" "${date:-0}" json_add_int "$type" "${bdate:-0}"
else else
json_add_int "$type" 0 json_add_int "$type" 0
fi fi
@ -47,7 +47,7 @@ case "$1" in
json_close_object json_close_object
# Container count # Container count
local containers=$(ls -d /srv/lxc/*/ 2>/dev/null | wc -l) containers=$(ls -d /srv/lxc/*/ 2>/dev/null | wc -l)
json_add_int "container_count" "$containers" json_add_int "container_count" "$containers"
json_dump json_dump
@ -58,7 +58,7 @@ case "$1" in
json_get_var type type json_get_var type type
type="${type:-all}" type="${type:-all}"
local storage=$(uci -q get backup.main.storage_path) storage=$(uci -q get backup.main.storage_path)
storage="${storage:-/srv/backups}" storage="${storage:-/srv/backups}"
json_init json_init
@ -66,7 +66,7 @@ case "$1" in
# List config backups # List config backups
if [ "$type" = "all" ] || [ "$type" = "config" ]; then if [ "$type" = "all" ] || [ "$type" = "config" ]; then
for f in "$storage/config/"*.tar* 2>/dev/null; do ls "$storage/config/"*.tar* 2>/dev/null | while read f; do
[ -f "$f" ] || continue [ -f "$f" ] || continue
json_add_object "" json_add_object ""
json_add_string "file" "$(basename "$f")" json_add_string "file" "$(basename "$f")"
@ -79,7 +79,7 @@ case "$1" in
# List container backups # List container backups
if [ "$type" = "all" ] || [ "$type" = "containers" ]; then if [ "$type" = "all" ] || [ "$type" = "containers" ]; then
for f in "$storage/containers/"*.tar* 2>/dev/null; do ls "$storage/containers/"*.tar* 2>/dev/null | while read f; do
[ -f "$f" ] || continue [ -f "$f" ] || continue
json_add_object "" json_add_object ""
json_add_string "file" "$(basename "$f")" json_add_string "file" "$(basename "$f")"
@ -92,7 +92,7 @@ case "$1" in
# List service backups # List service backups
if [ "$type" = "all" ] || [ "$type" = "services" ]; then if [ "$type" = "all" ] || [ "$type" = "services" ]; then
for f in "$storage/services/"*.tar* 2>/dev/null; do ls "$storage/services/"*.tar* 2>/dev/null | while read f; do
[ -f "$f" ] || continue [ -f "$f" ] || continue
json_add_object "" json_add_object ""
json_add_string "file" "$(basename "$f")" json_add_string "file" "$(basename "$f")"
@ -113,17 +113,17 @@ case "$1" in
for dir in /srv/lxc/*/; do for dir in /srv/lxc/*/; do
[ -d "$dir" ] || continue [ -d "$dir" ] || continue
local name=$(basename "$dir") name=$(basename "$dir")
[ -f "$dir/config" ] || continue [ -f "$dir/config" ] || continue
local state="stopped" state="stopped"
lxc-info -n "$name" 2>/dev/null | grep -q "RUNNING" && state="running" lxc-info -n "$name" 2>/dev/null | grep -q "RUNNING" && state="running"
local size=$(du -sh "$dir" 2>/dev/null | awk '{print $1}') size=$(du -sh "$dir" 2>/dev/null | awk '{print $1}')
# Count backups for this container # Count backups for this container
local storage=$(uci -q get backup.main.storage_path) storage=$(uci -q get backup.main.storage_path)
storage="${storage:-/srv/backups}" storage="${storage:-/srv/backups}"
local backup_count=$(ls -1 "$storage/containers/${name}-"*.tar* 2>/dev/null | wc -l) backup_count=$(ls -1 "$storage/containers/${name}-"*.tar* 2>/dev/null | wc -l)
json_add_object "" json_add_object ""
json_add_string "name" "$name" json_add_string "name" "$name"
@ -142,9 +142,8 @@ case "$1" in
json_get_var type type json_get_var type type
type="${type:-full}" type="${type:-full}"
local result
result=$($BACKUP_CMD create --$type 2>&1) result=$($BACKUP_CMD create --$type 2>&1)
local rc=$? rc=$?
json_init json_init
json_add_int "code" "$rc" json_add_int "code" "$rc"
@ -165,12 +164,11 @@ case "$1" in
exit 0 exit 0
} }
local opts="" opts=""
[ "$dry_run" = "1" ] || [ "$dry_run" = "true" ] && opts="--dry-run" [ "$dry_run" = "1" ] || [ "$dry_run" = "true" ] && opts="--dry-run"
local result
result=$($BACKUP_CMD restore "$file" $opts 2>&1) result=$($BACKUP_CMD restore "$file" $opts 2>&1)
local rc=$? rc=$?
json_init json_init
json_add_int "code" "$rc" json_add_int "code" "$rc"
@ -179,9 +177,8 @@ case "$1" in
;; ;;
cleanup) cleanup)
local result
result=$($BACKUP_CMD cleanup 2>&1) result=$($BACKUP_CMD cleanup 2>&1)
local rc=$? rc=$?
json_init json_init
json_add_int "code" "$rc" json_add_int "code" "$rc"
@ -201,9 +198,8 @@ case "$1" in
exit 0 exit 0
} }
local result
result=$($BACKUP_CMD container backup "$name" 2>&1) result=$($BACKUP_CMD container backup "$name" 2>&1)
local rc=$? rc=$?
json_init json_init
json_add_int "code" "$rc" json_add_int "code" "$rc"
@ -224,9 +220,8 @@ case "$1" in
exit 0 exit 0
} }
local result
result=$($BACKUP_CMD container restore "$name" "$file" 2>&1) result=$($BACKUP_CMD container restore "$name" "$file" 2>&1)
local rc=$? rc=$?
json_init json_init
json_add_int "code" "$rc" json_add_int "code" "$rc"

View File

@ -37,41 +37,41 @@ case "$1" in
status) status)
json_init json_init
local enabled=$(uci -q get $CONFIG.main.enabled) enabled=$(uci -q get $CONFIG.main.enabled)
local container=$(uci -q get $CONFIG.main.container) container=$(uci -q get $CONFIG.main.container)
container="${container:-mailserver}" container="${container:-mailserver}"
local domain=$(uci -q get $CONFIG.main.domain) domain=$(uci -q get $CONFIG.main.domain)
local hostname=$(uci -q get $CONFIG.main.hostname) hostname=$(uci -q get $CONFIG.main.hostname)
hostname="${hostname:-mail}" hostname="${hostname:-mail}"
local data_path=$(uci -q get $CONFIG.main.data_path) data_path=$(uci -q get $CONFIG.main.data_path)
data_path="${data_path:-/srv/mailserver}" data_path="${data_path:-/srv/mailserver}"
# Container state # Container state
local state="stopped" state="stopped"
lxc-info -n "$container" 2>/dev/null | grep -q "RUNNING" && state="running" lxc-info -n "$container" 2>/dev/null | grep -q "RUNNING" && state="running"
# User count # User count
local user_count=0 user_count=0
[ -f "$data_path/config/users" ] && user_count=$(wc -l < "$data_path/config/users" 2>/dev/null || echo "0") [ -f "$data_path/config/users" ] && user_count=$(wc -l < "$data_path/config/users" 2>/dev/null || echo "0")
# Storage # Storage
local storage=$(du -sh "$data_path" 2>/dev/null | awk '{print $1}') storage=$(du -sh "$data_path" 2>/dev/null | awk '{print $1}')
# SSL status # SSL status
local ssl_valid=0 ssl_valid=0
if [ -f "$data_path/ssl/fullchain.pem" ]; then if [ -f "$data_path/ssl/fullchain.pem" ]; then
local expiry=$(openssl x509 -in "$data_path/ssl/fullchain.pem" -noout -enddate 2>/dev/null | cut -d= -f2) expiry=$(openssl x509 -in "$data_path/ssl/fullchain.pem" -noout -enddate 2>/dev/null | cut -d= -f2)
[ -n "$expiry" ] && ssl_valid=1 [ -n "$expiry" ] && ssl_valid=1
fi fi
# Webmail # Webmail
local webmail_container=$(uci -q get $CONFIG.webmail.container) webmail_container=$(uci -q get $CONFIG.webmail.container)
webmail_container="${webmail_container:-secubox-webmail}" webmail_container="${webmail_container:-secubox-webmail}"
local webmail_running=0 webmail_running=0
docker ps 2>/dev/null | grep -q "$webmail_container" && webmail_running=1 docker ps 2>/dev/null | grep -q "$webmail_container" && webmail_running=1
# Mesh # Mesh
local mesh_enabled=$(uci -q get $CONFIG.mesh.enabled) mesh_enabled=$(uci -q get $CONFIG.mesh.enabled)
json_add_boolean "enabled" "${enabled:-0}" json_add_boolean "enabled" "${enabled:-0}"
json_add_string "state" "$state" json_add_string "state" "$state"
@ -101,7 +101,7 @@ case "$1" in
;; ;;
user_list) user_list)
local data_path=$(uci -q get $CONFIG.main.data_path) data_path=$(uci -q get $CONFIG.main.data_path)
data_path="${data_path:-/srv/mailserver}" data_path="${data_path:-/srv/mailserver}"
json_init json_init
@ -110,11 +110,11 @@ case "$1" in
if [ -f "$data_path/config/users" ]; then if [ -f "$data_path/config/users" ]; then
while IFS=: read -r email hash; do while IFS=: read -r email hash; do
[ -z "$email" ] && continue [ -z "$email" ] && continue
local domain=$(echo "$email" | cut -d@ -f2) udomain=$(echo "$email" | cut -d@ -f2)
local user=$(echo "$email" | cut -d@ -f1) user=$(echo "$email" | cut -d@ -f1)
local maildir="$data_path/mail/$domain/$user" maildir="$data_path/mail/$udomain/$user"
local size=$(du -sh "$maildir" 2>/dev/null | awk '{print $1}') size=$(du -sh "$maildir" 2>/dev/null | awk '{print $1}')
local count=$(find "$maildir" -type f 2>/dev/null | wc -l) count=$(find "$maildir" -type f 2>/dev/null | wc -l)
json_add_object "" json_add_object ""
json_add_string "email" "$email" json_add_string "email" "$email"
@ -129,17 +129,17 @@ case "$1" in
;; ;;
alias_list) alias_list)
local data_path=$(uci -q get $CONFIG.main.data_path) data_path=$(uci -q get $CONFIG.main.data_path)
data_path="${data_path:-/srv/mailserver}" data_path="${data_path:-/srv/mailserver}"
json_init json_init
json_add_array "aliases" json_add_array "aliases"
if [ -f "$data_path/config/valias" ]; then if [ -f "$data_path/config/valias" ]; then
while read -r alias target; do while read -r aalias target; do
[ -z "$alias" ] && continue [ -z "$aalias" ] && continue
json_add_object "" json_add_object ""
json_add_string "alias" "$alias" json_add_string "alias" "$aalias"
json_add_string "target" "$target" json_add_string "target" "$target"
json_close_object json_close_object
done < "$data_path/config/valias" done < "$data_path/config/valias"
@ -150,9 +150,9 @@ case "$1" in
;; ;;
webmail_status) webmail_status)
local webmail_container=$(uci -q get $CONFIG.webmail.container) webmail_container=$(uci -q get $CONFIG.webmail.container)
webmail_container="${webmail_container:-secubox-webmail}" webmail_container="${webmail_container:-secubox-webmail}"
local port=$(uci -q get $CONFIG.webmail.port) port=$(uci -q get $CONFIG.webmail.port)
port="${port:-8026}" port="${port:-8026}"
json_init json_init
@ -174,19 +174,19 @@ case "$1" in
json_get_var lines lines json_get_var lines lines
lines="${lines:-50}" lines="${lines:-50}"
local container=$(uci -q get $CONFIG.main.container) container=$(uci -q get $CONFIG.main.container)
container="${container:-mailserver}" container="${container:-mailserver}"
json_init json_init
local log_output=$(lxc-attach -n "$container" -- tail -n "$lines" /var/log/mail.log 2>/dev/null) log_output=$(lxc-attach -n "$container" -- tail -n "$lines" /var/log/mail.log 2>/dev/null)
json_add_string "logs" "$log_output" json_add_string "logs" "$log_output"
json_dump json_dump
;; ;;
install) install)
json_init json_init
local output=$($MAILCTL install 2>&1) output=$($MAILCTL install 2>&1)
local rc=$? rc=$?
json_add_int "code" "$rc" json_add_int "code" "$rc"
json_add_string "output" "$output" json_add_string "output" "$output"
json_dump json_dump
@ -223,7 +223,7 @@ case "$1" in
json_add_int "code" 1 json_add_int "code" 1
json_add_string "error" "Email required" json_add_string "error" "Email required"
else else
local output=$($MAILCTL user add "$email" "$password" 2>&1) output=$($MAILCTL user add "$email" "$password" 2>&1)
json_add_int "code" "$?" json_add_int "code" "$?"
json_add_string "output" "$output" json_add_string "output" "$output"
fi fi
@ -239,7 +239,7 @@ case "$1" in
json_add_int "code" 1 json_add_int "code" 1
json_add_string "error" "Email required" json_add_string "error" "Email required"
else else
local output=$($MAILCTL user del "$email" 2>&1) output=$($MAILCTL user del "$email" 2>&1)
json_add_int "code" "$?" json_add_int "code" "$?"
json_add_string "output" "$output" json_add_string "output" "$output"
fi fi
@ -256,7 +256,7 @@ case "$1" in
json_add_int "code" 1 json_add_int "code" 1
json_add_string "error" "Email and password required" json_add_string "error" "Email and password required"
else else
local output=$($MAILCTL user passwd "$email" "$password" 2>&1) output=$($MAILCTL user passwd "$email" "$password" 2>&1)
json_add_int "code" "$?" json_add_int "code" "$?"
json_add_string "output" "$output" json_add_string "output" "$output"
fi fi
@ -265,15 +265,15 @@ case "$1" in
alias_add) alias_add)
json_load "$3" json_load "$3"
json_get_var alias alias json_get_var aalias alias
json_get_var target target json_get_var target target
json_init json_init
if [ -z "$alias" ] || [ -z "$target" ]; then if [ -z "$aalias" ] || [ -z "$target" ]; then
json_add_int "code" 1 json_add_int "code" 1
json_add_string "error" "Alias and target required" json_add_string "error" "Alias and target required"
else else
local output=$($MAILCTL alias add "$alias" "$target" 2>&1) output=$($MAILCTL alias add "$aalias" "$target" 2>&1)
json_add_int "code" "$?" json_add_int "code" "$?"
json_add_string "output" "$output" json_add_string "output" "$output"
fi fi
@ -282,7 +282,7 @@ case "$1" in
dns_setup) dns_setup)
json_init json_init
local output=$($MAILCTL dns-setup 2>&1) output=$($MAILCTL dns-setup 2>&1)
json_add_int "code" "$?" json_add_int "code" "$?"
json_add_string "output" "$output" json_add_string "output" "$output"
json_dump json_dump
@ -290,7 +290,7 @@ case "$1" in
ssl_setup) ssl_setup)
json_init json_init
local output=$($MAILCTL ssl-setup 2>&1) output=$($MAILCTL ssl-setup 2>&1)
json_add_int "code" "$?" json_add_int "code" "$?"
json_add_string "output" "$output" json_add_string "output" "$output"
json_dump json_dump
@ -298,7 +298,7 @@ case "$1" in
webmail_configure) webmail_configure)
json_init json_init
local output=$($MAILCTL webmail configure 2>&1) output=$($MAILCTL webmail configure 2>&1)
json_add_int "code" "$?" json_add_int "code" "$?"
json_add_string "output" "$output" json_add_string "output" "$output"
json_dump json_dump
@ -306,7 +306,7 @@ case "$1" in
mesh_backup) mesh_backup)
json_init json_init
local output=$($MAILCTL mesh backup 2>&1) output=$($MAILCTL mesh backup 2>&1)
json_add_int "code" "$?" json_add_int "code" "$?"
json_add_string "output" "$output" json_add_string "output" "$output"
json_dump json_dump
@ -318,7 +318,7 @@ case "$1" in
mode="${mode:-push}" mode="${mode:-push}"
json_init json_init
local output=$($MAILCTL mesh sync "$mode" 2>&1) output=$($MAILCTL mesh sync "$mode" 2>&1)
json_add_int "code" "$?" json_add_int "code" "$?"
json_add_string "output" "$output" json_add_string "output" "$output"
json_dump json_dump