freedombone-utils-setup 18KB

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