]>
Commit | Line | Data |
---|---|---|
e05ef230 A |
1 | #!/usr/bin/env sh |
2 | ||
3 | # | |
6151debe | 4 | # REGRU_API_Username="test" |
e05ef230 | 5 | # |
6151debe A |
6 | # REGRU_API_Password="test" |
7 | # | |
e05ef230 | 8 | |
6151debe | 9 | REGRU_API_URL="https://api.reg.ru/api/regru2" |
e05ef230 A |
10 | |
11 | ######## Public functions ##################### | |
12 | ||
6151debe | 13 | dns_regru_add() { |
e05ef230 A |
14 | fulldomain=$1 |
15 | txtvalue=$2 | |
16 | ||
6151debe A |
17 | REGRU_API_Username="${REGRU_API_Username:-$(_readaccountconf_mutable REGRU_API_Username)}" |
18 | REGRU_API_Password="${REGRU_API_Password:-$(_readaccountconf_mutable REGRU_API_Password)}" | |
19 | if [ -z "$REGRU_API_Username" ] || [ -z "$REGRU_API_Password" ]; then | |
20 | REGRU_API_Username="" | |
21 | REGRU_API_Password="" | |
22 | _err "You don't specify regru password or username." | |
e05ef230 A |
23 | return 1 |
24 | fi | |
25 | ||
6151debe A |
26 | _saveaccountconf_mutable REGRU_API_Username "$REGRU_API_Username" |
27 | _saveaccountconf_mutable REGRU_API_Password "$REGRU_API_Password" | |
e05ef230 | 28 | |
5d0dde5c | 29 | _debug "First detect the root zone" |
30 | if ! _get_root "$fulldomain"; then | |
31 | _err "invalid domain" | |
32 | return 1 | |
33 | fi | |
34 | _debug _domain "$_domain" | |
35 | ||
956114fc A |
36 | _subdomain=$(echo "$fulldomain" | sed -r "s/.$_domain//") |
37 | _debug _subdomain "$_subdomain" | |
38 | ||
6151debe | 39 | _info "Adding TXT record to ${fulldomain}" |
956114fc | 40 | _regru_rest POST "zone/add_txt" "input_data={%22username%22:%22${REGRU_API_Username}%22,%22password%22:%22${REGRU_API_Password}%22,%22domains%22:[{%22dname%22:%22${_domain}%22}],%22subdomain%22:%22${_subdomain}%22,%22text%22:%22${txtvalue}%22,%22output_content_type%22:%22plain%22}&input_format=json" |
e05ef230 | 41 | |
5d0dde5c | 42 | if ! _contains "${response}" 'error'; then |
6151debe | 43 | return 0 |
e05ef230 | 44 | fi |
6151debe A |
45 | _err "Could not create resource record, check logs" |
46 | _err "${response}" | |
e05ef230 | 47 | return 1 |
e05ef230 A |
48 | } |
49 | ||
6151debe | 50 | dns_regru_rm() { |
e05ef230 A |
51 | fulldomain=$1 |
52 | txtvalue=$2 | |
6151debe A |
53 | |
54 | REGRU_API_Username="${REGRU_API_Username:-$(_readaccountconf_mutable REGRU_API_Username)}" | |
55 | REGRU_API_Password="${REGRU_API_Password:-$(_readaccountconf_mutable REGRU_API_Password)}" | |
56 | if [ -z "$REGRU_API_Username" ] || [ -z "$REGRU_API_Password" ]; then | |
57 | REGRU_API_Username="" | |
58 | REGRU_API_Password="" | |
59 | _err "You don't specify regru password or username." | |
e05ef230 A |
60 | return 1 |
61 | fi | |
e05ef230 | 62 | |
5d0dde5c | 63 | _debug "First detect the root zone" |
64 | if ! _get_root "$fulldomain"; then | |
65 | _err "invalid domain" | |
66 | return 1 | |
67 | fi | |
68 | _debug _domain "$_domain" | |
69 | ||
956114fc A |
70 | _subdomain=$(echo "$fulldomain" | sed -r "s/.$_domain//") |
71 | _debug _subdomain "$_subdomain" | |
72 | ||
6151debe | 73 | _info "Deleting resource record $fulldomain" |
956114fc | 74 | _regru_rest POST "zone/remove_record" "input_data={%22username%22:%22${REGRU_API_Username}%22,%22password%22:%22${REGRU_API_Password}%22,%22domains%22:[{%22dname%22:%22${_domain}%22}],%22subdomain%22:%22${_subdomain}%22,%22content%22:%22${txtvalue}%22,%22record_type%22:%22TXT%22,%22output_content_type%22:%22plain%22}&input_format=json" |
e05ef230 | 75 | |
5d0dde5c | 76 | if ! _contains "${response}" 'error'; then |
6151debe | 77 | return 0 |
e05ef230 | 78 | fi |
6151debe A |
79 | _err "Could not delete resource record, check logs" |
80 | _err "${response}" | |
e05ef230 A |
81 | return 1 |
82 | } | |
5d0dde5c | 83 | |
84 | #################### Private functions below ################################## | |
85 | #_acme-challenge.www.domain.com | |
86 | #returns | |
87 | # _domain=domain.com | |
88 | _get_root() { | |
89 | domain=$1 | |
90 | ||
91 | _regru_rest POST "service/get_list" "username=${REGRU_API_Username}&password=${REGRU_API_Password}&output_format=xml&servtype=domain" | |
92 | domains_list=$(echo "${response}" | grep dname | sed -r "s/.*dname=\"([^\"]+)\".*/\\1/g") | |
93 | ||
f00e2890 | 94 | for ITEM in ${domains_list}; do |
bd738238 | 95 | IDN_ITEM=${ITEM} |
5d0dde5c | 96 | case "${domain}" in |
e1a0f570 | 97 | *${IDN_ITEM}*) |
bd738238 | 98 | _domain="$(_idn "${ITEM}")" |
19c43451 | 99 | _debug _domain "${_domain}" |
100 | return 0 | |
101 | ;; | |
5d0dde5c | 102 | esac |
103 | done | |
104 | ||
105 | return 1 | |
106 | } | |
107 | ||
108 | #returns | |
109 | # response | |
110 | _regru_rest() { | |
111 | m=$1 | |
112 | ep="$2" | |
113 | data="$3" | |
114 | _debug "$ep" | |
115 | ||
116 | export _H1="Content-Type: application/x-www-form-urlencoded" | |
117 | ||
118 | if [ "$m" != "GET" ]; then | |
119 | _debug data "$data" | |
120 | response="$(_post "$data" "$REGRU_API_URL/$ep" "" "$m")" | |
121 | else | |
c16757b0 | 122 | response="$(_get "$REGRU_API_URL/$ep?$data")" |
5d0dde5c | 123 | fi |
124 | ||
c16757b0 | 125 | _debug response "${response}" |
5d0dde5c | 126 | return 0 |
44b9a8e7 | 127 | } |