Fix checking for hostname duplicates

master
Keith Irwin 2022-09-10 14:44:10 -06:00
parent 6ed23a9d08
commit 6af817636a
Signed by: ki9
GPG Key ID: DF773B3F4A88DA86
1 changed files with 6 additions and 6 deletions

View File

@ -31,11 +31,11 @@ qs="$(<<<"${2}" tr '&' '\n' | sed 's/?//')"
# Check hostname
hostname="$(<<<"${qs}" grep -oP 'name=(.*)' | sed 's/^name//' | xargs | tr -dc 'a-z0-9' | head -c10)"
printf '%s requested new peer with hostname %s\n' "${ip}" "${hostname}" >>"${LOGFILE}"
[[ ${#hostname} -ge 3 ]] || (
if ! [[ ${#hostname} -ge 3 ]]; then
printf 'Rejecting hostname %s because it is too short.\n' "${hostname}" >>"${LOGFILE}"
printf 'Hostname too short\n' | "${LIB_DIR}/http_res" 400
exit 7
)
fi
# Check token
token_fail(){
@ -58,17 +58,17 @@ printf '%s identified as %s\n' "${ip}" "${username}" >>"${LOGFILE}"
domain="${hostname}.${username}.${TLD}"
# Check if new peer already exists
printf '%s' "${hostnames}" >>"${LOGFILE}"
printf "${hostname}" >>"${LOGFILE}"
printf '%s' "${hostnames}" | grep -o "${hostname}" && (
if "${LIB_DIR}/ns_lookup_send" "${domain}"; then
printf '%s.%s.%s already exists!\n' "${hostname}" "${username}" "${TLD}" >>"${LOGFILE}"
printf 'Hostname %s already exists!\n' "${hostname}" | "${LIB_DIR}/http_res" 409
exit 6
)
fi
# Create new domain
domain="${hostname:?}.${username:?}.${TLD:?}"
printf 'New domain will be %s\n' "${domain}" >>"${LOGFILE}"
# Collect/parse existing peer data
# Create new IPs
peers="$("${LIB_DIR}/wg_peer_list" "${1}" tsv)" || exit 10
hostnames="$(<<<"${peers}" awk '{print $1}' | cut -d'.' -f1)"