freedombone-utils-setup 29KB

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