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.

123 lines
4.3 KiB

  1. #!/usr/bin/env sh
  2. # Script to deploy cert to Peplink Routers
  3. #
  4. # The following environment variables must be set:
  5. #
  6. # PEPLINK_Hostname - Peplink hostname
  7. # PEPLINK_Username - Peplink username to login
  8. # PEPLINK_Password - Peplink password to login
  9. #
  10. # The following environmental variables may be set if you don't like their
  11. # default values:
  12. #
  13. # PEPLINK_Certtype - Certificate type to target for replacement
  14. # defaults to "webadmin", can be one of:
  15. # * "chub" (ContentHub)
  16. # * "openvpn" (OpenVPN CA)
  17. # * "portal" (Captive Portal SSL)
  18. # * "webadmin" (Web Admin SSL)
  19. # * "webproxy" (Proxy Root CA)
  20. # * "wwan_ca" (Wi-Fi WAN CA)
  21. # * "wwan_client" (Wi-Fi WAN Client)
  22. # PEPLINK_Scheme - defaults to "https"
  23. # PEPLINK_Port - defaults to "443"
  24. #
  25. #returns 0 means success, otherwise error.
  26. ######## Public functions #####################
  27. _peplink_get_cookie_data() {
  28. grep -i "\W$1=" | grep -i "^Set-Cookie:" | _tail_n 1 | _egrep_o "$1=[^;]*;" | tr -d ';'
  29. }
  30. #domain keyfile certfile cafile fullchain
  31. peplink_deploy() {
  32. _cdomain="$1"
  33. _ckey="$2"
  34. _cfullchain="$5"
  35. _debug _cdomain "$_cdomain"
  36. _debug _cfullchain "$_cfullchain"
  37. _debug _ckey "$_ckey"
  38. # Get Hostname, Username and Password, but don't save until we successfully authenticate
  39. _getdeployconf PEPLINK_Hostname
  40. _getdeployconf PEPLINK_Username
  41. _getdeployconf PEPLINK_Password
  42. if [ -z "${PEPLINK_Hostname:-}" ] || [ -z "${PEPLINK_Username:-}" ] || [ -z "${PEPLINK_Password:-}" ]; then
  43. _err "PEPLINK_Hostname & PEPLINK_Username & PEPLINK_Password must be set"
  44. return 1
  45. fi
  46. _debug2 PEPLINK_Hostname "$PEPLINK_Hostname"
  47. _debug2 PEPLINK_Username "$PEPLINK_Username"
  48. _secure_debug2 PEPLINK_Password "$PEPLINK_Password"
  49. # Optional certificate type, scheme, and port for Peplink
  50. _getdeployconf PEPLINK_Certtype
  51. _getdeployconf PEPLINK_Scheme
  52. _getdeployconf PEPLINK_Port
  53. # Don't save the certificate type until we verify it exists and is supported
  54. _savedeployconf PEPLINK_Scheme "$PEPLINK_Scheme"
  55. _savedeployconf PEPLINK_Port "$PEPLINK_Port"
  56. # Default vaules for certificate type, scheme, and port
  57. [ -n "${PEPLINK_Certtype}" ] || PEPLINK_Certtype="webadmin"
  58. [ -n "${PEPLINK_Scheme}" ] || PEPLINK_Scheme="https"
  59. [ -n "${PEPLINK_Port}" ] || PEPLINK_Port="443"
  60. _debug2 PEPLINK_Certtype "$PEPLINK_Certtype"
  61. _debug2 PEPLINK_Scheme "$PEPLINK_Scheme"
  62. _debug2 PEPLINK_Port "$PEPLINK_Port"
  63. _base_url="$PEPLINK_Scheme://$PEPLINK_Hostname:$PEPLINK_Port"
  64. _debug _base_url "$_base_url"
  65. # Login, get the auth token from the cookie
  66. _info "Logging into $PEPLINK_Hostname:$PEPLINK_Port"
  67. encoded_username="$(printf "%s" "$PEPLINK_Username" | _url_encode)"
  68. encoded_password="$(printf "%s" "$PEPLINK_Password" | _url_encode)"
  69. response=$(_post "func=login&username=$encoded_username&password=$encoded_password" "$_base_url/cgi-bin/MANGA/api.cgi")
  70. auth_token=$(_peplink_get_cookie_data "bauth" <"$HTTP_HEADER")
  71. _debug3 response "$response"
  72. _debug auth_token "$auth_token"
  73. if [ -z "$auth_token" ]; then
  74. _err "Unable to authenticate to $PEPLINK_Hostname:$PEPLINK_Port using $PEPLINK_Scheme."
  75. _err "Check your username and password."
  76. return 1
  77. fi
  78. _H1="Cookie: $auth_token"
  79. export _H1
  80. _debug2 H1 "${_H1}"
  81. # Now that we know the hostnameusername and password are good, save them
  82. _savedeployconf PEPLINK_Hostname "$PEPLINK_Hostname"
  83. _savedeployconf PEPLINK_Username "$PEPLINK_Username"
  84. _savedeployconf PEPLINK_Password "$PEPLINK_Password"
  85. _info "Generate form POST request"
  86. encoded_key="$(_url_encode <"$_ckey")"
  87. encoded_fullchain="$(_url_encode <"$_cfullchain")"
  88. body="cert_type=$PEPLINK_Certtype&cert_uid=&section=CERT_modify&key_pem=$encoded_key&key_pem_passphrase=&key_pem_passphrase_confirm=&cert_pem=$encoded_fullchain"
  89. _debug3 body "$body"
  90. _info "Upload $PEPLINK_Certtype certificate to the Peplink"
  91. response=$(_post "$body" "$_base_url/cgi-bin/MANGA/admin.cgi")
  92. _debug3 response "$response"
  93. if echo "$response" | grep 'Success' >/dev/null; then
  94. # We've verified this certificate type is valid, so save it
  95. _savedeployconf PEPLINK_Certtype "$PEPLINK_Certtype"
  96. _info "Certificate was updated"
  97. return 0
  98. else
  99. _err "Unable to update certificate, error code $response"
  100. return 1
  101. fi
  102. }