freedombone-utils-setup 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. if [ ! $PROJECT_NAME ]; then
  31. PROJECT_NAME='freedombone'
  32. fi
  33. DEFAULT_DOMAIN_NAME=
  34. DEFAULT_DOMAIN_CODE=
  35. MY_USERNAME=
  36. SYSTEM_TYPE="full"
  37. # An optional configuration file which overrides some of these variables
  38. CONFIGURATION_FILE="$HOME/${PROJECT_NAME}.cfg"
  39. # Directory where source code is downloaded and compiled
  40. INSTALL_DIR=$HOME/build
  41. # device name for an attached usb drive
  42. USB_DRIVE=/dev/sda1
  43. # Location where the USB drive is mounted to
  44. USB_MOUNT=/mnt/usb
  45. # Number of days to keep backups for
  46. BACKUP_MAX_DAYS=30
  47. # file containing a list of remote locations to backup to
  48. # Format: [username@friendsdomain//home/username] [ssh_password]
  49. # With the only space character being between the server and the password
  50. FRIENDS_SERVERS_LIST=/home/$MY_USERNAME/backup.list
  51. export DEBIAN_FRONTEND=noninteractive
  52. # used to limit CPU usage
  53. CPULIMIT='/usr/bin/cpulimit -l 20 -e'
  54. # command to create a git repository
  55. CREATE_GIT_PROJECT_COMMAND='create-project'
  56. # File which keeps track of what has already been installed
  57. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  58. # log file where details of remote backups are stored
  59. REMOTE_BACKUPS_LOG=/var/log/remotebackups.log
  60. # message if something fails to install
  61. 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."
  62. # Default diffie-hellman key length in bits
  63. DH_KEYLENGTH=2048
  64. function initial_setup {
  65. if grep -Fxq "initial_setup" $COMPLETION_FILE; then
  66. return
  67. fi
  68. apt-get -y remove --purge apache*
  69. apt-get -y dist-upgrade
  70. apt-get -y install ca-certificates emacs24 cpulimit
  71. apt-get -y install cryptsetup libgfshare-bin obnam sshpass wget
  72. apt-get -y install avahi-daemon avahi-utils avahi-discover
  73. apt-get -y install connect-proxy
  74. if [ ! -d $INSTALL_DIR ]; then
  75. mkdir -p $INSTALL_DIR
  76. fi
  77. echo 'initial_setup' >> $COMPLETION_FILE
  78. }
  79. function search_for_attached_usb_drive {
  80. # If a USB drive is attached then search for email,
  81. # gpg, ssh keys and emacs configuration
  82. if grep -Fxq "search_for_attached_usb_drive" $COMPLETION_FILE; then
  83. return
  84. fi
  85. if [ -b $USB_DRIVE ]; then
  86. if [ ! -d $USB_MOUNT ]; then
  87. echo $'Mounting USB drive'
  88. mkdir $USB_MOUNT
  89. mount $USB_DRIVE $USB_MOUNT
  90. fi
  91. if [ -d $USB_MOUNT/Maildir ]; then
  92. echo $'Maildir found on USB drive'
  93. IMPORT_MAILDIR=$USB_MOUNT/Maildir
  94. fi
  95. if [ -d $USB_MOUNT/.gnupg ]; then
  96. echo $'Importing GPG keyring'
  97. cp -r $USB_MOUNT/.gnupg /home/$MY_USERNAME
  98. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg
  99. GPG_KEYS_IMPORTED="yes"
  100. if [ ! -f /home/$MY_USERNAME/.gnupg/secring.gpg ]; then
  101. echo $'GPG files did not copy'
  102. exit 73529
  103. fi
  104. fi
  105. if [ -f $USB_MOUNT/.procmailrc ]; then
  106. echo $'Importing procmail settings'
  107. cp $USB_MOUNT/.procmailrc /home/$MY_USERNAME
  108. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.procmailrc
  109. fi
  110. if [ -f $USB_MOUNT/private_key.gpg ]; then
  111. echo $'GPG private key found on USB drive'
  112. MY_GPG_PRIVATE_KEY=$USB_MOUNT/private_key.gpg
  113. fi
  114. if [ -f $USB_MOUNT/public_key.gpg ]; then
  115. echo $'GPG public key found on USB drive'
  116. MY_GPG_PUBLIC_KEY=$USB_MOUNT/public_key.gpg
  117. fi
  118. if [ -d $USB_MOUNT/prosody ]; then
  119. if [ ! -d $XMPP_DIRECTORY ]; then
  120. mkdir $XMPP_DIRECTORY
  121. fi
  122. cp -r $USB_MOUNT/prosody/* $XMPP_DIRECTORY
  123. chown -R prosody:prosody $XMPP_DIRECTORY
  124. fi
  125. if [ -d $USB_MOUNT/.ssh ]; then
  126. echo $'Importing ssh keys'
  127. cp -r $USB_MOUNT/.ssh /home/$MY_USERNAME
  128. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh
  129. # for security delete the ssh keys from the usb drive
  130. if [ ! -f /home/$MY_USERNAME/.ssh/id_rsa ]; then
  131. echo $'ssh files did not copy'
  132. exit 8
  133. fi
  134. fi
  135. if [ -f $USB_MOUNT/.emacs ]; then
  136. echo $'Importing .emacs file'
  137. cp -f $USB_MOUNT/.emacs /home/$MY_USERNAME/.emacs
  138. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.emacs
  139. fi
  140. if [ -d $USB_MOUNT/.emacs.d ]; then
  141. echo $'Importing .emacs.d directory'
  142. cp -r $USB_MOUNT/.emacs.d /home/$MY_USERNAME
  143. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.emacs.d
  144. fi
  145. if [ -d $USB_MOUNT/ssl ]; then
  146. echo $'Importing SSL certificates'
  147. cp -r $USB_MOUNT/ssl/* /etc/ssl
  148. chmod 640 /etc/ssl/certs/*
  149. chmod 400 /etc/ssl/private/*
  150. # change ownership of some certificates
  151. if [ -d /etc/prosody ]; then
  152. chown prosody:prosody /etc/ssl/private/xmpp.*
  153. chown prosody:prosody /etc/ssl/certs/xmpp.*
  154. fi
  155. if [ -d /etc/dovecot ]; then
  156. chown root:dovecot /etc/ssl/certs/dovecot.*
  157. chown root:dovecot /etc/ssl/private/dovecot.*
  158. fi
  159. if [ -f /etc/ssl/private/exim.key ]; then
  160. cp /etc/ssl/private/exim.key /etc/exim4
  161. cp /etc/ssl/certs/exim.crt /etc/exim4
  162. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  163. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  164. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  165. fi
  166. fi
  167. if [ -d $USB_MOUNT/personal ]; then
  168. echo $'Importing personal directory'
  169. cp -r $USB_MOUNT/personal /home/$MY_USERNAME
  170. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/personal
  171. fi
  172. else
  173. if [ -d $USB_MOUNT ]; then
  174. umount $USB_MOUNT
  175. rm -rf $USB_MOUNT
  176. fi
  177. echo $'No USB drive attached'
  178. fi
  179. echo 'search_for_attached_usb_drive' >> $COMPLETION_FILE
  180. }
  181. function mark_admin_user_account {
  182. if ! grep -q "Admin user:" $COMPLETION_FILE; then
  183. echo "Admin user:$MY_USERNAME" >> $COMPLETION_FILE
  184. fi
  185. }
  186. function remove_instructions_from_motd {
  187. sed -i '/## /d' /etc/motd
  188. }
  189. function remove_default_user {
  190. # make sure you don't use the default user account
  191. if [[ $MY_USERNAME == "debian" ]]; then
  192. echo 'Do not use the default debian user account. Create a different user with: adduser [username]'
  193. exit 68
  194. fi
  195. # remove the default debian user to prevent it from becoming an attack vector
  196. if [ -d /home/debian ]; then
  197. userdel -r debian
  198. echo 'Default debian user account removed'
  199. fi
  200. }
  201. function create_completion_file {
  202. if [ ! -f $COMPLETION_FILE ]; then
  203. touch $COMPLETION_FILE
  204. fi
  205. }
  206. function upgrade_installation {
  207. # TODO
  208. echo ''
  209. }
  210. function setup_firewall {
  211. function_check create_completion_file
  212. create_completion_file
  213. function_check configure_firewall
  214. configure_firewall
  215. function_check configure_firewall_ping
  216. configure_firewall_ping
  217. function_check configure_firewall_for_dns
  218. configure_firewall_for_dns
  219. function_check configure_firewall_for_avahi
  220. configure_firewall_for_avahi
  221. function_check global_rate_limit
  222. global_rate_limit
  223. }
  224. function setup_utils {
  225. read_config_param "PROJECT_REPO"
  226. write_config_param "PROJECT_REPO" "$PROJECT_REPO"
  227. function_check create_completion_file
  228. create_completion_file
  229. function_check read_configuration
  230. read_configuration
  231. function_check check_system_type
  232. check_system_type
  233. function_check upgrade_installation
  234. upgrade_installation
  235. function_check set_default_onion_domains
  236. set_default_onion_domains
  237. function_check locale_setup
  238. locale_setup
  239. function_check parse_args
  240. parse_args
  241. function_check check_domains
  242. check_domains
  243. function_check install_static_network
  244. install_static_network
  245. function_check remove_default_user
  246. remove_default_user
  247. function_check setup_firewall
  248. setup_firewall
  249. function_check create_repo_sources
  250. create_repo_sources
  251. function_check configure_dns
  252. configure_dns
  253. function_check initial_setup
  254. initial_setup
  255. function_check install_tor
  256. install_tor
  257. #function_check resolve_dns_via_tor
  258. #resolve_dns_via_tor
  259. function_check install_command_line_browser
  260. install_command_line_browser
  261. function_check enable_ssh_via_onion
  262. enable_ssh_via_onion
  263. function_check check_date
  264. check_date
  265. function_check install_dynamicdns
  266. install_dynamicdns
  267. function_check randomize_cron
  268. randomize_cron
  269. function_check create_freedns_updater
  270. create_freedns_updater
  271. function_check mark_admin_user_account
  272. mark_admin_user_account
  273. function_check enforce_good_passwords
  274. enforce_good_passwords
  275. function_check change_login_message
  276. change_login_message
  277. function_check enable_zram
  278. enable_zram
  279. function_check random_number_generator
  280. random_number_generator
  281. function_check set_your_domain_name
  282. set_your_domain_name
  283. function_check configure_internet_protocol
  284. configure_internet_protocol
  285. function_check create_git_project
  286. create_git_project
  287. function_check setup_wifi
  288. setup_wifi
  289. function_check configure_ssh
  290. configure_ssh
  291. function_check configure_ssh_onion
  292. configure_ssh_onion
  293. function_check allow_ssh_to_onion_address
  294. allow_ssh_to_onion_address
  295. function_check remove_instructions_from_motd
  296. remove_instructions_from_motd
  297. function_check check_hwrng
  298. check_hwrng
  299. function_check search_for_attached_usb_drive
  300. search_for_attached_usb_drive
  301. function_check regenerate_ssh_keys
  302. regenerate_ssh_keys
  303. function_check create_mirrors
  304. create_mirrors
  305. function_check create_upgrade_script
  306. create_upgrade_script
  307. function_check letsencrypt_renewals
  308. letsencrypt_renewals
  309. function_check install_watchdog_script
  310. install_watchdog_script
  311. function_check install_avahi
  312. install_avahi
  313. function_check create_avahi_onion_domains
  314. create_avahi_onion_domains
  315. #function_check install_atheros_wifi
  316. #install_atheros_wifi
  317. function_check route_outgoing_traffic_through_tor
  318. route_outgoing_traffic_through_tor
  319. function_check upgrade_golang
  320. upgrade_golang
  321. function_check install_tomb
  322. install_tomb
  323. }
  324. function setup_email {
  325. function_check create_completion_file
  326. create_completion_file
  327. function_check install_email
  328. install_email
  329. function_check create_procmail
  330. create_procmail
  331. function_check handle_admin_emails
  332. handle_admin_emails
  333. function_check spam_filtering
  334. spam_filtering
  335. function_check configure_imap
  336. configure_imap
  337. #function_check configure_imap_client_certs
  338. #configure_imap_client_certs
  339. function_check configure_gpg
  340. configure_gpg
  341. function_check refresh_gpg_keys
  342. refresh_gpg_keys
  343. function_check configure_backup_key
  344. configure_backup_key
  345. function_check install_monkeysphere
  346. install_monkeysphere
  347. function_check encrypt_incoming_email
  348. encrypt_incoming_email
  349. function_check encrypt_outgoing_email
  350. encrypt_outgoing_email
  351. function_check email_client
  352. email_client
  353. function_check email_archiving
  354. email_archiving
  355. function_check email_from_address
  356. email_from_address
  357. function_check create_public_mailing_list
  358. create_public_mailing_list
  359. #function check create_private_mailing_list
  360. #create_private_mailing_list
  361. function_check encrypt_all_email
  362. encrypt_all_email
  363. function_check import_email
  364. import_email
  365. }
  366. function setup_web {
  367. function_check create_completion_file
  368. create_completion_file
  369. function_check install_web_server
  370. install_web_server
  371. function_check install_web_server_access_control
  372. install_web_server_access_control
  373. }
  374. function upgrade_apps {
  375. function_check create_completion_file
  376. create_completion_file
  377. APPS_COMPLETED=()
  378. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  379. # for all the app scripts
  380. for filename in $FILES
  381. do
  382. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  383. item_in_array "${app_name}" "${APPS_COMPLETED[@]}"
  384. if [[ $? != 0 ]]; then
  385. function_check app_is_installed
  386. if [[ "$(app_is_installed $a)" == "1" ]]; then
  387. app_load_variables ${app_name}
  388. APPS_COMPLETED+=("${app_name}")
  389. function_check upgrade_${app_name}
  390. upgrade_${app_name}
  391. fi
  392. fi
  393. done
  394. }
  395. function setup_apps {
  396. function_check create_completion_file
  397. create_completion_file
  398. function_check choose_apps_for_variant
  399. choose_apps_for_variant "$SYSTEM_TYPE"
  400. echo $"System variant: $SYSTEM_TYPE"
  401. echo $'The following apps have been selected'
  402. echo ''
  403. function_check list_chosen_apps
  404. list_chosen_apps
  405. echo ''
  406. function_check upgrade_apps
  407. upgrade_apps
  408. function_check install_apps
  409. install_apps
  410. }
  411. function combine_all_scripts {
  412. combined_filename=$1
  413. # initial variables
  414. cp $PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars $combined_filename
  415. # utilities
  416. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  417. for f in $UTILS_FILES
  418. do
  419. # this removes the first line, which is #!/bin/bash
  420. tail -n +2 "$f" >> $combined_filename
  421. done
  422. # base system
  423. BASE_SYSTEM_FILES=/usr/share/${PROJECT_NAME}/base/${PROJECT_NAME}-base-*
  424. for f in $BASE_SYSTEM_FILES
  425. do
  426. tail -n +2 "$f" >> $combined_filename
  427. done
  428. # apps
  429. APP_FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  430. for f in $APP_FILES
  431. do
  432. tail -n +2 "$f" >> $combined_filename
  433. done
  434. }
  435. # NOTE: deliberately no exit 0