]>
Commit | Line | Data |
---|---|---|
72ce3770 MB |
1 | #!/usr/bin/env sh |
2 | ||
3 | # | |
4 | #PointHQ_Key="sdfsdfsdfljlbjkljlkjsdfoiwje" | |
5 | # | |
6 | #PointHQ_Email="xxxx@sss.com" | |
7 | ||
8 | PointHQ_Api="https://api.pointhq.com" | |
9 | ||
10 | ######## Public functions ##################### | |
11 | ||
12 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
13 | dns_pointhq_add() { | |
14 | fulldomain=$1 | |
15 | txtvalue=$2 | |
16 | ||
17 | PointHQ_Key="${PointHQ_Key:-$(_readaccountconf_mutable PointHQ_Key)}" | |
18 | PointHQ_Email="${PointHQ_Email:-$(_readaccountconf_mutable PointHQ_Email)}" | |
19 | if [ -z "$PointHQ_Key" ] || [ -z "$PointHQ_Email" ]; then | |
20 | PointHQ_Key="" | |
21 | PointHQ_Email="" | |
22 | _err "You didn't specify a PointHQ API key and email yet." | |
23 | _err "Please create the key and try again." | |
24 | return 1 | |
25 | fi | |
26 | ||
27 | if ! _contains "$PointHQ_Email" "@"; then | |
28 | _err "It seems that the PointHQ_Email=$PointHQ_Email is not a valid email address." | |
29 | _err "Please check and retry." | |
30 | return 1 | |
31 | fi | |
32 | ||
33 | #save the api key and email to the account conf file. | |
34 | _saveaccountconf_mutable PointHQ_Key "$PointHQ_Key" | |
35 | _saveaccountconf_mutable PointHQ_Email "$PointHQ_Email" | |
36 | ||
37 | _debug "First detect the root zone" | |
38 | if ! _get_root "$fulldomain"; then | |
39 | _err "invalid domain" | |
40 | return 1 | |
41 | fi | |
42 | _debug _sub_domain "$_sub_domain" | |
43 | _debug _domain "$_domain" | |
44 | ||
45 | _info "Adding record" | |
46 | if _pointhq_rest POST "zones/$_domain/records" "{\"zone_record\": {\"name\":\"$_sub_domain\",\"record_type\":\"TXT\",\"data\":\"$txtvalue\",\"ttl\":3600}}"; then | |
47 | if printf -- "%s" "$response" | grep "$fulldomain" >/dev/null; then | |
48 | _info "Added, OK" | |
49 | return 0 | |
50 | else | |
51 | _err "Add txt record error." | |
52 | return 1 | |
53 | fi | |
54 | fi | |
55 | _err "Add txt record error." | |
56 | return 1 | |
57 | } | |
58 | ||
59 | #fulldomain txtvalue | |
60 | dns_pointhq_rm() { | |
61 | fulldomain=$1 | |
62 | txtvalue=$2 | |
63 | ||
64 | PointHQ_Key="${PointHQ_Key:-$(_readaccountconf_mutable PointHQ_Key)}" | |
65 | PointHQ_Email="${PointHQ_Email:-$(_readaccountconf_mutable PointHQ_Email)}" | |
66 | if [ -z "$PointHQ_Key" ] || [ -z "$PointHQ_Email" ]; then | |
67 | PointHQ_Key="" | |
68 | PointHQ_Email="" | |
69 | _err "You didn't specify a PointHQ API key and email yet." | |
70 | _err "Please create the key and try again." | |
71 | return 1 | |
72 | fi | |
73 | ||
74 | _debug "First detect the root zone" | |
75 | if ! _get_root "$fulldomain"; then | |
76 | _err "invalid domain" | |
77 | return 1 | |
78 | fi | |
79 | _debug _sub_domain "$_sub_domain" | |
80 | _debug _domain "$_domain" | |
81 | ||
82 | _debug "Getting txt records" | |
83 | _pointhq_rest GET "zones/${_domain}/records?record_type=TXT&name=$_sub_domain" | |
84 | ||
85 | if ! printf "%s" "$response" | grep "^\[" >/dev/null; then | |
86 | _err "Error" | |
87 | return 1 | |
88 | fi | |
89 | ||
90 | if [ "$response" = "[]" ]; then | |
91 | _info "No records to remove." | |
92 | else | |
93 | record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*" | cut -d : -f 2 | tr -d \" | head -n 1) | |
94 | _debug "record_id" "$record_id" | |
95 | if [ -z "$record_id" ]; then | |
96 | _err "Can not get record id to remove." | |
97 | return 1 | |
98 | fi | |
99 | if ! _pointhq_rest DELETE "zones/$_domain/records/$record_id"; then | |
100 | _err "Delete record error." | |
101 | return 1 | |
102 | fi | |
103 | _contains "$response" '"status":"OK"' | |
104 | fi | |
105 | } | |
106 | ||
107 | #################### Private functions below ################################## | |
108 | #_acme-challenge.www.domain.com | |
109 | #returns | |
110 | # _sub_domain=_acme-challenge.www | |
111 | # _domain=domain.com | |
112 | _get_root() { | |
113 | domain=$1 | |
114 | i=2 | |
115 | p=1 | |
116 | while true; do | |
117 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
118 | _debug h "$h" | |
119 | if [ -z "$h" ]; then | |
120 | #not valid | |
121 | return 1 | |
122 | fi | |
123 | ||
124 | if ! _pointhq_rest GET "zones"; then | |
125 | return 1 | |
126 | fi | |
127 | ||
128 | if _contains "$response" "\"name\":\"$h\"" >/dev/null; then | |
129 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
130 | _domain=$h | |
131 | return 0 | |
132 | fi | |
133 | p=$i | |
134 | i=$(_math "$i" + 1) | |
135 | done | |
136 | return 1 | |
137 | } | |
138 | ||
139 | _pointhq_rest() { | |
140 | m=$1 | |
141 | ep="$2" | |
142 | data="$3" | |
143 | _debug "$ep" | |
144 | ||
145 | _pointhq_auth=$(printf "%s:%s" "$PointHQ_Email" "$PointHQ_Key" | _base64) | |
146 | ||
147 | export _H1="Authorization: Basic $_pointhq_auth" | |
148 | export _H2="Content-Type: application/json" | |
149 | export _H3="Accept: application/json" | |
150 | ||
151 | if [ "$m" != "GET" ]; then | |
152 | _debug data "$data" | |
153 | response="$(_post "$data" "$PointHQ_Api/$ep" "" "$m")" | |
154 | else | |
155 | response="$(_get "$PointHQ_Api/$ep")" | |
156 | fi | |
157 | ||
158 | if [ "$?" != "0" ]; then | |
159 | _err "error $ep" | |
160 | return 1 | |
161 | fi | |
162 | _debug2 response "$response" | |
163 | return 0 | |
164 | } |