|
| 1 | +#!/usr/bin/env sh |
| 2 | + |
| 3 | +# Here is a script to deploy cert to Ruckus ZoneDirector / Unleashed. |
| 4 | +# |
| 5 | +# Public domain, 2024, Tony Rielly <https://github.com/ms264556> |
| 6 | +# |
| 7 | +# ```sh |
| 8 | +# acme.sh --deploy -d ruckus.example.com --deploy-hook ruckus |
| 9 | +# ``` |
| 10 | +# |
| 11 | +# Then you need to set the environment variables for the |
| 12 | +# deploy script to work. |
| 13 | +# |
| 14 | +# ```sh |
| 15 | +# export RUCKUS_HOST=myruckus.example.com |
| 16 | +# export RUCKUS_USER=myruckususername |
| 17 | +# export RUCKUS_PASS=myruckuspassword |
| 18 | +# |
| 19 | +# acme.sh --deploy -d myruckus.example.com --deploy-hook ruckus |
| 20 | +# ``` |
| 21 | +# |
| 22 | +# returns 0 means success, otherwise error. |
| 23 | + |
| 24 | +######## Public functions ##################### |
| 25 | + |
| 26 | +#domain keyfile certfile cafile fullchain |
| 27 | +ruckus_deploy() { |
| 28 | + _cdomain="$1" |
| 29 | + _ckey="$2" |
| 30 | + _ccert="$3" |
| 31 | + _cca="$4" |
| 32 | + _cfullchain="$5" |
| 33 | + _err_code=0 |
| 34 | + |
| 35 | + _debug _cdomain "$_cdomain" |
| 36 | + _debug _ckey "$_ckey" |
| 37 | + _debug _ccert "$_ccert" |
| 38 | + _debug _cca "$_cca" |
| 39 | + _debug _cfullchain "$_cfullchain" |
| 40 | + |
| 41 | + _getdeployconf RUCKUS_HOST |
| 42 | + _getdeployconf RUCKUS_USER |
| 43 | + _getdeployconf RUCKUS_PASS |
| 44 | + |
| 45 | + if [ -z "$RUCKUS_HOST" ]; then |
| 46 | + _debug "Using _cdomain as RUCKUS_HOST, please set if not correct." |
| 47 | + RUCKUS_HOST="$_cdomain" |
| 48 | + fi |
| 49 | + |
| 50 | + if [ -z "$RUCKUS_USER" ]; then |
| 51 | + _err "Need to set the env variable RUCKUS_USER" |
| 52 | + return 1 |
| 53 | + fi |
| 54 | + |
| 55 | + if [ -z "$RUCKUS_PASS" ]; then |
| 56 | + _err "Need to set the env variable RUCKUS_PASS" |
| 57 | + return 1 |
| 58 | + fi |
| 59 | + |
| 60 | + _savedeployconf RUCKUS_HOST "$RUCKUS_HOST" |
| 61 | + _savedeployconf RUCKUS_USER "$RUCKUS_USER" |
| 62 | + _savedeployconf RUCKUS_PASS "$RUCKUS_PASS" |
| 63 | + |
| 64 | + _debug RUCKUS_HOST "$RUCKUS_HOST" |
| 65 | + _debug RUCKUS_USER "$RUCKUS_USER" |
| 66 | + _secure_debug RUCKUS_PASS "$RUCKUS_PASS" |
| 67 | + |
| 68 | + export ACME_HTTP_NO_REDIRECTS=1 |
| 69 | + |
| 70 | + _info "Discovering the login URL" |
| 71 | + _get "https://$RUCKUS_HOST" >/dev/null |
| 72 | + _login_url="$(_response_header 'Location')" |
| 73 | + if [ -n "$_login_url" ]; then |
| 74 | + _login_path=$(echo "$_login_url" | sed 's|https\?://[^/]\+||') |
| 75 | + if [ -z "$_login_path" ]; then |
| 76 | + # redirect was to a different host |
| 77 | + _err "Connection failed: redirected to a different host. Configure Unleashed with a Preferred Master or Management Interface." |
| 78 | + return 1 |
| 79 | + fi |
| 80 | + fi |
| 81 | + |
| 82 | + if [ -z "${_login_url}" ]; then |
| 83 | + _err "Connection failed: couldn't find login page." |
| 84 | + return 1 |
| 85 | + fi |
| 86 | + |
| 87 | + _base_url=$(dirname "$_login_url") |
| 88 | + _login_page=$(basename "$_login_url") |
| 89 | + |
| 90 | + if [ "$_login_page" = "index.html" ]; then |
| 91 | + _err "Connection temporarily unavailable: Unleashed Rebuilding." |
| 92 | + return 1 |
| 93 | + fi |
| 94 | + |
| 95 | + if [ "$_login_page" = "wizard.jsp" ]; then |
| 96 | + _err "Connection failed: Setup Wizard not complete." |
| 97 | + return 1 |
| 98 | + fi |
| 99 | + |
| 100 | + _info "Login" |
| 101 | + _username_encoded="$(printf "%s" "$RUCKUS_USER" | _url_encode)" |
| 102 | + _password_encoded="$(printf "%s" "$RUCKUS_PASS" | _url_encode)" |
| 103 | + _login_query="$(printf "%s" "username=${_username_encoded}&password=${_password_encoded}&ok=Log+In")" |
| 104 | + _post "$_login_query" "$_login_url" >/dev/null |
| 105 | + |
| 106 | + _login_code="$(_response_code)" |
| 107 | + if [ "$_login_code" = "200" ]; then |
| 108 | + _err "Login failed: incorrect credentials." |
| 109 | + return 1 |
| 110 | + fi |
| 111 | + |
| 112 | + _info "Collect Session Cookie" |
| 113 | + _H1="Cookie: $(_response_cookie)" |
| 114 | + export _H1 |
| 115 | + _info "Collect CSRF Token" |
| 116 | + _H2="X-CSRF-Token: $(_response_header 'HTTP_X_CSRF_TOKEN')" |
| 117 | + export _H2 |
| 118 | + |
| 119 | + _info "Uploading certificate" |
| 120 | + _post_upload "uploadcert" "$_cfullchain" |
| 121 | + |
| 122 | + _info "Uploading private key" |
| 123 | + _post_upload "uploadprivatekey" "$_ckey" |
| 124 | + |
| 125 | + _info "Replacing certificate" |
| 126 | + _replace_cert_ajax='<ajax-request action="docmd" comp="system" updater="rid.0.5" xcmd="replace-cert" checkAbility="6" timeout="-1"><xcmd cmd="replace-cert" cn="'$RUCKUS_HOST'"/></ajax-request>' |
| 127 | + _post "$_replace_cert_ajax" "$_base_url/_cmdstat.jsp" >/dev/null |
| 128 | + |
| 129 | + _info "Rebooting" |
| 130 | + _cert_reboot_ajax='<ajax-request action="docmd" comp="worker" updater="rid.0.5" xcmd="cert-reboot" checkAbility="6"><xcmd cmd="cert-reboot" action="undefined"/></ajax-request>' |
| 131 | + _post "$_cert_reboot_ajax" "$_base_url/_cmdstat.jsp" >/dev/null |
| 132 | + |
| 133 | + return 0 |
| 134 | +} |
| 135 | + |
| 136 | +_response_code() { |
| 137 | + _egrep_o <"$HTTP_HEADER" "^HTTP[^ ]* .*$" | cut -d " " -f 2-100 | tr -d "\f\n" | _egrep_o "^[0-9]*" |
| 138 | +} |
| 139 | + |
| 140 | +_response_header() { |
| 141 | + grep <"$HTTP_HEADER" -i "^$1:" | cut -d ':' -f 2- | tr -d "\r\n\t " |
| 142 | +} |
| 143 | + |
| 144 | +_response_cookie() { |
| 145 | + _response_header 'Set-Cookie' | sed 's/;.*//' |
| 146 | +} |
| 147 | + |
| 148 | +_post_upload() { |
| 149 | + _post_action="$1" |
| 150 | + _post_file="$2" |
| 151 | + |
| 152 | + _post_boundary="----FormBoundary$(date "+%s%N")" |
| 153 | + |
| 154 | + _post_data="$({ |
| 155 | + printf -- "--%s\r\n" "$_post_boundary" |
| 156 | + printf -- "Content-Disposition: form-data; name=\"u\"; filename=\"%s\"\r\n" "$_post_action" |
| 157 | + printf -- "Content-Type: application/octet-stream\r\n\r\n" |
| 158 | + printf -- "%s\r\n" "$(cat "$_post_file")" |
| 159 | +
|
| 160 | + printf -- "--%s\r\n" "$_post_boundary" |
| 161 | + printf -- "Content-Disposition: form-data; name=\"action\"\r\n\r\n" |
| 162 | + printf -- "%s\r\n" "$_post_action" |
| 163 | +
|
| 164 | + printf -- "--%s\r\n" "$_post_boundary" |
| 165 | + printf -- "Content-Disposition: form-data; name=\"callback\"\r\n\r\n" |
| 166 | + printf -- "%s\r\n" "uploader_$_post_action" |
| 167 | +
|
| 168 | + printf -- "--%s--\r\n\r\n" "$_post_boundary" |
| 169 | + })" |
| 170 | + |
| 171 | + _post "$_post_data" "$_base_url/_upload.jsp?request_type=xhr" "" "" "multipart/form-data; boundary=$_post_boundary" >/dev/null |
| 172 | +} |
0 commit comments