]>
Commit | Line | Data |
---|---|---|
80d63dbb KV |
1 | #!/usr/bin/env sh |
2 | ||
3 | DOMENESHOP_Api_Endpoint="https://api.domeneshop.no/v0" | |
4 | ||
5 | ##################### Public functions ##################### | |
6 | ||
7 | # Usage: dns_domeneshop_add <full domain> <txt record> | |
8 | # Example: dns_domeneshop_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
9 | dns_domeneshop_add() { | |
10 | fulldomain=$1 | |
11 | txtvalue=$2 | |
12 | ||
13 | # Get token and secret | |
14 | DOMENESHOP_Token="${DOMENESHOP_Token:-$(_readaccountconf_mutable DOMENESHOP_Token)}" | |
15 | DOMENESHOP_Secret="${DOMENESHOP_Secret:-$(_readaccountconf_mutable DOMENESHOP_Secret)}" | |
16 | ||
17 | if [ -z "$DOMENESHOP_Token" ] || [ -z "$DOMENESHOP_Secret" ]; then | |
18 | DOMENESHOP_Token="" | |
19 | DOMENESHOP_Secret="" | |
20 | _err "You need to spesify a Domeneshop/Domainnameshop API Token and Secret." | |
21 | return 1 | |
22 | fi | |
23 | ||
24 | # Save the api token and secret. | |
25 | _saveaccountconf_mutable DOMENESHOP_Token "$DOMENESHOP_Token" | |
26 | _saveaccountconf_mutable DOMENESHOP_Secret "$DOMENESHOP_Secret" | |
27 | ||
28 | # Get the domain name id | |
29 | if ! _get_domainid "$fulldomain"; then | |
30 | _err "Did not find domainname" | |
31 | return 1 | |
32 | fi | |
33 | ||
34 | # Create record | |
35 | _domeneshop_rest POST "domains/$_domainid/dns" "{\"type\":\"TXT\",\"host\":\"$_sub_domain\",\"data\":\"$txtvalue\",\"ttl\":120}" | |
36 | } | |
37 | ||
38 | # Usage: dns_domeneshop_rm <full domain> <txt record> | |
39 | # Example: dns_domeneshop_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
40 | dns_domeneshop_rm() { | |
41 | fulldomain=$1 | |
42 | txtvalue=$2 | |
43 | ||
44 | # Get token and secret | |
45 | DOMENESHOP_Token="${DOMENESHOP_Token:-$(_readaccountconf_mutable DOMENESHOP_Token)}" | |
46 | DOMENESHOP_Secret="${DOMENESHOP_Secret:-$(_readaccountconf_mutable DOMENESHOP_Secret)}" | |
47 | ||
48 | if [ -z "$DOMENESHOP_Token" ] || [ -z "$DOMENESHOP_Secret" ]; then | |
49 | DOMENESHOP_Token="" | |
50 | DOMENESHOP_Secret="" | |
51 | _err "You need to spesify a Domeneshop/Domainnameshop API Token and Secret." | |
52 | return 1 | |
53 | fi | |
54 | ||
55 | # Get the domain name id | |
56 | if ! _get_domainid "$fulldomain"; then | |
57 | _err "Did not find domainname" | |
58 | return 1 | |
59 | fi | |
60 | ||
61 | # Find record | |
62 | if ! _get_recordid "$_domainid" "$_sub_domain" "$txtvalue"; then | |
63 | _err "Did not find dns record" | |
64 | return 1 | |
65 | fi | |
66 | ||
67 | # Remove record | |
68 | _domeneshop_rest DELETE "domains/$_domainid/dns/$_recordid" | |
69 | } | |
70 | ||
71 | ##################### Private functions ##################### | |
72 | ||
73 | _get_domainid() { | |
74 | domain=$1 | |
75 | ||
76 | # Get domains | |
77 | _domeneshop_rest GET "domains" | |
78 | ||
79 | if ! _contains "$response" "\"id\":"; then | |
80 | _err "failed to get domain names" | |
81 | return 1 | |
82 | fi | |
83 | ||
84 | i=2 | |
85 | p=1 | |
86 | while true; do | |
87 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
88 | _debug "h" "$h" | |
89 | if [ -z "$h" ]; then | |
90 | #not valid | |
91 | return 1 | |
92 | fi | |
93 | ||
94 | if _contains "$response" "\"$h\"" >/dev/null; then | |
95 | # We have found the domain name. | |
96 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
97 | _domain=$h | |
98 | _domainid=$(printf "%s" "$response" | _egrep_o "[^{]*\"domain\":\"$_domain\"[^}]*" | _egrep_o "\"id\":[0-9]+" | cut -d : -f 2) | |
99 | return 0 | |
100 | fi | |
101 | p=$i | |
102 | i=$(_math "$i" + 1) | |
103 | done | |
104 | return 1 | |
105 | } | |
106 | ||
107 | _get_recordid() { | |
108 | domainid=$1 | |
109 | subdomain=$2 | |
110 | txtvalue=$3 | |
111 | ||
112 | # Get all dns records for the domainname | |
113 | _domeneshop_rest GET "domains/$domainid/dns" | |
114 | ||
115 | if ! _contains "$response" "\"id\":"; then | |
116 | _debug "No records in dns" | |
117 | return 1 | |
118 | fi | |
119 | ||
120 | if ! _contains "$response" "\"host\":\"$subdomain\""; then | |
121 | _debug "Record does not exist" | |
122 | return 1 | |
123 | fi | |
124 | ||
125 | # Get the id of the record in question | |
126 | _recordid=$(printf "%s" "$response" | _egrep_o "[^{]*\"host\":\"$subdomain\"[^}]*" | _egrep_o "[^{]*\"data\":\"$txtvalue\"[^}]*" | _egrep_o "\"id\":[0-9]+" | cut -d : -f 2) | |
127 | if [ -z "$_recordid" ]; then | |
128 | return 1 | |
129 | fi | |
130 | return 0 | |
131 | } | |
132 | ||
133 | _domeneshop_rest() { | |
134 | method=$1 | |
135 | endpoint=$2 | |
136 | data=$3 | |
137 | ||
138 | credentials=$(printf "%b" "$DOMENESHOP_Token:$DOMENESHOP_Secret" | _base64) | |
139 | ||
140 | export _H1="Authorization: Basic $credentials" | |
141 | export _H2="Content-Type: application/json" | |
142 | ||
143 | if [ "$method" != "GET" ]; then | |
144 | response="$(_post "$data" "$DOMENESHOP_Api_Endpoint/$endpoint" "" "$method")" | |
145 | else | |
146 | response="$(_get "$DOMENESHOP_Api_Endpoint/$endpoint")" | |
147 | fi | |
148 | ||
149 | if [ "$?" != "0" ]; then | |
150 | _err "error $endpoint" | |
151 | return 1 | |
152 | fi | |
153 | ||
154 | return 0 | |
155 | } |