freedombone-utils-setup 19KB

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