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
5.0 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_cpanel_info='cPanel Server API
  4. Manage DNS via cPanel Dashboard.
  5. Site: cPanel.net
  6. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_cpanel
  7. Options:
  8. cPanel_Username Username
  9. cPanel_Apitoken API Token
  10. cPanel_Hostname Server URL. E.g. "https://hostname:port"
  11. Issues: github.com/acmesh-official/acme.sh/issues/3732
  12. Author: Bjarne Saltbaek
  13. '
  14. ######## Public functions #####################
  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. _debug "_result is: $_result"
  68. if _successful_update; then return 0; fi
  69. _err "Couldn't delete entry!"
  70. return 1
  71. }
  72. #################### Private functions below ##################################
  73. _checkcredentials() {
  74. cPanel_Username="${cPanel_Username:-$(_readaccountconf_mutable cPanel_Username)}"
  75. cPanel_Apitoken="${cPanel_Apitoken:-$(_readaccountconf_mutable cPanel_Apitoken)}"
  76. cPanel_Hostname="${cPanel_Hostname:-$(_readaccountconf_mutable cPanel_Hostname)}"
  77. if [ -z "$cPanel_Username" ] || [ -z "$cPanel_Apitoken" ] || [ -z "$cPanel_Hostname" ]; then
  78. cPanel_Username=""
  79. cPanel_Apitoken=""
  80. cPanel_Hostname=""
  81. _err "You haven't specified cPanel username, apitoken and hostname yet."
  82. _err "Please add credentials and try again."
  83. return 1
  84. fi
  85. #save the credentials to the account conf file.
  86. _saveaccountconf_mutable cPanel_Username "$cPanel_Username"
  87. _saveaccountconf_mutable cPanel_Apitoken "$cPanel_Apitoken"
  88. _saveaccountconf_mutable cPanel_Hostname "$cPanel_Hostname"
  89. return 0
  90. }
  91. _cpanel_login() {
  92. if ! _checkcredentials; then return 1; fi
  93. if ! _myget "json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=CustInfo&cpanel_jsonapi_func=displaycontactinfo"; then
  94. _err "cPanel login failed for user $cPanel_Username."
  95. return 1
  96. fi
  97. return 0
  98. }
  99. _myget() {
  100. #Adds auth header to request
  101. export _H1="Authorization: cpanel $cPanel_Username:$cPanel_Apitoken"
  102. _result=$(_get "$cPanel_Hostname/$1")
  103. }
  104. _get_root() {
  105. _myget 'json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzones'
  106. _domains=$(echo "$_result" | _egrep_o '"[a-z0-9\.\-]*":\["; cPanel first' | cut -d':' -f1 | sed 's/"//g' | sed 's/{//g')
  107. _debug "_result is: $_result"
  108. _debug "_domains is: $_domains"
  109. if [ -z "$_domains" ]; then
  110. _err "Primary domain list not found!"
  111. return 1
  112. fi
  113. for _domain in $_domains; do
  114. _debug "Checking if $fulldomain ends with $_domain"
  115. if (_endswith "$fulldomain" "$_domain"); then
  116. _debug "Root domain: $_domain"
  117. return 0
  118. fi
  119. done
  120. return 1
  121. }
  122. _successful_update() {
  123. if (echo "$_result" | _egrep_o 'data":\[[^]]*]' | grep -q '"newserial":null'); then return 1; fi
  124. return 0
  125. }
  126. _findentry() {
  127. _debug "In _findentry"
  128. #returns id of dns entry, if it exists
  129. _myget "json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzone_records&domain=$_domain"
  130. _id=$(echo "$_result" | sed -e "s/},{/},\n{/g" | grep "$fulldomain" | grep "$txtvalue" | _egrep_o 'line":[0-9]+' | cut -d ':' -f 2)
  131. _debug "_result is: $_result"
  132. _debug "fulldomain. is $fulldomain."
  133. _debug "txtvalue is $txtvalue"
  134. _debug "_id is: $_id"
  135. if [ -n "$_id" ]; then
  136. _debug "Entry found with _id=$_id"
  137. return 0
  138. fi
  139. return 1
  140. }