freedombone-utils-setup 17KB

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