]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_he.sh
Merge pull request #4658 from Justman10000/master
[mirror_acme.sh.git] / dnsapi / dns_he.sh
CommitLineData
7d64e141
OS
1#!/usr/bin/env sh
2
7d64e141
OS
3########################################################################
4# Hurricane Electric hook script for acme.sh
5#
6# Environment variables:
7#
8# - $HE_Username (your dns.he.net username)
9# - $HE_Password (your dns.he.net password)
10#
11# Author: Ondrej Simek <me@ondrejsimek.com>
12# Git repo: https://github.com/angel333/acme.sh
13
7d64e141
OS
14#-- dns_he_add() - Add TXT record --------------------------------------
15# Usage: dns_he_add _acme-challenge.subdomain.domain.com "XyZ123..."
16
17dns_he_add() {
18 _full_domain=$1
19 _txt_value=$2
20 _info "Using DNS-01 Hurricane Electric hook"
21
2655e726 22 HE_Username="${HE_Username:-$(_readaccountconf_mutable HE_Username)}"
23 HE_Password="${HE_Password:-$(_readaccountconf_mutable HE_Password)}"
f7299403 24 if [ -z "$HE_Username" ] || [ -z "$HE_Password" ]; then
8534e3b2
OS
25 HE_Username=
26 HE_Password=
4dfdfa0b 27 _err "No auth details provided. Please set user credentials using the \$HE_Username and \$HE_Password environment variables."
4285d81c
OS
28 return 1
29 fi
2655e726 30 _saveaccountconf_mutable HE_Username "$HE_Username"
31 _saveaccountconf_mutable HE_Password "$HE_Password"
7d64e141 32
31b67ab9 33 # Fills in the $_zone_id
ff74778d 34 _find_zone "$_full_domain" || return 1
7d64e141 35 _debug "Zone id \"$_zone_id\" will be used."
792f3775 36 username_encoded="$(printf "%s" "${HE_Username}" | _url_encode)"
37 password_encoded="$(printf "%s" "${HE_Password}" | _url_encode)"
38 body="email=${username_encoded}&pass=${password_encoded}"
4285d81c 39 body="$body&account="
4285d81c
OS
40 body="$body&menu=edit_zone"
41 body="$body&Type=TXT"
42 body="$body&hosted_dns_zoneid=$_zone_id"
43 body="$body&hosted_dns_recordid="
44 body="$body&hosted_dns_editzone=1"
45 body="$body&Priority="
46 body="$body&Name=$_full_domain"
47 body="$body&Content=$_txt_value"
48 body="$body&TTL=300"
49 body="$body&hosted_dns_editrecord=Submit"
ff74778d 50 response="$(_post "$body" "https://dns.he.net/")"
ccf9a997
OS
51 exit_code="$?"
52 if [ "$exit_code" -eq 0 ]; then
88bb7b78 53 _info "TXT record added successfully."
ccf9a997
OS
54 else
55 _err "Couldn't add the TXT record."
ccf9a997 56 fi
f7299403 57 _debug2 response "$response"
4dd69a8b 58 return "$exit_code"
7d64e141
OS
59}
60
7d64e141
OS
61#-- dns_he_rm() - Remove TXT record ------------------------------------
62# Usage: dns_he_rm _acme-challenge.subdomain.domain.com "XyZ123..."
63
64dns_he_rm() {
65 _full_domain=$1
66 _txt_value=$2
67 _info "Cleaning up after DNS-01 Hurricane Electric hook"
2655e726 68 HE_Username="${HE_Username:-$(_readaccountconf_mutable HE_Username)}"
69 HE_Password="${HE_Password:-$(_readaccountconf_mutable HE_Password)}"
7d64e141 70 # fills in the $_zone_id
ff74778d 71 _find_zone "$_full_domain" || return 1
7d64e141
OS
72 _debug "Zone id \"$_zone_id\" will be used."
73
74 # Find the record id to clean
792f3775 75 username_encoded="$(printf "%s" "${HE_Username}" | _url_encode)"
76 password_encoded="$(printf "%s" "${HE_Password}" | _url_encode)"
77 body="email=${username_encoded}&pass=${password_encoded}"
4285d81c
OS
78 body="$body&hosted_dns_zoneid=$_zone_id"
79 body="$body&menu=edit_zone"
80 body="$body&hosted_dns_editzone="
7d64e141 81
6d6b2efd 82 response="$(_post "$body" "https://dns.he.net/")"
83 _debug2 "response" "$response"
84 if ! _contains "$response" "$_txt_value"; then
85 _debug "The txt record is not found, just skip"
86 return 0
87 fi
83cb89e4 88 _record_id="$(echo "$response" | tr -d "#" | sed "s/<tr/#<tr/g" | tr -d "\n" | tr "#" "\n" | grep "$_full_domain" | grep '"dns_tr"' | grep -- "$_txt_value" | cut -d '"' -f 4)"
6d6b2efd 89 _debug2 _record_id "$_record_id"
90 if [ -z "$_record_id" ]; then
91 _err "Can not find record id"
92 return 1
93 fi
7d64e141 94 # Remove the record
4c1f70af 95 username_encoded="$(printf "%s" "${HE_Username}" | _url_encode)"
96 password_encoded="$(printf "%s" "${HE_Password}" | _url_encode)"
97 body="email=${username_encoded}&pass=${password_encoded}"
4285d81c
OS
98 body="$body&menu=edit_zone"
99 body="$body&hosted_dns_zoneid=$_zone_id"
100 body="$body&hosted_dns_recordid=$_record_id"
101 body="$body&hosted_dns_editzone=1"
102 body="$body&hosted_dns_delrecord=1"
103 body="$body&hosted_dns_delconfirm=delete"
19c43451 104 _post "$body" "https://dns.he.net/" |
105 grep '<div id="dns_status" onClick="hideThis(this);">Successfully removed record.</div>' \
235b5b0c 106 >/dev/null
baa11605
OS
107 exit_code="$?"
108 if [ "$exit_code" -eq 0 ]; then
88bb7b78 109 _info "Record removed successfully."
7d64e141 110 else
31b67ab9 111 _err "Could not clean (remove) up the record. Please go to HE administration interface and clean it by hand."
baa11605 112 return "$exit_code"
7d64e141
OS
113 fi
114}
115
7d64e141
OS
116########################## PRIVATE FUNCTIONS ###########################
117
7d64e141 118_find_zone() {
7d64e141 119 _domain="$1"
792f3775 120 username_encoded="$(printf "%s" "${HE_Username}" | _url_encode)"
121 password_encoded="$(printf "%s" "${HE_Password}" | _url_encode)"
122 body="email=${username_encoded}&pass=${password_encoded}"
6d6b2efd 123 response="$(_post "$body" "https://dns.he.net/")"
124 _debug2 response "$response"
792f3775 125 if _contains "$response" '>Incorrect<'; then
126 _err "Unable to login to dns.he.net please check username and password"
127 return 1
128 fi
6d6b2efd 129 _table="$(echo "$response" | tr -d "#" | sed "s/<table/#<table/g" | tr -d "\n" | tr "#" "\n" | grep 'id="domains_table"')"
130 _debug2 _table "$_table"
d064260b 131 _matches="$(echo "$_table" | sed "s/<tr/#<tr/g" | tr "#" "\n" | grep 'alt="edit"' | tr -d " " | sed "s/<td/#<td/g" | tr "#" "\n" | grep 'hosted_dns_zoneid')"
6d6b2efd 132 _debug2 _matches "$_matches"
aefed1d1 133 # Zone names and zone IDs are in same order
6d6b2efd 134 _zone_ids=$(echo "$_matches" | _egrep_o "hosted_dns_zoneid=[0-9]*&" | cut -d = -f 2 | tr -d '&')
135 _zone_names=$(echo "$_matches" | _egrep_o "name=.*onclick" | cut -d '"' -f 2)
aefed1d1 136 _debug2 "These are the zones on this HE account:"
ee38ccca 137 _debug2 "_zone_names" "$_zone_names"
aefed1d1 138 _debug2 "And these are their respective IDs:"
ee38ccca 139 _debug2 "_zone_ids" "$_zone_ids"
6d6b2efd 140 if [ -z "$_zone_names" ] || [ -z "$_zone_ids" ]; then
141 _err "Can not get zone names."
142 return 1
143 fi
aefed1d1 144 # Walk through all possible zone names
7d64e141 145 _strip_counter=1
ff74778d
OS
146 while true; do
147 _attempted_zone=$(echo "$_domain" | cut -d . -f ${_strip_counter}-)
7d64e141
OS
148
149 # All possible zone names have been tried
ff74778d 150 if [ -z "$_attempted_zone" ]; then
7d64e141 151 _err "No zone for domain \"$_domain\" found."
aefed1d1 152 return 1
7d64e141
OS
153 fi
154
aefed1d1 155 _debug "Looking for zone \"${_attempted_zone}\""
a25b2af6 156
ee38ccca 157 line_num="$(echo "$_zone_names" | grep -n "^$_attempted_zone\$" | _head_n 1 | cut -d : -f 1)"
158 _debug2 line_num "$line_num"
6d6b2efd 159 if [ "$line_num" ]; then
160 _zone_id=$(echo "$_zone_ids" | sed -n "${line_num}p")
ee38ccca 161 if [ -z "$_zone_id" ]; then
162 _err "Can not find zone id."
163 return 1
164 fi
31b67ab9 165 _debug "Found relevant zone \"$_attempted_zone\" with id \"$_zone_id\" - will be used for domain \"$_domain\"."
aefed1d1
OS
166 return 0
167 fi
168
31b67ab9 169 _debug "Zone \"$_attempted_zone\" doesn't exist, let's try a less specific zone."
577380e9 170 _strip_counter=$(_math "$_strip_counter" + 1)
7d64e141 171 done
aefed1d1 172}
7d64e141 173# vim: et:ts=2:sw=2: