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