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