Cleaned up exit codes
parent
13ed8bd708
commit
442fefab32
|
@ -5,20 +5,20 @@
|
|||
# QUERYSTRING: ?t=$token&host=$hostname&user=$username
|
||||
# ERRORS:
|
||||
# 3: bad args/usage
|
||||
# 4: vars file not found
|
||||
# 5: Wireguard not installed
|
||||
# 6: Hostname in use
|
||||
# 7: Hostname too short
|
||||
# 4: Wireguard not installed
|
||||
# 5: vars file not found
|
||||
# 6: Servers file not found
|
||||
# 7: Token file not found
|
||||
# 8: Invalid token
|
||||
# 9: Token file not found
|
||||
# 10: Failed to get peer data from wg
|
||||
# 11: Failed to generate new IPs
|
||||
# 12: Servers file doesn't exist
|
||||
# 13: Hostname or username not provided
|
||||
# 14: Failed to parse out user's peers
|
||||
# 15: Failed to add user to local wireguard
|
||||
# 16: Failed to add user to federated server
|
||||
# 17: Admin not found
|
||||
# 9: Hostname or username not provided
|
||||
# 10: Hostname too short
|
||||
# 11: Username too short
|
||||
# 12: User already exists
|
||||
# 13: Wireguard failed to fetch peers
|
||||
# 14: Failed to create IP addresses
|
||||
# 15: Failed to create directory for user SSL certs
|
||||
# 16: Failed to add user to wireguard
|
||||
# 17: Failed to send user to federated wg server
|
||||
|
||||
CONFIG_FILE='/etc/wgapi/config'
|
||||
SERVERS_FILE='/etc/wgapi/servers'
|
||||
|
@ -27,18 +27,18 @@ if ! [ ${#} -eq 2 ]; then
|
|||
exit 3
|
||||
fi & if ! [ -x '/usr/bin/wg' ]; then
|
||||
printf 'ERROR! %s could not find /usr/bin/wg\n' "${0}" >>"${LOGFILE}"
|
||||
exit 5
|
||||
exit 4
|
||||
fi & if ! [ -f "${CONFIG_FILE}" ]; then
|
||||
printf 'ERROR! %s could not find %s!\n' "${0}" "${CONFIG_FILE}" >>"${LOGFILE}"
|
||||
exit 4
|
||||
exit 5
|
||||
fi
|
||||
source "${CONFIG_FILE}"
|
||||
if ! [ -f "${SERVERS_FILE}" ]; then
|
||||
printf 'ERROR! %s could not find %s!\n' "${0}" "${SERVERS_FILE}" >>"${LOGFILE}"
|
||||
exit 12
|
||||
exit 6
|
||||
fi & if ! [ -f "${TOKENS_FILE}" ]; then
|
||||
printf 'ERROR! %s could not find %s!\n' "${0}" "${TOKENS_FILE}" >>"${LOGFILE}"
|
||||
exit 9
|
||||
exit 7
|
||||
fi
|
||||
ip="${1}"
|
||||
qs="$(<<<"${2}" tr '&' '\n' | sed 's/?//')"
|
||||
|
@ -60,31 +60,31 @@ username="$(<<<"${qs}" grep -oP 'user=(.*)' | sed 's/^user=//' | xargs | tr -dc
|
|||
if ! domain="${hostname:?}.${username:?}.${TLD:?}"; then
|
||||
printf 'ERROR! Hostname "%s" or username "%s" or tld "%s" missing!\n' "${hostname}" "${username}" "${TLD}" >>"${LOGFILE}"
|
||||
printf 'Hostname or username missing!\n' | "${LIB_DIR}/http_res" 400
|
||||
exit 13
|
||||
exit 9
|
||||
else
|
||||
printf 'Admin %s requested new user created with initial peer of %s\n' "${ip}" "${domain}" >>"${LOGFILE}"
|
||||
fi
|
||||
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
|
||||
exit 10
|
||||
elif ! [[ ${#username} -ge 3 ]]; then
|
||||
printf 'Rejecting username %s because it is too short.\n' "${username}" >>"${LOGFILE}"
|
||||
printf 'Username too short\n' | "${LIB_DIR}/http_res" 400
|
||||
exit 7
|
||||
exit 11
|
||||
fi
|
||||
|
||||
# Check if user already exists
|
||||
if "${LIB_DIR}/ns_lookup_rxfr" | grep ".${username}.${TLD}" >/dev/null; then
|
||||
printf 'User %s already exists!\n' "${username}" | tee -a "${LOGFILE}" | "${LIB_DIR}/http_res" 409
|
||||
exit 6
|
||||
exit 12
|
||||
fi
|
||||
|
||||
# Get all peer IPs
|
||||
if ! peers="$(sudo /usr/bin/wg show "${TLD}" allowed-ips)"; then
|
||||
printf 'ERROR! Wireguard failed!\n' >>"${LOGFILE}"
|
||||
printf 'Wireguard failed!\n' | "${LIB_DIR}/http_res" 500
|
||||
exit 5
|
||||
exit 13
|
||||
fi
|
||||
|
||||
# Create new IPs
|
||||
|
@ -101,7 +101,7 @@ ipv6="${IPV6_NET%:*:*}:${usernumber}:${hostnumber}"
|
|||
if ! printf 'IP addresses for %s created: %s %s\n' "${domain:?}" "${ipv4:?}" "${ipv6:?}" \
|
||||
>>"${LOGFILE}"; then
|
||||
printf 'ERROR! Failed to create IP addresses for %s!' "${domain}" >>"${LOGFILE}"
|
||||
exit 11
|
||||
exit 14
|
||||
fi
|
||||
|
||||
# Create wg config
|
||||
|
@ -118,7 +118,7 @@ fi &
|
|||
# Create SSL cert
|
||||
if ! sudo mkdir "${SSL_CONFIG_DIR:?}/${username:?}/"; then
|
||||
printf 'Failed to create directory %s/%s/:\n' "${SSL_CONFIG_DIR}" "${username}" >>"${LOGFILE}"
|
||||
exit 7
|
||||
exit 15
|
||||
fi
|
||||
if "${LIB_DIR}/ssl_peer_add" "${hostname:?}" "${username:?}" "IP:${ipv4},IP:${ipv6}"
|
||||
then printf 'Successfully signed SSL certs for %s\n' "${domain}" >>"${LOGFILE}"
|
||||
|
@ -138,7 +138,7 @@ while IFS=$'\t' read -r server_hostname server_ipv4 server_ipv6 server_pubkey se
|
|||
else
|
||||
printf 'ERROR! Failed to add %s to local wireguard server!\n' "${domain}" >>"${LOGFILE}"
|
||||
# TODO: clear existing progress
|
||||
exit 15
|
||||
exit 16
|
||||
fi
|
||||
# TODO: Send new user config to federated server
|
||||
#else
|
||||
|
@ -147,7 +147,7 @@ while IFS=$'\t' read -r server_hostname server_ipv4 server_ipv6 server_pubkey se
|
|||
#else
|
||||
# printf 'ERROR! Failed to send %s to remote wireguard server %s!\n' "${domain}" "${server_hostname}" >>"${LOGFILE}"
|
||||
# # TODO: clear existing progress
|
||||
# exit 16
|
||||
# exit 17
|
||||
#fi
|
||||
fi
|
||||
done <"${SERVERS_FILE}"
|
||||
|
|
Loading…
Reference in New Issue