freedombone-utils-setup 29KB

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