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.

445 lines
20 KiB

10 months ago
8 months ago
10 months ago
10 months ago
5 years ago
10 months ago
10 months ago
  1. #!/bin/bash
  2. ################################################################################
  3. # ACME.sh 3rd party deploy plugin for Synology DSM
  4. ################################################################################
  5. # Authors: Brian Hartvigsen (creator), https://github.com/tresni
  6. # Martin Arndt (contributor), https://troublezone.net/
  7. # Updated: 2023-07-03
  8. # Issues: https://github.com/acmesh-official/acme.sh/issues/2727
  9. ################################################################################
  10. # Usage (shown values are the examples):
  11. # 1. Set required environment variables:
  12. # - use automatically created temp admin user to authenticate
  13. # export SYNO_USE_TEMP_ADMIN=1
  14. # - or provide your own admin user credential to authenticate
  15. # 1. export SYNO_USERNAME="adminUser"
  16. # 2. export SYNO_PASSWORD="adminPassword"
  17. # 2. Set optional environment variables
  18. # - common optional variables
  19. # - export SYNO_SCHEME="http" - defaults to "http"
  20. # - export SYNO_HOSTNAME="localhost" - defaults to "localhost"
  21. # - export SYNO_PORT="5000" - defaults to "5000"
  22. # - export SYNO_CREATE=1 - to allow creating the cert if it doesn't exist
  23. # - export SYNO_CERTIFICATE="" - to replace a specific cert by its
  24. # description
  25. # - temp admin optional variables
  26. # - export SYNO_LOCAL_HOSTNAME=1 - if set to 1, force to treat hostname is
  27. # targeting current local machine (since
  28. # this method only locally supported)
  29. # - exsiting admin 2FA-OTP optional variables
  30. # - export SYNO_OTP_CODE="XXXXXX" - if set, script won't require to
  31. # interactive input the OTP code
  32. # - export SYNO_DEVICE_NAME="CertRenewal" - if set, script won't require to
  33. # interactive input the device name
  34. # - export SYNO_DEVICE_ID="" - (deprecated, auth with OTP code instead)
  35. # required for omitting 2FA-OTP
  36. # 3. Run command:
  37. # acme.sh --deploy --deploy-hook synology_dsm -d example.com
  38. ################################################################################
  39. # Dependencies:
  40. # - curl
  41. # - synouser & synogroup & synosetkeyvalue (Required for SYNO_USE_TEMP_ADMIN=1)
  42. ################################################################################
  43. # Return value:
  44. # 0 means success, otherwise error.
  45. ################################################################################
  46. ########## Public functions ####################################################
  47. #domain keyfile certfile cafile fullchain
  48. synology_dsm_deploy() {
  49. _cdomain="$1"
  50. _ckey="$2"
  51. _ccert="$3"
  52. _cca="$4"
  53. _debug _cdomain "$_cdomain"
  54. # Get username and password, but don't save until we authenticated successfully
  55. _migratedeployconf SYNO_Username SYNO_USERNAME
  56. _migratedeployconf SYNO_Password SYNO_PASSWORD
  57. _migratedeployconf SYNO_Device_ID SYNO_DEVICE_ID
  58. _migratedeployconf SYNO_Device_Name SYNO_DEVICE_NAME
  59. _getdeployconf SYNO_USERNAME
  60. _getdeployconf SYNO_PASSWORD
  61. _getdeployconf SYNO_DEVICE_ID
  62. _getdeployconf SYNO_DEVICE_NAME
  63. # Prepare to use temp admin if SYNO_USE_TEMP_ADMIN is set
  64. _getdeployconf SYNO_USE_TEMP_ADMIN
  65. _check2cleardeployconfexp SYNO_USE_TEMP_ADMIN
  66. _debug2 SYNO_USE_TEMP_ADMIN "$SYNO_USE_TEMP_ADMIN"
  67. if [ -n "$SYNO_USE_TEMP_ADMIN" ]; then
  68. if ! _exists synouser || ! _exists synogroup || ! _exists synosetkeyvalue; then
  69. _err "Missing required tools to creat temp admin user, please set SYNO_USERNAME and SYNO_PASSWORD instead."
  70. _err "Notice: temp admin user authorization method only supports local deployment on DSM."
  71. return 1
  72. fi
  73. if synouser --help 2>&1 | grep -q 'Permission denied'; then
  74. _err "For creating temp admin user, the deploy script must be run as root."
  75. return 1
  76. fi
  77. [ -n "$SYNO_USERNAME" ] || _savedeployconf SYNO_USERNAME ""
  78. [ -n "$SYNO_PASSWORD" ] || _savedeployconf SYNO_PASSWORD ""
  79. _debug "Setting temp admin user credential..."
  80. SYNO_USERNAME=sc-acmesh-tmp
  81. SYNO_PASSWORD=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 16)
  82. # Set 2FA-OTP settings to empty consider they won't be needed.
  83. SYNO_DEVICE_ID=
  84. SYNO_DEVICE_NAME=
  85. SYNO_OTP_CODE=
  86. else
  87. _debug2 SYNO_USERNAME "$SYNO_USERNAME"
  88. _secure_debug2 SYNO_PASSWORD "$SYNO_PASSWORD"
  89. _debug2 SYNO_DEVICE_NAME "$SYNO_DEVICE_NAME"
  90. _secure_debug2 SYNO_DEVICE_ID "$SYNO_DEVICE_ID"
  91. fi
  92. if [ -z "$SYNO_USERNAME" ] || [ -z "$SYNO_PASSWORD" ]; then
  93. _err "You must set either SYNO_USE_TEMP_ADMIN, or set both SYNO_USERNAME and SYNO_PASSWORD."
  94. return 1
  95. fi
  96. # Optional scheme, hostname and port for Synology DSM
  97. _migratedeployconf SYNO_Scheme SYNO_SCHEME
  98. _migratedeployconf SYNO_Hostname SYNO_HOSTNAME
  99. _migratedeployconf SYNO_Port SYNO_PORT
  100. _getdeployconf SYNO_SCHEME
  101. _getdeployconf SYNO_HOSTNAME
  102. _getdeployconf SYNO_PORT
  103. # Default values for scheme, hostname and port
  104. # Defaulting to localhost and http, because it's localhost…
  105. [ -n "$SYNO_SCHEME" ] || SYNO_SCHEME="http"
  106. [ -n "$SYNO_HOSTNAME" ] || SYNO_HOSTNAME="localhost"
  107. [ -n "$SYNO_PORT" ] || SYNO_PORT="5000"
  108. _savedeployconf SYNO_SCHEME "$SYNO_SCHEME"
  109. _savedeployconf SYNO_HOSTNAME "$SYNO_HOSTNAME"
  110. _savedeployconf SYNO_PORT "$SYNO_PORT"
  111. _debug2 SYNO_SCHEME "$SYNO_SCHEME"
  112. _debug2 SYNO_HOSTNAME "$SYNO_HOSTNAME"
  113. _debug2 SYNO_PORT "$SYNO_PORT"
  114. # Get the certificate description, but don't save it until we verify it's real
  115. _migratedeployconf SYNO_Certificate SYNO_CERTIFICATE "base64"
  116. _getdeployconf SYNO_CERTIFICATE
  117. _check2cleardeployconfexp SYNO_CERTIFICATE
  118. _debug SYNO_CERTIFICATE "${SYNO_CERTIFICATE:-}"
  119. # shellcheck disable=SC1003 # We are not trying to escape a single quote
  120. if printf "%s" "$SYNO_CERTIFICATE" | grep '\\'; then
  121. _err "Do not use a backslash (\) in your certificate description"
  122. return 1
  123. fi
  124. _debug "Getting API version..."
  125. _base_url="$SYNO_SCHEME://$SYNO_HOSTNAME:$SYNO_PORT"
  126. _debug _base_url "$_base_url"
  127. response=$(_get "$_base_url/webapi/query.cgi?api=SYNO.API.Info&version=1&method=query&query=SYNO.API.Auth")
  128. api_path=$(echo "$response" | grep "SYNO.API.Auth" | sed -n 's/.*"path" *: *"\([^"]*\)".*/\1/p')
  129. api_version=$(echo "$response" | grep "SYNO.API.Auth" | sed -n 's/.*"maxVersion" *: *\([0-9]*\).*/\1/p')
  130. _debug3 response "$response"
  131. _debug3 api_path "$api_path"
  132. _debug3 api_version "$api_version"
  133. # Login, get the session ID and SynoToken from JSON
  134. _info "Logging into $SYNO_HOSTNAME:$SYNO_PORT..."
  135. encoded_username="$(printf "%s" "$SYNO_USERNAME" | _url_encode)"
  136. encoded_password="$(printf "%s" "$SYNO_PASSWORD" | _url_encode)"
  137. # ## START ## - DEPRECATED, for backward compatibility
  138. _getdeployconf SYNO_TOTP_SECRET
  139. if [ -n "$SYNO_TOTP_SECRET" ]; then
  140. _info "WARNING: Usage of SYNO_TOTP_SECRET is deprecated!"
  141. _info " See synology_dsm.sh script or ACME.sh Wiki page for details:"
  142. _info " https://github.com/acmesh-official/acme.sh/wiki/Synology-NAS-Guide"
  143. if ! _exists oathtool; then
  144. _err "oathtool could not be found, install oathtool to use SYNO_TOTP_SECRET"
  145. return 1
  146. fi
  147. DEPRECATED_otp_code="$(oathtool --base32 --totp "$SYNO_TOTP_SECRET" 2>/dev/null)"
  148. if [ -z "$SYNO_DEVICE_ID" ]; then
  149. _getdeployconf SYNO_DID
  150. [ -n "$SYNO_DID" ] || SYNO_DEVICE_ID="$SYNO_DID"
  151. fi
  152. if [ -n "$SYNO_DEVICE_ID" ]; then
  153. _H1="Cookie: did=$SYNO_DEVICE_ID"
  154. export _H1
  155. _debug3 H1 "${_H1}"
  156. fi
  157. response=$(_post "method=login&account=$encoded_username&passwd=$encoded_password&api=SYNO.API.Auth&version=$api_version&enable_syno_token=yes&otp_code=$DEPRECATED_otp_code&device_name=certrenewal&device_id=$SYNO_DEVICE_ID" "$_base_url/webapi/$api_path?enable_syno_token=yes")
  158. _debug3 response "$response"
  159. # ## END ## - DEPRECATED, for backward compatibility
  160. # If SYNO_DEVICE_ID or SYNO_OTP_CODE is set, we treat current account enabled 2FA-OTP.
  161. # Notice that if SYNO_USE_TEMP_ADMIN=1, both variables will be unset
  162. else
  163. if [ -n "$SYNO_DEVICE_ID" ] || [ -n "$SYNO_OTP_CODE" ]; then
  164. response='{"error":{"code":403}}'
  165. # Assume the current account disabled 2FA-OTP, try to log in right away.
  166. else
  167. if [ -n "$SYNO_USE_TEMP_ADMIN" ]; then
  168. _getdeployconf SYNO_LOCAL_HOSTNAME
  169. _debug SYNO_LOCAL_HOSTNAME "${SYNO_LOCAL_HOSTNAME:-}"
  170. if [ "$SYNO_LOCAL_HOSTNAME" != "1" ] && [ "$SYNO_LOCAL_HOSTNAME" == "$SYNO_HOSTNAME" ]; then
  171. if [ "$SYNO_HOSTNAME" != "localhost" ] && [ "$SYNO_HOSTNAME" != "127.0.0.1" ]; then
  172. _err "SYNO_USE_TEMP_ADMIN=1 only support local deployment, though if you are sure that the hostname $SYNO_HOSTNAME is targeting to your **current local machine**, execute 'export SYNO_LOCAL_HOSTNAME=1' then rerun."
  173. return 1
  174. fi
  175. fi
  176. _debug "Creating temp admin user in Synology DSM..."
  177. if synogroup --help | grep -q '\-\-memberadd '; then
  178. _temp_admin_create "$SYNO_USERNAME" "$SYNO_PASSWORD"
  179. synogroup --memberadd administrators "$SYNO_USERNAME" >/dev/null
  180. elif synogroup --help | grep -q '\-\-member '; then
  181. # For supporting DSM 6.x which only has `--member` parameter.
  182. cur_admins=$(synogroup --get administrators | awk -F '[][]' '/Group Members/,0{if(NF>1)printf "%s ", $2}')
  183. if [ -n "$cur_admins" ]; then
  184. _temp_admin_create "$SYNO_USERNAME" "$SYNO_PASSWORD"
  185. _secure_debug3 admin_users "$cur_admins$SYNO_USERNAME"
  186. # shellcheck disable=SC2086
  187. synogroup --member administrators $cur_admins $SYNO_USERNAME >/dev/null
  188. else
  189. _err "The tool synogroup may be broken, please set SYNO_USERNAME and SYNO_PASSWORD instead."
  190. return 1
  191. fi
  192. else
  193. _err "Unsupported synogroup tool detected, please set SYNO_USERNAME and SYNO_PASSWORD instead."
  194. return 1
  195. fi
  196. # havig a workaround to temporary disable enforce 2FA-OTP, will restore
  197. # it soon (after a single request), though if any accident occurs like
  198. # unexpected interruption, this setting can be easily reverted manually.
  199. otp_enforce_option=$(synogetkeyvalue /etc/synoinfo.conf otp_enforce_option)
  200. if [ -n "$otp_enforce_option" ] && [ "${otp_enforce_option:-"none"}" != "none" ]; then
  201. synosetkeyvalue /etc/synoinfo.conf otp_enforce_option none
  202. _info "Enforcing 2FA-OTP has been disabled to complete temp admin authentication."
  203. _info "Notice: it will be restored soon, if not, you can restore it manually via Control Panel."
  204. _info "previous_otp_enforce_option" "$otp_enforce_option"
  205. else
  206. otp_enforce_option=""
  207. fi
  208. fi
  209. response=$(_get "$_base_url/webapi/$api_path?api=SYNO.API.Auth&version=$api_version&method=login&format=sid&account=$encoded_username&passwd=$encoded_password&enable_syno_token=yes")
  210. if [ -n "$SYNO_USE_TEMP_ADMIN" ] && [ -n "$otp_enforce_option" ]; then
  211. synosetkeyvalue /etc/synoinfo.conf otp_enforce_option "$otp_enforce_option"
  212. _info "Restored previous enforce 2FA-OTP option."
  213. fi
  214. _debug3 response "$response"
  215. fi
  216. fi
  217. error_code=$(echo "$response" | grep '"error":' | grep -o '"code":[0-9]*' | grep -o '[0-9]*')
  218. _debug2 error_code "$error_code"
  219. # Account has 2FA-OTP enabled, since error 403 reported.
  220. # https://global.download.synology.com/download/Document/Software/DeveloperGuide/Os/DSM/All/enu/DSM_Login_Web_API_Guide_enu.pdf
  221. if [ "$error_code" == "403" ]; then
  222. if [ -z "$SYNO_DEVICE_NAME" ]; then
  223. printf "Enter device name or leave empty for default (CertRenewal): "
  224. read -r SYNO_DEVICE_NAME
  225. [ -n "$SYNO_DEVICE_NAME" ] || SYNO_DEVICE_NAME="CertRenewal"
  226. fi
  227. if [ -n "$SYNO_DEVICE_ID" ]; then
  228. # Omit OTP code with SYNO_DEVICE_ID.
  229. response=$(_get "$_base_url/webapi/$api_path?api=SYNO.API.Auth&version=$api_version&method=login&format=sid&account=$encoded_username&passwd=$encoded_password&enable_syno_token=yes&device_name=$SYNO_DEVICE_NAME&device_id=$SYNO_DEVICE_ID")
  230. _secure_debug3 response "$response"
  231. else
  232. # Require the OTP code if still unset.
  233. if [ -z "$SYNO_OTP_CODE" ]; then
  234. printf "Enter OTP code for user '%s': " "$SYNO_USERNAME"
  235. read -r SYNO_OTP_CODE
  236. fi
  237. _secure_debug SYNO_OTP_CODE "${SYNO_OTP_CODE:-}"
  238. if [ -z "$SYNO_OTP_CODE" ]; then
  239. response='{"error":{"code":404}}'
  240. else
  241. response=$(_get "$_base_url/webapi/$api_path?api=SYNO.API.Auth&version=$api_version&method=login&format=sid&account=$encoded_username&passwd=$encoded_password&enable_syno_token=yes&enable_device_token=yes&device_name=$SYNO_DEVICE_NAME&otp_code=$SYNO_OTP_CODE")
  242. _secure_debug3 response "$response"
  243. id_property='device_id'
  244. [ "${api_version}" -gt '6' ] || id_property='did'
  245. SYNO_DEVICE_ID=$(echo "$response" | grep "$id_property" | sed -n 's/.*"'$id_property'" *: *"\([^"]*\).*/\1/p')
  246. _secure_debug2 SYNO_DEVICE_ID "$SYNO_DEVICE_ID"
  247. fi
  248. fi
  249. error_code=$(echo "$response" | grep '"error":' | grep -o '"code":[0-9]*' | grep -o '[0-9]*')
  250. _debug2 error_code "$error_code"
  251. fi
  252. if [ -n "$error_code" ]; then
  253. if [ "$error_code" == "403" ] && [ -n "$SYNO_DEVICE_ID" ]; then
  254. _cleardeployconf SYNO_DEVICE_ID
  255. _err "Failed to authenticate with SYNO_DEVICE_ID (may expired or invalid), please try again in a new terminal window."
  256. elif [ "$error_code" == "404" ]; then
  257. _err "Failed to authenticate with provided 2FA-OTP code, please try again in a new terminal window."
  258. elif [ "$error_code" == "406" ]; then
  259. if [ -n "$SYNO_USE_TEMP_ADMIN" ]; then
  260. _err "Failed with unexcepted error, please report this by providing full log with '--debug 3'."
  261. else
  262. _err "Enforce auth with 2FA-OTP enabled, please configure the user to enable 2FA-OTP to continue."
  263. fi
  264. elif [ "$error_code" == "400" ]; then
  265. _err "Failed to authenticate, no such account or incorrect password."
  266. elif [ "$error_code" == "401" ]; then
  267. _err "Failed to authenticate with a non-existent account."
  268. elif [ "$error_code" == "408" ] || [ "$error_code" == "409" ] || [ "$error_code" == "410" ]; then
  269. _err "Failed to authenticate, the account password has expired or must be changed."
  270. else
  271. _err "Failed to authenticate with error: $error_code."
  272. fi
  273. _temp_admin_cleanup "$SYNO_USE_TEMP_ADMIN" "$SYNO_USERNAME"
  274. return 1
  275. fi
  276. sid=$(echo "$response" | grep "sid" | sed -n 's/.*"sid" *: *"\([^"]*\).*/\1/p')
  277. token=$(echo "$response" | grep "synotoken" | sed -n 's/.*"synotoken" *: *"\([^"]*\).*/\1/p')
  278. _debug "Session ID" "$sid"
  279. _debug SynoToken "$token"
  280. if [ -z "$sid" ] || [ -z "$token" ]; then
  281. # Still can't get necessary info even got no errors, may Synology have API updated?
  282. _err "Unable to authenticate to $_base_url, you may report this by providing full log with '--debug 3'."
  283. _temp_admin_cleanup "$SYNO_USE_TEMP_ADMIN" "$SYNO_USERNAME"
  284. return 1
  285. fi
  286. _H1="X-SYNO-TOKEN: $token"
  287. export _H1
  288. _debug2 H1 "${_H1}"
  289. # Now that we know the username and password are good, save them if not in temp admin mode.
  290. if [ -n "$SYNO_USE_TEMP_ADMIN" ]; then
  291. _cleardeployconf SYNO_USERNAME
  292. _cleardeployconf SYNO_PASSWORD
  293. _cleardeployconf SYNO_DEVICE_ID
  294. _cleardeployconf SYNO_DEVICE_NAME
  295. _savedeployconf SYNO_USE_TEMP_ADMIN "$SYNO_USE_TEMP_ADMIN"
  296. _savedeployconf SYNO_LOCAL_HOSTNAME "$SYNO_HOSTNAME"
  297. else
  298. _savedeployconf SYNO_USERNAME "$SYNO_USERNAME"
  299. _savedeployconf SYNO_PASSWORD "$SYNO_PASSWORD"
  300. _savedeployconf SYNO_DEVICE_ID "$SYNO_DEVICE_ID"
  301. _savedeployconf SYNO_DEVICE_NAME "$SYNO_DEVICE_NAME"
  302. fi
  303. _info "Getting certificates in Synology DSM..."
  304. response=$(_post "api=SYNO.Core.Certificate.CRT&method=list&version=1&_sid=$sid" "$_base_url/webapi/entry.cgi")
  305. _debug3 response "$response"
  306. escaped_certificate="$(printf "%s" "$SYNO_CERTIFICATE" | sed 's/\([].*^$[]\)/\\\1/g;s/"/\\\\"/g')"
  307. _debug escaped_certificate "$escaped_certificate"
  308. id=$(echo "$response" | sed -n "s/.*\"desc\":\"$escaped_certificate\",\"id\":\"\([^\"]*\).*/\1/p")
  309. _debug2 id "$id"
  310. error_code=$(echo "$response" | grep '"error":' | grep -o '"code":[0-9]*' | grep -o '[0-9]*')
  311. _debug2 error_code "$error_code"
  312. if [ -n "$error_code" ]; then
  313. if [ "$error_code" -eq 105 ]; then
  314. _err "Current user is not administrator and does not have sufficient permission for deploying."
  315. else
  316. _err "Failed to fetch certificate info: $error_code, please try again or contact Synology to learn more."
  317. fi
  318. _temp_admin_cleanup "$SYNO_USE_TEMP_ADMIN" "$SYNO_USERNAME"
  319. return 1
  320. fi
  321. _migratedeployconf SYNO_Create SYNO_CREATE
  322. _getdeployconf SYNO_CREATE
  323. _debug2 SYNO_CREATE "$SYNO_CREATE"
  324. if [ -z "$id" ] && [ -z "$SYNO_CREATE" ]; then
  325. _err "Unable to find certificate: $SYNO_CERTIFICATE and $SYNO_CREATE is not set."
  326. _temp_admin_cleanup "$SYNO_USE_TEMP_ADMIN" "$SYNO_USERNAME"
  327. return 1
  328. fi
  329. # We've verified this certificate description is a thing, so save it
  330. _savedeployconf SYNO_CERTIFICATE "$SYNO_CERTIFICATE" "base64"
  331. _info "Generating form POST request..."
  332. nl="\0015\0012"
  333. delim="--------------------------$(_utc_date | tr -d -- '-: ')"
  334. content="--$delim${nl}Content-Disposition: form-data; name=\"key\"; filename=\"$(basename "$_ckey")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_ckey")\0012"
  335. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"cert\"; filename=\"$(basename "$_ccert")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_ccert")\0012"
  336. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"inter_cert\"; filename=\"$(basename "$_cca")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_cca")\0012"
  337. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"id\"${nl}${nl}$id"
  338. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"desc\"${nl}${nl}${SYNO_CERTIFICATE}"
  339. if echo "$response" | sed -n "s/.*\"desc\":\"$escaped_certificate\",\([^{]*\).*/\1/p" | grep -- 'is_default":true' >/dev/null; then
  340. _debug2 default "This is the default certificate"
  341. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"as_default\"${nl}${nl}true"
  342. else
  343. _debug2 default "This is NOT the default certificate"
  344. fi
  345. content="$content${nl}--$delim--${nl}"
  346. content="$(printf "%b_" "$content")"
  347. content="${content%_}" # protect trailing \n
  348. _info "Upload certificate to the Synology DSM."
  349. response=$(_post "$content" "$_base_url/webapi/entry.cgi?api=SYNO.Core.Certificate&method=import&version=1&SynoToken=$token&_sid=$sid" "" "POST" "multipart/form-data; boundary=${delim}")
  350. _debug3 response "$response"
  351. if ! echo "$response" | grep '"error":' >/dev/null; then
  352. if echo "$response" | grep '"restart_httpd":true' >/dev/null; then
  353. _info "Restart HTTP services succeeded."
  354. else
  355. _info "Restart HTTP services failed."
  356. fi
  357. _temp_admin_cleanup "$SYNO_USE_TEMP_ADMIN" "$SYNO_USERNAME"
  358. _logout
  359. return 0
  360. else
  361. _temp_admin_cleanup "$SYNO_USE_TEMP_ADMIN" "$SYNO_USERNAME"
  362. _err "Unable to update certificate, got error response: $response."
  363. _logout
  364. return 1
  365. fi
  366. }
  367. #################### Private functions below ##################################
  368. _logout() {
  369. # Logout CERT user only to not occupy a permanent session, e.g. in DSM's "Connected Users" widget (based on previous variables)
  370. response=$(_get "$_base_url/webapi/$api_path?api=SYNO.API.Auth&version=$api_version&method=logout&_sid=$sid")
  371. _debug3 response "$response"
  372. }
  373. _temp_admin_create() {
  374. _username="$1"
  375. _password="$2"
  376. synouser --del "$_username" >/dev/null 2>/dev/null
  377. synouser --add "$_username" "$_password" "" 0 "scruelt@hotmail.com" 0 >/dev/null
  378. }
  379. _temp_admin_cleanup() {
  380. _flag=$1
  381. _username=$2
  382. if [ -n "${_flag}" ]; then
  383. _debug "Cleanuping temp admin info..."
  384. synouser --del "$_username" >/dev/null
  385. fi
  386. }
  387. #_cleardeployconf key
  388. _cleardeployconf() {
  389. _cleardomainconf "SAVED_$1"
  390. }
  391. # key
  392. _check2cleardeployconfexp() {
  393. _key="$1"
  394. _clear_key="CLEAR_$_key"
  395. # Clear saved settings if explicitly requested
  396. if [ -n "$(eval echo \$"$_clear_key")" ]; then
  397. _debug2 "$_key: value cleared from config, exported value will be ignored."
  398. _cleardeployconf "$_key"
  399. eval "$_key"=
  400. export "$_key"=
  401. eval SAVED_"$_key"=
  402. export SAVED_"$_key"=
  403. fi
  404. }