ddns-scripts: rename variable: s/retry_count/retry_max_count/
[feed/packages.git] / net / ddns-scripts / files / usr / lib / ddns / update_one_com.sh
1 #!/bin/sh
2
3 # ONE.COM DDNS SCRIPT
4 # REQUIRES CURL
5 # $ opkg install curl
6
7 # SCRIPT BY LUGICO
8 # CONTACT: main@lugico.de
9
10 [ -z "$CURL" ] && [ -z "$CURL_SSL" ] && write_log 14 "one.com communication require cURL with SSL support. Please install"
11 [ -z "$domain" ] && write_log 14 "Service section not configured correctly! Missing 'domain'"
12 [ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing 'username'"
13 [ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing 'password'"
14
15 . /usr/share/libubox/jshn.sh
16
17 write_log 0 "one.com ddns script started"
18
19 local __SUBDOMAIN __MAINDOMAIN __LOGINURL __RECORDID
20 local __TTL=3600
21
22 COOKIEJAR=$(mktemp /tmp/one_com_cookiejar.XXXXXX) || exit 1
23
24 __SUBDOMAIN=$(echo $domain | sed -e 's/[^\.]*\.[^\.]*$//' -e 's/\.$//' )
25 __MAINDOMAIN=$(echo $domain | sed -e "s/${__SUBDOMAIN}\.//" )
26
27
28 # LOGGING IN
29 # GET LOGIN POST URL FROM FORM
30 __LOGINURL=$( $CURL \
31 -RsSL \
32 --stderr $ERRFILE \
33 -c $COOKIEJAR \
34 "https://www.one.com/admin/" \
35 | grep 'Login-form login autofill' \
36 | sed -e 's/.*action="//' -e 's/".*//' -e 's/\&/\&/g' \
37 )
38
39 # POST LOGIN DATA
40 $CURL \
41 -RsSL \
42 --stderr $ERRFILE \
43 -c $COOKIEJAR \
44 -b $COOKIEJAR \
45 "${__LOGINURL}" \
46 -H "Content-Type: application/x-www-form-urlencoded" \
47 -X POST \
48 -d "username=${username}&password=${password}&credentialId=" \
49 | grep "Invalid username or password." > $DATFILE
50
51 if [ "$?" == "0" ] ; then
52 write_log 14 "Invalid credentials"
53 return 1
54 fi
55
56
57 # SETTING DOMAIN
58 $CURL -RsSL \
59 --stderr $ERRFILE \
60 -c $COOKIEJAR \
61 -b $COOKIEJAR \
62 "https://www.one.com/admin/select-admin-domain.do?domain=${__MAINDOMAIN}" \
63 | grep "<meta name=\"one.com:active-domain\" content=\"${__MAINDOMAIN}\"/>" > $DATFILE
64
65 if [ "$?" != "0" ] ; then
66 write_log 14 "Failed to select domain '${__MAINDOMAIN}'"
67 return 1
68 fi
69
70
71 # GETTING RECORD ID
72 records=$( $CURL \
73 -RsSL \
74 --stderr $ERRFILE \
75 -c $COOKIEJAR \
76 -b $COOKIEJAR \
77 "https://www.one.com/admin/api/domains/${__MAINDOMAIN}/dns/custom_records"
78 )
79
80 json_load "$records"
81
82 if json_is_a "result" "object" && \
83 json_select "result" && \
84 json_is_a "data" "array"
85 then
86 json_select "data"
87 i=1
88 while json_is_a ${i} "object" ; do
89 json_select "${i}"
90 json_select "attributes"
91 json_get_var "prefix" "prefix"
92 json_close_object
93 if [ "$prefix" == "$__SUBDOMAIN" ] ; then
94 json_get_var "__RECORDID" "id"
95 write_log 0 "Found record id : ${__RECORDID}"
96 break
97 fi
98 json_close_object
99 i=$(($i + 1))
100 done
101 fi
102
103
104 if [ "${__RECORDID}" == "" ] ; then
105 write_log 14 "domain record not found"
106 return 1
107 fi
108
109
110 # CREATING PATCH DATA
111 json_init
112 json_add_string "type" "dns_service_records"
113 json_add_string "id" "${__RECORDID}"
114 json_add_object "attributes"
115 json_add_string "type" "A"
116 json_add_string "prefix" "${__SUBDOMAIN}"
117 json_add_string "content" "${__IP}"
118 json_add_int "ttl" ${__TTL}
119 patchdata=$(json_dump)
120
121
122 # SENDING PATCH
123 $CURL \
124 -RsSL \
125 --stderr $ERRFILE \
126 -c $COOKIEJAR \
127 -b $COOKIEJAR \
128 -X PATCH \
129 -d "$patchdata" \
130 -H "Content-Type: application/json" \
131 "https://www.one.com/admin/api/domains/${__MAINDOMAIN}/dns/custom_records/${__RECORDID}" \
132 | grep "priority" > $DATFILE
133
134 if [ "$?" != "0" ] ; then
135 write_log 14 "one.com gave an unexpected response"
136 return 1
137 fi
138
139 rm $COOKIEJAR
140 write_log 0 "one.com ddns script finished without errors"
141
142 return 0