freedombone-utils-setup 30KB

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