freedombone-utils-setup 33KB

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