]>
Commit | Line | Data |
---|---|---|
5899d703 | 1 | #!/usr/bin/env sh |
2a05f24c | 2 | |
5899d703 | 3 | #LA_Id="test123" |
5899d703 | 4 | #LA_Key="d1j2fdo4dee3948" |
2a05f24c | 5 | |
6 | LA_Api="https://api.dns.la/api" | |
7 | ||
5899d703 | 8 | ######## Public functions ##################### |
2a05f24c | 9 | |
5899d703 | 10 | #Usage: dns_la_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" |
11 | dns_la_add() { | |
12 | fulldomain=$1 | |
13 | txtvalue=$2 | |
14 | ||
15 | LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}" | |
16 | LA_Key="${LA_Key:-$(_readaccountconf_mutable LA_Key)}" | |
2a05f24c | 17 | |
5899d703 | 18 | if [ -z "$LA_Id" ] || [ -z "$LA_Key" ]; then |
19 | LA_Id="" | |
20 | LA_Key="" | |
2a05f24c | 21 | _err "You didn't specify a dnsla api id and key yet." |
5899d703 | 22 | return 1 |
23 | fi | |
24 | ||
25 | #save the api key and email to the account conf file. | |
26 | _saveaccountconf_mutable LA_Id "$LA_Id" | |
27 | _saveaccountconf_mutable LA_Key "$LA_Key" | |
28 | ||
2a05f24c | 29 | _debug "First detect the root zone" |
5899d703 | 30 | if ! _get_root "$fulldomain"; then |
31 | _err "invalid domain" | |
32 | return 1 | |
33 | fi | |
2a05f24c | 34 | _debug _domain_id "$_domain_id" |
35 | _debug _sub_domain "$_sub_domain" | |
36 | _debug _domain "$_domain" | |
37 | ||
38 | _info "Adding record" | |
39 | if _la_rest "record.ashx?cmd=create&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&host=$_sub_domain&recordtype=TXT&recorddata=$txtvalue&recordline="; then | |
40 | if _contains "$response" '"resultid":'; then | |
41 | _info "Added, OK" | |
42 | return 0 | |
43 | elif _contains "$response" '"code":532'; then | |
44 | _info "Already exists, OK" | |
45 | return 0 | |
46 | else | |
47 | _err "Add txt record error." | |
48 | return 1 | |
49 | fi | |
50 | fi | |
51 | _err "Add txt record error." | |
52 | return 1 | |
5899d703 | 53 | |
54 | } | |
55 | ||
56 | #fulldomain txtvalue | |
57 | dns_la_rm() { | |
58 | fulldomain=$1 | |
59 | txtvalue=$2 | |
5899d703 | 60 | |
61 | LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}" | |
62 | LA_Key="${LA_Key:-$(_readaccountconf_mutable LA_Key)}" | |
e1eb0018 | 63 | |
64 | _debug "First detect the root zone" | |
5899d703 | 65 | if ! _get_root "$fulldomain"; then |
66 | _err "invalid domain" | |
67 | return 1 | |
68 | fi | |
2a05f24c | 69 | _debug _domain_id "$_domain_id" |
70 | _debug _sub_domain "$_sub_domain" | |
71 | _debug _domain "$_domain" | |
5899d703 | 72 | |
2a05f24c | 73 | _debug "Getting txt records" |
74 | if ! _la_rest "record.ashx?cmd=listn&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&domain=$_domain&host=$_sub_domain&recordtype=TXT&recorddata=$txtvalue"; then | |
75 | _err "Error" | |
5899d703 | 76 | return 1 |
77 | fi | |
78 | ||
2a05f24c | 79 | if ! _contains "$response" '"recordid":'; then |
80 | _info "Don't need to remove." | |
5899d703 | 81 | return 0 |
82 | fi | |
83 | ||
2a05f24c | 84 | record_id=$(printf "%s" "$response" | grep '"recordid":' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n') |
85 | _debug "record_id" "$record_id" | |
86 | if [ -z "$record_id" ]; then | |
87 | _err "Can not get record id to remove." | |
5899d703 | 88 | return 1 |
89 | fi | |
2a05f24c | 90 | if ! _la_rest "record.ashx?cmd=remove&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&domain=$_domain&recordid=$record_id"; then |
91 | _err "Delete record error." | |
5899d703 | 92 | return 1 |
93 | fi | |
2a05f24c | 94 | _contains "$response" '"code":300' |
5899d703 | 95 | |
5899d703 | 96 | } |
97 | ||
98 | #################### Private functions below ################################## | |
99 | #_acme-challenge.www.domain.com | |
100 | #returns | |
101 | # _sub_domain=_acme-challenge.www | |
102 | # _domain=domain.com | |
103 | # _domain_id=sdjkglgdfewsdfg | |
104 | _get_root() { | |
105 | domain=$1 | |
5fbaeda2 | 106 | i=1 |
5899d703 | 107 | p=1 |
2a05f24c | 108 | |
5899d703 | 109 | while true; do |
110 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
111 | if [ -z "$h" ]; then | |
112 | #not valid | |
113 | return 1 | |
114 | fi | |
115 | ||
2a05f24c | 116 | if ! _la_rest "domain.ashx?cmd=get&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domain=$h"; then |
5899d703 | 117 | return 1 |
118 | fi | |
119 | ||
2a05f24c | 120 | if _contains "$response" '"domainid":'; then |
121 | _domain_id=$(printf "%s" "$response" | grep '"domainid":' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n') | |
5899d703 | 122 | if [ "$_domain_id" ]; then |
123 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
5899d703 | 124 | _domain="$h" |
5899d703 | 125 | return 0 |
126 | fi | |
127 | return 1 | |
128 | fi | |
129 | p="$i" | |
130 | i=$(_math "$i" + 1) | |
131 | done | |
132 | return 1 | |
133 | } | |
134 | ||
2a05f24c | 135 | #Usage: URI |
136 | _la_rest() { | |
137 | url="$LA_Api/$1" | |
138 | _debug "$url" | |
5899d703 | 139 | |
2a05f24c | 140 | if ! response="$(_get "$url" | tr -d ' ' | tr "}" ",")"; then |
141 | _err "Error: $url" | |
5899d703 | 142 | return 1 |
143 | fi | |
2a05f24c | 144 | |
5899d703 | 145 | _debug2 response "$response" |
146 | return 0 | |
147 | } |