freedombone-utils-setup 30KB

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