install-freedombone.sh 47KB

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