]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_one.sh
fixed error in CI
[mirror_acme.sh.git] / dnsapi / dns_one.sh
CommitLineData
9ff6d6e7 1#!/usr/bin/env sh
2# -*- mode: sh; tab-width: 2; indent-tabs-mode: s; coding: utf-8 -*-
3
4# one.com ui wrapper for acme.sh
5# Author: github: @diseq
6# Created: 2019-02-17
68b42a00 7# Fixed by: @der-berni
8# Modified: 2019-05-20
9ff6d6e7 9#
e340593a 10# export ONECOM_User="username"
11# export ONECOM_Password="password"
9ff6d6e7 12#
13# Usage:
14# acme.sh --issue --dns dns_one -d example.com
15#
16# only single domain supported atm
17
18dns_one_add() {
68b42a00 19 fulldomain=$1
9ff6d6e7 20 txtvalue=$2
68b42a00 21
a3089a71 22 if ! _dns_one_login; then
23 _err "login failed"
24 return 1
25 fi
26
27 _debug "detect the root domain"
68b42a00 28 if ! _get_root "$fulldomain"; then
a3089a71 29 _err "root domain not found"
68b42a00 30 return 1
31 fi
32
33 mysubdomain=$_sub_domain
34 mydomain=$_domain
35 _debug mysubdomain "$mysubdomain"
36 _debug mydomain "$mydomain"
68b42a00 37
9ff6d6e7 38 # get entries
39 response="$(_get "https://www.one.com/admin/api/domains/$mydomain/dns/custom_records")"
40 _debug response "$response"
41
9ff6d6e7 42 # Update the IP address for domain entry
43 postdata="{\"type\":\"dns_custom_records\",\"attributes\":{\"priority\":0,\"ttl\":600,\"type\":\"TXT\",\"prefix\":\"$mysubdomain\",\"content\":\"$txtvalue\"}}"
44 _debug postdata "$postdata"
45 response="$(_post "$postdata" "https://www.one.com/admin/api/domains/$mydomain/dns/custom_records" "" "POST" "application/json")"
46 response="$(echo "$response" | _normalizeJson)"
47 _debug response "$response"
48
dac75a1d 49 id=$(echo "$response" | sed -n "s/{\"result\":{\"data\":{\"type\":\"dns_custom_records\",\"id\":\"\([^\"]*\)\",\"attributes\":{\"prefix\":\"$mysubdomain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"priority\":0,\"ttl\":600}}},\"metadata\":null}/\1/p")
68b42a00 50
9ff6d6e7 51 if [ -z "$id" ]; then
52 _err "Add txt record error."
53 return 1
54 else
55 _info "Added, OK ($id)"
56 return 0
57 fi
58
59}
60
61dns_one_rm() {
68b42a00 62 fulldomain=$1
9ff6d6e7 63 txtvalue=$2
68b42a00 64
a3089a71 65 if ! _dns_one_login; then
66 _err "login failed"
67 return 1
68 fi
69
70 _debug "detect the root domain"
68b42a00 71 if ! _get_root "$fulldomain"; then
a3089a71 72 _err "root domain not found"
68b42a00 73 return 1
74 fi
75
76 mysubdomain=$_sub_domain
77 mydomain=$_domain
78 _debug mysubdomain "$mysubdomain"
79 _debug mydomain "$mydomain"
68b42a00 80
9ff6d6e7 81 # get entries
82 response="$(_get "https://www.one.com/admin/api/domains/$mydomain/dns/custom_records")"
83 response="$(echo "$response" | _normalizeJson)"
84 _debug response "$response"
85
9ff6d6e7 86 id=$(printf -- "%s" "$response" | sed -n "s/.*{\"type\":\"dns_custom_records\",\"id\":\"\([^\"]*\)\",\"attributes\":{\"prefix\":\"$mysubdomain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"priority\":0,\"ttl\":600}.*/\1/p")
87
88 if [ -z "$id" ]; then
89 _err "Txt record not found."
90 return 1
91 fi
92
93 # delete entry
94 response="$(_post "$postdata" "https://www.one.com/admin/api/domains/$mydomain/dns/custom_records/$id" "" "DELETE" "application/json")"
95 response="$(echo "$response" | _normalizeJson)"
96 _debug response "$response"
68b42a00 97
0499d2b5 98 if [ "$response" = '{"result":null,"metadata":null}' ]; then
ed3f2646 99 _info "Removed, OK"
100 return 0
101 else
102 _err "Removing txt record error."
103 return 1
9ff6d6e7 104 fi
105
0bb746ba 106}
68b42a00 107
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
119 if [ -z "$h" ]; then
120 #not valid
121 return 1
122 fi
a3089a71 123
124 response="$(_get "https://www.one.com/admin/api/domains/$h/dns/custom_records")"
125
89e73594 126 if ! _contains "$response" "CRMRST_000302"; then
68b42a00 127 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
128 _domain="$h"
129 return 0
130 fi
131 p=$i
132 i=$(_math "$i" + 1)
133 done
134 _err "Unable to parse this domain"
135 return 1
136}
a3089a71 137
138_dns_one_login() {
139
140 # get credentials
141 ONECOM_User="${ONECOM_User:-$(_readaccountconf_mutable ONECOM_User)}"
142 ONECOM_Password="${ONECOM_Password:-$(_readaccountconf_mutable ONECOM_Password)}"
143 if [ -z "$ONECOM_User" ] || [ -z "$ONECOM_Password" ]; then
144 ONECOM_User=""
145 ONECOM_Password=""
146 _err "You didn't specify a one.com username and password yet."
147 _err "Please create the key and try again."
148 return 1
149 fi
150
151 #save the api key and email to the account conf file.
152 _saveaccountconf_mutable ONECOM_User "$ONECOM_User"
153 _saveaccountconf_mutable ONECOM_Password "$ONECOM_Password"
154
155 # Login with user and password
156 postdata="loginDomain=true"
157 postdata="$postdata&displayUsername=$ONECOM_User"
158 postdata="$postdata&username=$ONECOM_User"
159 postdata="$postdata&targetDomain="
160 postdata="$postdata&password1=$ONECOM_Password"
161 postdata="$postdata&loginTarget="
162 #_debug postdata "$postdata"
163
164 response="$(_post "$postdata" "https://www.one.com/admin/login.do" "" "POST" "application/x-www-form-urlencoded")"
165 #_debug response "$response"
166
167 # Get SessionID
168 JSESSIONID="$(grep "OneSIDCrmAdmin" "$HTTP_HEADER" | grep "^[Ss]et-[Cc]ookie:" | _head_n 1 | _egrep_o 'OneSIDCrmAdmin=[^;]*;' | tr -d ';')"
169 _debug jsessionid "$JSESSIONID"
170
171 if [ -z "$JSESSIONID" ]; then
172 _err "error sessionid cookie not found"
173 return 1
174 fi
175
176 export _H1="Cookie: ${JSESSIONID}"
177
178 return 0
179 }