freedombone-utils-setup 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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-2018 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. if [ ! "$LOCAL_NAME" ]; then
  34. LOCAL_NAME=${PROJECT_NAME}
  35. fi
  36. DEFAULT_DOMAIN_NAME=
  37. DEFAULT_DOMAIN_CODE=
  38. MY_USERNAME=
  39. if [ ! "$SYSTEM_TYPE" ]; then
  40. SYSTEM_TYPE="full"
  41. fi
  42. # An optional configuration file which overrides some of these variables
  43. if [ ! "$CONFIGURATION_FILE" ]; then
  44. CONFIGURATION_FILE="$HOME/${PROJECT_NAME}.cfg"
  45. fi
  46. # Directory where source code is downloaded and compiled
  47. INSTALL_DIR=$HOME/build
  48. # device name for an attached usb drive
  49. USB_DRIVE=/dev/sda1
  50. # Location where the USB drive is mounted to
  51. USB_MOUNT=/mnt/usb
  52. # Number of days to keep backups for
  53. BACKUP_MAX_DAYS=30
  54. # file containing a list of remote locations to backup to
  55. # Format: [username@friendsdomain//home/username] [ssh_password]
  56. # With the only space character being between the server and the password
  57. FRIENDS_SERVERS_LIST="/home/$MY_USERNAME/backup.list"
  58. export DEBIAN_FRONTEND=noninteractive
  59. # used to limit CPU usage
  60. CPULIMIT='/usr/bin/cpulimit -l 20 -e'
  61. # command to create a git repository
  62. CREATE_GIT_PROJECT_COMMAND='create-project'
  63. # File which keeps track of what has already been installed
  64. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  65. # log file where details of remote backups are stored
  66. REMOTE_BACKUPS_LOG=/var/log/remotebackups.log
  67. # message if something fails to install
  68. CHECK_MESSAGE="Check your internet connection, /etc/network/interfaces and /etc/resolvconf/resolv.conf.d/head, 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."
  69. # Default diffie-hellman key length in bits
  70. DH_KEYLENGTH=2048
  71. function support_256_colours {
  72. if ! grep 'xterm-256color' /etc/skel/.profile; then
  73. echo '' >> /etc/skel/.profile
  74. echo 'export TERM=xterm-256color' >> /etc/skel/.profile
  75. fi
  76. if ! grep 'xterm-256color' /home/$MY_USERNAME/.profile; then
  77. echo '' >> /home/$MY_USERNAME/.profile
  78. echo 'export TERM=xterm-256color' >> /home/$MY_USERNAME/.profile
  79. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.profile
  80. fi
  81. }
  82. function detect_usb_drive {
  83. # sets to the highest available drive letter
  84. # which is likely to be the last drive connected
  85. read_config_param USB_DRIVE
  86. partition_number='1'
  87. #if [[ "$1" == "nopath" ]]; then
  88. # partition_number=''
  89. #fi
  90. if [ -b /dev/sda${partition_number} ]; then
  91. USB_DRIVE=/dev/sda${partition_number}
  92. fi
  93. if [ -b /dev/sdb${partition_number} ]; then
  94. USB_DRIVE=/dev/sdb${partition_number}
  95. fi
  96. if [ -b /dev/sdc${partition_number} ]; then
  97. USB_DRIVE=/dev/sdc${partition_number}
  98. fi
  99. if [ -b /dev/sdd${partition_number} ]; then
  100. USB_DRIVE=/dev/sdd${partition_number}
  101. fi
  102. if [ -b /dev/sde${partition_number} ]; then
  103. USB_DRIVE=/dev/sde${partition_number}
  104. fi
  105. if [ -b /dev/sdf${partition_number} ]; then
  106. USB_DRIVE=/dev/sdf${partition_number}
  107. fi
  108. if [ -b /dev/sdg${partition_number} ]; then
  109. USB_DRIVE=/dev/sdg${partition_number}
  110. fi
  111. if [ -b /dev/sdh${partition_number} ]; then
  112. USB_DRIVE=/dev/sdh${partition_number}
  113. fi
  114. write_config_param USB_DRIVE "$USB_DRIVE"
  115. }
  116. function separate_tmp_filesystem {
  117. tmp_filesystem_size_mb=$1
  118. if [ ! -d /tmp ]; then
  119. mkdir -p /tmp
  120. fi
  121. if ! grep -q '/tmp' /etc/fstab; then
  122. mount -t tmpfs -o size="${tmp_filesystem_size_mb}m" tmpfs /tmp
  123. echo "tmpfs /tmp tmpfs nodev,nosuid,noexec,nodiratime,size=${tmp_filesystem_size_mb}M 0 0" >> /etc/fstab
  124. fi
  125. }
  126. function proc_filesystem_settings {
  127. if ! grep -q "proc proc defaults,nodev,nosuid " /etc/fstab; then
  128. sed -i 's|proc /proc proc defaults |proc /proc proc defaults,nodev,nosuid |g' /etc/fstab
  129. fi
  130. }
  131. function remove_bluetooth {
  132. bluetooth_changed=
  133. bnep_exists=$(lsmod | grep bnep)
  134. if [[ "$bnep_exists" == "bnep"* ]]; then
  135. rmmod -f bnep
  136. bluetooth_changed=1
  137. fi
  138. bluetooth_exists=$(lsmod | grep bluetooth)
  139. if [[ "$bluetooth_exists" == "bluetooth"* ]]; then
  140. rmmod -f bluetooth
  141. bluetooth_changed=1
  142. fi
  143. if [ -f /etc/default/bluetooth ]; then
  144. if grep -q "BLUETOOTH_ENABLED=" /etc/default/bluetooth; then
  145. sed -i 's|BLUETOOTH_ENABLED=.*|BLUETOOTH_ENABLED=0|g' /etc/default/bluetooth
  146. else
  147. echo "BLUETOOTH_ENABLED=0" >> /etc/default/bluetooth
  148. fi
  149. bluetooth_changed=1
  150. fi
  151. if ! grep -q 'blacklist bnep' /etc/modprobe.d/bluetooth.conf; then
  152. echo 'blacklist bnep' >> /etc/modprobe.d/bluetooth.conf
  153. bluetooth_changed=1
  154. fi
  155. if ! grep -q 'blacklist btusb' /etc/modprobe.d/bluetooth.conf; then
  156. echo 'blacklist btusb' >> /etc/modprobe.d/bluetooth.conf
  157. bluetooth_changed=1
  158. fi
  159. if ! grep -q 'blacklist bluetooth' /etc/modprobe.d/bluetooth.conf; then
  160. echo 'blacklist bluetooth' >> /etc/modprobe.d/bluetooth.conf
  161. bluetooth_changed=1
  162. fi
  163. if [ $bluetooth_changed ]; then
  164. update-initramfs -u -k "$(uname -r)" -v
  165. update-rc.d bluetooth remove
  166. fi
  167. }
  168. function running_as_root {
  169. if [[ $EUID != 0 ]] ; then
  170. echo "0"
  171. else
  172. echo "1"
  173. fi
  174. }
  175. function reset_usb_devices {
  176. for xhci in /sys/bus/pci/drivers/?hci-pci ; do
  177. if ! cd "$xhci" ; then
  178. return
  179. fi
  180. echo "Resetting devices from $xhci..."
  181. for i in ????:??:??.? ; do
  182. echo -n "$i" > unbind
  183. echo -n "$i" > bind
  184. done
  185. done
  186. udevadm control --reload-rules
  187. }
  188. function install_backports_kernel {
  189. # install backports kernel if possible
  190. architecture_type=$(uname -a)
  191. if [[ "$architecture_type" == *"amd64"* ]]; then
  192. package_installed=$(dpkg-query -W -f='${Package}\n' linux-image-amd64 2>/dev/null)
  193. if [ ! "$package_installed" ]; then
  194. apt-get -yq install linux-image-amd64
  195. fi
  196. fi
  197. }
  198. function turn_off_rsys_logging {
  199. if grep -q '/dev/null' /etc/rsyslog.conf; then
  200. return
  201. fi
  202. sed -i 's|mail,news.none.*|mail,news.none /dev/null|g' /etc/rsyslog.conf
  203. sed -i 's|auth,authpriv.\*.*|auth,authpriv.\* /dev/null|g' /etc/rsyslog.conf
  204. sed -i 's|mail.info.*|mail.info /dev/null|g' /etc/rsyslog.conf
  205. sed -i 's|mail.warn.*|mail.warn /dev/null|g' /etc/rsyslog.conf
  206. sed -i 's|mail.err.*|mail.err /dev/null|g' /etc/rsyslog.conf
  207. sed -i 's|daemon.\*.*|daemon.\* /dev/null|g' /etc/rsyslog.conf
  208. sed -i 's|mail.\*.*|mail.\* /dev/null|g' /etc/rsyslog.conf
  209. sed -i 's|user.\*.*|user.\* /dev/null|g' /etc/rsyslog.conf
  210. sed -i 's|news.none;mail.none.*|news.none;mail.none /dev/null|g' /etc/rsyslog.conf
  211. sed -i 's|\*.\*;auth,authpriv.none.*|\*.\*;auth,authpriv.none /dev/null|g' /etc/rsyslog.conf
  212. sed -i 's|#cron.\*|cron.\*|g' /etc/rsyslog.conf
  213. sed -i 's|cron.\*.*|cron.\* /dev/null|g' /etc/rsyslog.conf
  214. shred -zu /var/log/wtmp*
  215. shred -zu /var/log/debug*
  216. shred -zu /var/log/cron.*
  217. shred -zu /var/log/auth.*
  218. shred -zu /var/log/mail.*
  219. shred -zu /var/log/daemon.*
  220. shred -zu /var/log/user.*
  221. shred -zu /var/log/messages*
  222. }
  223. function initial_setup {
  224. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  225. return
  226. fi
  227. apt-get -yq remove --purge apache2-bin*
  228. apt-get -yq dist-upgrade
  229. apt-get -yq install ca-certificates
  230. apt-get -yq install apt-utils
  231. apt-get -yq install cryptsetup libgfshare-bin duplicity sshpass wget avahi-daemon
  232. apt-get -yq install avahi-utils avahi-discover connect-proxy openssh-server
  233. apt-get -yq install sudo git dialog build-essential avahi-daemon avahi-utils
  234. apt-get -yq install avahi-discover iptables dnsutils net-tools
  235. apt-get -yq install network-manager iputils-ping libnss-mdns libnss-myhostname
  236. apt-get -yq install libnss-gw-name nano man ntp locales locales-all debconf
  237. apt-get -yq install wireless-tools wpasupplicant usbutils zsh cpulimit screen
  238. apt-get -yq install pinentry-curses eatmydata iotop bc hostapd
  239. # With some VMs, the hardware cycles counter is emulated and deterministic,
  240. # and thus predictible, so havege should not be used
  241. if [[ $ARCHITECTURE != 'qemu'* ]]; then
  242. apt-get -yq install haveged
  243. fi
  244. if [[ $ARCHITECTURE == 'qemu'* || $ARCHITECTURE == 'amd64' || $ARCHITECTURE == 'x86_64' || $ARCHITECTURE == 'i686' || $ARCHITECTURE == 'i386' ]]; then
  245. apt-get -yq install grub2 lvm2
  246. fi
  247. if [ ! -d "$INSTALL_DIR" ]; then
  248. mkdir -p "$INSTALL_DIR"
  249. fi
  250. mark_completed "${FUNCNAME[0]}"
  251. }
  252. function turn_off_magic_sysrq {
  253. if grep -q 'kernel.sysrq = 0' /etc/sysctl.conf; then
  254. return
  255. fi
  256. if grep -q 'kernel.sysrq' /etc/sysctl.conf; then
  257. sed -i 's|#kernel.sysrq.*|kernel.sysrq = 0|g' /etc/sysctl.conf
  258. sed -i 's|kernel.sysrq.*|kernel.sysrq = 0|g' /etc/sysctl.conf
  259. else
  260. echo 'kernel.sysrq = 0' >> /etc/sysctl.conf
  261. fi
  262. }
  263. function setup_grub {
  264. if [[ $ARCHITECTURE == 'qemu'* || $ARCHITECTURE == 'amd64' || $ARCHITECTURE == 'x86_64' || $ARCHITECTURE == 'i686' || $ARCHITECTURE == 'i386' ]]; then
  265. if ! grep -q 'ifnames=0' /etc/default/grub; then
  266. sed -i 's|GRUB_CMDLINE_LINUX_DEFAULT=.*|GRUB_CMDLINE_LINUX_DEFAULT="quiet ifnames=0 slub_debug=FZP slab_nomerge page_poison=1 panic=0"|g' /etc/default/grub
  267. update-grub
  268. fi
  269. fi
  270. }
  271. function admin_user_sudo {
  272. # shellcheck disable=SC2154
  273. if ! grep -q "$MY_USERNAME ALL=(ALL) ALL" "$rootdir/etc/sudoers"; then
  274. echo "$MY_USERNAME ALL=(ALL) ALL" >> "$rootdir/etc/sudoers"
  275. fi
  276. }
  277. function search_for_attached_usb_drive {
  278. # If a USB drive is attached then search for email,
  279. # gpg, ssh keys and emacs configuration
  280. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  281. return
  282. fi
  283. detect_usb_drive
  284. if [ -b $USB_DRIVE ]; then
  285. if [ ! -d $USB_MOUNT ]; then
  286. echo $'Mounting USB drive'
  287. mkdir $USB_MOUNT
  288. mount $USB_DRIVE $USB_MOUNT
  289. fi
  290. if [ -d $USB_MOUNT/.gnupg ]; then
  291. echo $'Importing GPG keyring'
  292. cp -r $USB_MOUNT/.gnupg /home/$MY_USERNAME
  293. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg
  294. if [ ! -f /home/$MY_USERNAME/.gnupg/secring.gpg ]; then
  295. echo $'GPG files did not copy'
  296. exit 73529
  297. fi
  298. fi
  299. if [ -f $USB_MOUNT/private_key.gpg ]; then
  300. echo $'GPG private key found on USB drive'
  301. MY_GPG_PRIVATE_KEY="$USB_MOUNT/private_key.gpg"
  302. fi
  303. if [ -f $USB_MOUNT/public_key.gpg ]; then
  304. echo $'GPG public key found on USB drive'
  305. MY_GPG_PUBLIC_KEY="$USB_MOUNT/public_key.gpg"
  306. fi
  307. if [ -f $USB_MOUNT/letsencrypt ]; then
  308. echo $'Copying letsencrypt keys"'
  309. cp -r $USB_MOUNT/letsencrypt /etc
  310. fi
  311. if [ -d $USB_MOUNT/.ssh ]; then
  312. echo $'Importing ssh keys'
  313. cp -r $USB_MOUNT/.ssh /home/$MY_USERNAME
  314. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh
  315. # for security delete the ssh keys from the usb drive
  316. if [ ! -f /home/$MY_USERNAME/.ssh/id_rsa ]; then
  317. echo $'ssh files did not copy'
  318. exit 8
  319. fi
  320. fi
  321. if [ -f $USB_MOUNT/.emacs ]; then
  322. echo $'Importing .emacs file'
  323. cp -f $USB_MOUNT/.emacs /home/$MY_USERNAME/.emacs
  324. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.emacs
  325. fi
  326. if [ -d $USB_MOUNT/.emacs.d ]; then
  327. echo $'Importing .emacs.d directory'
  328. cp -r $USB_MOUNT/.emacs.d /home/$MY_USERNAME
  329. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.emacs.d
  330. fi
  331. if [ -d $USB_MOUNT/ssl ]; then
  332. echo $'Importing SSL certificates'
  333. cp -r $USB_MOUNT/ssl/* /etc/ssl
  334. chmod 640 /etc/ssl/certs/*
  335. chmod 400 /etc/ssl/private/*
  336. # change ownership of some certificates
  337. if [ -d /etc/prosody ]; then
  338. chown prosody:prosody /etc/ssl/private/xmpp.*
  339. chown prosody:prosody /etc/ssl/certs/xmpp.*
  340. fi
  341. if [ -d /etc/dovecot ]; then
  342. chown root:dovecot /etc/ssl/certs/dovecot.*
  343. chown root:dovecot /etc/ssl/private/dovecot.*
  344. fi
  345. if [ -f /etc/ssl/private/exim.key ]; then
  346. cp /etc/ssl/private/exim.key /etc/exim4
  347. cp /etc/ssl/certs/exim.crt /etc/exim4
  348. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  349. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  350. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  351. fi
  352. fi
  353. if [ -d $USB_MOUNT/personal ]; then
  354. echo $'Importing personal directory'
  355. cp -r $USB_MOUNT/personal /home/$MY_USERNAME
  356. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/personal
  357. fi
  358. else
  359. if [ -d $USB_MOUNT ]; then
  360. umount $USB_MOUNT
  361. rm -rf $USB_MOUNT
  362. fi
  363. echo $'No USB drive attached'
  364. fi
  365. mark_completed "${FUNCNAME[0]}"
  366. }
  367. function mark_admin_user_account {
  368. set_completion_param "Admin user" "$MY_USERNAME"
  369. }
  370. function remove_instructions_from_motd {
  371. if grep -q "## " /etc/motd; then
  372. sed -i '/## /d' /etc/motd
  373. fi
  374. }
  375. function remove_default_user {
  376. # make sure you don't use the default user account
  377. if [[ $MY_USERNAME == "debian" ]]; then
  378. echo 'Do not use the default debian user account. Create a different user with: adduser [username]'
  379. exit 68
  380. fi
  381. # remove the default debian user to prevent it from becoming an attack vector
  382. if [ -d /home/debian ]; then
  383. userdel -r debian
  384. echo 'Default debian user account removed'
  385. fi
  386. }
  387. function create_completion_file {
  388. if [ ! -f "$COMPLETION_FILE" ]; then
  389. touch "$COMPLETION_FILE"
  390. fi
  391. }
  392. function remove_management_engine_interface {
  393. # see https://www.kernel.org/doc/Documentation/misc-devices/mei/mei.txt
  394. # Disabling this interface doesn't cure the problems of ME, but it
  395. # might stop an adversary in control of AMT from using the command
  396. # interface to control the operating system.
  397. if [ -f /dev/mei0 ]; then
  398. rmmod mei_me
  399. rmmod mei0
  400. fi
  401. blacklist_changed=
  402. if [ ! -f /etc/modprobe.d/blacklist.conf ]; then
  403. touch /etc/modprobe.d/blacklist.conf
  404. blacklist_changed=1
  405. fi
  406. if ! grep -q "blacklist mei" /etc/modprobe.d/blacklist.conf; then
  407. echo "blacklist mei" >> /etc/modprobe.d/blacklist.conf
  408. blacklist_changed=1
  409. fi
  410. if ! grep -q "blacklist mei_me" /etc/modprobe.d/blacklist.conf; then
  411. echo "blacklist mei_me" >> /etc/modprobe.d/blacklist.conf
  412. blacklist_changed=1
  413. fi
  414. if [ $blacklist_changed ]; then
  415. depmod -ae -E
  416. update-initramfs -u
  417. fi
  418. }
  419. function set_login_umask {
  420. logindefs_umask=$(grep UMASK /etc/login.defs | grep -v '#')
  421. if [[ "$logindefs_umask" != *'077' ]]; then
  422. sed -i 's|UMASK\t.*|UMASK\t\t077|g' /etc/login.defs
  423. fi
  424. }
  425. function disable_deferred_execution {
  426. systemctl stop atd
  427. systemctl disable atd
  428. }
  429. function set_shadow_permissions {
  430. chown root:root /etc/shadow
  431. chmod 0000 /etc/shadow
  432. chown root:root /etc/gshadow
  433. chmod 0000 /etc/gshadow
  434. }
  435. function set_max_login_tries {
  436. max_tries=$1
  437. if ! grep -q ' deny=' /etc/pam.d/common-auth; then
  438. sed -i "/pam_deny.so/a auth required\\t\\t\\tpam_tally.so onerr=fail no_lock_time per_user deny=$max_tries" /etc/pam.d/common-auth
  439. else
  440. if ! grep -q " deny=$max_tries" /etc/pam.d/common-auth; then
  441. sed -i "s| deny=.*| deny=$max_tries|g" /etc/pam.d/common-auth
  442. fi
  443. fi
  444. if ! grep -q 'pam_tally.so' /etc/pam.d/common-account; then
  445. sed -i '/pam_deny.so/a account required\t\t\tpam_tally.so' /etc/pam.d/common-account
  446. fi
  447. }
  448. function limit_user_logins {
  449. # overall max logins
  450. if ! grep -q '\* hard maxsyslogins' /etc/security/limits.conf; then
  451. echo '* hard maxsyslogins 10' >> /etc/security/limits.conf
  452. else
  453. if ! grep -q '\* hard maxsyslogins 10' /etc/security/limits.conf; then
  454. sed -i 's|hard maxsyslogins.*|hard maxsyslogins 10|g' /etc/security/limits.conf
  455. fi
  456. fi
  457. # Max logins for each user
  458. if ! grep -q '\* hard maxlogins' /etc/security/limits.conf; then
  459. echo '* hard maxlogins 2' >> /etc/security/limits.conf
  460. else
  461. if ! grep -q '\* hard maxlogins 2' /etc/security/limits.conf; then
  462. sed -i 's|hard maxlogins.*|hard maxlogins 2|g' /etc/security/limits.conf
  463. fi
  464. fi
  465. }
  466. function remove_serial_logins {
  467. if grep -q 'ttyS' /etc/securetty; then
  468. cp /etc/securetty /etc/securetty_old
  469. sed -i '/ttyS/d' /etc/securetty
  470. fi
  471. }
  472. function set_sticky_bits {
  473. world_writable=$(find / -xdev -type d -perm -002 \! -perm -1000)
  474. for w in $world_writable; do
  475. echo "Setting sticky bit on $w"
  476. chmod +t "$w"
  477. done
  478. }
  479. function disable_ctrl_alt_del {
  480. ctrl_alt_del=$(ls -l /etc/systemd/system/ctrl-alt-del.target)
  481. if [[ "$ctrl_alt_del" != *'/dev/null' ]]; then
  482. ln -sf /dev/null /etc/systemd/system/ctrl-alt-del.target
  483. fi
  484. }
  485. function lockdown_permissions {
  486. if [ -d /root/.npm ]; then
  487. chmod -R 700 /root/.npm
  488. fi
  489. # All commands owned by root
  490. if [ -d /root/.cache/yarn ]; then
  491. rm -rf /root/.cache/yarn
  492. fi
  493. if [ -d /usr/local/share/.cache/yarn ]; then
  494. rm -rf /usr/local/share/.cache/yarn
  495. fi
  496. if [ -f /usr/lib/ssl/certs/ssl-cert-snakeoil.pem ]; then
  497. chown root:root /usr/lib/ssl/certs/ssl-cert-snake*
  498. fi
  499. if [ -d /bin ]; then
  500. chown root:root /bin/*
  501. fi
  502. if [ -d /usr/bin ]; then
  503. chown root:root /usr/bin/*
  504. fi
  505. if [ -d /usr/local/bin ]; then
  506. chown root:root /usr/local/bin/*
  507. fi
  508. if [ -d /sbin ]; then
  509. chown root:root /sbin/*
  510. fi
  511. if [ -d /usr/sbin ]; then
  512. chown root:root /usr/sbin/*
  513. fi
  514. if [ -d /usr/local/sbin ]; then
  515. chown root:root /usr/local/sbin/*
  516. fi
  517. if [ -d /usr/share/${PROJECT_NAME} ]; then
  518. chown -R root:root /usr/share/${PROJECT_NAME}
  519. chmod -R +r /usr/share/${PROJECT_NAME}
  520. fi
  521. # All libraries owned by root
  522. if [ -d /lib ]; then
  523. chown -R root:root /lib/*
  524. fi
  525. if [ -d /lib64 ]; then
  526. chown -R root:root /lib64/*
  527. fi
  528. if [ -d /usr/lib ]; then
  529. chown -R root:root /usr/lib/*
  530. if [ -d /usr/lib/node_modules ]; then
  531. chmod -R 750 /usr/lib/node_modules/*
  532. fi
  533. if [ -d /usr/lib/prosody ]; then
  534. chown -R prosody:prosody /usr/lib/prosody
  535. fi
  536. fi
  537. if [ -d /usr/lib64 ]; then
  538. chown -R root:root /usr/lib64/*
  539. fi
  540. # sudo permissions
  541. chmod 4755 /usr/bin/sudo
  542. chmod 4755 /usr/lib/sudo/sudoers.so
  543. chown root:root /etc/sudoers
  544. # permissions on email commands
  545. if [ -f /usr/bin/procmail ]; then
  546. chmod 6755 /usr/bin/procmail
  547. fi
  548. if [ -f /usr/sbin/exim ]; then
  549. chmod u+s /usr/sbin/exim
  550. fi
  551. if [ -f /usr/sbin/exim4 ]; then
  552. chmod u+s /usr/sbin/exim4
  553. fi
  554. set_sticky_bits
  555. # Create some directories to correspond with users in passwords file
  556. if [ ! -d /var/spool/lpd ]; then
  557. mkdir /var/spool/lpd
  558. fi
  559. if [ ! -d /var/spool/news ]; then
  560. mkdir /var/spool/news
  561. fi
  562. if [ ! -d /var/spool/uucp ]; then
  563. mkdir /var/spool/uucp
  564. fi
  565. if [ ! -d /var/list ]; then
  566. mkdir /var/list
  567. fi
  568. if [ ! -d /var/lib/gnats ]; then
  569. mkdir /var/lib/gnats
  570. fi
  571. if [ ! -d /var/lib/saned ]; then
  572. mkdir /var/lib/saned
  573. fi
  574. if [ -d /etc/prosody ]; then
  575. chown -R prosody /etc/prosody
  576. chmod -R 700 /etc/prosody/conf.d
  577. fi
  578. if [ -d /var/lib/prosody ]; then
  579. chown -R prosody /var/lib/prosody
  580. fi
  581. if [ -d /etc/letsencrypt ]; then
  582. chmod -R 600 /etc/letsencrypt
  583. chmod -R g=rX /etc/letsencrypt
  584. chown -R root:ssl-cert /etc/letsencrypt
  585. fi
  586. chown -f root:root /etc/motd /etc/issue*
  587. chmod -f 0444 /etc/motd /etc/issue*
  588. }
  589. function disable_core_dumps {
  590. if ! grep -q '\* hard core' /etc/security/limits.conf; then
  591. echo '* hard core 0' >> /etc/security/limits.conf
  592. else
  593. if ! grep -q '\* hard core 0' /etc/security/limits.conf; then
  594. sed -i 's|hard core.*|hard core 0|g' /etc/security/limits.conf
  595. fi
  596. fi
  597. }
  598. function dummy_nologin_command {
  599. if [ ! -f /sbin/nologin ]; then
  600. echo '#!/bin/bash' > /sbin/nologin
  601. chmod +x /sbin/nologin
  602. fi
  603. }
  604. function disable_null_passwords {
  605. if grep -q ' nullok_secure' /etc/pam.d/common-auth; then
  606. sed -i 's| nullok_secure||g' /etc/pam.d/common-auth
  607. fi
  608. }
  609. function create_usb_canary {
  610. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  611. return
  612. fi
  613. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  614. return
  615. fi
  616. echo "ACTION==\"add\", KERNEL==\"sd*[!0-9]\", RUN+=\"/usr/local/bin/${PROJECT_NAME}-usb-canary\"" > /etc/udev/rules.d/00-usb-canary.rules
  617. udevadm control --reload-rules
  618. mark_completed "${FUNCNAME[0]}"
  619. }
  620. function setup_firewall {
  621. function_check create_completion_file
  622. create_completion_file
  623. function_check configure_firewall
  624. configure_firewall
  625. function_check configure_firewall_ping
  626. configure_firewall_ping
  627. function_check firewall_drop_telnet
  628. firewall_drop_telnet
  629. function_check firewall_drop_spoofed_packets
  630. firewall_drop_spoofed_packets
  631. function_check firewall_rate_limits
  632. firewall_rate_limits
  633. function_check configure_firewall_for_dns
  634. configure_firewall_for_dns
  635. function_check configure_firewall_for_avahi
  636. configure_firewall_for_avahi
  637. function_check global_rate_limit
  638. global_rate_limit
  639. function_check firewall_block_bad_ip_ranges
  640. firewall_block_bad_ip_ranges
  641. }
  642. function setup_powerline {
  643. if [ -f ~/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline ]; then
  644. if [ ! -f ~/.powerline.bash ]; then
  645. cp ~/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline ~/.powerline.bash
  646. else
  647. HASH1=$(sha256sum ~/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline | awk -F ' ' '{print $1}')
  648. HASH2=$(sha256sum ~/.powerline.bash | awk -F ' ' '{print $1}')
  649. if [[ "$HASH1" != "$HASH2" ]]; then
  650. cp ~/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline ~/.powerline.bash
  651. fi
  652. fi
  653. if [ ! -f /etc/skel/.powerline.bash ]; then
  654. cp ~/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline /etc/skel/.powerline.bash
  655. else
  656. HASH1=$(sha256sum ~/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline | awk -F ' ' '{print $1}')
  657. HASH2=$(sha256sum /etc/skel/.powerline.bash | awk -F ' ' '{print $1}')
  658. if [[ "$HASH1" != "$HASH2" ]]; then
  659. cp ~/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline /etc/skel/.powerline.bash
  660. fi
  661. fi
  662. else
  663. if [ -f /home/${MY_USERNAME}/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline ]; then
  664. if [ ! -f ~/.powerline.bash ]; then
  665. cp /home/${MY_USERNAME}/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline ~/.powerline.bash
  666. else
  667. HASH1=$(sha256sum /home/${MY_USERNAME}/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline | awk -F ' ' '{print $1}')
  668. HASH2=$(sha256sum ~/.powerline.bash | awk -F ' ' '{print $1}')
  669. if [[ "$HASH1" != "$HASH2" ]]; then
  670. cp /home/${MY_USERNAME}/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline ~/.powerline.bash
  671. fi
  672. fi
  673. if [ ! -f /etc/skel/.powerline.bash ]; then
  674. cp /home/${MY_USERNAME}/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline /etc/skel/.powerline.bash
  675. else
  676. HASH1=$(sha256sum /home/${MY_USERNAME}/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline | awk -F ' ' '{print $1}')
  677. HASH2=$(sha256sum /etc/skel/.powerline.bash | awk -F ' ' '{print $1}')
  678. if [[ "$HASH1" != "$HASH2" ]]; then
  679. cp /home/${MY_USERNAME}/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline /etc/skel/.powerline.bash
  680. fi
  681. fi
  682. fi
  683. fi
  684. if ! grep -q "powerline" ~/.bashrc; then
  685. if [ -f ~/.powerline.bash ]; then
  686. echo 'source ~/.powerline.bash' >> ~/.bashrc
  687. fi
  688. fi
  689. if ! grep -q "powerline" /etc/skel/.bashrc; then
  690. if [ -f /etc/skel/.powerline.bash ]; then
  691. echo 'source ~/.powerline.bash' >> /etc/skel/.bashrc
  692. fi
  693. fi
  694. }
  695. function congestion_control {
  696. # see /proc/sys/net/ipv4/tcp_congestion_control
  697. if [ ! -f /etc/sysctl.d/10-custom-kernel-bbr.conf ]; then
  698. echo 'net.core.default_qdisc=fq' > /etc/sysctl.d/10-custom-kernel-bbr.conf
  699. echo 'net.ipv4.tcp_congestion_control=bbr' >> /etc/sysctl.d/10-custom-kernel-bbr.conf
  700. sysctl --system
  701. fi
  702. }
  703. function install_shellcheck {
  704. # Used by the tests command
  705. apt-get -yq install shellcheck
  706. }
  707. function setup_utils {
  708. read_config_param "PROJECT_REPO"
  709. write_config_param "PROJECT_REPO" "$PROJECT_REPO"
  710. function_check remove_management_engine_interface
  711. remove_management_engine_interface
  712. function_check support_256_colours
  713. support_256_colours
  714. function_check congestion_control
  715. congestion_control
  716. function_check enable_predictable_device_names
  717. enable_predictable_device_names
  718. function_check turn_off_magic_sysrq
  719. turn_off_magic_sysrq
  720. function_check separate_tmp_filesystem
  721. separate_tmp_filesystem 150
  722. function_check proc_filesystem_settings
  723. proc_filesystem_settings
  724. function_check optimise_filesystem
  725. optimise_filesystem
  726. function_check disable_null_passwords
  727. disable_null_passwords
  728. function_check disable_ctrl_alt_del
  729. disable_ctrl_alt_del
  730. function_check dummy_nologin_command
  731. dummy_nologin_command
  732. function_check disable_core_dumps
  733. disable_core_dumps
  734. function_check remove_serial_logins
  735. remove_serial_logins
  736. function_check set_max_login_tries
  737. set_max_login_tries 10
  738. function_check set_shadow_permissions
  739. set_shadow_permissions
  740. function_check remove_bluetooth
  741. remove_bluetooth
  742. function_check set_login_umask
  743. set_login_umask
  744. function_check disable_deferred_execution
  745. disable_deferred_execution
  746. function_check turn_off_rsys_logging
  747. turn_off_rsys_logging
  748. function_check install_backports_kernel
  749. install_backports_kernel
  750. function_check create_completion_file
  751. create_completion_file
  752. function_check read_configuration
  753. read_configuration
  754. function_check check_system_type
  755. check_system_type
  756. function_check set_default_onion_domains
  757. set_default_onion_domains
  758. function_check locale_setup
  759. locale_setup
  760. function_check parse_args
  761. parse_args
  762. function_check check_domains
  763. check_domains
  764. function_check install_static_network
  765. install_static_network
  766. function_check remove_default_user
  767. remove_default_user
  768. function_check setup_firewall
  769. setup_firewall
  770. function_check create_repo_sources
  771. create_repo_sources
  772. function_check configure_dns
  773. configure_dns
  774. function_check initial_setup
  775. initial_setup
  776. function_check setup_grub
  777. setup_grub
  778. function_check install_tor
  779. install_tor
  780. #function_check resolve_dns_via_tor
  781. #resolve_dns_via_tor
  782. function_check install_command_line_browser
  783. install_command_line_browser
  784. function_check enable_ssh_via_onion
  785. enable_ssh_via_onion
  786. function_check check_date
  787. check_date
  788. function_check install_dynamicdns
  789. install_dynamicdns
  790. function_check randomize_cron
  791. randomize_cron
  792. function_check create_freedns_updater
  793. create_freedns_updater
  794. function_check mark_admin_user_account
  795. mark_admin_user_account
  796. function_check enforce_good_passwords
  797. enforce_good_passwords
  798. function_check change_login_message
  799. change_login_message
  800. function_check enable_zram
  801. enable_zram
  802. function_check random_number_generator
  803. random_number_generator
  804. function_check set_your_domain_name
  805. set_your_domain_name
  806. function_check configure_internet_protocol
  807. configure_internet_protocol
  808. function_check create_git_project
  809. create_git_project
  810. function_check setup_wifi
  811. setup_wifi
  812. function_check configure_ssh
  813. configure_ssh
  814. function_check configure_ssh_onion
  815. configure_ssh_onion
  816. function_check allow_ssh_to_onion_address
  817. allow_ssh_to_onion_address
  818. function_check remove_instructions_from_motd
  819. remove_instructions_from_motd
  820. function_check check_hwrng
  821. check_hwrng
  822. function_check search_for_attached_usb_drive
  823. search_for_attached_usb_drive
  824. function_check regenerate_ssh_keys
  825. regenerate_ssh_keys
  826. function_check create_upgrade_script
  827. create_upgrade_script
  828. function_check letsencrypt_renewals
  829. letsencrypt_renewals
  830. function_check install_watchdog_script
  831. install_watchdog_script
  832. function_check install_avahi
  833. install_avahi
  834. function_check create_avahi_onion_domains
  835. create_avahi_onion_domains
  836. #function_check install_atheros_wifi
  837. #install_atheros_wifi
  838. function_check route_outgoing_traffic_through_tor
  839. route_outgoing_traffic_through_tor
  840. function_check upgrade_golang
  841. upgrade_golang
  842. function_check install_tomb
  843. install_tomb
  844. function_check admin_user_sudo
  845. admin_user_sudo
  846. function_check limit_user_logins
  847. limit_user_logins
  848. function_check schedule_stig_tests
  849. schedule_stig_tests
  850. function_check create_usb_canary
  851. create_usb_canary
  852. function_check setup_powerline
  853. setup_powerline
  854. function_check install_shellcheck
  855. install_shellcheck
  856. }
  857. function setup_email {
  858. function_check create_completion_file
  859. create_completion_file
  860. function_check install_email
  861. install_email
  862. function_check create_procmail
  863. create_procmail
  864. function_check handle_admin_emails
  865. handle_admin_emails
  866. function_check spam_filtering
  867. spam_filtering
  868. function_check configure_imap
  869. configure_imap
  870. #function_check configure_imap_client_certs
  871. #configure_imap_client_certs
  872. function_check configure_gpg
  873. configure_gpg
  874. function_check refresh_gpg_keys
  875. refresh_gpg_keys
  876. function_check configure_backup_key
  877. configure_backup_key
  878. #function_check install_monkeysphere
  879. #install_monkeysphere
  880. function_check email_client
  881. email_client
  882. function_check encrypt_incoming_email
  883. encrypt_incoming_email
  884. function_check encrypt_outgoing_email
  885. encrypt_outgoing_email
  886. function_check email_archiving
  887. email_archiving
  888. function_check email_from_address
  889. email_from_address
  890. function_check create_public_mailing_list
  891. #create_public_mailing_list
  892. #function check create_private_mailing_list
  893. #create_private_mailing_list
  894. function_check encrypt_all_email
  895. encrypt_all_email
  896. function_check import_email
  897. import_email
  898. }
  899. function setup_web {
  900. function_check create_completion_file
  901. create_completion_file
  902. function_check install_web_server
  903. install_web_server
  904. function_check install_web_server_access_control
  905. install_web_server_access_control
  906. function_check install_web_local_user_interface
  907. install_web_local_user_interface
  908. }
  909. function upgrade_apps {
  910. function_check create_completion_file
  911. create_completion_file
  912. APPS_COMPLETED=()
  913. FILES="/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*"
  914. # for all the app scripts
  915. for filename in $FILES
  916. do
  917. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  918. if ! item_in_array "${app_name}" "${APPS_COMPLETED[@]}"; then
  919. function_check app_is_installed
  920. if [[ "$(app_is_installed "$app_name")" == "1" ]]; then
  921. echo ''
  922. echo ''
  923. echo $"Upgrading $app_name"
  924. app_load_variables "${app_name}"
  925. APPS_COMPLETED+=("${app_name}")
  926. function_check "upgrade_${app_name}"
  927. "upgrade_${app_name}"
  928. fi
  929. fi
  930. done
  931. }
  932. function setup_apps {
  933. is_interactive=$1
  934. function_check create_completion_file
  935. create_completion_file
  936. function_check detect_installable_apps
  937. detect_installable_apps
  938. function_check choose_apps_for_variant
  939. choose_apps_for_variant "$SYSTEM_TYPE"
  940. echo $"System variant: $SYSTEM_TYPE"
  941. #echo $'The following apps have been selected'
  942. #echo ''
  943. #function_check list_chosen_apps
  944. #list_chosen_apps
  945. #echo ''
  946. function_check upgrade_apps
  947. upgrade_apps
  948. if [[ $is_interactive == "noninteractive" || $is_interactive == "headless" ]]; then
  949. function_check install_apps
  950. install_apps
  951. if [ ! "$APP_INSTALLED_SUCCESS" ]; then
  952. echo $'One or more apps failed to install'
  953. fi
  954. fi
  955. }
  956. function combine_all_scripts {
  957. combined_filename=$1
  958. # initial variables
  959. cp "$PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars" "$combined_filename"
  960. # utilities
  961. UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
  962. for f in $UTILS_FILES
  963. do
  964. # this removes the first line, which is #!/bin/bash
  965. tail -n +2 "$f" >> "$combined_filename"
  966. done
  967. # base system
  968. BASE_SYSTEM_FILES="/usr/share/${PROJECT_NAME}/base/${PROJECT_NAME}-base-*"
  969. for f in $BASE_SYSTEM_FILES
  970. do
  971. tail -n +2 "$f" >> "$combined_filename"
  972. done
  973. # apps
  974. APP_FILES="/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*"
  975. for f in $APP_FILES
  976. do
  977. tail -n +2 "$f" >> "$combined_filename"
  978. done
  979. }
  980. function check_ram_availability {
  981. minimum_ram_MB="$1"
  982. minimum_ram_bytes=$((minimum_ram_MB * 1024))
  983. ram_available=$(grep MemTotal /proc/meminfo | awk '{print $2}')
  984. if [ "$ram_available" -lt "$minimum_ram_bytes" ]; then
  985. echo $"Need at least ${minimum_ram_MB}MB RAM to install this app"
  986. exit 783524
  987. fi
  988. }
  989. # NOTE: deliberately no exit 0