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.

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