freedombone-utils-setup 35KB

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