freedombone-utils-onion 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Onion functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. function wait_for_onion_service {
  31. onion_service_name="$1"
  32. sleep_ctr=0
  33. while [ ! -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; do
  34. sleep 1
  35. sleep_ctr=$((sleep_ctr + 1))
  36. if [ $sleep_ctr -gt 10 ]; then
  37. break
  38. fi
  39. done
  40. if [ ! -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; then
  41. # restart and try a second time
  42. systemctl restart tor
  43. sleep_ctr=0
  44. while [ ! -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; do
  45. sleep 1
  46. sleep_ctr=$((sleep_ctr + 1))
  47. if [ $sleep_ctr -gt 10 ]; then
  48. break
  49. fi
  50. done
  51. fi
  52. }
  53. function remove_onion_service {
  54. onion_service_name="$1"
  55. onion_service_port_to=$2
  56. sed -i "/hidden_service_${onion_service_name}/d" /etc/tor/torrc
  57. sed -i "/127.0.0.1:${onion_service_port_to}/d" /etc/tor/torrc
  58. if [ $3 ]; then
  59. sed -i "/127.0.0.1:${3}/d" /etc/tor/torrc
  60. if [ $4 ]; then
  61. sed -i "/127.0.0.1:${4}/d" /etc/tor/torrc
  62. if [ $5 ]; then
  63. sed -i "/127.0.0.1:${5}/d" /etc/tor/torrc
  64. fi
  65. fi
  66. fi
  67. systemctl restart tor
  68. }
  69. function add_onion_service {
  70. onion_service_name="$1"
  71. onion_service_port_from=$2
  72. onion_service_port_to=$3
  73. if [ -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; then
  74. echo $(cat /var/lib/tor/hidden_service_${onion_service_name}/hostname)
  75. return
  76. fi
  77. if [ ! -d /var/lib/tor ]; then
  78. echo $"No Tor installation found. ${onion_service_name} onion site cannot be configured."
  79. exit 877367
  80. fi
  81. if ! grep -q "hidden_service_${onion_service_name}" /etc/tor/torrc; then
  82. echo "HiddenServiceDir /var/lib/tor/hidden_service_${onion_service_name}/" >> /etc/tor/torrc
  83. echo "HiddenServicePort ${onion_service_port_from} 127.0.0.1:${onion_service_port_to}" >> /etc/tor/torrc
  84. fi
  85. systemctl restart tor
  86. function_check wait_for_onion_service
  87. wait_for_onion_service ${onion_service_name}
  88. if [ ! -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; then
  89. echo $"${onion_service_name} onion site hostname not found"
  90. exit 76362
  91. fi
  92. echo $(cat /var/lib/tor/hidden_service_${onion_service_name}/hostname)
  93. }
  94. function set_default_onion_domains {
  95. # If sites are only visible via Tor then for installation
  96. # purposes assign them some default domain names
  97. if [[ $ONION_ONLY == "no" ]]; then
  98. return
  99. fi
  100. if [ ${#MICROBLOG_DOMAIN_NAME} -gt 1 ]; then
  101. MICROBLOG_DOMAIN_NAME='microblog.local'
  102. fi
  103. if [ ${#FULLBLOG_DOMAIN_NAME} -gt 1 ]; then
  104. FULLBLOG_DOMAIN_NAME='blog.local'
  105. fi
  106. if [ ${#WIKI_DOMAIN_NAME} -gt 1 ]; then
  107. WIKI_DOMAIN_NAME='wiki.local'
  108. fi
  109. if [ ${#DEFAULT_DOMAIN_NAME} -gt 1 ]; then
  110. DEFAULT_DOMAIN_NAME="${PROJECT_NAME}.local"
  111. fi
  112. if [ ${#GIT_DOMAIN_NAME} -gt 1 ]; then
  113. GIT_DOMAIN_NAME='git.local'
  114. fi
  115. if [ ${#MEDIAGOBLIN_DOMAIN_NAME} -gt 1 ]; then
  116. MEDIAGOBLIN_DOMAIN_NAME='media.local'
  117. fi
  118. }
  119. function create_avahi_onion_domains {
  120. if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  121. return
  122. fi
  123. if [ ! -d /etc/avahi/services ]; then
  124. return
  125. fi
  126. if [ $MICROBLOG_DOMAIN_NAME ]; then
  127. function_check create_avahi_service
  128. create_avahi_service microblog http tcp $MICROBLOG_ONION_PORT
  129. fi
  130. if [ $FULLBLOG_DOMAIN_NAME ]; then
  131. function_check create_avahi_service
  132. create_avahi_service blog http tcp $BLOG_ONION_PORT
  133. fi
  134. if [ $GIT_DOMAIN_NAME ]; then
  135. function_check create_avahi_service
  136. create_avahi_service git http tcp $GIT_ONION_PORT
  137. fi
  138. if [ $WIKI_DOMAIN_NAME ]; then
  139. function_check create_avahi_service
  140. create_avahi_service wiki http tcp $WIKI_ONION_PORT
  141. fi
  142. }
  143. function allow_ssh_to_onion_address {
  144. if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  145. return
  146. fi
  147. if [ ! -d /home/$MY_USERNAME/.ssh ]; then
  148. mkdir /home/$MY_USERNAME/.ssh
  149. fi
  150. if [ ! -d /etc/tor ]; then
  151. echo $'Tor not found when updating ssh'
  152. exit 528257
  153. fi
  154. if ! grep -q "onion" /home/$MY_USERNAME/.ssh/config; then
  155. echo 'Host *.onion' >> /home/$MY_USERNAME/.ssh/config
  156. echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /home/$MY_USERNAME/.ssh/config
  157. fi
  158. }
  159. function enable_ssh_via_onion {
  160. if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  161. return
  162. fi
  163. if grep -Fxq "enable_ssh_via_onion" $COMPLETION_FILE; then
  164. return
  165. fi
  166. apt-get -y install tor connect-proxy
  167. if ! grep -q 'Host *.onion' /home/$MY_USERNAME/.ssh/config; then
  168. if [ ! -d /home/$MY_USERNAME/.ssh ]; then
  169. mkdir /home/$MY_USERNAME/.ssh
  170. fi
  171. echo 'Host *.onion' >> /home/$MY_USERNAME/.ssh/config
  172. echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /home/$MY_USERNAME/.ssh/config
  173. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh
  174. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh/config
  175. fi
  176. if ! grep -q 'Host *.onion' /root/.ssh/config; then
  177. if [ ! -d /root/.ssh ]; then
  178. mkdir /root/.ssh
  179. fi
  180. echo 'Host *.onion' >> /root/.ssh/config
  181. echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /root/.ssh/config
  182. fi
  183. echo 'enable_ssh_via_onion' >> $COMPLETION_FILE
  184. }
  185. function configure_ssh_onion {
  186. if grep -Fxq "configure_ssh_onion" $COMPLETION_FILE; then
  187. return
  188. fi
  189. if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  190. return
  191. fi
  192. SSH_ONION_HOSTNAME=$(add_onion_service ssh ${SSH_PORT} ${SSH_PORT})
  193. if ! grep -q "ssh onion domain" $COMPLETION_FILE; then
  194. echo "ssh onion domain:${SSH_ONION_HOSTNAME}" >> $COMPLETION_FILE
  195. else
  196. sed -i "s|ssh onion domain.*|ssh onion domain:${SSH_ONION_HOSTNAME}|g" $COMPLETION_FILE
  197. fi
  198. echo 'configure_ssh_onion' >> $COMPLETION_FILE
  199. }
  200. function install_tor {
  201. if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  202. return
  203. fi
  204. if grep -Fxq "install_tor" $COMPLETION_FILE; then
  205. return
  206. fi
  207. apt-get -y install tor
  208. if [ ! -f /etc/tor/torrc ]; then
  209. echo 'Tor failed to install'
  210. exit 38259
  211. fi
  212. echo 'install_tor' >> $COMPLETION_FILE
  213. }
  214. function resolve_dns_via_tor {
  215. if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  216. return
  217. fi
  218. if grep -Fxq "resolve_dns_via_tor" $COMPLETION_FILE; then
  219. return
  220. fi
  221. if [ ! -f /etc/tor/torrc ]; then
  222. echo $'tor was not installed'
  223. exit 52952
  224. fi
  225. # resolve DNS via tor
  226. if ! grep 'DNSPort 53' /etc/tor/torrc; then
  227. echo 'DNSPort 53' >> /etc/tor/torrc
  228. echo 'AutomapHostsOnResolve 1' >> /etc/tor/torrc
  229. echo 'AutomapHostsSuffixes .exit,.onion' >> /etc/tor/torrc
  230. systemctl restart tor
  231. fi
  232. # don't change resolv.conf
  233. sed -i 's|, domain-name-servers||g' /etc/dhcp/dhclient.conf
  234. # point resolv.conf to tor
  235. echo 'nameserver 127.0.0.1:53' > /etc/resolv.conf
  236. # prevent resolv.conf from changing
  237. chattr +i /etc/resolv.conf
  238. echo 'resolve_dns_via_tor' >> $COMPLETION_FILE
  239. }
  240. # see https://trac.torproject.org/projects/tor/wiki/doc/TransparentProxy
  241. # Local Redirection and Anonymizing Middlebox
  242. function route_outgoing_traffic_through_tor {
  243. if grep -Fxq "route_outgoing_traffic_through_tor" $COMPLETION_FILE; then
  244. return
  245. fi
  246. if [[ $ROUTE_THROUGH_TOR != "yes" ]]; then
  247. return
  248. fi
  249. apt-get -y install tor tor-arm
  250. ### set variables
  251. # Destinations you don't want routed through Tor
  252. _non_tor="192.168.1.0/24 192.168.0.0/24"
  253. # The user that Tor runs as
  254. _tor_uid="debian-tor"
  255. # Tor's TransPort
  256. _trans_port="9040"
  257. # Your internal interface
  258. _int_if="eth0"
  259. ### Set iptables *nat
  260. iptables -t nat -A OUTPUT -o lo -j RETURN
  261. iptables -t nat -A OUTPUT -m owner --uid-owner $_tor_uid -j RETURN
  262. iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-ports 53
  263. # Allow clearnet access for hosts in $_non_tor
  264. for _clearnet in $_non_tor; do
  265. iptables -t nat -A OUTPUT -d $_clearnet -j RETURN
  266. iptables -t nat -A PREROUTING -i $_int_if -d $_clearnet -j RETURN
  267. done
  268. # Redirect all other pre-routing and output to Tor
  269. iptables -t nat -A OUTPUT -p tcp --syn -j REDIRECT --to-ports $_trans_port
  270. iptables -t nat -A PREROUTING -i $_int_if -p udp --dport 53 -j REDIRECT --to-ports 53
  271. iptables -t nat -A PREROUTING -i $_int_if -p tcp --syn -j REDIRECT --to-ports $_trans_port
  272. ### set iptables *filter
  273. iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  274. # Allow clearnet access for hosts in $_non_tor
  275. for _clearnet in $_non_tor 127.0.0.0/8; do
  276. iptables -A OUTPUT -d $_clearnet -j ACCEPT
  277. done
  278. # Allow only Tor output
  279. iptables -A OUTPUT -m owner --uid-owner $_tor_uid -j ACCEPT
  280. iptables -A OUTPUT -j REJECT
  281. function_check save_firewall_settings
  282. save_firewall_settings
  283. if ! grep -q "fs.file-max" /etc/sysctl.conf; then
  284. echo "fs.file-max=100000" >> /etc/sysctl.conf
  285. /sbin/sysctl -p
  286. fi
  287. echo 'domain localdomain' > /etc/resolv.conf
  288. echo 'search localdomain' >> /etc/resolv.conf
  289. echo 'nameserver 127.0.0.1' >> /etc/resolv.conf
  290. if ! grep -q "VirtualAddrNetworkIPv4" /etc/tor/torrc; then
  291. echo 'VirtualAddrNetworkIPv4 10.192.0.0/10' >> /etc/tor/torrc
  292. fi
  293. if ! grep -q "AutomapHostsOnResolve" /etc/tor/torrc; then
  294. echo 'AutomapHostsOnResolve 1' >> /etc/tor/torrc
  295. fi
  296. if ! grep -q "TransPort" /etc/tor/torrc; then
  297. echo 'TransPort 9040' >> /etc/tor/torrc
  298. fi
  299. if ! grep -q "TransListenAddress 127.0.0.1" /etc/tor/torrc; then
  300. echo 'TransListenAddress 127.0.0.1' >> /etc/tor/torrc
  301. fi
  302. if ! grep -q "TransListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" /etc/tor/torrc; then
  303. echo "TransListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" >> /etc/tor/torrc
  304. fi
  305. if ! grep -q "DNSPort" /etc/tor/torrc; then
  306. echo 'DNSPort 53' >> /etc/tor/torrc
  307. fi
  308. if ! grep -q "DNSListenAddress 127.0.0.1" /etc/tor/torrc; then
  309. echo 'DNSListenAddress 127.0.0.1' >> /etc/tor/torrc
  310. fi
  311. if ! grep -q "DNSListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" /etc/tor/torrc; then
  312. echo "DNSListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" >> /etc/tor/torrc
  313. fi
  314. echo 'route_outgoing_traffic_through_tor' >> $COMPLETION_FILE
  315. }
  316. # NOTE: deliberately no exit 0