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.

157 lines
4.8 KiB

  1. #!/usr/bin/env sh
  2. #
  3. #Author: Bjarne Saltbaek
  4. #Report Bugs here: https://github.com/acmesh-official/acme.sh/issues/3732
  5. #
  6. ######## Public functions #####################
  7. #
  8. # Export CPANEL username,api token and hostname in the following variables
  9. #
  10. # cPanel_Username=username
  11. # cPanel_Apitoken=apitoken
  12. # cPanel_Hostname=hostname
  13. #
  14. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  15. # Used to add txt record
  16. dns_cpanel_add() {
  17. fulldomain=$1
  18. txtvalue=$2
  19. _info "Adding TXT record to cPanel based system"
  20. _debug fulldomain "$fulldomain"
  21. _debug txtvalue "$txtvalue"
  22. _debug cPanel_Username "$cPanel_Username"
  23. _debug cPanel_Apitoken "$cPanel_Apitoken"
  24. _debug cPanel_Hostname "$cPanel_Hostname"
  25. if ! _cpanel_login; then
  26. _err "cPanel Login failed for user $cPanel_Username. Check $HTTP_HEADER file"
  27. return 1
  28. fi
  29. _debug "First detect the root zone"
  30. if ! _get_root "$fulldomain"; then
  31. _err "No matching root domain for $fulldomain found"
  32. return 1
  33. fi
  34. # adding entry
  35. _info "Adding the entry"
  36. stripped_fulldomain=$(echo "$fulldomain" | sed "s/.$_domain//")
  37. _debug "Adding $stripped_fulldomain to $_domain zone"
  38. _myget "json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=add_zone_record&domain=$_domain&name=$stripped_fulldomain&type=TXT&txtdata=$txtvalue&ttl=1"
  39. if _successful_update; then return 0; fi
  40. _err "Couldn't create entry!"
  41. return 1
  42. }
  43. # Usage: fulldomain txtvalue
  44. # Used to remove the txt record after validation
  45. dns_cpanel_rm() {
  46. fulldomain=$1
  47. txtvalue=$2
  48. _info "Using cPanel based system"
  49. _debug fulldomain "$fulldomain"
  50. _debug txtvalue "$txtvalue"
  51. if ! _cpanel_login; then
  52. _err "cPanel Login failed for user $cPanel_Username. Check $HTTP_HEADER file"
  53. return 1
  54. fi
  55. if ! _get_root; then
  56. _err "No matching root domain for $fulldomain found"
  57. return 1
  58. fi
  59. _findentry "$fulldomain" "$txtvalue"
  60. if [ -z "$_id" ]; then
  61. _info "Entry doesn't exist, nothing to delete"
  62. return 0
  63. fi
  64. _debug "Deleting record..."
  65. _myget "json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=remove_zone_record&domain=$_domain&line=$_id"
  66. # removing entry
  67. if _successful_update; then return 0; fi
  68. _err "Couldn't delete entry!"
  69. return 1
  70. }
  71. #################### Private functions below ##################################
  72. _checkcredentials() {
  73. cPanel_Username="${cPanel_Username:-$(_readaccountconf_mutable cPanel_Username)}"
  74. cPanel_Apitoken="${cPanel_Apitoken:-$(_readaccountconf_mutable cPanel_Apitoken)}"
  75. cPanel_Hostname="${cPanel_Hostname:-$(_readaccountconf_mutable cPanel_Hostname)}"
  76. if [ -z "$cPanel_Username" ] || [ -z "$cPanel_Apitoken" ] || [ -z "$cPanel_Hostname" ]; then
  77. cPanel_Username=""
  78. cPanel_Apitoken=""
  79. cPanel_Hostname=""
  80. _err "You haven't specified cPanel username, apitoken and hostname yet."
  81. _err "Please add credentials and try again."
  82. return 1
  83. fi
  84. #save the credentials to the account conf file.
  85. _saveaccountconf_mutable cPanel_Username "$cPanel_Username"
  86. _saveaccountconf_mutable cPanel_Apitoken "$cPanel_Apitoken"
  87. _saveaccountconf_mutable cPanel_Hostname "$cPanel_Hostname"
  88. return 0
  89. }
  90. _cpanel_login() {
  91. if ! _checkcredentials; then return 1; fi
  92. if ! _myget "json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=CustInfo&cpanel_jsonapi_func=displaycontactinfo"; then
  93. _err "cPanel login failed for user $cPanel_Username."
  94. return 1
  95. fi
  96. return 0
  97. }
  98. _myget() {
  99. #Adds auth header to request
  100. export _H1="Authorization: cpanel $cPanel_Username:$cPanel_Apitoken"
  101. _result=$(_get "$cPanel_Hostname/$1")
  102. }
  103. _get_root() {
  104. _myget 'json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzones'
  105. _domains=$(echo "$_result" | cut -d ':' -f10 | sed 's/"//g' | sed 's/{//g')
  106. _debug "_result is: $_result"
  107. _debug "_domains is: $_domains"
  108. if [ -z "$_domains" ]; then
  109. _err "Primary domain list not found!"
  110. return 1
  111. fi
  112. for _domain in $_domains; do
  113. _debug "Checking if $fulldomain ends with $_domain"
  114. if (_endswith "$fulldomain" "$_domain"); then
  115. _debug "Root domain: $_domain"
  116. return 0
  117. fi
  118. done
  119. return 1
  120. }
  121. _successful_update() {
  122. if (echo "$_result" | grep -q 'newserial'); then return 0; fi
  123. return 1
  124. }
  125. _findentry() {
  126. _debug "In _findentry"
  127. #returns id of dns entry, if it exists
  128. _myget "json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzone_records&domain=$_domain"
  129. jqquery=".cpanelresult.data[] | select(.name == \"$fulldomain.\")| {Line} | .Line"
  130. _id=$(echo "$_result" | jq "$jqquery")
  131. _debug "_result is: $_result"
  132. _debug "fulldomain. is $fulldomain."
  133. _debug "_id is: $_id"
  134. if [ -n "$_id" ]; then
  135. _debug "Entry found with _id=$_id"
  136. return 0
  137. fi
  138. return 1
  139. }