freedombone-utils-setup 16KB

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