From b6e9af7cd4a7cb611fdc209c2d40ee253598f671 Mon Sep 17 00:00:00 2001 From: Roman Lumetsberger Date: Sun, 9 Sep 2018 17:46:22 +0200 Subject: [PATCH 1/6] creating dns record finished --- dnsapi/dns_plesk.sh | 148 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 dnsapi/dns_plesk.sh diff --git a/dnsapi/dns_plesk.sh b/dnsapi/dns_plesk.sh new file mode 100644 index 00000000..5896f80b --- /dev/null +++ b/dnsapi/dns_plesk.sh @@ -0,0 +1,148 @@ +#!/usr/bin/env sh + +# +#PLESK_Host="host.com" +# +#PLESK_User="sdfsdfsdfljlbjkljlkjsdfoiwje" +# +#PLESK_Password="xxxx@sss.com" + + +######## Public functions ##################### + +#Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" +dns_plesk_add() { + fulldomain=$1 + txtvalue=$2 + + PLESK_Host="${CF_Key:-$(_readaccountconf_mutable PLESK_Host)}" + PLESK_SiteName="${CF_Key:-$(_readaccountconf_mutable PLESK_SiteName)}" + PLESK_User="${CF_Key:-$(_readaccountconf_mutable PLESK_User)}" + PLESK_Password="${CF_Key:-$(_readaccountconf_mutable PLESK_Password)}" + + if [ -z "$PLESK_Host" ] || [ -z "$PLESK_User" ] || [ -z "$PLESK_Password"] || [ -z "$PLESK_SiteName"]; then + PLESK_Host="" + PLESK_User="" + PLESK_Password="" + PLESK_SiteName="" + _err "You didn't specify a plesk credentials yet." + _err "Please create the key and try again." + return 1 + fi + + #save the api key and email to the account conf file. + _saveaccountconf_mutable PLESK_Host "$PLESK_Host" + _saveaccountconf_mutable PLESK_User "$PLESK_User" + _saveaccountconf_mutable PLESK_Password "$PLESK_Password" + _saveaccountconf_mutable PLESK_SiteName "$PLESK_SiteName" + + _debug "First detect the root zone" + if ! _get_root "$fulldomain"; then + _err "invalid domain" + return 1 + fi + _debug _domain_id "$_domain_id" + _debug _sub_domain "$_sub_domain" + _debug _domain "$_domain" + + _info "Adding record" + add_txt_record $_domain_id $_sub_domain $txtvalue + +} + + +function plesk_api() { + local request="$1" + + export _H1="HTTP_AUTH_LOGIN: $PLESK_User" + export _H2="HTTP_AUTH_PASSWD: $PLESK_Password" + export _H3="content-Type: text/xml" + export _H4="HTTP_PRETTY_PRINT: true" + + response="$(_post $request "https://$PLESK_Host:8443/enterprise/control/agent.php" "" "POST")" + _debug2 "response" "$response" + return 0 + +} + +function add_txt_record() { + local site_id=$1 + local subdomain=$2 + local txt_value=$3 + local request="$site_idTXT$subdomain$txt_value" + plesk_api $request + + if ! _contains "${response}" 'ok'; then + return 1 + fi + return 0 +} + +function get_domain_list() { + local request='' + + plesk_api $request + + if ! _contains "${response}" 'ok'; then + return 1 + fi + + _plesk_domain_names=($(echo "${response}" | sed -nr 's_(.*)_\1_p')); + _plesk_domain_ids=($(echo "${response}"| sed -nr 's_(.*)_\1_p')); + _plesk_domain_ids=("${_plesk_domain_ids[@]:1}") + +} + + #urls=($(sed -nr 's_(.*)_\1_p' resp.txt)); echo ${urls[@]} + +#domain.com +#returns +# _plesk_site_id=22 +function get_site_id() { + local site_name=$1 + request="$site_name" + plesk_api $request + echo $resonse + _plesk_site_id="$(echo $response | grep -Po '(?<=).*?(?=)')" + return 0 +} + +#_acme-challenge.www.domain.com +#returns +# _sub_domain=_acme-challenge.www +# _domain=domain.com +# _domain_id=sdjkglgdfewsdfg +_get_root() { + domain=$1 + i=2 + p=1 + + get_domain_list + + while true; do + h=$(printf "%s" "$domain" | cut -d . -f $i-100) + _debug h "$h" + if [ -z "$h" ]; then + #not valid + return 1 + fi + + j=0 + for item in "${_plesk_domain_names[@]}" + do + _debug "item" $item + if [ "$h" = "$item" ]; then + + _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) + _domain="$h" + _domain_id=${_plesk_domain_ids[$j]} + return 0 + fi + j=$(_math "$j" +1) + done + p=$i + i=$(_math "$i" + 1) + done + return 1 +} + From 66d623b702bb59be24408b5c31581cd66670ab11 Mon Sep 17 00:00:00 2001 From: Roman Lumetsberger Date: Sun, 9 Sep 2018 18:14:59 +0200 Subject: [PATCH 2/6] added remove dns record --- dnsapi/dns_plesk.sh | 84 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 5 deletions(-) diff --git a/dnsapi/dns_plesk.sh b/dnsapi/dns_plesk.sh index 5896f80b..1a5647b5 100644 --- a/dnsapi/dns_plesk.sh +++ b/dnsapi/dns_plesk.sh @@ -16,15 +16,13 @@ dns_plesk_add() { txtvalue=$2 PLESK_Host="${CF_Key:-$(_readaccountconf_mutable PLESK_Host)}" - PLESK_SiteName="${CF_Key:-$(_readaccountconf_mutable PLESK_SiteName)}" PLESK_User="${CF_Key:-$(_readaccountconf_mutable PLESK_User)}" PLESK_Password="${CF_Key:-$(_readaccountconf_mutable PLESK_Password)}" - if [ -z "$PLESK_Host" ] || [ -z "$PLESK_User" ] || [ -z "$PLESK_Password"] || [ -z "$PLESK_SiteName"]; then + if [ -z "$PLESK_Host" ] || [ -z "$PLESK_User" ] || [ -z "$PLESK_Password"]; then PLESK_Host="" PLESK_User="" PLESK_Password="" - PLESK_SiteName="" _err "You didn't specify a plesk credentials yet." _err "Please create the key and try again." return 1 @@ -34,8 +32,7 @@ dns_plesk_add() { _saveaccountconf_mutable PLESK_Host "$PLESK_Host" _saveaccountconf_mutable PLESK_User "$PLESK_User" _saveaccountconf_mutable PLESK_Password "$PLESK_Password" - _saveaccountconf_mutable PLESK_SiteName "$PLESK_SiteName" - + _debug "First detect the root zone" if ! _get_root "$fulldomain"; then _err "invalid domain" @@ -50,6 +47,42 @@ dns_plesk_add() { } +#fulldomain txtvalue +dns_plesk_rm() { + fulldomain=$1 + txtvalue=$2 + + PLESK_Host="${CF_Key:-$(_readaccountconf_mutable PLESK_Host)}" + PLESK_User="${CF_Key:-$(_readaccountconf_mutable PLESK_User)}" + PLESK_Password="${CF_Key:-$(_readaccountconf_mutable PLESK_Password)}" + + if [ -z "$PLESK_Host" ] || [ -z "$PLESK_User" ] || [ -z "$PLESK_Password"]; then + PLESK_Host="" + PLESK_User="" + PLESK_Password="" + _err "You didn't specify a plesk credentials yet." + _err "Please create the key and try again." + return 1 + fi + + #save the api key and email to the account conf file. + _saveaccountconf_mutable PLESK_Host "$PLESK_Host" + _saveaccountconf_mutable PLESK_User "$PLESK_User" + _saveaccountconf_mutable PLESK_Password "$PLESK_Password" + + _debug "First detect the root zone" + if ! _get_root "$fulldomain"; then + _err "invalid domain" + return 1 + fi + _debug _domain_id "$_domain_id" + _debug _sub_domain "$_sub_domain" + _debug _domain "$_domain" + + _info "Remove record" + del_txt_record $_domain_id $fulldomain +} + function plesk_api() { local request="$1" @@ -78,6 +111,32 @@ function add_txt_record() { return 0 } +function del_txt_record() { + local site_id=$1 + local fulldomain="${2}." + + get_dns_record_list $site_id + + j=0 + for item in "${_plesk_dns_host[@]}" + do + _debug "item" $item + if [ "$fulldomain" = "$item" ]; then + _dns_record_id=${_plesk_dns_ids[$j]} + fi + j=$(_math "$j" +1) + done + + _debug "record id" "$_dns_record_id" + local request="$_dns_record_id" + plesk_api $request + + if ! _contains "${response}" 'ok'; then + return 1 + fi + return 0 +} + function get_domain_list() { local request='' @@ -91,6 +150,21 @@ function get_domain_list() { _plesk_domain_ids=($(echo "${response}"| sed -nr 's_(.*)_\1_p')); _plesk_domain_ids=("${_plesk_domain_ids[@]:1}") +} + +function get_dns_record_list() { + local siteid=$1 + local request="$siteid" + + plesk_api $request + + if ! _contains "${response}" 'ok'; then + return 1 + fi + + _plesk_dns_host=($(echo "${response}" | sed -nr 's_(.*)_\1_p')); + _plesk_dns_ids=($(echo "${response}"| sed -nr 's_(.*)_\1_p')); + } #urls=($(sed -nr 's_(.*)_\1_p' resp.txt)); echo ${urls[@]} From 63e31196b2ccb0d4fd3dacbef6830c450352eae8 Mon Sep 17 00:00:00 2001 From: Roman Lumetsberger Date: Sun, 9 Sep 2018 20:47:01 +0200 Subject: [PATCH 3/6] refactoring and fixes --- dnsapi/dns_plesk.sh | 85 ++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 51 deletions(-) diff --git a/dnsapi/dns_plesk.sh b/dnsapi/dns_plesk.sh index 1a5647b5..963d8bd3 100644 --- a/dnsapi/dns_plesk.sh +++ b/dnsapi/dns_plesk.sh @@ -15,29 +15,16 @@ dns_plesk_add() { fulldomain=$1 txtvalue=$2 - PLESK_Host="${CF_Key:-$(_readaccountconf_mutable PLESK_Host)}" - PLESK_User="${CF_Key:-$(_readaccountconf_mutable PLESK_User)}" - PLESK_Password="${CF_Key:-$(_readaccountconf_mutable PLESK_Password)}" - - if [ -z "$PLESK_Host" ] || [ -z "$PLESK_User" ] || [ -z "$PLESK_Password"]; then - PLESK_Host="" - PLESK_User="" - PLESK_Password="" - _err "You didn't specify a plesk credentials yet." - _err "Please create the key and try again." + if ! init_config; then return 1 fi - #save the api key and email to the account conf file. - _saveaccountconf_mutable PLESK_Host "$PLESK_Host" - _saveaccountconf_mutable PLESK_User "$PLESK_User" - _saveaccountconf_mutable PLESK_Password "$PLESK_Password" - _debug "First detect the root zone" if ! _get_root "$fulldomain"; then _err "invalid domain" return 1 fi + _debug _domain_id "$_domain_id" _debug _sub_domain "$_sub_domain" _debug _domain "$_domain" @@ -52,11 +39,31 @@ dns_plesk_rm() { fulldomain=$1 txtvalue=$2 - PLESK_Host="${CF_Key:-$(_readaccountconf_mutable PLESK_Host)}" - PLESK_User="${CF_Key:-$(_readaccountconf_mutable PLESK_User)}" - PLESK_Password="${CF_Key:-$(_readaccountconf_mutable PLESK_Password)}" + if ! init_config; then + return 1 + fi - if [ -z "$PLESK_Host" ] || [ -z "$PLESK_User" ] || [ -z "$PLESK_Password"]; then + _debug "First detect the root zone" + if ! _get_root "$fulldomain"; then + _err "invalid domain" + return 1 + fi + + _debug _domain_id "$_domain_id" + _debug _sub_domain "$_sub_domain" + _debug _domain "$_domain" + + _info "Remove record" + del_txt_record $_domain_id $fulldomain +} + +#################### Private functions below ################################## +function init_config(){ + PLESK_Host="${PLESK_Host:-$(_readaccountconf_mutable PLESK_Host)}" + PLESK_User="${PLESK_User:-$(_readaccountconf_mutable PLESK_User)}" + PLESK_Password="${PLESK_Password:-$(_readaccountconf_mutable PLESK_Password)}" + + if [ -z "$PLESK_Host" ] || [ -z "$PLESK_User" ] || [ -z "$PLESK_Password" ]; then PLESK_Host="" PLESK_User="" PLESK_Password="" @@ -69,21 +76,9 @@ dns_plesk_rm() { _saveaccountconf_mutable PLESK_Host "$PLESK_Host" _saveaccountconf_mutable PLESK_User "$PLESK_User" _saveaccountconf_mutable PLESK_Password "$PLESK_Password" - - _debug "First detect the root zone" - if ! _get_root "$fulldomain"; then - _err "invalid domain" - return 1 - fi - _debug _domain_id "$_domain_id" - _debug _sub_domain "$_sub_domain" - _debug _domain "$_domain" - - _info "Remove record" - del_txt_record $_domain_id $fulldomain + } - - + function plesk_api() { local request="$1" @@ -106,9 +101,9 @@ function add_txt_record() { plesk_api $request if ! _contains "${response}" 'ok'; then - return 1 + return 1 fi - return 0 + return 0 } function del_txt_record() { @@ -137,6 +132,7 @@ function del_txt_record() { return 0 } +#fetches the domain list for the given account function get_domain_list() { local request='' @@ -148,10 +144,11 @@ function get_domain_list() { _plesk_domain_names=($(echo "${response}" | sed -nr 's_(.*)_\1_p')); _plesk_domain_ids=($(echo "${response}"| sed -nr 's_(.*)_\1_p')); - _plesk_domain_ids=("${_plesk_domain_ids[@]:1}") + _plesk_domain_ids=("${_plesk_domain_ids[@]:1}") #remove first entry because it is the customer id } +#fetches all dns records fo rthe given sit function get_dns_record_list() { local siteid=$1 local request="$siteid" @@ -166,21 +163,7 @@ function get_dns_record_list() { _plesk_dns_ids=($(echo "${response}"| sed -nr 's_(.*)_\1_p')); } - - #urls=($(sed -nr 's_(.*)_\1_p' resp.txt)); echo ${urls[@]} - -#domain.com -#returns -# _plesk_site_id=22 -function get_site_id() { - local site_name=$1 - request="$site_name" - plesk_api $request - echo $resonse - _plesk_site_id="$(echo $response | grep -Po '(?<=).*?(?=)')" - return 0 -} - + #_acme-challenge.www.domain.com #returns # _sub_domain=_acme-challenge.www From eeb08d6a6a36e0350f799d74d00c9b2222be274d Mon Sep 17 00:00:00 2001 From: Roman Lumetsberger Date: Sun, 9 Sep 2018 21:10:45 +0200 Subject: [PATCH 4/6] shellcheck fixes --- dnsapi/dns_plesk.sh | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/dnsapi/dns_plesk.sh b/dnsapi/dns_plesk.sh index 963d8bd3..68849896 100644 --- a/dnsapi/dns_plesk.sh +++ b/dnsapi/dns_plesk.sh @@ -30,7 +30,7 @@ dns_plesk_add() { _debug _domain "$_domain" _info "Adding record" - add_txt_record $_domain_id $_sub_domain $txtvalue + add_txt_record "$_domain_id" "$_sub_domain" "$txtvalue" } @@ -54,7 +54,7 @@ dns_plesk_rm() { _debug _domain "$_domain" _info "Remove record" - del_txt_record $_domain_id $fulldomain + del_txt_record "$_domain_id" "$fulldomain" } #################### Private functions below ################################## @@ -80,25 +80,25 @@ function init_config(){ } function plesk_api() { - local request="$1" + request="$1" export _H1="HTTP_AUTH_LOGIN: $PLESK_User" export _H2="HTTP_AUTH_PASSWD: $PLESK_Password" export _H3="content-Type: text/xml" export _H4="HTTP_PRETTY_PRINT: true" - response="$(_post $request "https://$PLESK_Host:8443/enterprise/control/agent.php" "" "POST")" + response="$(_post "$request" "https://$PLESK_Host:8443/enterprise/control/agent.php" "" "POST")" _debug2 "response" "$response" return 0 } function add_txt_record() { - local site_id=$1 - local subdomain=$2 - local txt_value=$3 - local request="$site_idTXT$subdomain$txt_value" - plesk_api $request + site_id=$1 + subdomain=$2 + txt_value=$3 + request="$site_idTXT$subdomain$txt_value" + plesk_api "$request" if ! _contains "${response}" 'ok'; then return 1 @@ -107,15 +107,15 @@ function add_txt_record() { } function del_txt_record() { - local site_id=$1 - local fulldomain="${2}." + site_id=$1 + fulldomain="${2}." - get_dns_record_list $site_id + get_dns_record_list "$site_id" j=0 for item in "${_plesk_dns_host[@]}" do - _debug "item" $item + _debug "item" "$item" if [ "$fulldomain" = "$item" ]; then _dns_record_id=${_plesk_dns_ids[$j]} fi @@ -123,8 +123,8 @@ function del_txt_record() { done _debug "record id" "$_dns_record_id" - local request="$_dns_record_id" - plesk_api $request + request="$_dns_record_id" + plesk_api "$request" if ! _contains "${response}" 'ok'; then return 1 @@ -134,9 +134,9 @@ function del_txt_record() { #fetches the domain list for the given account function get_domain_list() { - local request='' + request='' - plesk_api $request + plesk_api "$request" if ! _contains "${response}" 'ok'; then return 1 @@ -150,10 +150,10 @@ function get_domain_list() { #fetches all dns records fo rthe given sit function get_dns_record_list() { - local siteid=$1 - local request="$siteid" + siteid=$1 + request="$siteid" - plesk_api $request + plesk_api "$request" if ! _contains "${response}" 'ok'; then return 1 @@ -187,7 +187,7 @@ _get_root() { j=0 for item in "${_plesk_domain_names[@]}" do - _debug "item" $item + _debug "item" "$item" if [ "$h" = "$item" ]; then _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) From 9a8930fc02e285e918e94d2d67f6d526f8c84b64 Mon Sep 17 00:00:00 2001 From: Roman Lumetsberger Date: Sun, 9 Sep 2018 21:20:59 +0200 Subject: [PATCH 5/6] added plesk info to readme --- dnsapi/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/dnsapi/README.md b/dnsapi/README.md index 891417f3..87a37bf6 100644 --- a/dnsapi/README.md +++ b/dnsapi/README.md @@ -990,6 +990,25 @@ Now you can issue a certificate. acme.sh --issue --dns dns_gdnsdk -d example.com -d *.example.com ``` +## 53. Use plesk domain API to automatically issue cert + +The plesk plugin uses the xml api to add and remvoe the dns records. Therefore the url, username +and password have to be configured. + +``` +export PLESK_Host="host.com" +export PLESK_User="plesk username" +export PLESK_Password="plesk password" +``` + +Ok, let's issue a cert now: +``` +acme.sh --issue --dns dns_plesk -d example.com -d www.example.com +``` + +The `PLESK_Host`, `PLESK_User` and `PLESK_Password` will be saved in `~/.acme.sh/account.conf` and will be reused when needed. + + # Use custom API If your API is not supported yet, you can write your own DNS API. From 7775bc9d8770a14906a76aa70248243fb9e07071 Mon Sep 17 00:00:00 2001 From: Roman Lumetsberger Date: Sun, 9 Sep 2018 21:24:10 +0200 Subject: [PATCH 6/6] added plesk to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 904a4789..81950291 100644 --- a/README.md +++ b/README.md @@ -326,6 +326,7 @@ You don't have to do anything manually! 1. ConoHa (https://www.conoha.jp) 1. netcup DNS API (https://www.netcup.de) 1. GratisDNS.dk (https://gratisdns.dk) +1. Plesk (https://www.plesk.com) And: