freedombone-utils-setup 29KB

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