freedombone-utils-setup 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Setup 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. # Different system variants which may be specified within
  31. # the SYSTEM_TYPE option
  32. VARIANT_FULL="full"
  33. VARIANT_WRITER="writer"
  34. VARIANT_CLOUD="cloud"
  35. VARIANT_CHAT="chat"
  36. VARIANT_MAILBOX="mailbox"
  37. VARIANT_NONMAILBOX="nonmailbox"
  38. VARIANT_SOCIAL="social"
  39. VARIANT_MEDIA="media"
  40. VARIANT_DEVELOPER="developer"
  41. VARIANT_MESH="mesh"
  42. DEFAULT_DOMAIN_NAME=
  43. DEFAULT_DOMAIN_CODE=
  44. MY_USERNAME=
  45. SYSTEM_TYPE=$VARIANT_FULL
  46. # An optional configuration file which overrides some of these variables
  47. CONFIGURATION_FILE="${PROJECT_NAME}.cfg"
  48. # Directory where source code is downloaded and compiled
  49. INSTALL_DIR=$HOME/build
  50. # device name for an attached usb drive
  51. USB_DRIVE=/dev/sda1
  52. # Location where the USB drive is mounted to
  53. USB_MOUNT=/mnt/usb
  54. # Number of days to keep backups for
  55. BACKUP_MAX_DAYS=30
  56. # file containing a list of remote locations to backup to
  57. # Format: [username@friendsdomain//home/username] [ssh_password]
  58. # With the only space character being between the server and the password
  59. FRIENDS_SERVERS_LIST=/home/$MY_USERNAME/backup.list
  60. export DEBIAN_FRONTEND=noninteractive
  61. # used to limit CPU usage
  62. CPULIMIT='/usr/bin/cpulimit -l 20 -e'
  63. # command to create a git repository
  64. CREATE_GIT_PROJECT_COMMAND='create-project'
  65. # File which keeps track of what has already been installed
  66. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  67. # log file where details of remote backups are stored
  68. REMOTE_BACKUPS_LOG=/var/log/remotebackups.log
  69. # message if something fails to install
  70. CHECK_MESSAGE="Check your internet connection, /etc/network/interfaces and /etc/resolv.conf, then delete $COMPLETION_FILE, run 'rm -fR /var/lib/apt/lists/* && apt-get update --fix-missing' and run this script again. If hash sum mismatches persist then try setting $DEBIAN_REPO to a different mirror and also change /etc/apt/sources.list."
  71. # Default diffie-hellman key length in bits
  72. DH_KEYLENGTH=2048
  73. function initial_setup {
  74. if grep -Fxq "initial_setup" $COMPLETION_FILE; then
  75. return
  76. fi
  77. apt-get -y remove --purge apache*
  78. apt-get -y dist-upgrade
  79. apt-get -y install ca-certificates emacs24 cpulimit
  80. apt-get -y install cryptsetup libgfshare-bin obnam sshpass wget
  81. apt-get -y install avahi-daemon avahi-utils avahi-discover
  82. apt-get -y install connect-proxy
  83. if [ ! -d $INSTALL_DIR ]; then
  84. mkdir -p $INSTALL_DIR
  85. fi
  86. echo 'initial_setup' >> $COMPLETION_FILE
  87. }
  88. function search_for_attached_usb_drive {
  89. # If a USB drive is attached then search for email,
  90. # gpg, ssh keys and emacs configuration
  91. if grep -Fxq "search_for_attached_usb_drive" $COMPLETION_FILE; then
  92. return
  93. fi
  94. if [ -b $USB_DRIVE ]; then
  95. if [ ! -d $USB_MOUNT ]; then
  96. echo $'Mounting USB drive'
  97. mkdir $USB_MOUNT
  98. mount $USB_DRIVE $USB_MOUNT
  99. fi
  100. if ! [[ $SYSTEM_TYPE == "$VARIANT_WRITER" || $SYSTEM_TYPE == "$VARIANT_CLOUD" || $SYSTEM_TYPE == "$VARIANT_CHAT" || $SYSTEM_TYPE == "$VARIANT_SOCIAL" || $SYSTEM_TYPE == "$VARIANT_MEDIA" || $SYSTEM_TYPE == "$VARIANT_DEVELOPER" || $SYSTEM_TYPE == "$VARIANT_MESH" || $SYSTEM_TYPE == "$VARIANT_NONMAILBOX" ]]; then
  101. if [ -d $USB_MOUNT/Maildir ]; then
  102. echo $'Maildir found on USB drive'
  103. IMPORT_MAILDIR=$USB_MOUNT/Maildir
  104. fi
  105. if [ -d $USB_MOUNT/.gnupg ]; then
  106. echo $'Importing GPG keyring'
  107. cp -r $USB_MOUNT/.gnupg /home/$MY_USERNAME
  108. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg
  109. GPG_KEYS_IMPORTED="yes"
  110. if [ ! -f /home/$MY_USERNAME/.gnupg/secring.gpg ]; then
  111. echo $'GPG files did not copy'
  112. exit 73529
  113. fi
  114. fi
  115. if [ -f $USB_MOUNT/.procmailrc ]; then
  116. echo $'Importing procmail settings'
  117. cp $USB_MOUNT/.procmailrc /home/$MY_USERNAME
  118. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.procmailrc
  119. fi
  120. if [ -f $USB_MOUNT/private_key.gpg ]; then
  121. echo $'GPG private key found on USB drive'
  122. MY_GPG_PRIVATE_KEY=$USB_MOUNT/private_key.gpg
  123. fi
  124. if [ -f $USB_MOUNT/public_key.gpg ]; then
  125. echo $'GPG public key found on USB drive'
  126. MY_GPG_PUBLIC_KEY=$USB_MOUNT/public_key.gpg
  127. fi
  128. fi
  129. if [ -d $USB_MOUNT/prosody ]; then
  130. if [ ! -d $XMPP_DIRECTORY ]; then
  131. mkdir $XMPP_DIRECTORY
  132. fi
  133. cp -r $USB_MOUNT/prosody/* $XMPP_DIRECTORY
  134. chown -R prosody:prosody $XMPP_DIRECTORY
  135. fi
  136. if [ -d $USB_MOUNT/.ssh ]; then
  137. echo $'Importing ssh keys'
  138. cp -r $USB_MOUNT/.ssh /home/$MY_USERNAME
  139. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh
  140. # for security delete the ssh keys from the usb drive
  141. if [ ! -f /home/$MY_USERNAME/.ssh/id_rsa ]; then
  142. echo $'ssh files did not copy'
  143. exit 8
  144. fi
  145. fi
  146. if [ -f $USB_MOUNT/.emacs ]; then
  147. echo $'Importing .emacs file'
  148. cp -f $USB_MOUNT/.emacs /home/$MY_USERNAME/.emacs
  149. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.emacs
  150. fi
  151. if [ -d $USB_MOUNT/.emacs.d ]; then
  152. echo $'Importing .emacs.d directory'
  153. cp -r $USB_MOUNT/.emacs.d /home/$MY_USERNAME
  154. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.emacs.d
  155. fi
  156. if [ -d $USB_MOUNT/ssl ]; then
  157. echo $'Importing SSL certificates'
  158. cp -r $USB_MOUNT/ssl/* /etc/ssl
  159. chmod 640 /etc/ssl/certs/*
  160. chmod 400 /etc/ssl/private/*
  161. # change ownership of some certificates
  162. if [ -d /etc/prosody ]; then
  163. chown prosody:prosody /etc/ssl/private/xmpp.*
  164. chown prosody:prosody /etc/ssl/certs/xmpp.*
  165. fi
  166. if [ -d /etc/dovecot ]; then
  167. chown root:dovecot /etc/ssl/certs/dovecot.*
  168. chown root:dovecot /etc/ssl/private/dovecot.*
  169. fi
  170. if [ -f /etc/ssl/private/exim.key ]; then
  171. cp /etc/ssl/private/exim.key /etc/exim4
  172. cp /etc/ssl/certs/exim.crt /etc/exim4
  173. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  174. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  175. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  176. fi
  177. fi
  178. if [ -d $USB_MOUNT/personal ]; then
  179. echo $'Importing personal directory'
  180. cp -r $USB_MOUNT/personal /home/$MY_USERNAME
  181. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/personal
  182. fi
  183. else
  184. if [ -d $USB_MOUNT ]; then
  185. umount $USB_MOUNT
  186. rm -rf $USB_MOUNT
  187. fi
  188. echo $'No USB drive attached'
  189. fi
  190. echo 'search_for_attached_usb_drive' >> $COMPLETION_FILE
  191. }
  192. function mark_admin_user_account {
  193. if ! grep -q "Admin user:" $COMPLETION_FILE; then
  194. echo "Admin user:$MY_USERNAME" >> $COMPLETION_FILE
  195. fi
  196. }
  197. function remove_instructions_from_motd {
  198. sed -i '/## /d' /etc/motd
  199. }
  200. function remove_default_user {
  201. # make sure you don't use the default user account
  202. if [[ $MY_USERNAME == "debian" ]]; then
  203. echo 'Do not use the default debian user account. Create a different user with: adduser [username]'
  204. exit 68
  205. fi
  206. # remove the default debian user to prevent it from becoming an attack vector
  207. if [ -d /home/debian ]; then
  208. userdel -r debian
  209. echo 'Default debian user account removed'
  210. fi
  211. }
  212. function setup_firewall {
  213. configure_firewall
  214. configure_firewall_ping
  215. configure_firewall_for_dns
  216. configure_firewall_for_avahi
  217. }
  218. function setup_utils {
  219. read_configuration
  220. set_default_onion_domains
  221. locale_setup
  222. parse_args
  223. check_domains
  224. install_static_network
  225. remove_default_user
  226. setup_firewall
  227. create_repo_sources
  228. configure_dns
  229. initial_setup
  230. install_tor
  231. #resolve_dns_via_tor
  232. install_command_line_browser
  233. enable_ssh_via_onion
  234. check_date
  235. install_dynamicdns
  236. randomize_cron
  237. create_freedns_updater
  238. mark_admin_user_account
  239. enforce_good_passwords
  240. install_editor
  241. change_login_message
  242. enable_zram
  243. random_number_generator
  244. set_your_domain_name
  245. time_synchronisation_tlsdate
  246. configure_internet_protocol
  247. create_git_project
  248. setup_wifi
  249. configure_ssh
  250. configure_ssh_onion
  251. allow_ssh_to_onion_address
  252. remove_instructions_from_motd
  253. check_hwrng
  254. search_for_attached_usb_drive
  255. regenerate_ssh_keys
  256. create_mirrors
  257. create_upgrade_script
  258. letsencrypt_renewals
  259. install_watchdog_script
  260. configure_avahi
  261. create_avahi_onion_domains
  262. #install_atheros_wifi
  263. route_outgoing_traffic_through_tor
  264. }
  265. function setup_mesh {
  266. install_cjdns
  267. install_cjdns_tools
  268. install_batman
  269. install_babel
  270. }
  271. function setup_email {
  272. configure_email
  273. create_procmail
  274. handle_admin_emails
  275. spam_filtering
  276. configure_imap
  277. #configure_imap_client_certs
  278. configure_gpg
  279. refresh_gpg_keys
  280. configure_backup_key
  281. install_monkeysphere
  282. encrypt_incoming_email
  283. encrypt_outgoing_email
  284. email_client
  285. email_archiving
  286. email_from_address
  287. create_public_mailing_list
  288. #create_private_mailing_list
  289. encrypt_all_email
  290. import_email
  291. }
  292. function setup_web {
  293. install_web_server
  294. install_web_server_access_control
  295. }
  296. function setup_apps {
  297. install_zeronet
  298. install_zeronet_blog
  299. install_zeronet_mail
  300. install_zeronet_forum
  301. install_syncthing
  302. upgrade_golang
  303. install_gogs
  304. install_xmpp
  305. install_xmpp_client
  306. install_tox_node
  307. install_tox_client
  308. tox_avahi
  309. install_irc_server
  310. install_irc_client
  311. install_mumble
  312. install_sip
  313. update_sipwitch_daemon
  314. install_wiki
  315. install_sip_turn
  316. install_blog
  317. mark_blog_domain
  318. install_gnu_social
  319. expire_gnu_social_posts
  320. install_gnu_social_theme
  321. install_gnu_social_markdown
  322. install_gnu_social_plugin_sharings
  323. install_gnu_social_plugin_sharings_theme
  324. install_rss_reader
  325. install_rss_reader_gnusocial
  326. install_rss_mobile_reader
  327. install_hubzilla
  328. #install_webmail
  329. #install_search_engine
  330. install_dlna_server
  331. #install_mediagoblin
  332. #install_ipfs
  333. repair_databases_script
  334. backup_to_friends_servers
  335. }
  336. # NOTE: deliberately no exit 0