freedombone-utils-setup 33KB

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