install-freedombone.sh 52KB

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