freedombone-utils-setup 17KB

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