]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_he.sh
nginx
[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
f7299403 22 if [ -z "$HE_Username" ] || [ -z "$HE_Password" ]; then
8534e3b2
OS
23 HE_Username=
24 HE_Password=
ff74778d 25 _err "No auth details provided. Please set user credentials using the \$HE_Username and \$HE_Password envoronment variables."
4285d81c
OS
26 return 1
27 fi
7d64e141
OS
28 _saveaccountconf HE_Username "$HE_Username"
29 _saveaccountconf HE_Password "$HE_Password"
30
31b67ab9 31 # Fills in the $_zone_id
ff74778d 32 _find_zone "$_full_domain" || return 1
7d64e141
OS
33 _debug "Zone id \"$_zone_id\" will be used."
34
4285d81c
OS
35 body="email=${HE_Username}&pass=${HE_Password}"
36 body="$body&account="
4285d81c
OS
37 body="$body&menu=edit_zone"
38 body="$body&Type=TXT"
39 body="$body&hosted_dns_zoneid=$_zone_id"
40 body="$body&hosted_dns_recordid="
41 body="$body&hosted_dns_editzone=1"
42 body="$body&Priority="
43 body="$body&Name=$_full_domain"
44 body="$body&Content=$_txt_value"
45 body="$body&TTL=300"
46 body="$body&hosted_dns_editrecord=Submit"
ff74778d 47 response="$(_post "$body" "https://dns.he.net/")"
ccf9a997
OS
48 exit_code="$?"
49 if [ "$exit_code" -eq 0 ]; then
88bb7b78 50 _info "TXT record added successfully."
ccf9a997
OS
51 else
52 _err "Couldn't add the TXT record."
ccf9a997 53 fi
f7299403 54 _debug2 response "$response"
4dd69a8b 55 return "$exit_code"
7d64e141
OS
56}
57
7d64e141
OS
58#-- dns_he_rm() - Remove TXT record ------------------------------------
59# Usage: dns_he_rm _acme-challenge.subdomain.domain.com "XyZ123..."
60
61dns_he_rm() {
62 _full_domain=$1
63 _txt_value=$2
64 _info "Cleaning up after DNS-01 Hurricane Electric hook"
65
7d64e141 66 # fills in the $_zone_id
ff74778d 67 _find_zone "$_full_domain" || return 1
7d64e141
OS
68 _debug "Zone id \"$_zone_id\" will be used."
69
70 # Find the record id to clean
4285d81c
OS
71 body="email=${HE_Username}&pass=${HE_Password}"
72 body="$body&hosted_dns_zoneid=$_zone_id"
73 body="$body&menu=edit_zone"
74 body="$body&hosted_dns_editzone="
f438ff4b 75 domain_regex="$(echo "$_full_domain" | sed 's/\./\\./g')" # escape dots
ff74778d 76 _record_id=$(_post "$body" "https://dns.he.net/" \
f7299403 77 | tr -d '\n' \
f438ff4b
OS
78 | _egrep_o "data=\"&quot;${_txt_value}&quot;([^>]+>){6}[^<]+<[^;]+;deleteRecord\('[0-9]+','${domain_regex}','TXT'\)" \
79 | _egrep_o "[0-9]+','${domain_regex}','TXT'\)$" \
f7299403
OS
80 | _egrep_o "^[0-9]+"
81 )
82 # The series of egreps above could have been done a bit shorter but
83 # I wanted to double-check whether it's the correct record (in case
84 # HE changes their website somehow).
7d64e141
OS
85
86 # Remove the record
4285d81c
OS
87 body="email=${HE_Username}&pass=${HE_Password}"
88 body="$body&menu=edit_zone"
89 body="$body&hosted_dns_zoneid=$_zone_id"
90 body="$body&hosted_dns_recordid=$_record_id"
91 body="$body&hosted_dns_editzone=1"
92 body="$body&hosted_dns_delrecord=1"
93 body="$body&hosted_dns_delconfirm=delete"
ff74778d 94 _post "$body" "https://dns.he.net/" \
7d64e141 95 | grep '<div id="dns_status" onClick="hideThis(this);">Successfully removed record.</div>' \
235b5b0c 96 >/dev/null
baa11605
OS
97 exit_code="$?"
98 if [ "$exit_code" -eq 0 ]; then
88bb7b78 99 _info "Record removed successfully."
7d64e141 100 else
31b67ab9 101 _err "Could not clean (remove) up the record. Please go to HE administration interface and clean it by hand."
baa11605 102 return "$exit_code"
7d64e141
OS
103 fi
104}
105
7d64e141
OS
106########################## PRIVATE FUNCTIONS ###########################
107
7d64e141 108#-- _find_zone() -------------------------------------------------------
7d64e141
OS
109# Returns the most specific zone found in administration interface.
110#
7d64e141
OS
111# Example:
112#
113# _find_zone first.second.third.co.uk
114#
115# ... will return the first zone that exists in admin out of these:
116# - "first.second.third.co.uk"
117# - "second.third.co.uk"
118# - "third.co.uk"
119# - "co.uk" <-- unlikely
120# - "uk" <-'
121#
122# (another approach would be something like this:
123# https://github.com/hlandau/acme/blob/master/_doc/dns.hook
124# - that's better if there are multiple pages. It's so much simpler.
125# )
126
127_find_zone() {
128
129 _domain="$1"
130
4285d81c 131 body="email=${HE_Username}&pass=${HE_Password}"
aefed1d1
OS
132 _matches=$(_post "$body" "https://dns.he.net/" \
133 | _egrep_o "delete_dom.*name=\"[^\"]+\" value=\"[0-9]+"
134 )
135 # Zone names and zone IDs are in same order
31b67ab9
OS
136 _zone_ids=$(echo "$_matches" | cut -d '"' -f 5)
137 _zone_names=$(echo "$_matches" | cut -d '"' -f 3)
aefed1d1
OS
138 _debug2 "These are the zones on this HE account:"
139 _debug2 "$_zone_names"
140 _debug2 "And these are their respective IDs:"
141 _debug2 "$_zone_ids"
142
143 # Walk through all possible zone names
7d64e141 144 _strip_counter=1
ff74778d
OS
145 while true; do
146 _attempted_zone=$(echo "$_domain" | cut -d . -f ${_strip_counter}-)
7d64e141
OS
147
148 # All possible zone names have been tried
ff74778d 149 if [ -z "$_attempted_zone" ]; then
7d64e141 150 _err "No zone for domain \"$_domain\" found."
aefed1d1 151 return 1
7d64e141
OS
152 fi
153
aefed1d1 154 _debug "Looking for zone \"${_attempted_zone}\""
a25b2af6
OS
155
156 # Take care of "." and only match whole lines. Note that grep -F
157 # cannot be used because there's no way to make it match whole
158 # lines.
159 regex="^$(echo "$_attempted_zone" | sed 's/\./\\./g')$"
160 line_num=$(echo "$_zone_names" \
161 | grep -n "$regex" \
162 | cut -d : -f 1
163 )
164
165 if [ -n "$line_num" ]; then
166 _zone_id=$(echo "$_zone_ids" | sed "${line_num}q;d")
31b67ab9 167 _debug "Found relevant zone \"$_attempted_zone\" with id \"$_zone_id\" - will be used for domain \"$_domain\"."
aefed1d1
OS
168 return 0
169 fi
170
31b67ab9 171 _debug "Zone \"$_attempted_zone\" doesn't exist, let's try a less specific zone."
577380e9 172 _strip_counter=$(_math "$_strip_counter" + 1)
7d64e141 173 done
aefed1d1 174}
7d64e141 175# vim: et:ts=2:sw=2: