freedombone-utils-setup 30KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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 -q "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 -q 'blacklist bnep' /etc/modprobe.d/bluetooth.conf; then
  129. echo 'blacklist bnep' >> /etc/modprobe.d/bluetooth.conf
  130. fi
  131. if ! grep -q 'blacklist btusb' /etc/modprobe.d/bluetooth.conf; then
  132. echo 'blacklist btusb' >> /etc/modprobe.d/bluetooth.conf
  133. fi
  134. if ! grep -q '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 lvm2
  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 remove_management_engine_interface {
  341. # see https://www.kernel.org/doc/Documentation/misc-devices/mei/mei.txt
  342. # Disabling this interface doesn't cure the problems of ME, but it
  343. # might stop an adversary in control of AMT from using the command
  344. # interface to control the operating system.
  345. if [ -f /dev/mei0 ]; then
  346. rmmod mei_me
  347. rmmod mei0
  348. fi
  349. blacklist_changed=
  350. if [ ! -f /etc/modprobe.d/blacklist.conf ]; then
  351. touch /etc/modprobe.d/blacklist.conf
  352. blacklist_changed=1
  353. fi
  354. if ! grep -q "blacklist mei" /etc/modprobe.d/blacklist.conf; then
  355. echo "blacklist mei" >> /etc/modprobe.d/blacklist.conf
  356. blacklist_changed=1
  357. fi
  358. if ! grep -q "blacklist mei_me" /etc/modprobe.d/blacklist.conf; then
  359. echo "blacklist mei_me" >> /etc/modprobe.d/blacklist.conf
  360. blacklist_changed=1
  361. fi
  362. if [ $blacklist_changed ]; then
  363. depmod -ae -E
  364. update-initramfs -u
  365. fi
  366. }
  367. function set_login_umask {
  368. sed -i 's|UMASK\t.*|UMASK\t\t077|g' /etc/login.defs
  369. }
  370. function disable_deferred_execution {
  371. systemctl stop atd
  372. systemctl disable atd
  373. }
  374. function set_shadow_permissions {
  375. chown root:root /etc/shadow
  376. chmod 0000 /etc/shadow
  377. chown root:root /etc/gshadow
  378. chmod 0000 /etc/gshadow
  379. }
  380. function set_max_login_tries {
  381. max_tries=$1
  382. if ! grep -q ' deny=' /etc/pam.d/common-auth; then
  383. 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
  384. else
  385. sed -i "s| deny=.*| deny=$max_tries|g" /etc/pam.d/common-auth
  386. fi
  387. if ! grep -q ' deny=' /etc/pam.d/common-account; then
  388. sed -i '/pam_deny.so/a account required\t\t\tpam_tally.so' /etc/pam.d/common-account
  389. else
  390. sed -i "s| deny=.*| deny=$max_tries|g" /etc/pam.d/common-account
  391. fi
  392. }
  393. function limit_user_logins {
  394. # overall max logins
  395. if ! grep -q '* hard maxsyslogins' /etc/security/limits.conf; then
  396. echo '* hard maxsyslogins 10' >> /etc/security/limits.conf
  397. else
  398. sed -i 's|hard maxsyslogins.*|hard maxsyslogins 10|g' /etc/security/limits.conf
  399. fi
  400. # Max logins for each user
  401. if ! grep -q '* hard maxlogins' /etc/security/limits.conf; then
  402. echo '* hard maxlogins 2' >> /etc/security/limits.conf
  403. else
  404. sed -i 's|hard maxlogins.*|hard maxlogins 2|g' /etc/security/limits.conf
  405. fi
  406. }
  407. function remove_serial_logins {
  408. if grep -q 'ttyS' /etc/securetty; then
  409. cp /etc/securetty /etc/securetty_old
  410. sed -i '/ttyS/d' /etc/securetty
  411. fi
  412. }
  413. function set_sticky_bits {
  414. world_writable=$(find / -xdev -type d -perm -002 \! -perm -1000)
  415. for w in $world_writable; do
  416. echo "Setting sticky bit on $w"
  417. chmod +t $w
  418. done
  419. }
  420. function disable_ctrl_alt_del {
  421. ln -sf /dev/null /etc/systemd/system/ctrl-alt-del.target
  422. }
  423. function lockdown_permissions {
  424. if [ -d /root/.npm ]; then
  425. find /root/.npm -name package.json -exec chmod 700 {} \;
  426. fi
  427. # All commands owned by root
  428. if [ -d /root/.cache/yarn ]; then
  429. rm -rf /root/.cache/yarn
  430. fi
  431. if [ -d /usr/local/share/.cache/yarn ]; then
  432. rm -rf /usr/local/share/.cache/yarn
  433. fi
  434. if [ -f /usr/lib/ssl/certs/ssl-cert-snakeoil.pem ]; then
  435. chown root:root /usr/lib/ssl/certs/ssl-cert-snake*
  436. fi
  437. if [ -d /bin ]; then
  438. chown root:root /bin/*
  439. fi
  440. if [ -d /usr/bin ]; then
  441. chown root:root /usr/bin/*
  442. fi
  443. if [ -d /usr/local/bin ]; then
  444. chown root:root /usr/local/bin/*
  445. fi
  446. if [ -d /sbin ]; then
  447. chown root:root /sbin/*
  448. fi
  449. if [ -d /usr/sbin ]; then
  450. chown root:root /usr/sbin/*
  451. fi
  452. if [ -d /usr/local/sbin ]; then
  453. chown root:root /usr/local/sbin/*
  454. fi
  455. if [ -d /usr/share/${PROJECT_NAME} ]; then
  456. chown -R root:root /usr/share/${PROJECT_NAME}
  457. chmod -R +r /usr/share/${PROJECT_NAME}
  458. fi
  459. # All libraries owned by root
  460. if [ -d /lib ]; then
  461. chown -R root:root /lib/*
  462. fi
  463. if [ -d /lib64 ]; then
  464. chown -R root:root /lib64/*
  465. fi
  466. if [ -d /usr/lib ]; then
  467. chown -R root:root /usr/lib/*
  468. fi
  469. if [ -d /usr/lib64 ]; then
  470. chown -R root:root /usr/lib64/*
  471. fi
  472. # sudo permissions
  473. chmod 4755 /usr/bin/sudo
  474. chmod 4755 /usr/lib/sudo/sudoers.so
  475. chown root:root /etc/sudoers
  476. # permissions on email commands
  477. if [ -f /usr/bin/procmail ]; then
  478. chmod 6755 /usr/bin/procmail
  479. fi
  480. if [ -f /usr/sbin/exim ]; then
  481. chmod u+s /usr/sbin/exim
  482. fi
  483. if [ -f /usr/sbin/exim4 ]; then
  484. chmod u+s /usr/sbin/exim4
  485. fi
  486. set_sticky_bits
  487. # Create some directories to correspond with users in passwords file
  488. if [ ! -d /var/spool/lpd ]; then
  489. mkdir /var/spool/lpd
  490. fi
  491. if [ ! -d /var/spool/news ]; then
  492. mkdir /var/spool/news
  493. fi
  494. if [ ! -d /var/spool/uucp ]; then
  495. mkdir /var/spool/uucp
  496. fi
  497. if [ ! -d /var/list ]; then
  498. mkdir /var/list
  499. fi
  500. if [ ! -d /var/lib/gnats ]; then
  501. mkdir /var/lib/gnats
  502. fi
  503. if [ ! -d /var/lib/saned ]; then
  504. mkdir /var/lib/saned
  505. fi
  506. if [ -d /etc/prosody ]; then
  507. chown -R prosody /etc/prosody
  508. chmod -R 700 /etc/prosody/conf.d
  509. fi
  510. if [ -d /var/lib/prosody ]; then
  511. chown -R prosody /var/lib/prosody
  512. fi
  513. if [ -d /etc/letsencrypt ]; then
  514. chmod -R 600 /etc/letsencrypt
  515. chmod -R g=rX /etc/letsencrypt
  516. fi
  517. }
  518. function disable_core_dumps {
  519. if ! grep -q '* hard core 0' /etc/security/limits.conf; then
  520. echo '* hard core 0' >> /etc/security/limits.conf
  521. else
  522. sed -i 's|hard core.*|hard core 0|g' /etc/security/limits.conf
  523. fi
  524. }
  525. function dummy_nologin_command {
  526. if [ ! -f /sbin/nologin ]; then
  527. echo '#!/bin/bash' > /sbin/nologin
  528. chmod +x /sbin/nologin
  529. fi
  530. }
  531. function disable_null_passwords {
  532. sed -i 's| nullok_secure||g' /etc/pam.d/common-auth
  533. }
  534. function create_usb_canary {
  535. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  536. return
  537. fi
  538. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  539. return
  540. fi
  541. echo "ACTION==\"add\", KERNEL==\"sd*[!0-9]\", RUN+=\"/usr/local/bin/${PROJECT_NAME}-usb-canary\"" > /etc/udev/rules.d/00-usb-canary.rules
  542. udevadm control --reload-rules
  543. mark_completed $FUNCNAME
  544. }
  545. function setup_firewall {
  546. function_check create_completion_file
  547. create_completion_file
  548. function_check configure_firewall
  549. configure_firewall
  550. function_check configure_firewall_ping
  551. configure_firewall_ping
  552. function_check firewall_drop_telnet
  553. firewall_drop_telnet
  554. function_check firewall_drop_spoofed_packets
  555. firewall_drop_spoofed_packets
  556. function_check firewall_rate_limits
  557. firewall_rate_limits
  558. function_check configure_firewall_for_dns
  559. configure_firewall_for_dns
  560. function_check configure_firewall_for_avahi
  561. configure_firewall_for_avahi
  562. function_check global_rate_limit
  563. global_rate_limit
  564. function_check firewall_block_bad_ip_ranges
  565. firewall_block_bad_ip_ranges
  566. }
  567. function setup_powerline {
  568. if [ -f ~/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline ]; then
  569. cp ~/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline ~/.powerline.bash
  570. cp ~/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline /etc/skel/.powerline.bash
  571. else
  572. if [ -f /home/${MY_USERNAME}/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline ]; then
  573. cp /home/${MY_USERNAME}/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline ~/.powerline.bash
  574. cp /home/${MY_USERNAME}/${PROJECT_NAME}/src/${PROJECT_NAME}-powerline /etc/skel/.powerline.bash
  575. fi
  576. fi
  577. if ! grep -q "powerline" ~/.bashrc; then
  578. if [ -f ~/.powerline.bash ]; then
  579. echo 'source ~/.powerline.bash' >> ~/.bashrc
  580. fi
  581. fi
  582. if ! grep -q "powerline" /etc/skel/.bashrc; then
  583. if [ -f /etc/skel/.powerline.bash ]; then
  584. echo 'source ~/.powerline.bash' >> /etc/skel/.bashrc
  585. fi
  586. fi
  587. }
  588. function setup_utils {
  589. read_config_param "PROJECT_REPO"
  590. write_config_param "PROJECT_REPO" "$PROJECT_REPO"
  591. function_check remove_management_engine_interface
  592. remove_management_engine_interface
  593. function_check enable_predictable_device_names
  594. enable_predictable_device_names
  595. function_check separate_tmp_filesystem
  596. separate_tmp_filesystem 150
  597. function_check proc_filesystem_settings
  598. proc_filesystem_settings
  599. function_check optimise_filesystem
  600. optimise_filesystem
  601. function_check disable_null_passwords
  602. disable_null_passwords
  603. function_check disable_ctrl_alt_del
  604. disable_ctrl_alt_del
  605. function_check dummy_nologin_command
  606. dummy_nologin_command
  607. function_check disable_core_dumps
  608. disable_core_dumps
  609. function_check remove_serial_logins
  610. remove_serial_logins
  611. function_check set_max_login_tries
  612. set_max_login_tries 10
  613. function_check set_shadow_permissions
  614. set_shadow_permissions
  615. function_check remove_bluetooth
  616. remove_bluetooth
  617. function_check set_login_umask
  618. set_login_umask
  619. function_check disable_deferred_execution
  620. disable_deferred_execution
  621. function_check turn_off_rsys_logging
  622. turn_off_rsys_logging
  623. function_check install_backports_kernel
  624. install_backports_kernel
  625. function_check create_completion_file
  626. create_completion_file
  627. function_check read_configuration
  628. read_configuration
  629. function_check check_system_type
  630. check_system_type
  631. function_check set_default_onion_domains
  632. set_default_onion_domains
  633. function_check locale_setup
  634. locale_setup
  635. function_check parse_args
  636. parse_args
  637. function_check check_domains
  638. check_domains
  639. function_check install_static_network
  640. install_static_network
  641. function_check remove_default_user
  642. remove_default_user
  643. function_check setup_firewall
  644. setup_firewall
  645. function_check create_repo_sources
  646. create_repo_sources
  647. function_check configure_dns
  648. configure_dns
  649. function_check initial_setup
  650. initial_setup
  651. function_check setup_grub
  652. setup_grub
  653. function_check install_tor
  654. install_tor
  655. #function_check resolve_dns_via_tor
  656. #resolve_dns_via_tor
  657. function_check install_command_line_browser
  658. install_command_line_browser
  659. function_check enable_ssh_via_onion
  660. enable_ssh_via_onion
  661. function_check check_date
  662. check_date
  663. function_check install_dynamicdns
  664. install_dynamicdns
  665. function_check randomize_cron
  666. randomize_cron
  667. function_check create_freedns_updater
  668. create_freedns_updater
  669. function_check mark_admin_user_account
  670. mark_admin_user_account
  671. function_check enforce_good_passwords
  672. enforce_good_passwords
  673. function_check change_login_message
  674. change_login_message
  675. function_check enable_zram
  676. enable_zram
  677. function_check random_number_generator
  678. random_number_generator
  679. function_check set_your_domain_name
  680. set_your_domain_name
  681. function_check configure_internet_protocol
  682. configure_internet_protocol
  683. function_check create_git_project
  684. create_git_project
  685. function_check setup_wifi
  686. setup_wifi
  687. function_check configure_ssh
  688. configure_ssh
  689. function_check configure_ssh_onion
  690. configure_ssh_onion
  691. function_check allow_ssh_to_onion_address
  692. allow_ssh_to_onion_address
  693. function_check remove_instructions_from_motd
  694. remove_instructions_from_motd
  695. function_check check_hwrng
  696. check_hwrng
  697. function_check search_for_attached_usb_drive
  698. search_for_attached_usb_drive
  699. function_check regenerate_ssh_keys
  700. regenerate_ssh_keys
  701. function_check create_mirrors
  702. create_mirrors
  703. function_check create_upgrade_script
  704. create_upgrade_script
  705. function_check letsencrypt_renewals
  706. letsencrypt_renewals
  707. function_check install_watchdog_script
  708. install_watchdog_script
  709. function_check install_avahi
  710. install_avahi
  711. function_check create_avahi_onion_domains
  712. create_avahi_onion_domains
  713. #function_check install_atheros_wifi
  714. #install_atheros_wifi
  715. function_check route_outgoing_traffic_through_tor
  716. route_outgoing_traffic_through_tor
  717. function_check upgrade_golang
  718. upgrade_golang
  719. function_check install_tomb
  720. install_tomb
  721. function_check admin_user_sudo
  722. admin_user_sudo
  723. function_check limit_user_logins
  724. limit_user_logins
  725. function_check schedule_stig_tests
  726. schedule_stig_tests
  727. function_check create_usb_canary
  728. create_usb_canary
  729. function_check setup_powerline
  730. setup_powerline
  731. }
  732. function setup_email {
  733. function_check create_completion_file
  734. create_completion_file
  735. function_check install_email
  736. install_email
  737. function_check create_procmail
  738. create_procmail
  739. function_check handle_admin_emails
  740. handle_admin_emails
  741. function_check spam_filtering
  742. spam_filtering
  743. function_check configure_imap
  744. configure_imap
  745. #function_check configure_imap_client_certs
  746. #configure_imap_client_certs
  747. function_check configure_gpg
  748. configure_gpg
  749. function_check refresh_gpg_keys
  750. refresh_gpg_keys
  751. function_check configure_backup_key
  752. configure_backup_key
  753. #function_check install_monkeysphere
  754. #install_monkeysphere
  755. function_check email_client
  756. email_client
  757. function_check encrypt_incoming_email
  758. encrypt_incoming_email
  759. function_check encrypt_outgoing_email
  760. encrypt_outgoing_email
  761. function_check email_archiving
  762. email_archiving
  763. function_check email_from_address
  764. email_from_address
  765. function_check create_public_mailing_list
  766. #create_public_mailing_list
  767. #function check create_private_mailing_list
  768. #create_private_mailing_list
  769. function_check encrypt_all_email
  770. encrypt_all_email
  771. function_check import_email
  772. import_email
  773. }
  774. function setup_web {
  775. function_check create_completion_file
  776. create_completion_file
  777. function_check install_web_server
  778. install_web_server
  779. function_check install_web_server_access_control
  780. install_web_server_access_control
  781. }
  782. function upgrade_apps {
  783. function_check create_completion_file
  784. create_completion_file
  785. APPS_COMPLETED=()
  786. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  787. # for all the app scripts
  788. for filename in $FILES
  789. do
  790. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  791. item_in_array "${app_name}" "${APPS_COMPLETED[@]}"
  792. if [[ $? != 0 ]]; then
  793. function_check app_is_installed
  794. if [[ "$(app_is_installed $app_name)" == "1" ]]; then
  795. echo ''
  796. echo ''
  797. echo $"Upgrading $app_name"
  798. app_load_variables ${app_name}
  799. APPS_COMPLETED+=("${app_name}")
  800. function_check upgrade_${app_name}
  801. upgrade_${app_name}
  802. fi
  803. fi
  804. done
  805. }
  806. function setup_apps {
  807. is_interactive=$1
  808. function_check create_completion_file
  809. create_completion_file
  810. function_check detect_installable_apps
  811. detect_installable_apps
  812. function_check choose_apps_for_variant
  813. choose_apps_for_variant "$SYSTEM_TYPE"
  814. echo $"System variant: $SYSTEM_TYPE"
  815. #echo $'The following apps have been selected'
  816. #echo ''
  817. #function_check list_chosen_apps
  818. #list_chosen_apps
  819. #echo ''
  820. function_check upgrade_apps
  821. upgrade_apps
  822. if [[ $is_interactive == "noninteractive" || $is_interactive == "headless" ]]; then
  823. function_check install_apps
  824. install_apps
  825. if [ ! $APP_INSTALLED_SUCCESS ]; then
  826. echo $'One or more apps failed to install'
  827. fi
  828. fi
  829. }
  830. function combine_all_scripts {
  831. combined_filename=$1
  832. # initial variables
  833. cp $PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars $combined_filename
  834. # utilities
  835. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  836. for f in $UTILS_FILES
  837. do
  838. # this removes the first line, which is #!/bin/bash
  839. tail -n +2 "$f" >> $combined_filename
  840. done
  841. # base system
  842. BASE_SYSTEM_FILES=/usr/share/${PROJECT_NAME}/base/${PROJECT_NAME}-base-*
  843. for f in $BASE_SYSTEM_FILES
  844. do
  845. tail -n +2 "$f" >> $combined_filename
  846. done
  847. # apps
  848. APP_FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  849. for f in $APP_FILES
  850. do
  851. tail -n +2 "$f" >> $combined_filename
  852. done
  853. }
  854. function check_ram_availability {
  855. minimum_ram_MB="$1"
  856. minimum_ram_bytes=$((minimum_ram_MB * 1024))
  857. ram_available=$(grep MemTotal /proc/meminfo | awk '{print $2}')
  858. if [ $ram_available -lt $minimum_ram_bytes ]; then
  859. echo $"Need at least ${minimum_ram_gb}MB RAM to install this app"
  860. exit 783524
  861. fi
  862. }
  863. # NOTE: deliberately no exit 0