freedombone-utils-dns 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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="freedns"
  37. DDNS_USERNAME=
  38. DDNS_PASSWORD=
  39. INADYN_REPO="https://github.com/troglobit/inadyn"
  40. INADYN_COMMIT='3fe26d2235ddda9047cf33e8ed1453bcc3bbbf19'
  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. if grep -q "{" "${INADYN_CONFIG_FILE}"; then
  101. sed -i "s|provider .*|provider $DDNS_PROVIDER {|g" "${INADYN_CONFIG_FILE}"
  102. sed -i "s|username .*|username = $DDNS_USERNAME|g" "${INADYN_CONFIG_FILE}"
  103. sed -i "s|password .*|password = $DDNS_PASSWORD|g" "${INADYN_CONFIG_FILE}"
  104. sed -i "s|checkip-server .*|checkip-server = $GET_IP_ADDRESS_URL|g" "${INADYN_CONFIG_FILE}"
  105. else
  106. # Old inadyn config file format
  107. # store any previous aliases
  108. grep 'alias ' "${INADYN_CONFIG_FILE}" > /tmp/inadyn_aliases
  109. # remove entry for any previous ddns
  110. sed -i '/system /,$d' "${INADYN_CONFIG_FILE}"
  111. # add the new provider
  112. { echo '';
  113. echo "system $DDNS_PROVIDER";
  114. echo ' ssl';
  115. echo " checkip-url $GET_IP_ADDRESS_URL /"; } >> "${INADYN_CONFIG_FILE}"
  116. if [ "$DDNS_USERNAME" ]; then
  117. echo " username $DDNS_USERNAME" >> "${INADYN_CONFIG_FILE}"
  118. fi
  119. if [ "$DDNS_PASSWORD" ]; then
  120. echo " password $DDNS_PASSWORD" >> "${INADYN_CONFIG_FILE}"
  121. fi
  122. if [ -f /tmp/inadyn_aliases ]; then
  123. cat /tmp/inadyn_aliases >> "${INADYN_CONFIG_FILE}"
  124. rm /tmp/inadyn_aliases
  125. fi
  126. fi
  127. else
  128. if grep -q "{" "${INADYN_CONFIG_FILE}"; then
  129. sed -i "s|username .*|username = $DDNS_USERNAME|g" "${INADYN_CONFIG_FILE}"
  130. sed -i "s|password .*|password = $DDNS_PASSWORD|g" "${INADYN_CONFIG_FILE}"
  131. sed -i "s|checkip-server .*|checkip-server = $GET_IP_ADDRESS_URL|g" "${INADYN_CONFIG_FILE}"
  132. else
  133. # Old inadyn config file format
  134. # change username/password for an existing provider
  135. if [ "$DDNS_USERNAME" ]; then
  136. if grep -q " username " "${INADYN_CONFIG_FILE}"; then
  137. sed -i "s| username .*| username $DDNS_USERNAME|g" "${INADYN_CONFIG_FILE}"
  138. else
  139. echo " username $DDNS_USERNAME" >> "${INADYN_CONFIG_FILE}"
  140. fi
  141. fi
  142. if [ "$DDNS_PASSWORD" ]; then
  143. if grep -q " password " "${INADYN_CONFIG_FILE}"; then
  144. sed -i "s| password .*| password $DDNS_PASSWORD|g" "${INADYN_CONFIG_FILE}"
  145. else
  146. echo " password $DDNS_PASSWORD" >> "${INADYN_CONFIG_FILE}"
  147. fi
  148. fi
  149. fi
  150. fi
  151. systemctl enable inadyn
  152. systemctl restart inadyn
  153. }
  154. function create_freedns_updater {
  155. if [[ $ONION_ONLY != "no" ]]; then
  156. return
  157. fi
  158. # currently inadyn doesn't work as expected with freeDNS, so this is a workaround
  159. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  160. return
  161. fi
  162. if [[ $DDNS_PROVIDER != *"freedns"* ]]; then
  163. return
  164. fi
  165. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  166. return
  167. fi
  168. # remove any legacy command
  169. if [ -f /usr/bin/dynamicdns ]; then
  170. rm /usr/bin/dynamicdns
  171. fi
  172. if grep -q "dynamicdns" /etc/crontab; then
  173. sed -i '/dynamicdns/d' /etc/crontab
  174. fi
  175. # add the update command to cron
  176. if ! grep -q "/usr/local/bin/${PROJECT_NAME}-freedns" /etc/crontab; then
  177. function_check cron_add_mins
  178. cron_add_mins 3 "/usr/local/bin/${PROJECT_NAME}-freedns"
  179. systemctl restart cron
  180. fi
  181. mark_completed "${FUNCNAME[0]}"
  182. }
  183. function add_ddns_domain {
  184. if [ ! "$1" ]; then
  185. echo $'ddns domain not specified'
  186. exit 5638
  187. fi
  188. CURRENT_DDNS_DOMAIN="$1"
  189. if [[ $ONION_ONLY != "no" ]]; then
  190. return
  191. fi
  192. if [ ! "$DDNS_PROVIDER" ]; then
  193. return
  194. fi
  195. if [[ "$DDNS_PROVIDER" == 'none' ]]; then
  196. return
  197. fi
  198. if [ ! -f "${INADYN_CONFIG_FILE}" ]; then
  199. echo $'Unable to find inadyn configuration file "${INADYN_CONFIG_FILE}"'
  200. exit 5745
  201. fi
  202. if ! grep -q "$DDNS_PROVIDER" "${INADYN_CONFIG_FILE}"; then
  203. read_config_param DEFAULT_DOMAIN_NAME
  204. { echo 'period = 300';
  205. echo '';
  206. echo "provider $DDNS_PROVIDER {";
  207. echo " ssl = true";
  208. echo " username = $DDNS_USERNAME";
  209. echo " password = $DDNS_PASSWORD";
  210. echo " hostname = $DEFAULT_DOMAIN_NAME";
  211. echo '}'; } > "${INADYN_CONFIG_FILE}"
  212. fi
  213. sed -i "s|hostname .*|hostname = $DEFAULT_DOMAIN_NAME"
  214. chmod 600 "${INADYN_CONFIG_FILE}"
  215. systemctl daemon-reload
  216. systemctl restart inadyn
  217. }
  218. function remove_ddns_domain {
  219. if [ ! "$1" ]; then
  220. echo $'ddns domain not specified'
  221. exit 5638
  222. fi
  223. CURRENT_DDNS_DOMAIN="$1"
  224. if [[ $ONION_ONLY != "no" ]]; then
  225. return
  226. fi
  227. if [[ "$DDNS_PROVIDER" == 'none' ]]; then
  228. return
  229. fi
  230. if [ ! -f "${INADYN_CONFIG_FILE}" ]; then
  231. echo $'Unable to find inadyn configuration file "${INADYN_CONFIG_FILE}"'
  232. exit 5745
  233. fi
  234. #if grep -q "$CURRENT_DDNS_DOMAIN" "${INADYN_CONFIG_FILE}"; then
  235. # systemctl stop inadyn
  236. # sed -i "s|, ${CURRENT_DDNS_DOMAIN}||g" "${INADYN_CONFIG_FILE}"
  237. # if grep -q "$CURRENT_DDNS_DOMAIN" "${INADYN_CONFIG_FILE}"; then
  238. # sed -i "s|${CURRENT_DDNS_DOMAIN}||g" "${INADYN_CONFIG_FILE}"
  239. # fi
  240. # systemctl daemon-reload
  241. # systemctl start inadyn
  242. #fi
  243. }
  244. function configure_dns {
  245. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  246. return
  247. fi
  248. apt-get -yq install resolvconf
  249. resolvconf=/etc/resolvconf/resolv.conf.d/head
  250. # allow changes to resolv.conf
  251. chattr -i $resolvconf
  252. { echo 'domain localdomain';
  253. echo 'search localdomain';
  254. echo "nameserver $NAMESERVER1";
  255. echo "nameserver $NAMESERVER2";
  256. echo "nameserver $NAMESERVER3";
  257. echo "nameserver $NAMESERVER4";
  258. echo "nameserver $NAMESERVER5";
  259. echo "nameserver $NAMESERVER6"; } > $resolvconf
  260. # prevent resolv.conf from changing
  261. resolvconf -u
  262. mark_completed "${FUNCNAME[0]}"
  263. }
  264. function set_hostname {
  265. DEFAULT_DOMAIN_NAME="$1"
  266. echo "$DEFAULT_DOMAIN_NAME" > /etc/hostname
  267. hostname "$DEFAULT_DOMAIN_NAME"
  268. echo "$DEFAULT_DOMAIN_NAME" > /etc/mailname
  269. if grep -q "127.0.1.1" /etc/hosts; then
  270. sed -i "s/127.0.1.1.*/127.0.1.1 $DEFAULT_DOMAIN_NAME/g" /etc/hosts
  271. else
  272. echo "127.0.1.1 $DEFAULT_DOMAIN_NAME" >> /etc/hosts
  273. fi
  274. }
  275. function set_your_domain_name {
  276. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  277. return
  278. fi
  279. function_check set_hostname
  280. set_hostname "$DEFAULT_DOMAIN_NAME"
  281. mark_completed "${FUNCNAME[0]}"
  282. }
  283. function configure_firewall_for_dns {
  284. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  285. return
  286. fi
  287. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  288. # docker does its own firewalling
  289. return
  290. fi
  291. iptables -A INPUT -p udp -m udp --dport 1024:65535 --sport 53 -j ACCEPT
  292. function_check save_firewall_settings
  293. save_firewall_settings
  294. mark_completed "${FUNCNAME[0]}"
  295. }
  296. # NOTE: deliberately no exit 0