freedombone-utils-setup 31KB

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