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.

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