freedombone-utils-setup 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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 -t jessie-backports
  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 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 cryptsetup 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. }
  503. function disable_core_dumps {
  504. if ! grep '* hard core 0' /etc/security/limits.conf; then
  505. echo '* hard core 0' >> /etc/security/limits.conf
  506. else
  507. sed -i 's|hard core.*|hard core 0|g' /etc/security/limits.conf
  508. fi
  509. }
  510. function dummy_nologin_command {
  511. if [ ! -f /sbin/nologin ]; then
  512. echo '#!/bin/bash' > /sbin/nologin
  513. chmod +x /sbin/nologin
  514. fi
  515. }
  516. function disable_null_passwords {
  517. sed -i 's| nullok_secure||g' /etc/pam.d/common-auth
  518. }
  519. function create_usb_canary {
  520. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  521. return
  522. fi
  523. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  524. return
  525. fi
  526. echo "ACTION==\"add\", KERNEL==\"sd*[!0-9]\", RUN+=\"/usr/local/bin/${PROJECT_NAME}-usb-canary\"" > /etc/udev/rules.d/00-usb-canary.rules
  527. udevadm control --reload-rules
  528. mark_completed $FUNCNAME
  529. }
  530. function setup_firewall {
  531. function_check create_completion_file
  532. create_completion_file
  533. function_check configure_firewall
  534. configure_firewall
  535. function_check configure_firewall_ping
  536. configure_firewall_ping
  537. function_check firewall_drop_telnet
  538. firewall_drop_telnet
  539. function_check firewall_drop_spoofed_packets
  540. firewall_drop_spoofed_packets
  541. function_check firewall_rate_limits
  542. firewall_rate_limits
  543. function_check configure_firewall_for_dns
  544. configure_firewall_for_dns
  545. function_check configure_firewall_for_avahi
  546. configure_firewall_for_avahi
  547. function_check global_rate_limit
  548. global_rate_limit
  549. function_check firewall_block_bad_ip_ranges
  550. firewall_block_bad_ip_ranges
  551. }
  552. function setup_utils {
  553. read_config_param "PROJECT_REPO"
  554. write_config_param "PROJECT_REPO" "$PROJECT_REPO"
  555. function_check remove_management_engine_interface
  556. remove_management_engine_interface
  557. function_check separate_tmp_filesystem
  558. separate_tmp_filesystem 150
  559. function_check disable_null_passwords
  560. disable_null_passwords
  561. function_check disable_ctrl_alt_del
  562. disable_ctrl_alt_del
  563. function_check dummy_nologin_command
  564. dummy_nologin_command
  565. function_check disable_core_dumps
  566. disable_core_dumps
  567. function_check remove_serial_logins
  568. remove_serial_logins
  569. function_check set_max_login_tries
  570. set_max_login_tries 10
  571. function_check set_shadow_permissions
  572. set_shadow_permissions
  573. function_check remove_bluetooth
  574. remove_bluetooth
  575. function_check disable_nfs_insecure_locks
  576. disable_nfs_insecure_locks
  577. function_check set_login_umask
  578. set_login_umask
  579. function_check disable_deferred_execution
  580. disable_deferred_execution
  581. function_check turn_off_rsys_logging
  582. turn_off_rsys_logging
  583. function_check install_backports_kernel
  584. install_backports_kernel
  585. function_check create_completion_file
  586. create_completion_file
  587. function_check read_configuration
  588. read_configuration
  589. function_check check_system_type
  590. check_system_type
  591. function_check set_default_onion_domains
  592. set_default_onion_domains
  593. function_check locale_setup
  594. locale_setup
  595. function_check parse_args
  596. parse_args
  597. function_check check_domains
  598. check_domains
  599. function_check install_static_network
  600. install_static_network
  601. function_check remove_default_user
  602. remove_default_user
  603. function_check setup_firewall
  604. setup_firewall
  605. function_check create_repo_sources
  606. create_repo_sources
  607. function_check configure_dns
  608. configure_dns
  609. function_check initial_setup
  610. initial_setup
  611. function_check install_tor
  612. install_tor
  613. #function_check resolve_dns_via_tor
  614. #resolve_dns_via_tor
  615. function_check install_command_line_browser
  616. install_command_line_browser
  617. function_check enable_ssh_via_onion
  618. enable_ssh_via_onion
  619. function_check check_date
  620. check_date
  621. function_check install_dynamicdns
  622. install_dynamicdns
  623. function_check randomize_cron
  624. randomize_cron
  625. function_check create_freedns_updater
  626. create_freedns_updater
  627. function_check mark_admin_user_account
  628. mark_admin_user_account
  629. function_check enforce_good_passwords
  630. enforce_good_passwords
  631. function_check change_login_message
  632. change_login_message
  633. function_check enable_zram
  634. enable_zram
  635. function_check random_number_generator
  636. random_number_generator
  637. function_check set_your_domain_name
  638. set_your_domain_name
  639. function_check configure_internet_protocol
  640. configure_internet_protocol
  641. function_check create_git_project
  642. create_git_project
  643. function_check setup_wifi
  644. setup_wifi
  645. function_check configure_ssh
  646. configure_ssh
  647. function_check configure_ssh_onion
  648. configure_ssh_onion
  649. function_check allow_ssh_to_onion_address
  650. allow_ssh_to_onion_address
  651. function_check remove_instructions_from_motd
  652. remove_instructions_from_motd
  653. function_check check_hwrng
  654. check_hwrng
  655. function_check search_for_attached_usb_drive
  656. search_for_attached_usb_drive
  657. function_check regenerate_ssh_keys
  658. regenerate_ssh_keys
  659. function_check create_mirrors
  660. create_mirrors
  661. function_check create_upgrade_script
  662. create_upgrade_script
  663. function_check letsencrypt_renewals
  664. letsencrypt_renewals
  665. function_check install_watchdog_script
  666. install_watchdog_script
  667. function_check install_avahi
  668. install_avahi
  669. function_check create_avahi_onion_domains
  670. create_avahi_onion_domains
  671. #function_check install_atheros_wifi
  672. #install_atheros_wifi
  673. function_check route_outgoing_traffic_through_tor
  674. route_outgoing_traffic_through_tor
  675. function_check upgrade_golang
  676. upgrade_golang
  677. function_check install_tomb
  678. install_tomb
  679. function_check admin_user_sudo
  680. admin_user_sudo
  681. function_check limit_user_logins
  682. limit_user_logins
  683. function_check schedule_stig_tests
  684. schedule_stig_tests
  685. function_check create_usb_canary
  686. create_usb_canary
  687. }
  688. function setup_email {
  689. function_check create_completion_file
  690. create_completion_file
  691. function_check install_email
  692. install_email
  693. function_check create_procmail
  694. create_procmail
  695. function_check handle_admin_emails
  696. handle_admin_emails
  697. function_check spam_filtering
  698. spam_filtering
  699. function_check configure_imap
  700. configure_imap
  701. #function_check configure_imap_client_certs
  702. #configure_imap_client_certs
  703. function_check configure_gpg
  704. configure_gpg
  705. function_check refresh_gpg_keys
  706. refresh_gpg_keys
  707. function_check configure_backup_key
  708. configure_backup_key
  709. function_check install_monkeysphere
  710. install_monkeysphere
  711. function_check encrypt_incoming_email
  712. encrypt_incoming_email
  713. function_check encrypt_outgoing_email
  714. encrypt_outgoing_email
  715. function_check email_client
  716. email_client
  717. function_check email_archiving
  718. email_archiving
  719. function_check email_from_address
  720. email_from_address
  721. function_check create_public_mailing_list
  722. #create_public_mailing_list
  723. #function check create_private_mailing_list
  724. #create_private_mailing_list
  725. function_check encrypt_all_email
  726. encrypt_all_email
  727. function_check import_email
  728. import_email
  729. }
  730. function setup_web {
  731. function_check create_completion_file
  732. create_completion_file
  733. function_check install_web_server
  734. install_web_server
  735. function_check install_web_server_access_control
  736. install_web_server_access_control
  737. }
  738. function upgrade_apps {
  739. function_check create_completion_file
  740. create_completion_file
  741. APPS_COMPLETED=()
  742. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  743. # for all the app scripts
  744. for filename in $FILES
  745. do
  746. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  747. item_in_array "${app_name}" "${APPS_COMPLETED[@]}"
  748. if [[ $? != 0 ]]; then
  749. function_check app_is_installed
  750. if [[ "$(app_is_installed $app_name)" == "1" ]]; then
  751. echo ''
  752. echo ''
  753. echo $"Upgrading $app_name"
  754. app_load_variables ${app_name}
  755. APPS_COMPLETED+=("${app_name}")
  756. function_check upgrade_${app_name}
  757. upgrade_${app_name}
  758. fi
  759. fi
  760. done
  761. }
  762. function setup_apps {
  763. is_interactive=$1
  764. function_check create_completion_file
  765. create_completion_file
  766. function_check detect_installable_apps
  767. detect_installable_apps
  768. function_check choose_apps_for_variant
  769. choose_apps_for_variant "$SYSTEM_TYPE"
  770. echo $"System variant: $SYSTEM_TYPE"
  771. echo $'The following apps have been selected'
  772. echo ''
  773. function_check list_chosen_apps
  774. list_chosen_apps
  775. echo ''
  776. function_check upgrade_apps
  777. upgrade_apps
  778. if [[ $is_interactive == "noninteractive" || $is_interactive == "headless" ]]; then
  779. function_check install_apps
  780. install_apps
  781. if [ ! $APP_INSTALLED_SUCCESS ]; then
  782. echo $'One or more apps failed to install'
  783. fi
  784. fi
  785. }
  786. function combine_all_scripts {
  787. combined_filename=$1
  788. # initial variables
  789. cp $PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars $combined_filename
  790. # utilities
  791. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  792. for f in $UTILS_FILES
  793. do
  794. # this removes the first line, which is #!/bin/bash
  795. tail -n +2 "$f" >> $combined_filename
  796. done
  797. # base system
  798. BASE_SYSTEM_FILES=/usr/share/${PROJECT_NAME}/base/${PROJECT_NAME}-base-*
  799. for f in $BASE_SYSTEM_FILES
  800. do
  801. tail -n +2 "$f" >> $combined_filename
  802. done
  803. # apps
  804. APP_FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  805. for f in $APP_FILES
  806. do
  807. tail -n +2 "$f" >> $combined_filename
  808. done
  809. }
  810. # NOTE: deliberately no exit 0