install-freedombone.sh 54KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. #!/bin/bash
  2. # Freedombone install script intended for use with Debian Jessie
  3. #
  4. # Note on dynamic dns
  5. # ===================
  6. #
  7. # I'm not particularly trying to promote freedns.afraid.org
  8. # as a service, it just happens to be a dynamic DNS system which
  9. # provides free (as in beer) accounts, and I'm trying to make the
  10. # process of setting up a working server as trivial as possible.
  11. # Other dynamic DNS systems are available, and if you're using
  12. # something different then comment out the section within
  13. # argument_checks and the call to dynamic_dns_freedns.
  14. #
  15. # Prerequisites
  16. # =============
  17. #
  18. # cd ~/
  19. # wget http://freedombone.uk.to/debian-jessie-console-armhf-2014-08-13.tar.xz
  20. #
  21. # Verify it.
  22. #
  23. # sha256sum debian-jessie-console-armhf-2014-08-13.tar.xz
  24. # fc225cfb3c2dfad92cccafa97e92c3cd3db9d94f4771af8da364ef59609f43de
  25. #
  26. # Uncompress it.
  27. #
  28. # tar xJf debian-jessie-console-armhf-2014-08-13.tar.xz
  29. # cd debian-jessie-console-armhf-2014-08-13
  30. #
  31. # sudo apt-get install u-boot-tools dosfstools git-core kpartx wget parted
  32. # sudo ./setup_sdcard.sh --mmc /dev/sdX --dtb beaglebone
  33. #
  34. # When finished eject the micrtoSD then reinsert it
  35. #
  36. # sudo cp /media/$USER/BOOT/bbb-uEnv.txt /media/$USER/BOOT/uEnv.txt
  37. # sync
  38. #
  39. # Eject microSD, insert into BBB, attach USB cable between BBB and laptop.
  40. # On Ubuntu wait until you see the "connected" message.
  41. #
  42. # ssh-keygen -f "/home/$USER/.ssh/known_hosts" -R 192.168.7.2
  43. # ssh debian@192.168.7.2 (password "temppwd")
  44. # su (password "root")
  45. # passwd
  46. # adduser $MY_USERNAME
  47. # sed -i '/iface eth0 inet dhcp/a\iface eth0 inet static' /etc/network/interfaces
  48. # sed -i '/iface eth0 inet static/a\ dns-nameservers 213.73.91.35 85.214.20.141' /etc/network/interfaces
  49. # sed -i "/iface eth0 inet static/a\ gateway $MY_ROUTER_IP" /etc/network/interfaces
  50. # sed -i '/iface eth0 inet static/a\ netmask 255.255.255.0' /etc/network/interfaces
  51. # sed -i "/iface eth0 inet static/a\ address $MY_BBB_STATIC_IP" /etc/network/interfaces
  52. # sed -i '/iface usb0 inet static/,/ gateway 192.168.7.1/ s/^/#/' /etc/network/interfaces
  53. # shutdown now
  54. #
  55. # Connect BBB to router
  56. #
  57. # scp install-freedombone.sh $MY_USERNAME@$MY_BBB_STATIC_IP:/home/$MY_USERNAME
  58. # ssh $MY_USERNAME@$MY_BBB_STATIC_IP
  59. # su
  60. # ./install-freedombone.sh [DOMAIN_NAME] [MY_USERNAME]
  61. DOMAIN_NAME=$1
  62. MY_USERNAME=$2
  63. FREEDNS_SUBDOMAIN_CODE=$3
  64. SSH_PORT=2222
  65. KERNEL_VERSION="v3.15.10-bone7"
  66. USE_HWRNG="yes"
  67. INSTALLED_WITHIN_DOCKER="no"
  68. GPG_KEYSERVER="hkp://keys.gnupg.net"
  69. # optionally you can provide your exported GPG key pair here
  70. # Note that the private key file will be deleted after use
  71. # If these are unspecified then a new GPG key will be created
  72. MY_GPG_PUBLIC_KEY=
  73. MY_GPG_PRIVATE_KEY=
  74. # If you have existing mail within a Maildir
  75. # you can specify the directory here and the files
  76. # will be imported
  77. IMPORT_MAILDIR=
  78. # The Debian package repository to use.
  79. DEBIAN_REPO="ftp.de.debian.org"
  80. DEBIAN_VERSION="jessie"
  81. # Directory where source code is downloaded and compiled
  82. INSTALL_DIR=$HOME/build
  83. # device name for an attached usb drive
  84. USB_DRIVE=/dev/sda1
  85. export DEBIAN_FRONTEND=noninteractive
  86. # File which keeps track of what has already been installed
  87. COMPLETION_FILE=$HOME/freedombone-completed.txt
  88. if [ ! -f $COMPLETION_FILE ]; then
  89. touch $COMPLETION_FILE
  90. fi
  91. function argument_checks {
  92. SYNTAX='./install-freedombone.sh [domain] [username] [subdomain code]'
  93. if [ ! -d /home/$MY_USERNAME ]; then
  94. echo "There is no user '$MY_USERNAME' on the system. Use 'adduser $MY_USERNAME' to create the user."
  95. exit 1
  96. fi
  97. if [ ! $DOMAIN_NAME ]; then
  98. echo ''
  99. echo $SYNTAX
  100. echo 'Please specify your domain name'
  101. exit 2
  102. fi
  103. if [ ! $MY_USERNAME ]; then
  104. echo ''
  105. echo $SYNTAX
  106. echo 'Please specify your username'
  107. exit 3
  108. fi
  109. if [ ! $FREEDNS_SUBDOMAIN_CODE ]; then
  110. echo ''
  111. echo $SYNTAX
  112. echo 'Please specify the freedns subdomain code. To find it from '
  113. echo "https://freedns.afraid.org select 'Dynamic DNS', then 'quick "
  114. echo "cron example' and copy the code located between '?' and '=='."
  115. exit 4
  116. fi
  117. }
  118. function change_login_message {
  119. if grep -Fxq "change_login_message" $COMPLETION_FILE; then
  120. return
  121. fi
  122. echo '' > /etc/motd
  123. echo ".---. . . " >> /etc/motd
  124. echo "| | | " >> /etc/motd
  125. echo "|--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-. " >> /etc/motd
  126. echo "| | (.-' (.-' ( | ( )| | | | )( )| | (.-' " >> /etc/motd
  127. echo "' ' --' --' -' - -' ' ' -' -' -' ' - --'" >> /etc/motd
  128. echo '' >> /etc/motd
  129. echo ' Freedom in the Cloud' >> /etc/motd
  130. echo '' >> /etc/motd
  131. echo 'change_login_message' >> $COMPLETION_FILE
  132. }
  133. function search_for_attached_usb_drive {
  134. # If a USB drive is attached then search for email,
  135. # gpg, ssh keys and emacs configuration
  136. if grep -Fxq "search_for_attached_usb_drive" $COMPLETION_FILE; then
  137. return
  138. fi
  139. if [ -d $USB_DRIVE ]; then
  140. if [ ! -d /media/usb ]; then
  141. mount $USB_DRIVE /media/usb
  142. fi
  143. if [ -d /media/usb/Maildir ]; then
  144. echo 'Maildir found on USB drive'
  145. IMPORT_MAILDIR=/media/usb/Maildir
  146. fi
  147. if [ -d /media/usb/.gnupg ]; then
  148. echo 'Importing GPG keyring'
  149. cp -r /media/usb/.gnupg /home/$MY_USERNAME/.gnupg
  150. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg
  151. shred -zu /media/usb/.gnupg/secring.gpg
  152. shred -zu /media/usb/.gnupg/random_seed
  153. shred -zu /media/usb/.gnupg/trustdb.gpg
  154. rm -rf /media/usb/.gnupg
  155. fi
  156. if [ -f /media/usb/private_key.gpg ]; then
  157. echo 'GPG private key found on USB drive'
  158. MY_GPG_PRIVATE_KEY=/media/usb/private_key.gpg
  159. fi
  160. if [ -f /media/usb/public_key.gpg ]; then
  161. echo 'GPG public key found on USB drive'
  162. MY_GPG_PUBLIC_KEY=/media/usb/public_key.gpg
  163. fi
  164. if [ -d /media/usb/.ssh ]; then
  165. echo 'Importing ssh keys'
  166. cp -r /media/usb/.ssh /home/$MY_USERNAME/.ssh
  167. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh
  168. # for security delete the ssh keys from the usb drive
  169. shred -zu /media/usb/.ssh/id_rsa
  170. shred -zu /media/usb/.ssh/id_rsa.pub
  171. shred -zu /media/usb/.ssh/known_hosts
  172. rm -rf /media/usb/.ssh
  173. fi
  174. if [ -f /media/usb/.emacs ]; then
  175. echo 'Importing .emacs file'
  176. cp -f /media/usb/.emacs /home/$MY_USERNAME/.emacs
  177. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.emacs
  178. fi
  179. if [ -d /media/usb/.emacs.d ]; then
  180. echo 'Importing .emacs.d directory'
  181. cp -r /media/usb/.emacs.d /home/$MY_USERNAME/.emacs.d
  182. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.emacs.d
  183. fi
  184. if [ -d /media/usb/personal ]; then
  185. echo 'Importing personal directory'
  186. cp -r /media/usb/personal /home/$MY_USERNAME/personal
  187. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/personal
  188. fi
  189. fi
  190. echo 'search_for_attached_usb_drive' >> $COMPLETION_FILE
  191. }
  192. function remove_proprietary_repos {
  193. if grep -Fxq "remove_proprietary_repos" $COMPLETION_FILE; then
  194. return
  195. fi
  196. sed -i 's/ non-free//g' /etc/apt/sources.list
  197. echo 'remove_proprietary_repos' >> $COMPLETION_FILE
  198. }
  199. function change_debian_repos {
  200. if grep -Fxq "change_debian_repos" $COMPLETION_FILE; then
  201. return
  202. fi
  203. rm -rf /var/lib/apt/lists/*
  204. apt-get clean
  205. sed -i "s/ftp.us.debian.org/$DEBIAN_REPO/g" /etc/apt/sources.list
  206. # ensure that there is a security repo
  207. if ! grep -q "security" /etc/apt/sources.list; then
  208. if grep -q "jessie" /etc/apt/sources.list; then
  209. echo "deb http://security.debian.org/ jessie/updates main contrib" >> /etc/apt/sources.list
  210. echo "#deb-src http://security.debian.org/ jessie/updates main contrib" >> /etc/apt/sources.list
  211. else
  212. if grep -q "wheezy" /etc/apt/sources.list; then
  213. echo "deb http://security.debian.org/ wheezy/updates main contrib" >> /etc/apt/sources.list
  214. echo "#deb-src http://security.debian.org/ wheezy/updates main contrib" >> /etc/apt/sources.list
  215. fi
  216. fi
  217. fi
  218. apt-get update
  219. apt-get -y --force-yes install apt-transport-https
  220. echo 'change_debian_repos' >> $COMPLETION_FILE
  221. }
  222. function initial_setup {
  223. if grep -Fxq "initial_setup" $COMPLETION_FILE; then
  224. return
  225. fi
  226. apt-get -y remove --purge apache*
  227. apt-get -y dist-upgrade
  228. apt-get -y install ca-certificates emacs24
  229. echo 'initial_setup' >> $COMPLETION_FILE
  230. }
  231. function install_editor {
  232. if grep -Fxq "install_editor" $COMPLETION_FILE; then
  233. return
  234. fi
  235. update-alternatives --set editor /usr/bin/emacs24
  236. echo 'install_editor' >> $COMPLETION_FILE
  237. }
  238. function enable_backports {
  239. if grep -Fxq "enable_backports" $COMPLETION_FILE; then
  240. return
  241. fi
  242. if ! grep -Fxq "deb http://$DEBIAN_REPO/debian jessie-backports main" /etc/apt/sources.list; then
  243. echo "deb http://$DEBIAN_REPO/debian jessie-backports main" >> /etc/apt/sources.list
  244. fi
  245. echo 'enable_backports' >> $COMPLETION_FILE
  246. }
  247. function update_the_kernel {
  248. if grep -Fxq "update_the_kernel" $COMPLETION_FILE; then
  249. return
  250. fi
  251. if [ $INSTALLED_WITHIN_DOCKER == "yes" ]; then
  252. return
  253. fi
  254. cd /opt/scripts/tools
  255. ./update_kernel.sh --kernel $KERNEL_VERSION
  256. echo 'update_the_kernel' >> $COMPLETION_FILE
  257. }
  258. function enable_zram {
  259. if grep -Fxq "enable_zram" $COMPLETION_FILE; then
  260. return
  261. fi
  262. if [ $INSTALLED_WITHIN_DOCKER == "yes" ]; then
  263. return
  264. fi
  265. if ! grep -q "options zram num_devices=1" /etc/modprobe.d/zram.conf; then
  266. echo 'options zram num_devices=1' >> /etc/modprobe.d/zram.conf
  267. fi
  268. echo '#!/bin/bash' > /etc/init.d/zram
  269. echo '### BEGIN INIT INFO' >> /etc/init.d/zram
  270. echo '# Provides: zram' >> /etc/init.d/zram
  271. echo '# Required-Start:' >> /etc/init.d/zram
  272. echo '# Required-Stop:' >> /etc/init.d/zram
  273. echo '# Default-Start: 2 3 4 5' >> /etc/init.d/zram
  274. echo '# Default-Stop: 0 1 6' >> /etc/init.d/zram
  275. echo '# Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM)' >> /etc/init.d/zram
  276. echo '# Description: Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram' >> /etc/init.d/zram
  277. echo '### END INIT INFO' >> /etc/init.d/zram
  278. echo 'start() {' >> /etc/init.d/zram
  279. echo ' # get the number of CPUs' >> /etc/init.d/zram
  280. echo ' num_cpus=$(grep -c processor /proc/cpuinfo)' >> /etc/init.d/zram
  281. echo ' # if something goes wrong, assume we have 1' >> /etc/init.d/zram
  282. echo ' [ "$num_cpus" != 0 ] || num_cpus=1' >> /etc/init.d/zram
  283. echo ' # set decremented number of CPUs' >> /etc/init.d/zram
  284. echo ' decr_num_cpus=$((num_cpus - 1))' >> /etc/init.d/zram
  285. echo ' # get the amount of memory in the machine' >> /etc/init.d/zram
  286. echo ' mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching "[[:digit:]]+")' >> /etc/init.d/zram
  287. echo ' mem_total=$((mem_total_kb * 1024))' >> /etc/init.d/zram
  288. echo ' # load dependency modules' >> /etc/init.d/zram
  289. echo ' modprobe zram num_devices=$num_cpus' >> /etc/init.d/zram
  290. echo ' # initialize the devices' >> /etc/init.d/zram
  291. echo ' for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
  292. echo ' echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize' >> /etc/init.d/zram
  293. echo ' done' >> /etc/init.d/zram
  294. echo ' # Creating swap filesystems' >> /etc/init.d/zram
  295. echo ' for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
  296. echo ' mkswap /dev/zram$i' >> /etc/init.d/zram
  297. echo ' done' >> /etc/init.d/zram
  298. echo ' # Switch the swaps on' >> /etc/init.d/zram
  299. echo ' for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
  300. echo ' swapon -p 100 /dev/zram$i' >> /etc/init.d/zram
  301. echo ' done' >> /etc/init.d/zram
  302. echo '}' >> /etc/init.d/zram
  303. echo 'stop() {' >> /etc/init.d/zram
  304. echo ' # get the number of CPUs' >> /etc/init.d/zram
  305. echo ' num_cpus=$(grep -c processor /proc/cpuinfo)' >> /etc/init.d/zram
  306. echo ' # set decremented number of CPUs' >> /etc/init.d/zram
  307. echo ' decr_num_cpus=$((num_cpus - 1))' >> /etc/init.d/zram
  308. echo ' # Switching off swap' >> /etc/init.d/zram
  309. echo ' for i in $(seq 0 $decr_num_cpus); do' >> /etc/init.d/zram
  310. echo ' if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then' >> /etc/init.d/zram
  311. echo ' swapoff /dev/zram$i' >> /etc/init.d/zram
  312. echo ' sleep 1' >> /etc/init.d/zram
  313. echo ' fi' >> /etc/init.d/zram
  314. echo ' done' >> /etc/init.d/zram
  315. echo ' sleep 1' >> /etc/init.d/zram
  316. echo ' rmmod zram' >> /etc/init.d/zram
  317. echo '}' >> /etc/init.d/zram
  318. echo 'case "$1" in' >> /etc/init.d/zram
  319. echo ' start)' >> /etc/init.d/zram
  320. echo ' start' >> /etc/init.d/zram
  321. echo ' ;;' >> /etc/init.d/zram
  322. echo ' stop)' >> /etc/init.d/zram
  323. echo ' stop' >> /etc/init.d/zram
  324. echo ' ;;' >> /etc/init.d/zram
  325. echo ' restart)' >> /etc/init.d/zram
  326. echo ' stop' >> /etc/init.d/zram
  327. echo ' sleep 3' >> /etc/init.d/zram
  328. echo ' start' >> /etc/init.d/zram
  329. echo ' ;;' >> /etc/init.d/zram
  330. echo ' *)' >> /etc/init.d/zram
  331. echo ' echo "Usage: $0 {start|stop|restart}"' >> /etc/init.d/zram
  332. echo ' RETVAL=1' >> /etc/init.d/zram
  333. echo 'esac' >> /etc/init.d/zram
  334. echo 'exit $RETVAL' >> /etc/init.d/zram
  335. chmod +x /etc/init.d/zram
  336. update-rc.d zram defaults
  337. echo 'enable_zram' >> $COMPLETION_FILE
  338. }
  339. function random_number_generator {
  340. if grep -Fxq "random_number_generator" $COMPLETION_FILE; then
  341. return
  342. fi
  343. if [ $INSTALLED_WITHIN_DOCKER == "yes" ]; then
  344. # it is assumed that docker uses the random number
  345. # generator of the host system
  346. return
  347. fi
  348. if [ $USE_HWRNG == "yes" ]; then
  349. apt-get -y --force-yes install rng-tools
  350. sed -i 's|#HRNGDEVICE=/dev/hwrng|HRNGDEVICE=/dev/hwrng|g' /etc/default/rng-tools
  351. else
  352. apt-get -y --force-yes install haveged
  353. fi
  354. echo 'random_number_generator' >> $COMPLETION_FILE
  355. }
  356. function configure_ssh {
  357. if grep -Fxq "configure_ssh" $COMPLETION_FILE; then
  358. return
  359. fi
  360. sed -i "s/Port 22/Port $SSH_PORT/g" /etc/ssh/sshd_config
  361. sed -i 's/PermitRootLogin without-password/PermitRootLogin no/g' /etc/ssh/sshd_config
  362. sed -i 's/X11Forwarding yes/X11Forwarding no/g' /etc/ssh/sshd_config
  363. sed -i 's/ServerKeyBits 1024/ServerKeyBits 4096/g' /etc/ssh/sshd_config
  364. sed -i 's/TCPKeepAlive yes/TCPKeepAlive no/g' /etc/ssh/sshd_config
  365. sed -i 's|HostKey /etc/ssh/ssh_host_dsa_key|#HostKey /etc/ssh/ssh_host_dsa_key|g' /etc/ssh/sshd_config
  366. sed -i 's|HostKey /etc/ssh/ssh_host_ecdsa_key|#HostKey /etc/ssh/ssh_host_ecdsa_key|g' /etc/ssh/sshd_config
  367. echo 'ClientAliveInterval 60' >> /etc/ssh/sshd_config
  368. echo 'ClientAliveCountMax 3' >> /etc/ssh/sshd_config
  369. echo 'Ciphers aes256-ctr,aes128-ctr' >> /etc/ssh/sshd_config
  370. echo 'MACs hmac-sha2-512,hmac-sha2-256,hmac-ripemd160
  371. KexAlgorithms diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1' >> /etc/ssh/sshd_config
  372. apt-get -y --force-yes install fail2ban
  373. echo 'configure_ssh' >> $COMPLETION_FILE
  374. echo ''
  375. echo ''
  376. echo ' *** Rebooting to initialise ssh settings and random number generator ***'
  377. echo ''
  378. echo " *** Reconnect via ssh on port $SSH_PORT, then run this script again ***"
  379. echo ''
  380. reboot
  381. }
  382. function regenerate_ssh_keys {
  383. if grep -Fxq "regenerate_ssh_keys" $COMPLETION_FILE; then
  384. return
  385. fi
  386. rm -f /etc/ssh/ssh_host_*
  387. dpkg-reconfigure openssh-server
  388. service ssh restart
  389. echo 'regenerate_ssh_keys' >> $COMPLETION_FILE
  390. }
  391. function configure_dns {
  392. if grep -Fxq "configure_dns" $COMPLETION_FILE; then
  393. return
  394. fi
  395. echo 'domain localdomain' > /etc/resolv.conf
  396. echo 'search localdomain' >> /etc/resolv.conf
  397. echo 'nameserver 213.73.91.35' >> /etc/resolv.conf
  398. echo 'nameserver 85.214.20.141' >> /etc/resolv.conf
  399. echo 'configure_dns' >> $COMPLETION_FILE
  400. }
  401. function set_your_domain_name {
  402. if grep -Fxq "set_your_domain_name" $COMPLETION_FILE; then
  403. return
  404. fi
  405. echo "$DOMAIN_NAME" > /etc/hostname
  406. hostname $DOMAIN_NAME
  407. sed -i "s/127.0.1.1 arm/127.0.1.1 $DOMAIN_NAME/g" /etc/hosts
  408. echo "127.0.1.1 $DOMAIN_NAME" >> /etc/hosts
  409. echo 'set_your_domain_name' >> $COMPLETION_FILE
  410. }
  411. function time_synchronisation {
  412. if grep -Fxq "time_synchronisation" $COMPLETION_FILE; then
  413. return
  414. fi
  415. apt-get -y --force-yes install tlsdate
  416. apt-get -y remove ntpdate
  417. echo '#!/bin/bash' > /usr/bin/updatedate
  418. echo 'TIMESOURCE=google.com' >> /usr/bin/updatedate
  419. echo 'TIMESOURCE2=www.ptb.de' >> /usr/bin/updatedate
  420. echo 'LOGFILE=/var/log/tlsdate.log' >> /usr/bin/updatedate
  421. echo 'TIMEOUT=5' >> /usr/bin/updatedate
  422. echo "EMAIL=$MY_USERNAME@$DOMAIN_NAME" >> /usr/bin/updatedate
  423. echo '# File which contains the previous date as a number' >> /usr/bin/updatedate
  424. echo 'BEFORE_DATE_FILE=/var/log/tlsdateprevious.txt' >> /usr/bin/updatedate
  425. echo '# File which contains the previous date as a string' >> /usr/bin/updatedate
  426. echo 'BEFORE_FULLDATE_FILE=/var/log/tlsdate.txt' >> /usr/bin/updatedate
  427. echo 'DATE_BEFORE=$(date)' >> /usr/bin/updatedate
  428. echo 'BEFORE=$(date -d "$Y-$M-$D" "+%s")' >> /usr/bin/updatedate
  429. echo 'BACKWARDS_BETWEEN=0' >> /usr/bin/updatedate
  430. echo '# If the date was previously set' >> /usr/bin/updatedate
  431. echo 'if [[ -f "$BEFORE_DATE_FILE" ]]; then' >> /usr/bin/updatedate
  432. echo ' BEFORE_FILE=$(cat $BEFORE_DATE_FILE)' >> /usr/bin/updatedate
  433. echo ' BEFORE_FULLDATE=$(cat $BEFORE_FULLDATE_FILE)' >> /usr/bin/updatedate
  434. echo ' # is the date going backwards?' >> /usr/bin/updatedate
  435. echo ' if (( BEFORE_FILE > BEFORE )); then' >> /usr/bin/updatedate
  436. echo ' echo -n "Date went backwards between tlsdate updates. " >> $LOGFILE' >> /usr/bin/updatedate
  437. echo ' echo -n "$BEFORE_FILE > $BEFORE, " >> $LOGFILE' >> /usr/bin/updatedate
  438. echo ' echo "$BEFORE_FULLDATE > $DATE_BEFORE" >> $LOGFILE' >> /usr/bin/updatedate
  439. echo ' # Send a warning email' > /usr/bin/updatedate
  440. echo ' echo $(tail $LOGFILE -n 2) | mail -s "tlsdate anomaly" $EMAIL' >> /usr/bin/updatedate
  441. echo ' # Try another time source' >> /usr/bin/updatedate
  442. echo ' TIMESOURCE=$TIMESOURCE2' >> /usr/bin/updatedate
  443. echo ' # try running without any parameters' >> /usr/bin/updatedate
  444. echo ' tlsdate >> $LOGFILE' >> /usr/bin/updatedate
  445. echo ' BACKWARDS_BETWEEN=1' >> /usr/bin/updatedate
  446. echo ' fi' >> /usr/bin/updatedate
  447. echo 'fi' >> /usr/bin/updatedate
  448. echo '# Set the date' >> /usr/bin/updatedate
  449. echo '/usr/bin/timeout $TIMEOUT tlsdate -l -t -H $TIMESOURCE -p 443 >> $LOGFILE' >> /usr/bin/updatedate
  450. echo 'DATE_AFTER=$(date)' >> /usr/bin/updatedate
  451. echo 'AFTER=$(date -d "$Y-$M-$D" '+%s')' >> /usr/bin/updatedate
  452. echo '# After setting the date did it go backwards?' >> /usr/bin/updatedate
  453. echo 'if (( AFTER < BEFORE )); then' >> /usr/bin/updatedate
  454. echo ' echo "Incorrect date: $DATE_BEFORE -> $DATE_AFTER" >> $LOGFILE' >> /usr/bin/updatedate
  455. echo ' # Send a warning email' >> /usr/bin/updatedate
  456. echo ' echo $(tail $LOGFILE -n 2) | mail -s "tlsdate anomaly" $EMAIL' >> /usr/bin/updatedate
  457. echo ' # Try resetting the date from another time source' >> /usr/bin/updatedate
  458. echo ' /usr/bin/timeout $TIMEOUT tlsdate -l -t -H $TIMESOURCE2 -p 443 >> $LOGFILE' >> /usr/bin/updatedate
  459. echo ' DATE_AFTER=$(date)' >> /usr/bin/updatedate
  460. echo ' AFTER=$(date -d "$Y-$M-$D" "+%s")' >> /usr/bin/updatedate
  461. echo 'else' >> /usr/bin/updatedate
  462. echo ' echo -n $TIMESOURCE >> $LOGFILE' >> /usr/bin/updatedate
  463. echo ' if [[ -f "$BEFORE_DATE_FILE" ]]; then' >> /usr/bin/updatedate
  464. echo ' echo -n " " >> $LOGFILE' >> /usr/bin/updatedate
  465. echo ' echo -n $BEFORE_FILE >> $LOGFILE' >> /usr/bin/updatedate
  466. echo ' fi' >> /usr/bin/updatedate
  467. echo ' echo -n " " >> $LOGFILE' >> /usr/bin/updatedate
  468. echo ' echo -n $BEFORE >> $LOGFILE' >> /usr/bin/updatedate
  469. echo ' echo -n " " >> $LOGFILE' >> /usr/bin/updatedate
  470. echo ' echo -n $AFTER >> $LOGFILE' >> /usr/bin/updatedate
  471. echo ' echo -n " " >> $LOGFILE' >> /usr/bin/updatedate
  472. echo ' echo $DATE_AFTER >> $LOGFILE' >> /usr/bin/updatedate
  473. echo 'fi' >> /usr/bin/updatedate
  474. echo '# Log the last date' >> /usr/bin/updatedate
  475. echo 'if [ BACKWARDS_BETWEEN == 0 ]; then' >> /usr/bin/updatedate
  476. echo ' echo "$AFTER" > $BEFORE_DATE_FILE' >> /usr/bin/updatedate
  477. echo ' echo "$DATE_AFTER" > $BEFORE_FULLDATE_FILE' >> /usr/bin/updatedate
  478. echo ' exit 0' >> /usr/bin/updatedate
  479. echo 'else' >> /usr/bin/updatedate
  480. echo ' exit 1' >> /usr/bin/updatedate
  481. echo 'fi' >> /usr/bin/updatedate
  482. chmod +x /usr/bin/updatedate
  483. echo '*/15 * * * * root /usr/bin/updatedate' >> /etc/crontab
  484. service cron restart
  485. echo '#!/bin/bash' > /etc/init.d/tlsdate
  486. echo '# /etc/init.d/tlsdate' >> /etc/init.d/tlsdate
  487. echo '### BEGIN INIT INFO' >> /etc/init.d/tlsdate
  488. echo '# Provides: tlsdate' >> /etc/init.d/tlsdate
  489. echo '# Required-Start: $remote_fs $syslog' >> /etc/init.d/tlsdate
  490. echo '# Required-Stop: $remote_fs $syslog' >> /etc/init.d/tlsdate
  491. echo '# Default-Start: 2 3 4 5' >> /etc/init.d/tlsdate
  492. echo '# Default-Stop: 0 1 6' >> /etc/init.d/tlsdate
  493. echo '# Short-Description: Initially calls tlsdate with the timewarp option' >> /etc/init.d/tlsdate
  494. echo '# Description: Initially calls tlsdate with the timewarp option' >> /etc/init.d/tlsdate
  495. echo '### END INIT INFO' >> /etc/init.d/tlsdate
  496. echo '# Author: Bob Mottram <bob@robotics.uk.to>' >> /etc/init.d/tlsdate
  497. echo 'PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:/sbin:/usr/sbin:/bin"' >> /etc/init.d/tlsdate
  498. echo 'LOGFILE="/var/log/tlsdate.log"' >> /etc/init.d/tlsdate
  499. echo 'TLSDATECOMMAND="tlsdate --timewarp -l -H www.ptb.de -p 443 >> $LOGFILE"' >> /etc/init.d/tlsdate
  500. echo '#Start-Stop here' >> /etc/init.d/tlsdate
  501. echo 'case "$1" in' >> /etc/init.d/tlsdate
  502. echo ' start)' >> /etc/init.d/tlsdate
  503. echo ' echo "tlsdate started"' >> /etc/init.d/tlsdate
  504. echo ' $TLSDATECOMMAND' >> /etc/init.d/tlsdate
  505. echo ' ;;' >> /etc/init.d/tlsdate
  506. echo ' stop)' >> /etc/init.d/tlsdate
  507. echo ' echo "tlsdate stopped"' >> /etc/init.d/tlsdate
  508. echo ' ;;' >> /etc/init.d/tlsdate
  509. echo ' restart)' >> /etc/init.d/tlsdate
  510. echo ' echo "tlsdate restarted"' >> /etc/init.d/tlsdate
  511. echo ' $TLSDATECOMMAND' >> /etc/init.d/tlsdate
  512. echo ' ;;' >> /etc/init.d/tlsdate
  513. echo ' *)' >> /etc/init.d/tlsdate
  514. echo ' echo "Usage: $0 {start|stop|restart}"' >> /etc/init.d/tlsdate
  515. echo ' exit 1' >> /etc/init.d/tlsdate
  516. echo ' ;;' >> /etc/init.d/tlsdate
  517. echo 'esac' >> /etc/init.d/tlsdate
  518. echo 'exit 0' >> /etc/init.d/tlsdate
  519. chmod +x /etc/init.d/tlsdate
  520. update-rc.d tlsdate defaults
  521. echo 'time_synchronisation' >> $COMPLETION_FILE
  522. }
  523. function configure_firewall {
  524. if grep -Fxq "configure_firewall" $COMPLETION_FILE; then
  525. return
  526. fi
  527. if [ $INSTALLED_WITHIN_DOCKER == "yes" ]; then
  528. # docker does its own firewalling
  529. return
  530. fi
  531. iptables -P INPUT ACCEPT
  532. ip6tables -P INPUT ACCEPT
  533. iptables -F
  534. ip6tables -F
  535. iptables -X
  536. ip6tables -X
  537. iptables -P INPUT DROP
  538. ip6tables -P INPUT DROP
  539. iptables -A INPUT -i lo -j ACCEPT
  540. iptables -A INPUT -i eth0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
  541. echo 'configure_firewall' >> $COMPLETION_FILE
  542. }
  543. function save_firewall_settings {
  544. iptables-save > /etc/firewall.conf
  545. ip6tables-save > /etc/firewall6.conf
  546. printf '#!/bin/sh\n' > /etc/network/if-up.d/iptables
  547. printf 'iptables-restore < /etc/firewall.conf\n' >> /etc/network/if-up.d/iptables
  548. printf 'ip6tables-restore < /etc/firewall6.conf\n' >> /etc/network/if-up.d/iptables
  549. chmod +x /etc/network/if-up.d/iptables
  550. }
  551. function configure_firewall_for_dns {
  552. if grep -Fxq "configure_firewall_for_dns" $COMPLETION_FILE; then
  553. return
  554. fi
  555. if [ $INSTALLED_WITHIN_DOCKER == "yes" ]; then
  556. # docker does its own firewalling
  557. return
  558. fi
  559. iptables -A INPUT -i eth0 -p udp -m udp --dport 1024:65535 --sport 53 -j ACCEPT
  560. save_firewall_settings
  561. echo 'configure_firewall_for_dns' >> $COMPLETION_FILE
  562. }
  563. function configure_firewall_for_ftp {
  564. if grep -Fxq "configure_firewall_for_ftp" $COMPLETION_FILE; then
  565. return
  566. fi
  567. if [ $INSTALLED_WITHIN_DOCKER == "yes" ]; then
  568. # docker does its own firewalling
  569. return
  570. fi
  571. iptables -I INPUT -i eth0 -p tcp --dport 1024:65535 --sport 20:21 -j ACCEPT
  572. save_firewall_settings
  573. echo 'configure_firewall_for_ftp' >> $COMPLETION_FILE
  574. }
  575. function configure_firewall_for_web {
  576. if grep -Fxq "configure_firewall_for_web" $COMPLETION_FILE; then
  577. return
  578. fi
  579. if [ $INSTALLED_WITHIN_DOCKER == "yes" ]; then
  580. # docker does its own firewalling
  581. return
  582. fi
  583. iptables -A INPUT -i eth0 -p tcp --dport 32768:61000 --sport 80 -j ACCEPT
  584. iptables -A INPUT -i eth0 -p tcp --dport 32768:61000 --sport 443 -j ACCEPT
  585. save_firewall_settings
  586. echo 'configure_firewall_for_web' >> $COMPLETION_FILE
  587. }
  588. function configure_firewall_for_ssh {
  589. if grep -Fxq "configure_firewall_for_ssh" $COMPLETION_FILE; then
  590. return
  591. fi
  592. if [ $INSTALLED_WITHIN_DOCKER == "yes" ]; then
  593. # docker does its own firewalling
  594. return
  595. fi
  596. iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT
  597. iptables -A INPUT -i eth0 -p tcp --dport $SSH_PORT -j ACCEPT
  598. save_firewall_settings
  599. echo 'configure_firewall_for_ssh' >> $COMPLETION_FILE
  600. }
  601. function configure_firewall_for_git {
  602. if grep -Fxq "configure_firewall_for_git" $COMPLETION_FILE; then
  603. return
  604. fi
  605. if [ $INSTALLED_WITHIN_DOCKER == "yes" ]; then
  606. # docker does its own firewalling
  607. return
  608. fi
  609. iptables -A INPUT -i eth0 -p tcp --dport 9418 -j ACCEPT
  610. save_firewall_settings
  611. echo 'configure_firewall_for_git' >> $COMPLETION_FILE
  612. }
  613. function configure_firewall_for_email {
  614. if grep -Fxq "configure_firewall_for_email" $COMPLETION_FILE; then
  615. return
  616. fi
  617. if [ $INSTALLED_WITHIN_DOCKER == "yes" ]; then
  618. # docker does its own firewalling
  619. return
  620. fi
  621. iptables -A INPUT -i eth0 -p tcp --dport 25 -j ACCEPT
  622. iptables -A INPUT -i eth0 -p tcp --dport 587 -j ACCEPT
  623. iptables -A INPUT -i eth0 -p tcp --dport 465 -j ACCEPT
  624. iptables -A INPUT -i eth0 -p tcp --dport 993 -j ACCEPT
  625. save_firewall_settings
  626. echo 'configure_firewall_for_email' >> $COMPLETION_FILE
  627. }
  628. function configure_internet_protocol {
  629. if grep -Fxq "configure_internet_protocol" $COMPLETION_FILE; then
  630. return
  631. fi
  632. sed -i "s/#net.ipv4.tcp_syncookies=1/net.ipv4.tcp_syncookies=1/g" /etc/sysctl.conf
  633. sed -i "s/#net.ipv4.conf.all.accept_redirects = 0/net.ipv4.conf.all.accept_redirects = 0/g" /etc/sysctl.conf
  634. sed -i "s/#net.ipv6.conf.all.accept_redirects = 0/net.ipv6.conf.all.accept_redirects = 0/g" /etc/sysctl.conf
  635. sed -i "s/#net.ipv4.conf.all.send_redirects = 0/net.ipv4.conf.all.send_redirects = 0/g" /etc/sysctl.conf
  636. sed -i "s/#net.ipv4.conf.all.accept_source_route = 0/net.ipv4.conf.all.accept_source_route = 0/g" /etc/sysctl.conf
  637. sed -i "s/#net.ipv6.conf.all.accept_source_route = 0/net.ipv6.conf.all.accept_source_route = 0/g" /etc/sysctl.conf
  638. sed -i "s/#net.ipv4.conf.default.rp_filter=1/net.ipv4.conf.default.rp_filter=1/g" /etc/sysctl.conf
  639. sed -i "s/#net.ipv4.conf.all.rp_filter=1/net.ipv4.conf.all.rp_filter=1/g" /etc/sysctl.conf
  640. sed -i "s/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=0/g" /etc/sysctl.conf
  641. sed -i "s/#net.ipv6.conf.all.forwarding=1/net.ipv6.conf.all.forwarding=0/g" /etc/sysctl.conf
  642. echo '# ignore pings' >> /etc/sysctl.conf
  643. echo 'net.ipv4.icmp_echo_ignore_all = 1' >> /etc/sysctl.conf
  644. echo 'net.ipv6.icmp_echo_ignore_all = 1' >> /etc/sysctl.conf
  645. echo '# disable ipv6' >> /etc/sysctl.conf
  646. echo 'net.ipv6.conf.all.disable_ipv6 = 1' >> /etc/sysctl.conf
  647. echo 'net.ipv4.tcp_synack_retries = 2' >> /etc/sysctl.conf
  648. echo 'net.ipv4.tcp_syn_retries = 1' >> /etc/sysctl.conf
  649. echo '# keepalive' >> /etc/sysctl.conf
  650. echo 'net.ipv4.tcp_keepalive_probes = 9' >> /etc/sysctl.conf
  651. echo 'net.ipv4.tcp_keepalive_intvl = 75' >> /etc/sysctl.conf
  652. echo 'net.ipv4.tcp_keepalive_time = 7200' >> /etc/sysctl.conf
  653. echo 'configure_internet_protocol' >> $COMPLETION_FILE
  654. }
  655. function script_to_make_self_signed_certificates {
  656. if grep -Fxq "script_to_make_self_signed_certificates" $COMPLETION_FILE; then
  657. return
  658. fi
  659. echo '#!/bin/bash' > /usr/bin/makecert
  660. echo 'HOSTNAME=$1' >> /usr/bin/makecert
  661. echo 'COUNTRY_CODE="US"' >> /usr/bin/makecert
  662. echo 'AREA="Free Speech Zone"' >> /usr/bin/makecert
  663. echo 'LOCATION="Freedomville"' >> /usr/bin/makecert
  664. echo 'ORGANISATION="Freedombone"' >> /usr/bin/makecert
  665. echo 'UNIT="Freedombone Unit"' >> /usr/bin/makecert
  666. echo 'if ! which openssl > /dev/null ;then' >> /usr/bin/makecert
  667. echo ' echo "$0: openssl is not installed, exiting" 1>&2' >> /usr/bin/makecert
  668. echo ' exit 1' >> /usr/bin/makecert
  669. echo 'fi' >> /usr/bin/makecert
  670. echo 'openssl req -x509 -nodes -days 3650 -sha256 -subj "/O=$ORGANISATION/OU=$UNIT/C=$COUNTRY_CODE/ST=$AREA/L=$LOCATION/CN=$HOSTNAME" -newkey rsa:4096 -keyout /etc/ssl/private/$HOSTNAME.key -out /etc/ssl/certs/$HOSTNAME.crt' >> /usr/bin/makecert
  671. echo 'openssl dhparam -check -text -5 1024 -out /etc/ssl/certs/$HOSTNAME.dhparam' >> /usr/bin/makecert
  672. echo 'chmod 400 /etc/ssl/private/$HOSTNAME.key' >> /usr/bin/makecert
  673. echo 'chmod 640 /etc/ssl/certs/$HOSTNAME.crt' >> /usr/bin/makecert
  674. echo 'chmod 640 /etc/ssl/certs/$HOSTNAME.dhparam' >> /usr/bin/makecert
  675. echo 'if [ -f /etc/init.d/nginx ]; then' >> /usr/bin/makecert
  676. echo ' /etc/init.d/nginx reload' >> /usr/bin/makecert
  677. echo 'fi' >> /usr/bin/makecert
  678. echo '# add the public certificate to a separate directory' >> /usr/bin/makecert
  679. echo '# so that we can redistribute it easily' >> /usr/bin/makecert
  680. echo 'if [ ! -d /etc/ssl/mycerts ]; then' >> /usr/bin/makecert
  681. echo ' mkdir /etc/ssl/mycerts' >> /usr/bin/makecert
  682. echo 'fi' >> /usr/bin/makecert
  683. echo 'cp /etc/ssl/certs/$HOSTNAME.crt /etc/ssl/mycerts' >> /usr/bin/makecert
  684. echo '# Create a bundle of your certificates' >> /usr/bin/makecert
  685. echo 'cat /etc/ssl/mycerts/*.crt > /etc/ssl/freedombone-bundle.crt' >> /usr/bin/makecert
  686. echo 'tar -czvf /etc/ssl/freedombone-certs.tar.gz /etc/ssl/mycerts/*.crt' >> /usr/bin/makecert
  687. chmod +x /usr/bin/makecert
  688. echo 'script_to_make_self_signed_certificates' >> $COMPLETION_FILE
  689. }
  690. function configure_email {
  691. if grep -Fxq "configure_email" $COMPLETION_FILE; then
  692. return
  693. fi
  694. apt-get -y remove postfix
  695. apt-get -y --force-yes install exim4 sasl2-bin swaks libnet-ssleay-perl procmail
  696. echo 'dc_eximconfig_configtype="internet"' > /etc/exim4/update-exim4.conf.conf
  697. echo "dc_other_hostnames='$DOMAIN_NAME'" >> /etc/exim4/update-exim4.conf.conf
  698. echo "dc_local_interfaces=''" >> /etc/exim4/update-exim4.conf.conf
  699. echo "dc_readhost=''" >> /etc/exim4/update-exim4.conf.conf
  700. echo "dc_relay_domains=''" >> /etc/exim4/update-exim4.conf.conf
  701. echo "dc_minimaldns='false'" >> /etc/exim4/update-exim4.conf.conf
  702. echo "dc_relay_nets='192.168.1.0/24'" >> /etc/exim4/update-exim4.conf.conf
  703. echo "dc_smarthost=''" >> /etc/exim4/update-exim4.conf.conf
  704. echo "CFILEMODE='644'" >> /etc/exim4/update-exim4.conf.conf
  705. echo "dc_use_split_config='false'" >> /etc/exim4/update-exim4.conf.conf
  706. echo "dc_hide_mailname=''" >> /etc/exim4/update-exim4.conf.conf
  707. echo "dc_mailname_in_oh='true'" >> /etc/exim4/update-exim4.conf.conf
  708. echo "dc_localdelivery='maildir_home'" >> /etc/exim4/update-exim4.conf.conf
  709. update-exim4.conf
  710. sed -i "s/START=no/START=yes/g" /etc/default/saslauthd
  711. /etc/init.d/saslauthd start
  712. # make a tls certificate for email
  713. makecert exim
  714. mv /etc/ssl/private/exim.key /etc/exim4
  715. mv /etc/ssl/certs/exim.crt /etc/exim4
  716. mv /etc/ssl/certs/exim.dhparam /etc/exim4
  717. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  718. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  719. sed -i '/login_saslauthd_server/,/.endif/ s/# *//' /etc/exim4/exim4.conf.template
  720. sed -i "/.ifdef MAIN_HARDCODE_PRIMARY_HOSTNAME/i\MAIN_HARDCODE_PRIMARY_HOSTNAME = $DOMAIN_NAME\nMAIN_TLS_ENABLE = true" /etc/exim4/exim4.conf.template
  721. sed -i "s|SMTPLISTENEROPTIONS=''|SMTPLISTENEROPTIONS='-oX 465:25:587 -oP /var/run/exim4/exim.pid'|g" /etc/default/exim4
  722. if ! grep -q "tls_on_connect_ports=465" /etc/exim4/exim4.conf.template; then
  723. sed -i '/SSL configuration for exim/i\tls_on_connect_ports=465' /etc/exim4/exim4.conf.template
  724. fi
  725. adduser $MY_USERNAME sasl
  726. addgroup Debian-exim sasl
  727. /etc/init.d/exim4 restart
  728. if [ ! -d /etc/skel/Maildir ]; then
  729. mkdir -m 700 /etc/skel/Maildir
  730. mkdir -m 700 /etc/skel/Maildir/Sent
  731. mkdir -m 700 /etc/skel/Maildir/Sent/tmp
  732. mkdir -m 700 /etc/skel/Maildir/Sent/cur
  733. mkdir -m 700 /etc/skel/Maildir/Sent/new
  734. mkdir -m 700 /etc/skel/Maildir/.learn-spam
  735. mkdir -m 700 /etc/skel/Maildir/.learn-spam/cur
  736. mkdir -m 700 /etc/skel/Maildir/.learn-spam/new
  737. mkdir -m 700 /etc/skel/Maildir/.learn-spam/tmp
  738. mkdir -m 700 /etc/skel/Maildir/.learn-ham
  739. mkdir -m 700 /etc/skel/Maildir/.learn-ham/cur
  740. mkdir -m 700 /etc/skel/Maildir/.learn-ham/new
  741. mkdir -m 700 /etc/skel/Maildir/.learn-ham/tmp
  742. ln -s /etc/skel/Maildir/.learn-spam /etc/skel/Maildir/spam
  743. ln -s /etc/skel/Maildir/.learn-ham /etc/skel/Maildir/ham
  744. fi
  745. if [ ! -d /home/$MY_USERNAME/Maildir ]; then
  746. mkdir -m 700 /home/$MY_USERNAME/Maildir
  747. mkdir -m 700 /home/$MY_USERNAME/Maildir/cur
  748. mkdir -m 700 /home/$MY_USERNAME/Maildir/tmp
  749. mkdir -m 700 /home/$MY_USERNAME/Maildir/new
  750. mkdir -m 700 /home/$MY_USERNAME/Maildir/Sent
  751. mkdir -m 700 /home/$MY_USERNAME/Maildir/Sent/cur
  752. mkdir -m 700 /home/$MY_USERNAME/Maildir/Sent/tmp
  753. mkdir -m 700 /home/$MY_USERNAME/Maildir/Sent/new
  754. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-spam
  755. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-spam/cur
  756. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-spam/new
  757. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-spam/tmp
  758. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-ham
  759. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-ham/cur
  760. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-ham/new
  761. mkdir -m 700 /home/$MY_USERNAME/Maildir/.learn-ham/tmp
  762. ln -s /home/$MY_USERNAME/Maildir/.learn-spam /home/$MY_USERNAME/Maildir/spam
  763. ln -s /home/$MY_USERNAME/Maildir/.learn-ham /home/$MY_USERNAME/Maildir/ham
  764. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/Maildir
  765. fi
  766. echo 'configure_email' >> $COMPLETION_FILE
  767. }
  768. function spam_filtering {
  769. # NOTE: spamassassin installation currently doesn't work, sa-compile fails with a make error 23/09/2014
  770. if grep -Fxq "spam_filtering" $COMPLETION_FILE; then
  771. return
  772. fi
  773. apt-get -y --force-yes install exim4-daemon-heavy
  774. apt-get -y --force-yes install spamassassin
  775. sa-update -v
  776. sed -i 's/ENABLED=0/ENABLED=1/g' /etc/default/spamassassin
  777. sed -i 's/# spamd_address = 127.0.0.1 783/spamd_address = 127.0.0.1 783/g' /etc/exim4/exim4.conf.template
  778. # This configuration is based on https://wiki.debian.org/DebianSpamAssassin
  779. sed -i 's/local_parts = postmaster/local_parts = postmaster:abuse/g' /etc/exim4/conf.d/acl/30_exim4-config_check_rcpt
  780. sed -i '/domains = +local_domains : +relay_to_domains/a\ set acl_m0 = rfcnames' /etc/exim4/conf.d/acl/30_exim4-config_check_rcpt
  781. sed -i 's/accept/accept condition = ${if eq{$acl_m0}{rfcnames} {1}{0}}/g' /etc/exim4/conf.d/acl/40_exim4-config_check_data
  782. echo 'warn message = X-Spam-Score: $spam_score ($spam_bar)' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  783. echo ' spam = nobody:true' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  784. echo 'warn message = X-Spam-Flag: YES' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  785. echo ' spam = nobody' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  786. echo 'warn message = X-Spam-Report: $spam_report' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  787. echo ' spam = nobody' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  788. echo '# reject spam at high scores (> 12)' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  789. echo 'deny message = This message scored $spam_score spam points.' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  790. echo ' spam = nobody:true' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  791. echo ' condition = ${if >{$spam_score_int}{120}{1}{0}}' >> /etc/exim4/conf.d/acl/40_exim4-config_check_data
  792. # procmail configuration
  793. echo 'MAILDIR=$HOME/Maildir' > /home/$MY_USERNAME/.procmailrc
  794. echo 'DEFAULT=$MAILDIR/' >> /home/$MY_USERNAME/.procmailrc
  795. echo 'LOGFILE=$HOME/log/procmail.log' >> /home/$MY_USERNAME/.procmailrc
  796. echo 'LOGABSTRACT=all' >> /home/$MY_USERNAME/.procmailrc
  797. echo '# get spamassassin to check emails' >> /home/$MY_USERNAME/.procmailrc
  798. echo ':0fw: .spamassassin.lock' >> /home/$MY_USERNAME/.procmailrc
  799. echo ' * < 256000' >> /home/$MY_USERNAME/.procmailrc
  800. echo '| spamc' >> /home/$MY_USERNAME/.procmailrc
  801. echo '# strong spam are discarded' >> /home/$MY_USERNAME/.procmailrc
  802. echo ':0' >> /home/$MY_USERNAME/.procmailrc
  803. echo ' * ^X-Spam-Level: \*\*\*\*\*\*' >> /home/$MY_USERNAME/.procmailrc
  804. echo '/dev/null' >> /home/$MY_USERNAME/.procmailrc
  805. echo '# weak spam are kept just in case - clear this out every now and then' >> /home/$MY_USERNAME/.procmailrc
  806. echo ':0' >> /home/$MY_USERNAME/.procmailrc
  807. echo ' * ^X-Spam-Level: \*\*\*\*\*' >> /home/$MY_USERNAME/.procmailrc
  808. echo '.0-spam/' >> /home/$MY_USERNAME/.procmailrc
  809. echo '# otherwise, marginal spam goes here for revision' >> /home/$MY_USERNAME/.procmailrc
  810. echo ':0' >> /home/$MY_USERNAME/.procmailrc
  811. echo ' * ^X-Spam-Level: \*\*' >> /home/$MY_USERNAME/.procmailrc
  812. echo '.spam/' >> /home/$MY_USERNAME/.procmailrc
  813. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.procmailrc
  814. # filtering scripts
  815. echo '#!/bin/bash' > /usr/bin/filterspam
  816. echo 'USERNAME=$1' >> /usr/bin/filterspam
  817. echo 'MAILDIR=/home/$USERNAME/Maildir/.learn-spam' >> /usr/bin/filterspam
  818. echo 'if [ ! -d "$MAILDIR" ]; then' >> /usr/bin/filterspam
  819. echo ' exit' >> /usr/bin/filterspam
  820. echo 'fi' >> /usr/bin/filterspam
  821. echo 'for f in `ls $MAILDIR/cur`' >> /usr/bin/filterspam
  822. echo 'do' >> /usr/bin/filterspam
  823. echo ' spamc -L spam < "$MAILDIR/cur/$f" > /dev/null' >> /usr/bin/filterspam
  824. echo ' rm "$MAILDIR/cur/$f"' >> /usr/bin/filterspam
  825. echo 'done' >> /usr/bin/filterspam
  826. echo 'for f in `ls $MAILDIR/new`' >> /usr/bin/filterspam
  827. echo 'do' >> /usr/bin/filterspam
  828. echo ' spamc -L spam < "$MAILDIR/new/$f" > /dev/null' >> /usr/bin/filterspam
  829. echo ' rm "$MAILDIR/new/$f"' >> /usr/bin/filterspam
  830. echo 'done' >> /usr/bin/filterspam
  831. echo '#!/bin/bash' > /usr/bin/filterham
  832. echo 'USERNAME=$1' >> /usr/bin/filterham
  833. echo 'MAILDIR=/home/$USERNAME/Maildir/.learn-ham' >> /usr/bin/filterham
  834. echo 'if [ ! -d "$MAILDIR" ]; then' >> /usr/bin/filterham
  835. echo ' exit' >> /usr/bin/filterham
  836. echo 'fi' >> /usr/bin/filterham
  837. echo 'for f in `ls $MAILDIR/cur`' >> /usr/bin/filterham
  838. echo 'do' >> /usr/bin/filterham
  839. echo ' spamc -L ham < "$MAILDIR/cur/$f" > /dev/null' >> /usr/bin/filterham
  840. echo ' rm "$MAILDIR/cur/$f"' >> /usr/bin/filterham
  841. echo 'done' >> /usr/bin/filterham
  842. echo 'for f in `ls $MAILDIR/new`' >> /usr/bin/filterham
  843. echo 'do' >> /usr/bin/filterham
  844. echo ' spamc -L ham < "$MAILDIR/new/$f" > /dev/null' >> /usr/bin/filterham
  845. echo ' rm "$MAILDIR/new/$f"' >> /usr/bin/filterham
  846. echo 'done' >> /usr/bin/filterham
  847. if ! grep -q "filterspam" /etc/crontab; then
  848. echo "*/3 * * * * root /usr/bin/timeout 120 /usr/bin/filterspam $MY_USERNAME" >> /etc/crontab
  849. fi
  850. if ! grep -q "filterham" /etc/crontab; then
  851. echo "*/3 * * * * root /usr/bin/timeout 120 /usr/bin/filterham $MY_USERNAME" >> /etc/crontab
  852. fi
  853. chmod 655 /usr/bin/filterspam /usr/bin/filterham
  854. sed -i 's/# use_bayes 1/use_bayes 1/g' /etc/mail/spamassassin/local.cf
  855. sed -i 's/# bayes_auto_learn 1/bayes_auto_learn 1/g' /etc/mail/spamassassin/local.cf
  856. service spamassassin restart
  857. service exim4 restart
  858. service cron restart
  859. echo 'spam_filtering' >> $COMPLETION_FILE
  860. }
  861. function configure_imap {
  862. if grep -Fxq "configure_imap" $COMPLETION_FILE; then
  863. return
  864. fi
  865. apt-get -y --force-yes install dovecot-common dovecot-imapd
  866. makecert dovecot
  867. chown root:dovecot /etc/ssl/certs/dovecot.crt
  868. chown root:dovecot /etc/ssl/private/dovecot.key
  869. chown root:dovecot /etc/ssl/private/dovecot.dhparams
  870. sed -i 's|#ssl = yes|ssl = yes|g' /etc/dovecot/conf.d/10-ssl.conf
  871. sed -i 's|ssl_cert = </etc/dovecot/dovecot.pem|ssl_cert = </etc/ssl/certs/dovecot.crt|g' /etc/dovecot/conf.d/10-ssl.conf
  872. sed -i 's|ssl_key = </etc/dovecot/private/dovecot.pem|/etc/ssl/private/dovecot.key|g' /etc/dovecot/conf.d/10-ssl.conf
  873. sed -i 's|#ssl_dh_parameters_length = 1024|ssl_dh_parameters_length = 1024|g' /etc/dovecot/conf.d/10-ssl.conf
  874. sed -i 's/#ssl_prefer_server_ciphers = no/ssl_prefer_server_ciphers = yes/g' /etc/dovecot/conf.d/10-ssl.conf
  875. echo "ssl_cipher_list = 'EDH+CAMELLIA:EDH+aRSA:EECDH+aRSA+AESGCM:EECDH+aRSA+SHA384:EECDH+aRSA+SHA256:EECDH:+CAMELLIA256:+AES256:+CAMELLIA128:+AES128:+SSLv3:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!DSS:!RC4:!SEED:!ECDSA:CAMELLIA256-SHA:AES256-SHA:CAMELLIA128-SHA:AES128-SHA'" >> /etc/dovecot/conf.d/10-ssl.conf
  876. sed -i 's/#listen = *, ::/listen = */g' /etc/dovecot/dovecot.conf
  877. sed -i 's/#disable_plaintext_auth = yes/disable_plaintext_auth = no/g' /etc/dovecot/conf.d/10-auth.conf
  878. sed -i 's/auth_mechanisms = plain/auth_mechanisms = plain login/g' /etc/dovecot/conf.d/10-auth.conf
  879. sed -i 's|# mail_location = maildir:~/Maildir| mail_location = maildir:~/Maildir:LAYOUT=fs|g' /etc/dovecot/conf.d/10-mail.conf
  880. echo 'configure_imap' >> $COMPLETION_FILE
  881. }
  882. function configure_gpg {
  883. if grep -Fxq "configure_gpg" $COMPLETION_FILE; then
  884. return
  885. fi
  886. apt-get -y --force-yes install gnupg
  887. if [ ! -d /home/$MY_USERNAME/.gnupg ]; then
  888. mkdir /home/$MY_USERNAME/.gnupg
  889. echo 'keyserver hkp://keys.gnupg.net' >> /home/$MY_USERNAME/.gnupg/gpg.conf
  890. echo 'keyserver-options auto-key-retrieve' >> /home/$MY_USERNAME/.gnupg/gpg.conf
  891. fi
  892. sed -i "s|keyserver hkp://keys.gnupg.net|keyserver $GPG_KEYSERVER|g" /home/$MY_USERNAME/.gnupg/gpg.conf
  893. if ! grep -q "# default preferences" /home/$MY_USERNAME/.gnupg/gpg.conf; then
  894. echo '' >> /home/$MY_USERNAME/.gnupg/gpg.conf
  895. echo '# default preferences' >> /home/$MY_USERNAME/.gnupg/gpg.conf
  896. echo 'personal-digest-preferences SHA256' >> /home/$MY_USERNAME/.gnupg/gpg.conf
  897. echo 'cert-digest-algo SHA256' >> /home/$MY_USERNAME/.gnupg/gpg.conf
  898. echo 'default-preference-list SHA512 SHA384 SHA256 SHA224 AES256 AES192 AES CAST5 ZLIB BZIP2 ZIP Uncompressed' >> /home/$MY_USERNAME/.gnupg/gpg.conf
  899. fi
  900. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg
  901. if [[ $MY_GPG_PUBLIC_KEY && $MY_GPG_PRIVATE_KEY ]]; then
  902. # use your existing GPG keys which were exported
  903. if [ ! -f $MY_GPG_PUBLIC_KEY ]; then
  904. echo "GPG public key file $MY_GPG_PUBLIC_KEY was not found"
  905. exit 5
  906. fi
  907. if [ ! -f $MY_GPG_PRIVATE_KEY ]; then
  908. echo "GPG private key file $MY_GPG_PRIVATE_KEY was not found"
  909. exit 6
  910. fi
  911. su -c "gpg --import $MY_GPG_PUBLIC_KEY" - $MY_USERNAME
  912. su -c "gpg --allow-secret-key-import --import $MY_GPG_PRIVATE_KEY" - $MY_USERNAME
  913. # for security ensure that the private key file doesn't linger around
  914. shred -zu $MY_GPG_PRIVATE_KEY
  915. else
  916. # Generate a GPG key
  917. echo 'Key-Type: 1' > /home/$MY_USERNAME/gpg-genkey.conf
  918. echo 'Key-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
  919. echo 'Subkey-Type: 1' >> /home/$MY_USERNAME/gpg-genkey.conf
  920. echo 'Subkey-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
  921. echo "Name-Real: `hostname --fqdn`" >> /home/$MY_USERNAME/gpg-genkey.conf
  922. echo "Name-Email: $MY_USERNAME@`hostname --fqdn`" >> /home/$MY_USERNAME/gpg-genkey.conf
  923. echo 'Expire-Date: 0' >> /home/$MY_USERNAME/gpg-genkey.conf
  924. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/gpg-genkey.conf
  925. su -c "gpg --batch --gen-key /home/$MY_USERNAME/gpg-genkey.conf" - $MY_USERNAME
  926. shred -zu /home/$MY_USERNAME/gpg-genkey.conf
  927. fi
  928. echo 'configure_gpg' >> $COMPLETION_FILE
  929. }
  930. function email_client {
  931. if grep -Fxq "email_client" $COMPLETION_FILE; then
  932. return
  933. fi
  934. apt-get -y --force-yes install mutt-patched lynx abook
  935. if [ ! -d /home/$MY_USERNAME/.mutt ]; then
  936. mkdir /home/$MY_USERNAME/.mutt
  937. fi
  938. echo "text/html; lynx -dump -width=78 -nolist %s | sed ‘s/^ //’; copiousoutput; needsterminal; nametemplate=%s.html" > /home/$MY_USERNAME/.mutt/mailcap
  939. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.mutt
  940. echo 'set mbox_type=Maildir' >> /etc/Muttrc
  941. echo 'set folder="~/Maildir"' >> /etc/Muttrc
  942. echo 'set mask="!^\\.[^.]"' >> /etc/Muttrc
  943. echo 'set mbox="~/Maildir"' >> /etc/Muttrc
  944. echo 'set record="+Sent"' >> /etc/Muttrc
  945. echo 'set postponed="+Drafts"' >> /etc/Muttrc
  946. echo 'set trash="+Trash"' >> /etc/Muttrc
  947. echo 'set spoolfile="~/Maildir"' >> /etc/Muttrc
  948. echo 'auto_view text/x-vcard text/html text/enriched' >> /etc/Muttrc
  949. echo 'set editor="emacs"' >> /etc/Muttrc
  950. echo 'set header_cache="+.cache"' >> /etc/Muttrc
  951. echo '' >> /etc/Muttrc
  952. echo 'macro index S "<tag-prefix><save-message>=.learn-spam<enter>" "move to learn-spam"' >> /etc/Muttrc
  953. echo 'macro pager S "<save-message>=.learn-spam<enter>" "move to learn-spam"' >> /etc/Muttrc
  954. echo 'macro index H "<tag-prefix><copy-message>=.learn-ham<enter>" "copy to learn-ham"' >> /etc/Muttrc
  955. echo 'macro pager H "<copy-message>=.learn-ham<enter>" "copy to learn-ham"' >> /etc/Muttrc
  956. echo '' >> /etc/Muttrc
  957. echo '# set up the sidebar' >> /etc/Muttrc
  958. echo 'set sidebar_width=12' >> /etc/Muttrc
  959. echo 'set sidebar_visible=yes' >> /etc/Muttrc
  960. echo "set sidebar_delim='|'" >> /etc/Muttrc
  961. echo 'set sidebar_sort=yes' >> /etc/Muttrc
  962. echo '' >> /etc/Muttrc
  963. echo 'set rfc2047_parameters' >> /etc/Muttrc
  964. echo '' >> /etc/Muttrc
  965. echo '# Show inbox and sent items' >> /etc/Muttrc
  966. echo 'mailboxes = =Sent' >> /etc/Muttrc
  967. echo '' >> /etc/Muttrc
  968. echo '# Alter these colours as needed for maximum bling' >> /etc/Muttrc
  969. echo 'color sidebar_new yellow default' >> /etc/Muttrc
  970. echo 'color normal white default' >> /etc/Muttrc
  971. echo 'color hdrdefault brightcyan default' >> /etc/Muttrc
  972. echo 'color signature green default' >> /etc/Muttrc
  973. echo 'color attachment brightyellow default' >> /etc/Muttrc
  974. echo 'color quoted green default' >> /etc/Muttrc
  975. echo 'color quoted1 white default' >> /etc/Muttrc
  976. echo 'color tilde blue default' >> /etc/Muttrc
  977. echo '' >> /etc/Muttrc
  978. echo '# ctrl-n, ctrl-p to select next, prev folder' >> /etc/Muttrc
  979. echo '# ctrl-o to open selected folder' >> /etc/Muttrc
  980. echo 'bind index \Cp sidebar-prev' >> /etc/Muttrc
  981. echo 'bind index \Cn sidebar-next' >> /etc/Muttrc
  982. echo 'bind index \Co sidebar-open' >> /etc/Muttrc
  983. echo 'bind pager \Cp sidebar-prev' >> /etc/Muttrc
  984. echo 'bind pager \Cn sidebar-next' >> /etc/Muttrc
  985. echo 'bind pager \Co sidebar-open' >> /etc/Muttrc
  986. echo '' >> /etc/Muttrc
  987. echo '# ctrl-b toggles sidebar visibility' >> /etc/Muttrc
  988. echo "macro index,pager \Cb '<enter-command>toggle sidebar_visible<enter><redraw-screen>' 'toggle sidebar'" >> /etc/Muttrc
  989. echo '' >> /etc/Muttrc
  990. echo '# esc-m Mark new messages as read' >> /etc/Muttrc
  991. echo 'macro index <esc>m "T~N<enter>;WNT~O<enter>;WO\CT~T<enter>" "mark all messages read"' >> /etc/Muttrc
  992. echo '' >> /etc/Muttrc
  993. echo '# Collapsing threads' >> /etc/Muttrc
  994. echo 'macro index [ "<collapse-thread>" "collapse/uncollapse thread"' >> /etc/Muttrc
  995. echo 'macro index ] "<collapse-all>" "collapse/uncollapse all threads"' >> /etc/Muttrc
  996. echo '' >> /etc/Muttrc
  997. echo '# threads containing new messages' >> /etc/Muttrc
  998. echo 'uncolor index "~(~N)"' >> /etc/Muttrc
  999. echo 'color index brightblue default "~(~N)"' >> /etc/Muttrc
  1000. echo '' >> /etc/Muttrc
  1001. echo '# new messages themselves' >> /etc/Muttrc
  1002. echo 'uncolor index "~N"' >> /etc/Muttrc
  1003. echo 'color index brightyellow default "~N"' >> /etc/Muttrc
  1004. echo '' >> /etc/Muttrc
  1005. echo '# GPG/PGP integration' >> /etc/Muttrc
  1006. echo '# this set the number of seconds to keep in memory the passphrase used to encrypt/sign' >> /etc/Muttrc
  1007. echo 'set pgp_timeout=60' >> /etc/Muttrc
  1008. echo '' >> /etc/Muttrc
  1009. echo '# automatically sign and encrypt with PGP/MIME' >> /etc/Muttrc
  1010. echo 'set pgp_autosign # autosign all outgoing mails' >> /etc/Muttrc
  1011. echo 'set pgp_replyencrypt # autocrypt replies to crypted' >> /etc/Muttrc
  1012. echo 'set pgp_replysign # autosign replies to signed' >> /etc/Muttrc
  1013. echo 'set pgp_auto_decode=yes # decode attachments' >> /etc/Muttrc
  1014. echo 'unset smime_is_default' >> /etc/Muttrc
  1015. echo '' >> /etc/Muttrc
  1016. echo 'set alias_file=~/.mutt-alias' >> /etc/Muttrc
  1017. echo 'source ~/.mutt-alias' >> /etc/Muttrc
  1018. echo 'set query_command= "abook --mutt-query \"%s\""' >> /etc/Muttrc
  1019. echo 'macro index,pager A "<pipe-message>abook --add-email-quiet<return>" "add the sender address to abook"' >> /etc/Muttrc
  1020. cp -f /etc/Muttrc /home/$MY_USERNAME/.muttrc
  1021. touch /home/$MY_USERNAME/.mutt-alias
  1022. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.muttrc
  1023. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.mutt-alias
  1024. echo 'email_client' >> $COMPLETION_FILE
  1025. }
  1026. function folders_for_mailing_lists {
  1027. if grep -Fxq "folders_for_mailing_lists" $COMPLETION_FILE; then
  1028. return
  1029. fi
  1030. echo '#!/bin/bash' > /usr/bin/mailinglistrule
  1031. echo 'MYUSERNAME=$1' >> /usr/bin/mailinglistrule
  1032. echo 'MAILINGLIST=$2' >> /usr/bin/mailinglistrule
  1033. echo 'SUBJECTTAG=$3' >> /usr/bin/mailinglistrule
  1034. echo 'MUTTRC=/home/$MYUSERNAME/.muttrc' >> /usr/bin/mailinglistrule
  1035. echo 'PM=/home/$MYUSERNAME/.procmailrc' >> /usr/bin/mailinglistrule
  1036. echo 'LISTDIR=/home/$MYUSERNAME/Maildir/$MAILINGLIST' >> /usr/bin/mailinglistrule
  1037. echo 'if [ ! -d "$LISTDIR" ]; then' >> /usr/bin/mailinglistrule
  1038. echo ' mkdir -m 700 $LISTDIR' >> /usr/bin/mailinglistrule
  1039. echo ' mkdir -m 700 $LISTDIR/tmp' >> /usr/bin/mailinglistrule
  1040. echo ' mkdir -m 700 $LISTDIR/new' >> /usr/bin/mailinglistrule
  1041. echo ' mkdir -m 700 $LISTDIR/cur' >> /usr/bin/mailinglistrule
  1042. echo 'fi' >> /usr/bin/mailinglistrule
  1043. echo 'chown -R $MYUSERNAME:$MYUSERNAME $LISTDIR' >> /usr/bin/mailinglistrule
  1044. echo 'echo "" >> $PM' >> /usr/bin/mailinglistrule
  1045. echo 'echo ":0" >> $PM' >> /usr/bin/mailinglistrule
  1046. echo 'echo " * ^Subject:.*()\[$SUBJECTTAG\]" >> $PM' >> /usr/bin/mailinglistrule
  1047. echo 'echo "$LISTDIR/new" >> $PM' >> /usr/bin/mailinglistrule
  1048. echo 'chown $MYUSERNAME:$MYUSERNAME $PM' >> /usr/bin/mailinglistrule
  1049. echo 'if [ ! -f "$MUTTRC" ]; then' >> /usr/bin/mailinglistrule
  1050. echo ' cp /etc/Muttrc $MUTTRC' >> /usr/bin/mailinglistrule
  1051. echo ' chown $MYUSERNAME:$MYUSERNAME $MUTTRC' >> /usr/bin/mailinglistrule
  1052. echo 'fi' >> /usr/bin/mailinglistrule
  1053. echo 'PROCMAILLOG=/home/$MYUSERNAME/log' >> /usr/bin/mailinglistrule
  1054. echo 'if [ ! -d $PROCMAILLOG ]; then' >> /usr/bin/mailinglistrule
  1055. echo ' mkdir $PROCMAILLOG' >> /usr/bin/mailinglistrule
  1056. echo ' chown -R $MYUSERNAME:$MYUSERNAME $PROCMAILLOG' >> /usr/bin/mailinglistrule
  1057. echo 'fi' >> /usr/bin/mailinglistrule
  1058. chmod +x /usr/bin/mailinglistrule
  1059. echo 'folders_for_mailing_lists' >> $COMPLETION_FILE
  1060. }
  1061. function folders_for_email_addresses {
  1062. if grep -Fxq "folders_for_email_addresses" $COMPLETION_FILE; then
  1063. return
  1064. fi
  1065. echo '#!/bin/bash' > /usr/bin/emailrule
  1066. echo 'MYUSERNAME=$1' >> /usr/bin/emailrule
  1067. echo 'EMAILADDRESS=$2' >> /usr/bin/emailrule
  1068. echo 'MAILINGLIST=$3' >> /usr/bin/emailrule
  1069. echo 'MUTTRC=/home/$MYUSERNAME/.muttrc' >> /usr/bin/emailrule
  1070. echo 'PM=/home/$MYUSERNAME/.procmailrc' >> /usr/bin/emailrule
  1071. echo 'LISTDIR=/home/$MYUSERNAME/Maildir/$MAILINGLIST' >> /usr/bin/emailrule
  1072. echo 'if [ ! -d "$LISTDIR" ]; then' >> /usr/bin/emailrule
  1073. echo ' mkdir -m 700 $LISTDIR' >> /usr/bin/emailrule
  1074. echo ' mkdir -m 700 $LISTDIR/tmp' >> /usr/bin/emailrule
  1075. echo ' mkdir -m 700 $LISTDIR/new' >> /usr/bin/emailrule
  1076. echo ' mkdir -m 700 $LISTDIR/cur' >> /usr/bin/emailrule
  1077. echo 'fi' >> /usr/bin/emailrule
  1078. echo 'chown -R $MYUSERNAME:$MYUSERNAME $LISTDIR' >> /usr/bin/emailrule
  1079. echo 'echo "" >> $PM' >> /usr/bin/emailrule
  1080. echo 'echo ":0" >> $PM' >> /usr/bin/emailrule
  1081. echo 'echo " * ^From: $EMAILADDRESS" >> $PM' >> /usr/bin/emailrule
  1082. echo 'echo "$LISTDIR/new" >> $PM' >> /usr/bin/emailrule
  1083. echo 'chown $MYUSERNAME:$MYUSERNAME $PM' >> /usr/bin/emailrule
  1084. echo 'if [ ! -f "$MUTTRC" ]; then' >> /usr/bin/emailrule
  1085. echo ' cp /etc/Muttrc $MUTTRC' >> /usr/bin/emailrule
  1086. echo ' chown $MYUSERNAME:$MYUSERNAME $MUTTRC' >> /usr/bin/emailrule
  1087. echo 'fi' >> /usr/bin/emailrule
  1088. echo 'PROCMAILLOG=/home/$MYUSERNAME/log' >> /usr/bin/emailrule
  1089. echo 'if [ ! -d $PROCMAILLOG ]; then' >> /usr/bin/emailrule
  1090. echo ' mkdir $PROCMAILLOG' >> /usr/bin/emailrule
  1091. echo ' chown -R $MYUSERNAME:$MYUSERNAME $PROCMAILLOG' >> /usr/bin/emailrule
  1092. echo 'fi' >> /usr/bin/emailrule
  1093. chmod +x /usr/bin/emailrule
  1094. echo 'folders_for_email_addresses' >> $COMPLETION_FILE
  1095. }
  1096. function dynamic_dns_freedns {
  1097. if grep -Fxq "dynamic_dns_freedns" $COMPLETION_FILE; then
  1098. return
  1099. fi
  1100. echo '#!/bin/bash' > /usr/bin/dynamicdns
  1101. echo '# subdomain name 1' >> /usr/bin/dynamicdns
  1102. echo "wget -O - https://freedns.afraid.org/dynamic/update.php?$FREEDNS_SUBDOMAIN_CODE== >> /dev/null 2>&1" >> /usr/bin/dynamicdns
  1103. echo '# add any other subdomains below' >> /usr/bin/dynamicdns
  1104. chmod 600 /usr/bin/dynamicdns
  1105. chmod +x /usr/bin/dynamicdns
  1106. if ! grep -q "dynamicdns" /etc/crontab; then
  1107. sed -i '/# m h dom mon dow user command/a\*/5 * * * * root /usr/bin/timeout 240 /usr/bin/dynamicdns' /etc/crontab
  1108. fi
  1109. service cron restart
  1110. echo 'dynamic_dns_freedns' >> $COMPLETION_FILE
  1111. }
  1112. function import_email {
  1113. if grep -Fxq "import_email" $COMPLETION_FILE; then
  1114. return
  1115. fi
  1116. if [ $IMPORT_MAILDIR ]; then
  1117. if [ -d $IMPORT_MAILDIR ]; then
  1118. echo 'Transfering email files'
  1119. cp -r $IMPORT_MAILDIR/* /home/$MY_USERNAME/Maildir/
  1120. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/Maildir
  1121. else
  1122. echo "Email import directory $IMPORT_MAILDIR not found"
  1123. exit
  1124. fi
  1125. fi
  1126. echo 'import_email' >> $COMPLETION_FILE
  1127. }
  1128. function install_final {
  1129. if grep -Fxq "install_final" $COMPLETION_FILE; then
  1130. return
  1131. fi
  1132. echo 'install_final' >> $COMPLETION_FILE
  1133. echo ''
  1134. echo ' *** Freedombone installation is complete. Rebooting... ***'
  1135. echo ''
  1136. reboot
  1137. }
  1138. argument_checks
  1139. configure_firewall
  1140. configure_firewall_for_ssh
  1141. configure_firewall_for_dns
  1142. configure_firewall_for_ftp
  1143. configure_firewall_for_web
  1144. remove_proprietary_repos
  1145. change_debian_repos
  1146. enable_backports
  1147. configure_dns
  1148. initial_setup
  1149. install_editor
  1150. change_login_message
  1151. update_the_kernel
  1152. enable_zram
  1153. random_number_generator
  1154. set_your_domain_name
  1155. time_synchronisation
  1156. configure_internet_protocol
  1157. configure_ssh
  1158. search_for_attached_usb_drive
  1159. regenerate_ssh_keys
  1160. script_to_make_self_signed_certificates
  1161. configure_email
  1162. spam_filtering
  1163. configure_imap
  1164. configure_gpg
  1165. email_client
  1166. configure_firewall_for_email
  1167. folders_for_mailing_lists
  1168. folders_for_email_addresses
  1169. dynamic_dns_freedns
  1170. import_email
  1171. install_final
  1172. echo 'Freedombone installation is complete'