freedombone-utils-dns 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # DNS functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. # DNS
  29. NAMESERVER1='91.239.100.100'
  30. NAMESERVER2='89.233.43.71'
  31. NAMESERVER3='87.118.100.175'
  32. NAMESERVER4='94.75.228.29'
  33. NAMESERVER5='85.25.251.254'
  34. NAMESERVER6='2.141.58.13'
  35. # parameters used when adding a new domain
  36. DDNS_PROVIDER="default@freedns.afraid.org"
  37. DDNS_USERNAME=
  38. DDNS_PASSWORD=
  39. INADYN_REPO="https://github.com/bashrc/inadyn"
  40. INADYN_COMMIT='fadbe17f520d337dfb8d69ee4bf1fcaa23fce0d6'
  41. INADYN_CONFIG_FILE=/etc/inadyn.conf
  42. # web site used to obtain the external IP address of the system
  43. GET_IP_ADDRESS_URL="checkip.two-dns.de"
  44. # other possible services to obtain the external IP address
  45. EXTERNAL_IP_SERVICES=( \
  46. 'https://check.torproject.org/' \
  47. 'https://www.whatsmydns.net/whats-my-ip-address.html' \
  48. 'https://www.privateinternetaccess.com/pages/whats-my-ip/' \
  49. 'http://checkip.two-dns.de' \
  50. 'http://ip.dnsexit.com' \
  51. 'http://ifconfig.me/ip' \
  52. 'http://ipecho.net/plain' \
  53. 'http://checkip.dyndns.org/plain' \
  54. 'http://ipogre.com/linux.php' \
  55. 'http://whatismyipaddress.com/' \
  56. 'http://ip.my-proxy.com/' \
  57. 'http://websiteipaddress.com/WhatIsMyIp' \
  58. 'http://getmyipaddress.org/' \
  59. 'http://www.my-ip-address.net/' \
  60. 'http://myexternalip.com/raw' \
  61. 'http://www.canyouseeme.org/' \
  62. 'http://www.trackip.net/' \
  63. 'http://icanhazip.com/' \
  64. 'http://www.iplocation.net/' \
  65. 'http://www.howtofindmyipaddress.com/' \
  66. 'http://www.ipchicken.com/' \
  67. 'http://whatsmyip.net/' \
  68. 'http://www.ip-adress.com/' \
  69. 'http://checkmyip.com/' \
  70. 'http://www.tracemyip.org/' \
  71. 'http://checkmyip.net/' \
  72. 'http://www.lawrencegoetz.com/programs/ipinfo/' \
  73. 'http://www.findmyip.co/' \
  74. 'http://ip-lookup.net/' \
  75. 'http://www.dslreports.com/whois' \
  76. 'http://www.mon-ip.com/en/my-ip/' \
  77. 'http://www.myip.ru' \
  78. 'http://ipgoat.com/' \
  79. 'http://www.myipnumber.com/my-ip-address.asp' \
  80. 'http://www.whatsmyipaddress.net/' \
  81. 'http://formyip.com/' \
  82. 'http://www.displaymyip.com/' \
  83. 'http://www.bobborst.com/tools/whatsmyip/' \
  84. 'http://www.geoiptool.com/' \
  85. 'http://checkip.dyndns.com/' \
  86. 'http://myexternalip.com/' \
  87. 'http://www.ip-adress.eu/' \
  88. 'http://www.infosniper.net/' \
  89. 'http://wtfismyip.com/' \
  90. 'http://ipinfo.io/' \
  91. 'http://httpbin.org/ip')
  92. function update_inadyn_config {
  93. if [ ! -f "${INADYN_CONFIG_FILE}" ]; then
  94. return
  95. fi
  96. if [[ "$DDNS_PROVIDER" == *'freedns'* ]]; then
  97. return
  98. fi
  99. if ! grep -q "$DDNS_PROVIDER" "${INADYN_CONFIG_FILE}"; then
  100. # store any previous aliases
  101. grep 'alias ' "${INADYN_CONFIG_FILE}" > /tmp/inadyn_aliases
  102. # remove entry for any previous ddns
  103. sed -i '/system /,$d' "${INADYN_CONFIG_FILE}"
  104. # add the new provider
  105. { echo '';
  106. echo "system $DDNS_PROVIDER";
  107. echo ' ssl';
  108. echo " checkip-url $GET_IP_ADDRESS_URL /"; } >> "${INADYN_CONFIG_FILE}"
  109. if [ "$DDNS_USERNAME" ]; then
  110. echo " username $DDNS_USERNAME" >> "${INADYN_CONFIG_FILE}"
  111. fi
  112. if [ "$DDNS_PASSWORD" ]; then
  113. echo " password $DDNS_PASSWORD" >> "${INADYN_CONFIG_FILE}"
  114. fi
  115. if [ -f /tmp/inadyn_aliases ]; then
  116. cat /tmp/inadyn_aliases >> "${INADYN_CONFIG_FILE}"
  117. rm /tmp/inadyn_aliases
  118. fi
  119. else
  120. # change username/password for an existing provider
  121. if [ "$DDNS_USERNAME" ]; then
  122. if grep -q " username " "${INADYN_CONFIG_FILE}"; then
  123. sed -i "s| username .*| username $DDNS_USERNAME|g" "${INADYN_CONFIG_FILE}"
  124. else
  125. echo " username $DDNS_USERNAME" >> "${INADYN_CONFIG_FILE}"
  126. fi
  127. fi
  128. if [ "$DDNS_PASSWORD" ]; then
  129. if grep -q " password " "${INADYN_CONFIG_FILE}"; then
  130. sed -i "s| password .*| password $DDNS_PASSWORD|g" "${INADYN_CONFIG_FILE}"
  131. else
  132. echo " password $DDNS_PASSWORD" >> "${INADYN_CONFIG_FILE}"
  133. fi
  134. fi
  135. fi
  136. systemctl enable inadyn
  137. systemctl restart inadyn
  138. }
  139. function create_freedns_updater {
  140. if [[ $ONION_ONLY != "no" ]]; then
  141. return
  142. fi
  143. # currently inadyn doesn't work as expected with freeDNS, so this is a workaround
  144. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  145. return
  146. fi
  147. if [[ $DDNS_PROVIDER != *"freedns"* ]]; then
  148. return
  149. fi
  150. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  151. return
  152. fi
  153. # remove any legacy command
  154. if [ -f /usr/bin/dynamicdns ]; then
  155. rm /usr/bin/dynamicdns
  156. fi
  157. if grep -q "dynamicdns" /etc/crontab; then
  158. sed -i '/dynamicdns/d' /etc/crontab
  159. fi
  160. # add the update command to cron
  161. if ! grep -q "/usr/local/bin/${PROJECT_NAME}-freedns" /etc/crontab; then
  162. function_check cron_add_mins
  163. cron_add_mins 3 "/usr/local/bin/${PROJECT_NAME}-freedns"
  164. systemctl restart cron
  165. fi
  166. mark_completed "${FUNCNAME[0]}"
  167. }
  168. function add_ddns_domain {
  169. if [ ! "$1" ]; then
  170. echo $'ddns domain not specified'
  171. exit 5638
  172. fi
  173. CURRENT_DDNS_DOMAIN="$1"
  174. if [[ $ONION_ONLY != "no" ]]; then
  175. return
  176. fi
  177. if [ ! "$DDNS_PROVIDER" ]; then
  178. return
  179. fi
  180. if [[ "$DDNS_PROVIDER" == 'none' ]]; then
  181. return
  182. fi
  183. if [ ! -f "${INADYN_CONFIG_FILE}" ]; then
  184. echo $'Unable to find inadyn configuration file "${INADYN_CONFIG_FILE}"'
  185. exit 5745
  186. fi
  187. if ! grep -q "$DDNS_PROVIDER" "${INADYN_CONFIG_FILE}"; then
  188. { echo '';
  189. echo "system $DDNS_PROVIDER";
  190. echo ' ssl';
  191. echo " checkip-url $GET_IP_ADDRESS_URL /"; } >> "${INADYN_CONFIG_FILE}"
  192. if [ $DDNS_USERNAME ]; then
  193. echo " username $DDNS_USERNAME" >> "${INADYN_CONFIG_FILE}"
  194. fi
  195. if [ $DDNS_PASSWORD ]; then
  196. echo " password $DDNS_PASSWORD" >> "${INADYN_CONFIG_FILE}"
  197. fi
  198. fi
  199. if ! grep -q "$CURRENT_DDNS_DOMAIN" "${INADYN_CONFIG_FILE}"; then
  200. echo " alias $CURRENT_DDNS_DOMAIN" >> "${INADYN_CONFIG_FILE}"
  201. fi
  202. chmod 600 "${INADYN_CONFIG_FILE}"
  203. systemctl restart inadyn
  204. systemctl daemon-reload
  205. }
  206. function remove_ddns_domain {
  207. if [ ! "$1" ]; then
  208. echo $'ddns domain not specified'
  209. exit 5638
  210. fi
  211. CURRENT_DDNS_DOMAIN="$1"
  212. if [[ $ONION_ONLY != "no" ]]; then
  213. return
  214. fi
  215. if [[ "$DDNS_PROVIDER" == 'none' ]]; then
  216. return
  217. fi
  218. if [ ! -f "${INADYN_CONFIG_FILE}" ]; then
  219. echo $'Unable to find inadyn configuration file "${INADYN_CONFIG_FILE}"'
  220. exit 5745
  221. fi
  222. if grep -q "$CURRENT_DDNS_DOMAIN" "${INADYN_CONFIG_FILE}"; then
  223. systemctl stop inadyn
  224. sed -i "/alias $CURRENT_DDNS_DOMAIN/d" "${INADYN_CONFIG_FILE}"
  225. systemctl start inadyn
  226. systemctl daemon-reload
  227. fi
  228. }
  229. function configure_dns {
  230. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  231. return
  232. fi
  233. apt-get -yq install resolvconf
  234. resolvconf=/etc/resolvconf/resolv.conf.d/head
  235. # allow changes to resolv.conf
  236. chattr -i $resolvconf
  237. { echo 'domain localdomain';
  238. echo 'search localdomain';
  239. echo "nameserver $NAMESERVER1";
  240. echo "nameserver $NAMESERVER2";
  241. echo "nameserver $NAMESERVER3";
  242. echo "nameserver $NAMESERVER4";
  243. echo "nameserver $NAMESERVER5";
  244. echo "nameserver $NAMESERVER6"; } > $resolvconf
  245. # prevent resolv.conf from changing
  246. resolvconf -u
  247. mark_completed "${FUNCNAME[0]}"
  248. }
  249. function set_hostname {
  250. DEFAULT_DOMAIN_NAME="$1"
  251. echo "$DEFAULT_DOMAIN_NAME" > /etc/hostname
  252. hostname "$DEFAULT_DOMAIN_NAME"
  253. echo "$DEFAULT_DOMAIN_NAME" > /etc/mailname
  254. if grep -q "127.0.1.1" /etc/hosts; then
  255. sed -i "s/127.0.1.1.*/127.0.1.1 $DEFAULT_DOMAIN_NAME/g" /etc/hosts
  256. else
  257. echo "127.0.1.1 $DEFAULT_DOMAIN_NAME" >> /etc/hosts
  258. fi
  259. }
  260. function set_your_domain_name {
  261. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  262. return
  263. fi
  264. function_check set_hostname
  265. set_hostname "$DEFAULT_DOMAIN_NAME"
  266. mark_completed "${FUNCNAME[0]}"
  267. }
  268. function configure_firewall_for_dns {
  269. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  270. return
  271. fi
  272. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  273. # docker does its own firewalling
  274. return
  275. fi
  276. iptables -A INPUT -p udp -m udp --dport 1024:65535 --sport 53 -j ACCEPT
  277. function_check save_firewall_settings
  278. save_firewall_settings
  279. mark_completed "${FUNCNAME[0]}"
  280. }
  281. # NOTE: deliberately no exit 0