You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.7 KiB

  1. #!/bin/bash
  2. set -eux
  3. NTFY_BROKER="${NTFY_BROKER:-core-01.0k.io}"
  4. ## Uncipher ntfy key to destination
  5. umask 077
  6. ntfy_key_ciphered="src/etc/ssh/ntfy-key"
  7. if [ ! -f "$ntfy_key_ciphered" ]; then
  8. echo "Error: ciphered ntfy key not found" >&2
  9. exit 1
  10. fi
  11. ntfy_key_dest=/etc/ssh/ntfy-key
  12. if [ ! -f "$ntfy_key_dest" ]; then
  13. cat "$ntfy_key_ciphered" |
  14. gpg -d --batch --yes --passphrase 'uniquepass' > "$ntfy_key_dest" || {
  15. echo "Error while unpacking ntfy key to '${ntfy_key_dest}'" >&2
  16. exit 1
  17. }
  18. fi
  19. ## Request token to ntfy server and add to config file
  20. known_host="/root/.ssh/known_hosts"
  21. if ! ssh-keygen -F "$NTFY_BROKER" -f "$known_host" >/dev/null; then
  22. ssh-keyscan -H "$NTFY_BROKER" >> "$known_host" || {
  23. echo "Error while adding '$NTFY_BROKER' to known_hosts" >&2
  24. exit 1
  25. }
  26. fi
  27. config_file="/etc/ntfy/ntfy.conf"
  28. mkdir -p "${config_file%/*}"
  29. if ! [ -f "$config_file" ]; then
  30. touch "$config_file" || {
  31. echo "Error: couldn’t create config file '$config_file'" >&2;
  32. exit 1
  33. }
  34. fi
  35. LOGIN=""
  36. PASSWORD=""
  37. source "$config_file" || {
  38. echo "Error: couldn't source config file '$config_file'" >&2
  39. exit 1
  40. }
  41. ## Note that we require the forcing of stdin to /dev/null to avoid
  42. ## the rest of the script to be vacuumed by the ssh command.
  43. ## This effect will only happen when launching this script in special
  44. ## conditions involving stdin.
  45. cred=$(ssh -i "$ntfy_key_dest" ntfy@"${NTFY_BROKER}" \
  46. request-token "$LOGIN" "$PASSWORD" </dev/null) || {
  47. echo "Error while requesting token to ntfy server" >&2
  48. exit 1
  49. }
  50. ## XXXvlab: ideally it should be received from the last call
  51. server="https://ntfy.0k.io/"
  52. login=$(printf "%q" "${cred%$'\n'*}")
  53. password=$(printf "%q" "${cred#*$'\n'}")
  54. ## check if password doesn't contain '%'
  55. for var in server login password; do
  56. if [ "${!var}" == "''" ] || [[ "${!var}" == *$'\n'* ]]; then
  57. echo "Error: empty or invalid multi-line values retrieved for '$var'" \
  58. "from ntfy server. Received:" >&2
  59. printf "%s" "$cred" | sed -r 's/^/ | /g' >&2
  60. exit 1
  61. fi
  62. if [[ "${!var}" == *%* ]]; then
  63. ## We need a separator char for sed replacement in the config file
  64. echo "Error: forbidden character '%' found in $var" >&2
  65. exit 1
  66. fi
  67. if grep -qE "^${var^^}=" "$config_file"; then
  68. sed -ri "s%^${var^^}=.*$%${var^^}=\"${!var}\"%g" "$config_file"
  69. else
  70. echo "${var^^}=\"${!var}\"" >> "$config_file"
  71. fi
  72. done
  73. if ! [ -f "/etc/ntfy/topics.yml" ]; then
  74. cat <<'EOF' > /etc/ntfy/topics.yml
  75. .*\.(emerg|alert|crit|err|warning|notice):
  76. - ${LOGIN}_main
  77. EOF
  78. fi
  79. ## provide 'send' command
  80. cp -f "$PWD/src/bin/send" /usr/local/bin/send