Skip to content

Commit 5db3d92

Browse files
authored
Add hostname validation and replace recursion for invalid inputs in advanced settings (#5291)
1 parent 796aaaa commit 5db3d92

File tree

1 file changed

+50
-31
lines changed

1 file changed

+50
-31
lines changed

misc/build.func

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ base_settings() {
370370
write_config() {
371371
mkdir -p /opt/community-scripts
372372
# This function writes the configuration to a file.
373-
if whiptail --backtitle "[dev] Proxmox VE Helper Scripts" --defaultno --title "Write configfile" --yesno "Do you want to write the selections to a config file?" 10 60; then
373+
if whiptail --backtitle "Proxmox VE Helper Scripts" --defaultno --title "Write configfile" --yesno "Do you want to write the selections to a config file?" 10 60; then
374374
FILEPATH="/opt/community-scripts/${NSAPP}.conf"
375375
if [[ ! -f $FILEPATH ]]; then
376376
cat <<EOF >"$FILEPATH"
@@ -403,7 +403,7 @@ EOF
403403
echo -e "${INFO}${BOLD}${GN}Writing configuration to ${FILEPATH}${CL}"
404404
else
405405
echo -e "${INFO}${BOLD}${RD}Configuration file already exists at ${FILEPATH}${CL}"
406-
if whiptail --backtitle "[dev] Proxmox VE Helper Scripts" --defaultno --title "Overwrite configfile" --yesno "Do you want to overwrite the existing config file?" 10 60; then
406+
if whiptail --backtitle "Proxmox VE Helper Scripts" --defaultno --title "Overwrite configfile" --yesno "Do you want to overwrite the existing config file?" 10 60; then
407407
rm -f "$FILEPATH"
408408
cat <<EOF >"$FILEPATH"
409409
# ${NSAPP} Configuration File
@@ -558,53 +558,72 @@ advanced_settings() {
558558
exit_script
559559
fi
560560

561-
if CT_NAME=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Hostname" 8 58 "$NSAPP" --title "HOSTNAME" 3>&1 1>&2 2>&3); then
562-
if [ -z "$CT_NAME" ]; then
563-
HN="$NSAPP"
561+
while true; do
562+
if CT_NAME=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Hostname" 8 58 "$NSAPP" --title "HOSTNAME" 3>&1 1>&2 2>&3); then
563+
if [ -z "$CT_NAME" ]; then
564+
HN="$NSAPP"
565+
else
566+
HN=$(echo "${CT_NAME,,}" | tr -d ' ')
567+
fi
568+
569+
if [[ "$HN" =~ ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$ ]]; then
570+
echo -e "${HOSTNAME}${BOLD}${DGN}Hostname: ${BGN}$HN${CL}"
571+
break
572+
else
573+
whiptail --backtitle "Proxmox VE Helper Scripts" \
574+
--msgbox "❌ Invalid hostname: '$HN'\n\nOnly lowercase letters, digits and hyphens (-) are allowed.\nUnderscores (_) or other characters are not permitted!" 10 70
575+
fi
564576
else
565-
HN=$(echo "${CT_NAME,,}" | tr -d ' ')
577+
exit_script
566578
fi
567-
echo -e "${HOSTNAME}${BOLD}${DGN}Hostname: ${BGN}$HN${CL}"
568-
else
569-
exit_script
570-
fi
579+
done
580+
581+
while true; do
582+
DISK_SIZE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Disk Size in GB" 8 58 "$var_disk" --title "DISK SIZE" 3>&1 1>&2 2>&3) || exit_script
571583

572-
if DISK_SIZE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Disk Size in GB" 8 58 "$var_disk" --title "DISK SIZE" 3>&1 1>&2 2>&3); then
573584
if [ -z "$DISK_SIZE" ]; then
574585
DISK_SIZE="$var_disk"
586+
fi
587+
588+
if [[ "$DISK_SIZE" =~ ^[1-9][0-9]*$ ]]; then
575589
echo -e "${DISKSIZE}${BOLD}${DGN}Disk Size: ${BGN}${DISK_SIZE} GB${CL}"
590+
break
576591
else
577-
if ! [[ $DISK_SIZE =~ $INTEGER ]]; then
578-
echo -e "{INFO}${HOLD}${RD} DISK SIZE MUST BE AN INTEGER NUMBER!${CL}"
579-
advanced_settings
580-
fi
581-
echo -e "${DISKSIZE}${BOLD}${DGN}Disk Size: ${BGN}${DISK_SIZE} GB${CL}"
592+
whiptail --msgbox "Disk size must be a positive integer!" 8 58
582593
fi
583-
else
584-
exit_script
585-
fi
594+
done
595+
596+
while true; do
597+
CORE_COUNT=$(whiptail --backtitle "Proxmox VE Helper Scripts" \
598+
--inputbox "Allocate CPU Cores" 8 58 "$var_cpu" --title "CORE COUNT" 3>&1 1>&2 2>&3) || exit_script
586599

587-
if CORE_COUNT=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate CPU Cores" 8 58 "$var_cpu" --title "CORE COUNT" 3>&1 1>&2 2>&3); then
588600
if [ -z "$CORE_COUNT" ]; then
589601
CORE_COUNT="$var_cpu"
602+
fi
603+
604+
if [[ "$CORE_COUNT" =~ ^[1-9][0-9]*$ ]]; then
590605
echo -e "${CPUCORE}${BOLD}${DGN}CPU Cores: ${BGN}$CORE_COUNT${CL}"
606+
break
591607
else
592-
echo -e "${CPUCORE}${BOLD}${DGN}CPU Cores: ${BGN}$CORE_COUNT${CL}"
608+
whiptail --msgbox "CPU core count must be a positive integer!" 8 58
593609
fi
594-
else
595-
exit_script
596-
fi
610+
done
611+
612+
while true; do
613+
RAM_SIZE=$(whiptail --backtitle "Proxmox VE Helper Scripts" \
614+
--inputbox "Allocate RAM in MiB" 8 58 "$var_ram" --title "RAM" 3>&1 1>&2 2>&3) || exit_script
597615

598-
if RAM_SIZE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate RAM in MiB" 8 58 "$var_ram" --title "RAM" 3>&1 1>&2 2>&3); then
599616
if [ -z "$RAM_SIZE" ]; then
600617
RAM_SIZE="$var_ram"
618+
fi
619+
620+
if [[ "$RAM_SIZE" =~ ^[1-9][0-9]*$ ]]; then
601621
echo -e "${RAMSIZE}${BOLD}${DGN}RAM Size: ${BGN}${RAM_SIZE} MiB${CL}"
622+
break
602623
else
603-
echo -e "${RAMSIZE}${BOLD}${DGN}RAM Size: ${BGN}${RAM_SIZE} MiB${CL}"
624+
whiptail --msgbox "RAM size must be a positive integer!" 8 58
604625
fi
605-
else
606-
exit_script
607-
fi
626+
done
608627

609628
BRIDGES=""
610629
IFACE_FILEPATH_LIST="/etc/network/interfaces"$'\n'$(find "/etc/network/interfaces.d/" -type f)
@@ -779,14 +798,14 @@ advanced_settings() {
779798
exit_script
780799
fi
781800

782-
SSH_AUTHORIZED_KEY="$(whiptail --backtitle "[dev] Proxmox VE Helper Scripts" --inputbox "SSH Authorized key for root (leave empty for none)" 8 58 --title "SSH Key" 3>&1 1>&2 2>&3)"
801+
SSH_AUTHORIZED_KEY="$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "SSH Authorized key for root (leave empty for none)" 8 58 --title "SSH Key" 3>&1 1>&2 2>&3)"
783802

784803
if [[ -z "${SSH_AUTHORIZED_KEY}" ]]; then
785804
SSH_AUTHORIZED_KEY=""
786805
fi
787806

788807
if [[ "$PW" == -password* || -n "$SSH_AUTHORIZED_KEY" ]]; then
789-
if (whiptail --backtitle "[dev] Proxmox VE Helper Scripts" --defaultno --title "SSH ACCESS" --yesno "Enable Root SSH Access?" 10 58); then
808+
if (whiptail --backtitle "Proxmox VE Helper Scripts" --defaultno --title "SSH ACCESS" --yesno "Enable Root SSH Access?" 10 58); then
790809
SSH="yes"
791810
else
792811
SSH="no"

0 commit comments

Comments
 (0)