]>
Commit | Line | Data |
---|---|---|
5a689ce8 SP |
1 | #!/usr/bin/env sh |
2 | ||
3 | # bug reports to stepan@plyask.in | |
4 | ||
5 | # | |
6 | # export VEESP_User="username" | |
7 | # export VEESP_Password="password" | |
8 | ||
9 | VEESP_Api="https://secure.veesp.com/api" | |
10 | ||
11 | ######## Public functions ##################### | |
12 | ||
13 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
14 | dns_veesp_add() { | |
15 | fulldomain=$1 | |
16 | txtvalue=$2 | |
17 | ||
18 | VEESP_Password="${VEESP_Password:-$(_readaccountconf_mutable VEESP_Password)}" | |
19 | VEESP_User="${VEESP_User:-$(_readaccountconf_mutable VEESP_User)}" | |
20 | VEESP_auth=$(printf "%s" "$VEESP_User:$VEESP_Password" | _base64) | |
21 | ||
22 | if [ -z "$VEESP_Password" ] || [ -z "$VEESP_User" ]; then | |
23 | VEESP_Password="" | |
24 | VEESP_User="" | |
25 | _err "You don't specify veesp api key and email yet." | |
26 | _err "Please create you key and try again." | |
27 | return 1 | |
28 | fi | |
29 | ||
30 | #save the api key and email to the account conf file. | |
31 | _saveaccountconf_mutable VEESP_Password "$VEESP_Password" | |
32 | _saveaccountconf_mutable VEESP_User "$VEESP_User" | |
33 | ||
34 | _debug "First detect the root zone" | |
35 | if ! _get_root "$fulldomain"; then | |
36 | _err "invalid domain" | |
37 | return 1 | |
38 | fi | |
39 | _debug _domain_id "$_domain_id" | |
40 | _debug _sub_domain "$_sub_domain" | |
41 | _debug _domain "$_domain" | |
42 | ||
43 | _info "Adding record" | |
44 | if VEESP_rest POST "service/$_service_id/dns/$_domain_id/records" "{\"name\":\"$fulldomain\",\"ttl\":1,\"priority\":0,\"type\":\"TXT\",\"content\":\"$txtvalue\"}"; then | |
45 | if _contains "$response" "\"success\":true"; then | |
46 | _info "Added" | |
47 | #todo: check if the record takes effect | |
48 | return 0 | |
49 | else | |
50 | _err "Add txt record error." | |
51 | return 1 | |
52 | fi | |
53 | fi | |
54 | } | |
55 | ||
56 | # Usage: fulldomain txtvalue | |
57 | # Used to remove the txt record after validation | |
58 | dns_veesp_rm() { | |
59 | fulldomain=$1 | |
60 | txtvalue=$2 | |
61 | ||
62 | VEESP_Password="${VEESP_Password:-$(_readaccountconf_mutable VEESP_Password)}" | |
63 | VEESP_User="${VEESP_User:-$(_readaccountconf_mutable VEESP_User)}" | |
64 | VEESP_auth=$(printf "%s" "$VEESP_User:$VEESP_Password" | _base64) | |
65 | ||
66 | _debug "First detect the root zone" | |
67 | if ! _get_root "$fulldomain"; then | |
68 | _err "invalid domain" | |
69 | return 1 | |
70 | fi | |
71 | _debug _domain_id "$_domain_id" | |
72 | _debug _sub_domain "$_sub_domain" | |
73 | _debug _domain "$_domain" | |
74 | ||
75 | _debug "Getting txt records" | |
76 | VEESP_rest GET "service/$_service_id/dns/$_domain_id" | |
77 | ||
78 | count=$(printf "%s\n" "$response" | _egrep_o "\"type\":\"TXT\",\"content\":\".\"$txtvalue.\"\"" | wc -l | tr -d " ") | |
79 | _debug count "$count" | |
80 | if [ "$count" = "0" ]; then | |
81 | _info "Don't need to remove." | |
82 | else | |
83 | record_id=$(printf "%s\n" "$response" | _egrep_o "{\"id\":[^}]*\"type\":\"TXT\",\"content\":\".\"$txtvalue.\"\"" | cut -d\" -f4) | |
84 | _debug "record_id" "$record_id" | |
85 | if [ -z "$record_id" ]; then | |
86 | _err "Can not get record id to remove." | |
87 | return 1 | |
88 | fi | |
89 | if ! VEESP_rest DELETE "service/$_service_id/dns/$_domain_id/records/$record_id"; then | |
90 | _err "Delete record error." | |
91 | return 1 | |
92 | fi | |
93 | _contains "$response" "\"success\":true" | |
94 | fi | |
95 | } | |
96 | ||
97 | #################### Private functions below ################################## | |
98 | #_acme-challenge.www.domain.com | |
99 | #returns | |
100 | # _sub_domain=_acme-challenge.www | |
101 | # _domain=domain.com | |
102 | # _domain_id=sdjkglgdfewsdfg | |
103 | _get_root() { | |
104 | domain=$1 | |
105 | i=2 | |
106 | p=1 | |
107 | if ! VEESP_rest GET "dns"; then | |
108 | return 1 | |
109 | fi | |
110 | while true; do | |
111 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
112 | _debug h "$h" | |
113 | if [ -z "$h" ]; then | |
114 | #not valid | |
115 | return 1 | |
116 | fi | |
117 | ||
118 | if _contains "$response" "\"name\":\"$h\""; then | |
119 | _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"domain_id\":[^,]*,\"name\":\"$h\"" | cut -d : -f 2 | cut -d , -f 1 | cut -d '"' -f 2) | |
120 | _debug _domain_id "$_domain_id" | |
121 | _service_id=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$h\",\"service_id\":[^}]*" | cut -d : -f 3 | cut -d '"' -f 2) | |
122 | _debug _service_id "$_service_id" | |
123 | if [ "$_domain_id" ]; then | |
124 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
125 | _domain="$h" | |
126 | return 0 | |
127 | fi | |
128 | return 1 | |
129 | fi | |
130 | p=$i | |
131 | i=$(_math "$i" + 1) | |
132 | done | |
133 | return 1 | |
134 | } | |
135 | ||
136 | VEESP_rest() { | |
137 | m=$1 | |
138 | ep="$2" | |
139 | data="$3" | |
140 | _debug "$ep" | |
141 | ||
142 | export _H1="Accept: application/json" | |
143 | export _H2="Authorization: Basic $VEESP_auth" | |
144 | if [ "$m" != "GET" ]; then | |
145 | _debug data "$data" | |
146 | export _H3="Content-Type: application/json" | |
147 | response="$(_post "$data" "$VEESP_Api/$ep" "" "$m")" | |
148 | else | |
149 | response="$(_get "$VEESP_Api/$ep")" | |
150 | fi | |
151 | ||
152 | if [ "$?" != "0" ]; then | |
153 | _err "error $ep" | |
154 | return 1 | |
155 | fi | |
156 | _debug2 response "$response" | |
157 | return 0 | |
158 | } |